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

Ingmar
  • 6,690
  • 5
  • 26
  • 47
rappel
  • 13

1 Answers1

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:.

cabohah
  • 11,455
  • 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) – Ulrike Fischer Aug 08 '23 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 Aug 08 '23 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 Aug 08 '23 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 Aug 08 '23 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 Aug 08 '23 at 20:34
  • @rappel Sorry, but I cannot adapt unknown code. Please ask a new question with better example and specification of the problem. – cabohah Aug 09 '23 at 05:27
  • @cabohah I'm sorry, my comment was ambiguous. I was trying to say that I tried to implement the \listoftables solution, but found that it cannot address all customization needs (non-link entries, multiple depths), plus it splitting the lists broke your patch. This is why I am still looking for a solution to my original problem (why the hyperlink doesn't work with \nameref but \ref), which is clearly specified in the initial example. I agree I should start a separate post if the \listoftables solution was what I was looking for, but I don't think it can address all customization needs – rappel Aug 09 '23 at 16:16
  • @rappel Instead of making the lists by hand, I would still recommend to use automatic lists and fix the problems not shown in the question, e.g., using split lists. But If you really just want links from the caption to the reference and use them to make everything else manually, see the second and third example in my answer. But I'm still thinking, this is nonsense, because with manually done lists the risk of missing entries or wrong order of entries always exists. Automatic lists are a very powerful tool, that should be used! – cabohah Aug 10 '23 at 06:33
  • @cabohah thank you so much, I really appreciate the time you took to put this answer together, and now it indeed answers my initial question (as well as suggesting an alternative way using automated lists). I agree that there is more scope for error using manual lists, but given the extent of customization, I think I'll just use the \captionbecktonameref and namerefwithbackref commands you provided - they work like a charm. Thank you so much! – rappel Aug 11 '23 at 02:12