Hyperlinks of footnotes with multiple numbers/markers

expl3footnoteshyperlinkhyperrefmacros

In this answer, I created two commands \footnotenumber and \multifootnote for writing footnote with multiple numbers. The problem with these commands is that hyperlinks do not work as in usual footnotes: for example, if you click on the red boxed "1" in the document generated by the code below, nothing would happen.

I tried to use \hyperlink and \hypertarget in these commands without success. To me the difficulties are: 1) I don't know much about the internal of \footnotemark, thus have no idea where to place the \hyperlinked number/mark; 2) in the \clist_put_right:Nn line, if I use \ref*{ ##1 } as the text of a \hypertarget, there shall be multiple errors like Undefined control sequence. \@hyper@@anchor ...r@spot {#2#3}\let \put@me@back.

Thus my question is that, what should be done to make the hyperlinks work? Any suggestion is greatly appreciated!

\documentclass{article}

\usepackage{hyperref}

\ExplSyntaxOn

\NewDocumentCommand \multifootnotemark { O{} }
  {
    \footnotemark
    \group_begin:
      \tl_if_blank:nF { #1 }
        {
          \addtocounter { footnote } { -1 }
          \refstepcounter { footnote }
          \label { #1 }
        }
    \group_end:
  }
\NewCommandCopy \footnotenumber \multifootnotemark

\NewDocumentCommand \multifootnotetext { O{} m }
  {
    \group_begin:
    \tl_if_blank:nF { #1 }
      {
        \clist_clear:N \l_tmpa_clist
        \clist_map_inline:nn { #1 }
          {
            \clist_put_right:Nn \l_tmpa_clist { \ref*{ ##1 } }
          }
        \def \thefootnote { \clist_use:Nn \l_tmpa_clist { , } }
      }
    \footnotetext { #2 }
    \group_end:
  }
\NewCommandCopy \multifootnote \multifootnotetext

\ExplSyntaxOff


\begin{document}

Lorem ipsum\footnotenumber[fn1] dolor sit amet\footnotenumber[fn2], consectetur adipiscing elit\footnotenumber[fn3].
\multifootnote[fn1,fn2,fn3]{Test footnote.}

\end{document}

enter image description here

enter image description here

Best Answer

You can try something like this:

\documentclass{article}

\usepackage{hyperref}

\ExplSyntaxOn
\makeatletter

\NewDocumentCommand \multifootnotemark { O{} }
  {
    \footnotemark
    \group_begin:
      \tl_if_blank:nF { #1 }
        {
          \addtocounter { footnote } { -1 }
          \refstepcounter { footnote }
          \global\let\@currentHref\Hy@footnote@currentHref
          \label { #1 }
        }
    \group_end:
  }
\NewCommandCopy \footnotenumber \multifootnotemark

\NewDocumentCommand \multifootnotetext { O{} m }
  {
    \group_begin:
    \tl_if_blank:nF { #1 }
      {
        \clist_clear:N \l_tmpa_clist
        \clist_map_inline:nn { #1 }
          {
            \clist_put_right:Nn \l_tmpa_clist 
             { \ref*{ ##1 } }
            \clist_put_right:Nn \l_tmpb_clist
             { \hypertarget{\getrefbykeydefault{##1}{anchor}{Doc-Start}}{} } 
          }
        \def \thefootnote { \clist_use:Nn \l_tmpa_clist { , } }
      }
    \xdef\Hy@footnote@currentHref{x\Hy@footnote@currentHref}  
    \footnotetext { \clist_use:Nn\l_tmpb_clist{}\ignorespaces #2 }
    \group_end:
  }
\NewCommandCopy \multifootnote \multifootnotetext

\ExplSyntaxOff


\begin{document}

Lorem ipsum\footnotenumber[fn1] dolor sit amet\footnotenumber[fn2], consectetur adipiscing elit\footnotenumber[fn3].
\multifootnote[fn1,fn2,fn3]{\label{blub}Test footnote.}

\end{document}
Related Question