4

(Final code Here)

This is an extension for an extended figure caption function I requested in Extended figure caption and accompanying "List of" .

The given solution worked, but I would like it to give the function the hability to handle multiple paragraphs. Hence, I duplicated the \addtocontents and \contentsline functions to respectivelly \addefcline and \efcline, differing only by the definition \long\def instead of \def, giving them the hability to handle multiple paragraphs.

As a result, it works fine, but in the current form I have to use \protect every new line I make in the \extcaption{. Is there a way to automatically protect all new paragraph ?

\documentclass[a4paper,10pt]{report}
\usepackage{fontspec}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{caption}

% Redefinition of \addcontentsline
\long\def\addefcline#1#2#3{%
  \addtocontents{#1}{\protect\efcline{#2}{#3}{\thepage}}}

% Redefinition of \contentsline
\makeatletter
\long\def\efcline#1{\csname l@#1\endcsname}
\makeatother

% Extended figure caption
\newcommand{\extcaption}[1]{% Write to .efc file
  \addefcline{efc}{extfig}{{\bfseries \protect\hyperlink{fig:\thefigure}{Figure \thefigure, page~\thepage:}} #1}}
\makeatletter
\newcommand{\printextcaptions}{\@starttoc{efc}}% Read .efc file
\newcommand{\l@extfig}[2]{\noindent #1 \vspace{\parskip} \par}% How each .efc entry is handled
\makeatother

\usepackage[linktoc=all]{hyperref}

\begin{document}

\chapter{A chapter}

\section{A section}
\lipsum[1]

\begin{figure}
\includegraphics{example-image-a.pdf}
\caption{Picture of a big A}
\extcaption{The reasons why A is magnificent
\protect\begin{itemize}
\protect\item "A" like Awesome
\protect\item "A" like Amazing
\protect\end{itemize}
}
\end{figure}

\begin{figure}
\includegraphics{example-image-b.pdf}
\caption{Picture of a big B}
\extcaption{The reasons why B is awful
\protect\begin{itemize}
\protect\item "B" like Bad
\protect\item "B" like Banana
\protect\end{itemize}
}
\end{figure}

\section{Extended figure captions}
\printextcaptions
\end{document}
1
  • 2
    I'd say \unexpanded{\unexpanded{#1}} instead of #1 in the definition of \extcaption. However, no hyperlink is made, because no target is defined, but I think it's a different problem.
    – egreg
    Commented Nov 23, 2015 at 0:55

1 Answer 1

4

You can use \unexpanded{\unexpanded{#1}}:

\documentclass[a4paper,10pt]{report}
\usepackage{fontspec}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{caption}

% Redefinition of \addcontentsline
\long\def\addefcline#1#2#3{%
  \addtocontents{#1}{\efcline{#2}{#3}{\thepage}}}

% Redefinition of \contentsline
\makeatletter
\long\def\efcline#1{\csname l@#1\endcsname}
\makeatother

% Extended figure caption
\newcommand{\extcaption}[1]{% Write to .efc file
  \addefcline{efc}{extfig}{{%
    \bfseries \protect\hyperlink{fig:\thefigure}{Figure \thefigure, page~\thepage:}} %
    \unexpanded{\unexpanded{#1}}%
  }%
}
\makeatletter
\newcommand{\printextcaptions}{\@starttoc{efc}}% Read .efc file
\newcommand{\l@extfig}[2]{\noindent #1 \vspace{\parskip} \par}% How each .efc entry is handled
\makeatother

\usepackage[linktoc=all]{hyperref}

\begin{document}

\chapter{A chapter}

\section{A section}
\lipsum[1]

\begin{figure}
\includegraphics{example-image-a.pdf}
\caption{Picture of a big A}\hypertarget{fig:\thefigure}{}
\extcaption{The reasons why A is magnificent
\begin{itemize}
\item "A" like Awesome
\item "A" like Amazing
\end{itemize}
}
\end{figure}

\begin{figure}
\includegraphics{example-image-b.pdf}
\caption{Picture of a big B}\hypertarget{fig:\thefigure}{}
\extcaption{The reasons why B is awful
\begin{itemize}
\item "B" like Bad
\item "B" like Banana
\end{itemize}
}
\end{figure}

\section{Extended figure captions}
\printextcaptions
\end{document}

I added a couple of targets in order that the links work.

enter image description here

1
  • Thanks a lot indeed ! This is one good thing done. The last remaining point would be, is there a way to get the hyperlinks to point at the figure without having to define an hypertarget every time ? The problem with redefining \contentsline is that hyperref do not alter the other functions ... or should I start a new question ?
    – HcN
    Commented Nov 23, 2015 at 1:21

You must log in to answer this question.

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