1

I am trying to implement the following, I have a conditionally defined new command that works with the caption command so that I can give it a short and long caption, or just a long caption, achieved in the following way

\documentclass{book} 
\usepackage{graphicx}
\usepackage{hyperref}

\usepackage{xparse}
\NewDocumentCommand \capLF { m o }{%
\IfNoValueTF{#2}
    {% No optional 'o' given which is 2nd entry -> use mandatory option, which is 1st arguemnt 'm'
    \caption[#1]{\hyperlink{LoF}{#1}}%
    }
    {% Optional given -> use both
    \caption[#2]{\hyperlink{LoF}{#1}}
    }%
}

\begin{document}

\hypertarget{LoF}{}\listoffigures

\newpage

\begin{figure}[htb]
    \centering 
    \includegraphics[width=0.5\textwidth]{G.pdf}
    \capLF{Long Caption for Image}[Short Caption]
    \label{fig:sample_image}
\end{figure}

\begin{figure}[htb]
    \centering 
    \includegraphics[width=0.5\textwidth]{G.pdf}
    \capLF{Long Caption for Image}
    \label{fig:sample_image}
\end{figure}


\end{document}

Currently I have it such that when the caption is clicked, that it takes you to the top of the page for the list of figures (lof), however, I would like to be able make it such that when the caption is clicked, that it takes you to the entry of the caption in the lof, as has been done here, which I am using in the following way

\documentclass{book} 
\usepackage[demo]{graphicx}
\usepackage{hyperref}

\newcounter{mycntr}
\makeatletter
\newcommand\LCaption{%
  \stepcounter{mycntr}\@dblarg\@LCaption}
\def\@LCaption[#1]#2{%
  \caption[\protect\hypertarget{image\themycntr}{#1}]%
    {\hyperlink{image\themycntr}{#2}}}
\makeatother

\begin{document}

\listoffigures

\newpage

\begin{figure}[htb]
    \centering 
    \includegraphics[width=0.5\textwidth]{G.pdf}
    \LCaption[Description in LOF, taken from]{Caption for Image}
    \label{fig:sample_image}
\end{figure}


\end{document}

I am trying to combine these two while keeping the functionality of my defined command, I would appreciate any advice on how to go about achieving this, as I am not fully familiar with \NewDocumentCommand and how to incorporate the second MWE into it.

As a minor side note, the second MWE takes to the entry in the lof list, however, it goes to just below the entry, that is you need to scroll up to see the actual entry. If anyone knows how this can be fixed I would appreciate it, though it is not as big of an issue as the original question.

Edit

In response to Ulrike Fischer's answer, this almost gets me where I want, when I try your code it works, however, when clicking the lof link it takes me to the top of the caption, as opposed to the top of the image for the figure itself.

Using the caption package in the preamble fixes this, however, the caption link for the figures now takes you to the top of the whole document instead of to the lof entry, when I add a table of contents (toc), or anything else above the lof for that matter, the same occurs.

\documentclass{book}
\usepackage[demo]{graphicx}
\usepackage{hyperref}
\usepackage{etoolbox}
\usepackage{xparse}
\usepackage{caption}
\usepackage[nottoc,numbib]{tocbibind} 

\makeatletter
\patchcmd\contentsline{\hyper@linkstart}%
  {\def\my@content@Htargetname{#4}\csname my@content@hypertarget@#1\endcsname\hyper@linkstart}{}{\fail}
\newcommand\my@content@hypertarget@figure{\raisebox{15pt}[0pt][0pt]{\hypertarget{lof.\my@content@Htargetname}{}}}
\NewDocumentCommand \capLF { m o }{%
\IfNoValueTF{#2}
    {% No optional 'o' given which is 2nd entry -> use mandatory option, which is 1st arguemnt 'm'
    \caption[#1]{\hyperlink{lof.figure.\theHfigure}{#1}}%
    }
    {% Optional given -> use both
    \caption[#2]{\hyperlink{lof.figure.\theHfigure}{#1}}
    }%
}

\makeatother

\begin{document}

Text

\tableofcontents

\listoffigures

\newpage

\chapter{figure 1}

\begin{figure}[htb]
    \centering 
    \includegraphics{image.pdf}
    \capLF{Long Caption for Image}[Short Caption]
    \label{fig:sample_image}
\end{figure}

\chapter{figure 2}

\begin{figure}[htb]
    \centering 
    \includegraphics{image.pdf}
    \capLF{Long Caption for Image}
    \label{fig:sample_image}
\end{figure}

\end{document}

Edit 2

The above works, provided \usepackage[all]{hypcap} is used instead of \usepackage{caption}, thank you @UlrikeFischer for all the help.

3

I don't really see the use. Being able to go from the list of figures to a specific figure is nice, but I never wanted to find the list of figures while looking at a caption. But if you want: better patch \contentsline:

\documentclass{book}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{etoolbox}

\makeatletter
\patchcmd\contentsline{\hyper@linkstart}%
  {\def\my@content@Htargetname{#4}\csname my@content@hypertarget@#1\endcsname\hyper@linkstart}{}{\fail}

\newcommand\my@content@hypertarget@figure{\raisebox{15pt}[0pt][0pt]{\hypertarget{lof.\my@content@Htargetname}{}}}
\NewDocumentCommand \capLF {  o m }{%
\IfNoValueTF{#2}
    {% No optional 'o' given which is 2nd entry -> use mandatory option, which is 1st arguemnt 'm'
    \caption[#1]{\hyperlink{lof.figure.\theHfigure}{#1}}%
    }
    {% Optional given -> use both
    \caption[#2]{\hyperlink{lof.figure.\theHfigure}{#1}}
    }%
}
\makeatother

\begin{document}


\listoffigures

\newpage

\begin{figure}[htb]
    \centering
    \includegraphics[width=0.5\textwidth]{example-image-duck.pdf}
    \capLF[Description in LOF, taken from]{Caption for Image}
    \label{fig:sample_image}
\end{figure}

\begin{figure}[htb]
    \centering
    \includegraphics[width=0.5\textwidth,page=2]{example-image-duck.pdf}
    \capLF[Second Description in LOF, taken from]{Second Caption for Image}
    \label{fig:sample_image2}
\end{figure}


\end{document}
2
  • Having this functionality is useful because in my document I have a lot of figures, resulting in a multiple paged lof and linking back and forth between the figure entries and captions is convenient.
    – TaukDe
    Feb 9 at 5:17
  • I managed to make this work, thank you for your help, my problem was that I was using \hypersetup{linktoc=all}, and this was causing the jumping to the top of the page.
    – TaukDe
    Feb 15 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.