2

I want to write an extended figure caption section, and wondered if I could get LaTeX to do most of the hard work for me.

Ideally, it would take the form of a function, let say \extcaption, that we could use within a float, like

\begin{figure}
\includegraphics{example-image-a.pdf}
\caption{Important things.}
\extcaption{Less important things.}
\label{fig:bigA}
\end{figure}

and that we could print with

\printextcaptions

and that would return

Figure 1.1, page 2: Less important things.

Anyway to do it? Here is a MWE:

\documentclass[a4paper,10pt]{report}
\usepackage{fontspec}
\usepackage{graphicx}
\usepackage[titles]{tocloft}

\begin{document}
\chapter{A chapter}

\section{A section}

\begin{figure}
\includegraphics{example-image-a.pdf}
\caption{Picture of a big A}
%\extcaption{This picture is magnificent}
\label{fig:bigA}
\end{figure}

\begin{figure}
\includegraphics{example-image-b.pdf}
\caption[This picture is horrible]{Picture of a big B}
%\extcaption{This picture is horrible.}
\label{fig:bigB}
\end{figure}

%\printextcaptions

\end{document}

The best result I got was by setting a new list with the tocloft package

\newlistof[section]{extfig}{efc}{Extended figure captions}
\newcommand{\extcaption}[1]{%
\refstepcounter{extfig}
\par\noindent\textbf{\theextfig #1}
\addcontentsline{efc}{extfig}{\protect\numberline{\theextfig}#1}\par}
\renewcommand{\theextfig}{}

But this prints a new chapter while I want nothing, it does not print the extended caption in the format the way I want, and does not read the label of the figure (or table) float, so I would have to set everything manually.

HcN
  • 1,274
  • 9
  • 25
  • 1
    You can always write \thefigure instead of \theextfig to the file. – cfr Nov 22 '15 at 03:49
  • Where can I find locloft? – cfr Nov 22 '15 at 03:52
  • @cfr tocloft ? It can be obtained from ctan: http://www.ctan.org/pkg/tocloft – HcN Nov 22 '15 at 03:58
  • 1
    Oh, a typo. OK. \makeatletter \newwrite\@efcwrite \immediate\openout\@efcwrite\jobname.efc \newcommand\extcaption[1]{% \par\noindent\textbf{#1}% \protected@write\@efcwrite{}{\figurename{} \thefigure: #1\par}} \makeatother Unfortunately, I don't know how to read it - only write it. – cfr Nov 22 '15 at 04:30
  • 1
    That is, I have only figured out how to write the things. I can't read them back. Sadly. – cfr Nov 22 '15 at 04:38

1 Answers1

2

The following writes out ToC-like entries to a new .efc file and reads it in using \printextcaptions:

enter image description here

\documentclass{report}
\usepackage{graphicx,lipsum}

\newcommand{\extcaption}[1]{% Write to .efc file
  \addcontentsline{efc}{extfig}{\thefigure, page~\thepage: #1}}
\makeatletter
\newcommand{\printextcaptions}{\@starttoc{efc}}% Read .efc file
\newcommand{\l@extfig}[2]{\noindent #1\par}% How each .efc entry is handled
\makeatother

\begin{document}

\sloppy% Just for this example

\chapter{A chapter}

\section{A section}

\printextcaptions

\lipsum[1-50]

\begin{figure}[ht]
  \includegraphics{example-image-a}
  \caption{Picture of a big A}
  \extcaption{This picture is magnificent}
\end{figure}

\lipsum[1-50]\lipsum[1-50]

\begin{figure}[ht]
  \includegraphics{example-image-b}
  \caption[This picture is horrible]{Picture of a big B}
  \extcaption{This picture is horrible}
\end{figure}

\end{document}
Werner
  • 603,163
  • Works perfectly, thanks ! Is there a way to write multiple-paragraphs in the extended caption ? – HcN Nov 22 '15 at 13:43
  • @HcN: The command \addcontentsline is defined using a \def, not \long\def, and therefore doesn't accept paragraphs. One can either copy the definition and define it as \long\def or \newcommand, or "hide" the paragraph breaks by using \protect\endgraf. No empty/blank line should be in your \extcaption argument, as these translate to an implicit \par, which is not allowed for \def-ed macros. – Werner Nov 22 '15 at 18:26
  • It is certainly worth at least trying to define an alternative \addcontentsline, but I cannot locate where the function is. Would you know where it is ? The \protect\endgraf works fine. – HcN Nov 22 '15 at 19:33
  • @HcN Look in latex.ltx. Probably. – cfr Nov 22 '15 at 22:43
  • @cfr Found it, and made something that essentially works. I asked another question for the remaining dodgy bits in http://tex.stackexchange.com/questions/279516/automatically-protect-new-paragraph-in-extended-figure-caption – HcN Nov 23 '15 at 00:30