#set ($linkBlock = $latex.block)
## Convert the image to a local image to embed it in the result
#set ($resourceReference = $latex.resourceConverter.convert($linkBlock))
#set ($type = $resourceReference.type.scheme)
## Consider that we're on a local link when the reference is empty and anchor is not empty
#set ($reference = $resourceReference.reference)
#set ($anchor = $latex.block.reference.getParameter('anchor'))
#if ($type == 'doc')
  #if ("$!reference" == '' && "$!anchor" != '')
    #if ($latex.block.getChildren().isEmpty())
      \ref{${anchor}}##
    #else
      \hyperref[${anchor}]{${latex.processor.process($latex.block.getChildren())}}##
    #end
  #else
    ## Ignore links that are not local, just display the label if there's one or the reference otherwise
    #if ($latex.block.getChildren().isEmpty())
      $latex.tool.escape($reference)##
    #else
      ${latex.processor.process($latex.block.getChildren())}##
    #end
  #end
#elseif ($type == 'url' || $type == 'path')
  #if ($latex.block.getChildren().isEmpty())
    \url{${reference}}##
  #else
    \href{${reference}}{${latex.processor.process($latex.block.getChildren())}}##
  #end
#elseif ($type == 'mailto')
  #if ($latex.block.getChildren().isEmpty())
    \href{mailto:${reference}}{$latex.tool.escape($reference)}##
  #else
    \href{mailto:${reference}}{${latex.processor.process($latex.block.getChildren())}}##
  #end
#elseif ($type == 'attach')
  #set ($escapedReference = $latex.tool.escape($reference))
  #if ($latex.block.getChildren().isEmpty())
    ## Use a default icon when no label is provided. Another option would be to use the filename as text.
    \attachfile{$latex.tool.escape($reference)}##
  #else
    \textattachfile{$latex.tool.escape($reference)}{${latex.processor.process($latex.block.getChildren())}}##
  #end
#end