1

I'm trying to create a two-way link between tables/figures and the list of all tables/figures that I manually created. I.e., each item in the list of tables should link to a table, and clicking on the table caption should link back to the reference.

When linking the table/figure, I would like to use the table/figure name, not its number, as is possible with \namref.

I found a solution that works when using \ref, but when I try to insert \nameref inside of the hyperlink, it doesn't work anymore. I tried inserting \phantomsection before the table label and removing the link using \nameref* to avoid clashes between the links, but that didn't help. I also tried various other link setups, such as with cleverref, but didn't succeed yet.

So the following code doesn't work, but if I replaced \nameref with \ref, it would work. But I want the table name in the reference (without manually typing it out because I have hundreds of tables), and not the table number.

\documentclass{article}
\usepackage{hyperref}

\newcommand{\mylabel}[2]{%
  \hyperlink{back:#1}{\hypertarget{#1}{#2}}
}
\newcommand{\myref}[2]{%
  \hyperlink{#1}{\hypertarget{back:#1}{#2}}
}

\begin{document}

\begin{enumerate}
    \item \myref{link}{\nameref{testtab}}
\end{enumerate}

\newpage

\begin{table}
\caption{\mylabel{link}{My Table}}\label{testtab}
\centering
\begin{tabular}[t]{rr}
\toprule
\textbf{Col A} & \textbf{Col B} \\
\midrule
1 & A \\
2 & B \\
\end{tabular}
\end{table}
  
\end{document}

Weirdly, with \nameref, the output shows two links, with one on top that seems to link to itself, and the other being only clickable at the very right and linking correctly to the table (see screenshot).

output_link

How could I set this up correctly?

Thank you so much in advance for any tips, I'm new to StackExchange and relatively new to LaTeX, so any support is highly appreciated.

Edit: The list(s) of tables/figures I want to create needs to be highly customizable (contain both linked and non-linked entries, multiple depths, be split into multiple parts, contain tables and figures), such that \listoftables doesn't seem to be an alternative way to solve the issue

3
  • Welcome to TeX.SE!
    – Mensch
    Commented Aug 8, 2023 at 7:18
  • Your example is missing the booktabs package.
    – daleif
    Commented Aug 8, 2023 at 7:36
  • 2
    why don't you use \listoftables to create the list?? Commented Aug 8, 2023 at 8:34

1 Answer 1

0

I would recommend to use \listoftables and \listoffigures. In this case, you can patch \contentsline to add an additional hyper target and patch \caption to use this additional hyper target:

\documentclass{article}
\usepackage{caption}% To support \caption above tabular.
\usepackage{booktabs}
\usepackage{lipsum}% Dummy text for the example.
\usepackage{hyperref}

\makeatletter
\AtBeginDocument{%
% 1. Patch \contentsline to automatically add a hyper target to the LoT entry
%    and the LoF entry.
  \addtocontents{lot}{%
    \string\let\string\normalcontentsline\string\contentsline
    \string\let\string\contentsline\string\backrefcontentsline
  }%
  \addtocontents{lof}{%
    \string\let\string\normalcontentsline\string\contentsline
    \string\let\string\contentsline\string\backrefcontentsline
  }%
  \newcommand{\backrefcontentsline}[4]{%
    \hypertarget{back.#4}{\normalcontentsline{#1}{#2}{#3}{#4}}%
  }%
% 2. Patch \caption to automatically link to the hyper target of the LoT/LoF entry.
  \NewCommandCopy\Caption\caption
  \RenewDocumentCommand\caption{O{#2}m}{%
    \Caption[{#1}]{\hyperlink{back.\@currentHref}{#2}}
  }%
}
\makeatother

\begin{document}

\listoftables
\listoffigures

\newpage

\begin{table}
  \show\caption
\caption{My Table}\label{testtab}
\centering
\begin{tabular}{rr}
\toprule
\textbf{Col A} & \textbf{Col B} \\
\midrule
1 & A \\
2 & B \\
\end{tabular}
\end{table}

\begin{figure}
  \centering
  \rule{1cm}{1cm}
  \caption{My figure}
\end{figure}

\lipsum

\begin{table}
  \centering
  \caption{One more Table}
  \begin{tabular}{lr}
    left & right
  \end{tabular}
\end{table}

\end{document}

Note: This patch work, with or without package caption.

But to really not have links from the caption into the \listoffigures and \listoftables but one dedicated reference, I would define new commands:

\documentclass{article}
\usepackage{hyperref}

\NewDocumentCommand\captionbacktonameref{O{#2}mm}{%
  \caption[{#1}]{\hyperlink{nameback:#3}{#2}}\label{#3}%
}
\newcommand*\namerefwithbackref[1]{%
  \hypertarget{nameback:#1}{\nameref{#1}}%
}

\begin{document}
See \namerefwithbackref{tab:test} (the link
anchor from the table would is here).

See also (without back-ref) \nameref{fig:test}.

\newpage

\begin{table}
  \centering
  \begin{tabular}{lr}
    left & right
  \end{tabular}
  \captionbacktonameref{Test Table}{tab:test}
\end{table}

\begin{figure}
  \centering
  \rule{1cm}{1cm}
  \captionbacktonameref{Test Figure}{fig:test}
\end{figure}

\clearpage

See \namerefwithbackref{fig:test} (the link
anchor from the figure is here).

See also \nameref{tab:test}.

\end{document}

So now it is your decision to use either \caption without link back to any reference or \captiontobackref (with link back to exactly one \namerefwithbackref. You can also decide where to have the anchor for the link from within the caption using either \namerefwithbackref or still using \nameref.

This can also be extended to support back also links from/to the number instead of the name:

\documentclass{article}
\usepackage{hyperref}

\NewDocumentCommand\captionbacktonameref{O{#2}mm}{%
  \caption[{#1}]{\hyperlink{nameback:#3}{#2}}\label{#3}%
}
\makeatletter
\NewDocumentCommand\captionbacktorefs{O{#2}mm}{%
  \expandafter\let\expandafter\orig@fnum\csname fnum@\@captype\endcsname
  \@namedef{fnum@\@captype}{%
    \hyperlink{numback:#3}{\orig@fnum}%
  }%
  \caption[{#1}]{\hyperlink{nameback:#3}{#2}}\label{#3}%
}
\makeatother
\newcommand*\namerefwithbackref[1]{%
  \hypertarget{nameback:#1}{\nameref{#1}}%
}
\newcommand*\refwithbackref[1]{%
  \hypertarget{numback:#1}{\ref{#1}}%
}

\begin{document}
See \namerefwithbackref{tab:test} (the link
anchor from the table would is here).

See also Figure \refwithbackref{fig:test} (the link anchor from the figure number is
here) \nameref{fig:test} (the name isn't an anchor).

\newpage

\begin{table}
  \centering
  \begin{tabular}{lr}
    left & right
  \end{tabular}
  \captionbacktonameref{Test Table}{tab:test}
\end{table}

\begin{figure}
  \centering
  \rule{1cm}{1cm}
  \captionbacktorefs{Test Figure}{fig:test}
\end{figure}

\clearpage

See Figure \ref{fig:test} (the number isn't an anchor)
  \namerefwithbackref{fig:test} (the link anchor from the figure is here).

See also \nameref{tab:test}.

\end{document}

Note: You need different target to \namerefwithbackref and \refwithbackref. That's why \captionwithbackrefs uses an additional link for the number and \refwithbackref uses numback: prefix instead of nameback:.

9
  • with a current LaTeX I would probably do something like \AddToHookWithArguments{cmd/l@table/before}{\MakeLinkTarget*{back.\Hy@tocdestname}} instead of your patches (with slight unease as a target there could affect spacing or pagebreak) Commented Aug 8, 2023 at 12:22
  • Thank you so much for the suggestion @cabohah and @Ulrike Fischer! I want to create my own list rather than \listoftables or \listoffigures because I need to customize the list to: (1) have multiple separate parts/lists (in different parts of the paper) with only figures/tables belonging to that part listed in there, (2) intermingle tables and figures (e.g., when Figure 1 is on the same topic as Table 1, I want them be subsequent, not on separate lists), (3) have subheadings to help navigate it (these are not linked to a figure/table and thus wouldn't appear) and different levels
    – rappel
    Commented Aug 8, 2023 at 15:15
  • @UlrikeFischer I've also tested hook into \numberline. But the link destination was always below the baseline of the entry. The shown suggestion was the only one, with correct location. So I've decided to show this one.
    – cabohah
    Commented Aug 8, 2023 at 16:29
  • @rappel There are packages to provide several such lists without doing everything manually. Using the same list for tables and figures is also possible.
    – cabohah
    Commented Aug 8, 2023 at 16:32
  • @cabohah thank you! I found ways to break up lists of tables/figures, and combine them for figures and tables, but that broke the code that adds hyperlinks (i.e., the patch above didn't work anymore). I also couldn't find a way yet to create non-linked entries in such a list, and to have multiple depths. Finding a solution to the hyperlink issue (i.e., particularly why \nameref doesn't work, or shows two links) would allow for full customization. But if you have advice on how to adapt the code to make it work for separate multiple lists of tables and figures, I will definitely try it
    – rappel
    Commented Aug 8, 2023 at 20:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .