4

I am trying to renew the latex \caption command where I want a \listoffigures with the short description and both short and long descriptions in the figure's caption. The output that I expect is as follows:

enter image description here

I got the above output with the following code:

\documentclass{article}
\usepackage{graphicx}

\newcommand{\mycaption}[2][]{\caption[#1]{\textbf{#1} #2}}

\begin{document}

\listoffigures

\begin{figure} \centering \includegraphics[width=3cm]{example-image} \mycaption[Short Description.]{Long Description} \end{figure}

\end{document}

But, what I want:

\renewcommand{\caption}[2][]{\caption[#1]{\textbf{#1} #2}}

I would like to know if there is any build-in latex syntax to achieve my purpose.

cgnieder
  • 66,645

3 Answers3

5

\renewcommand{\caption}[2][]{\caption[#1]{\textbf{#1} #2}} won't work (as you probably discovered) because it creates a recursive definition, causing TeX to loop infinitely until something stops it.

One way around that would be to have:

\let\oldcaption=\caption
\renewcommand{\caption}[2][]{\oldcaption[#1]{\textbf{#1} #2}}
Teepeemm
  • 6,708
4

Your definition has more problems than just the name (\mycaption): if you don't use the optional argument of \mycaption you'll get

  • an empty entry in the list, and
  • a spurious space right before the caption.
\documentclass{article}
\usepackage{graphicx}

\newcommand{\mycaption}[2][]{\caption[#1]{\textbf{#1} #2}}

\begin{document}

\listoffigures

\begin{figure} \centering \includegraphics[width=3cm]{example-image} \mycaption{Only Description} \end{figure}

\begin{figure} \centering \includegraphics[width=3cm]{example-image} \mycaption[Short Description.]{Long Description} \end{figure}

\end{document}

enter image description here

This means you should test if a value to the optional argument has been given or not:

\documentclass{article}
\usepackage{graphicx}

\NewCommandCopy\mycaption\caption \RenewDocumentCommand\caption{om}{% \IfNoValueTF{#1}{\mycaption{#2}}{\mycaption[#1]{\textbf{#1} #2}}% }

\begin{document}

\listoffigures

\begin{figure} \centering \includegraphics[width=3cm]{example-image} \caption{Only Description} \end{figure}

\begin{figure} \centering \includegraphics[width=3cm]{example-image} \caption[Short Description.]{Long Description} \end{figure}

\end{document}

enter image description here

cgnieder
  • 66,645
4

I'd do it differently and define \mycaption with a trailing optional argument for the long description. The first optional argument has, by default, the same argument as the mandatory one (the short description), in which the punctuation is added only if really needed. However, you can still provide an entry for the list of figures/tables (say you want to fix a bad line break in either part).

\documentclass{article}
\usepackage{graphicx}
\usepackage{amsthm} % for \@addpunct

\makeatletter \NewDocumentCommand{\mycaption}{O{#2}mo}{% \caption[#1]{% \textbf{#2\IfValueT{#3}{@addpunct{.}}}% \IfValueT{#3}{ #3@addpunct{.}}% }% } \makeatother

\begin{document}

\listoffigures

\begin{figure}[htp] \centering \includegraphics[width=1cm]{example-image} \mycaption{Short Description}[Long Description] \end{figure}

\begin{figure}[htp] \centering \includegraphics[width=1cm]{example-image} \mycaption[Short]{Short Description?}[Long Description?] \end{figure}

\begin{figure}[htp] \centering \includegraphics[width=1cm]{example-image} \mycaption{Short Description} \end{figure}

\end{document}

enter image description here

egreg
  • 1,121,712
  • If possible, could you explain what the O and mo mean in this command please \NewDocumentCommand{\mycaption}{O{#2}mo}{%? I can follow the rest of your command but I cannot see why those letters in particular? Edit: Probably O "optional", mo "mandatory" "optional" arguments. –  Mar 25 '22 at 00:02