I need to create a manual entry in the list of figures using \addtocontents (yes I need \addtocontents rather than \addcontentsline).

When using the hyperref package, an extra argument is added to the standard command, for the link. I am trying to base the 'link' off a label, but the following is throwing an error:

Undefined control sequence. ... {0.1}{\ignorespaces Short}}{2}{figure.0.1}

I am sure this is a simple problem to solve, but I have had no luck. Can one of the LaTeX experts help me out?

\documentclass{memoir}
\usepackage{hyperref}

\begin{document}

\listoffigures

\clearpage

\begin{figure}[htbp]
\caption[Short]{My Caption}\label{fig:myfigure}
\end{figure}

\protect\contentsline
{figure}
{\numberline{1}A Manual Entry}
{\pageref{fig:myfigure}}
{figure.\ref{fig:myfigure}} %<<<<<<<<<<<<<<<<<<< HOW???
}%

\end{document}

• You have a trailing white space after the \ref{fig:myfigure} -- this confuses the anchor generation in addition. It's not the cause of your problem, however – user31729 Aug 8 '17 at 19:25

\ref isn't expandable, so this is nothing that can be written this way to the .aux file (and then to the .lof)

I suggest to use \getrefpagenumber and \getrefbykeydefault{labelname}{anchor}{}, both are expandable commands, the second one extracts the correct hyper anchor -- there is no guarantee that the anchor to a figure labeled entity is really starting with figure nor is the rest of the anchor made of the output of \ref.

Consider the example that \thefigure is defined as \thechapter.\alph{figure}.

The usual anchor still use the integer expression of the counter: It is most likely figure.0.1 and not figure.0.a.

Since this is hidden from the user, \getrefbykeydefault is the better variant here.

Usually, this requires refcount to be loaded, but hyperref does this already (both packages are written by the same author)

\documentclass{memoir}
\usepackage{hyperref}

\begin{document}

\listoffigures

\clearpage

\begin{figure}[htbp]
\caption[Short]{My Caption}\label{fig:myfigure}
\end{figure}

\protect\contentsline
{figure}
{\numberline{1}A Manual Entry}
{\getpagerefnumber{fig:myfigure}}
{\getrefbykeydefault{fig:myfigure}{anchor}{}} %<<<<<<<<<<<<<<<<<<< HOW???
}%

\end{document}

• Personally I would not use such \addtocontents in this way directly, but there's is no real use - case and I don't understand why this seems to be necessary – user31729 Aug 8 '17 at 19:24
• Thanks. I am extending this question: tex.stackexchange.com/questions/362004/…, and condensing multiple lines into a single line (my list of figures in my thesis is 10 pages long or something), where the figure number becomes a range, and the page number also becomes a range of numbers, like: [1-4 Figures One, Two, Three and Four................ 11-14] – Nicholas Hamilton Aug 8 '17 at 22:30
• if you know how to add a line to the toc or list of figures, where the figure number is actually a range of figures, and the page numbers are a range of numbers (ie a single line that represents X number of figures with extremely similar captions) then I am all ears. – Nicholas Hamilton Aug 8 '17 at 22:38
• @NicholasHamilton: That's another question indeed. I can think of a solution where the \caption command is changed and 'collects' the information from some figure environments and writes this to the .aux file then, but what is the criterion for this collection? Why figures 1 - 4 and not 1 -6, for example? That's something not really to be detected automatically. – user31729 Aug 9 '17 at 5:53
• I have asked another question, which also demonstrates what I am trying to achieve. tex.stackexchange.com/questions/385556/… – Nicholas Hamilton Aug 9 '17 at 12:26