1

Almost a duplicate of Display short title and long title (both) in a caption with subcaption

But they use KOMA and I use report class. I understand for me it should work simply with just:

\usepackage[compatibility=false]{caption}
\makeatletter
\let\x@caption\caption % original \caption
\def\x@@caption[#1]#2{\x@caption[{#1}]{#1 --- #2}} % with optional arg
\def\x@@@caption#1{\x@caption[{#1}]{#1}} % without optional arg
\def\caption{\@ifnextchar[\x@@caption\x@@@caption} % new \caption
\makeatother

But it doesn't. Although I get a warning (as expected) Package caption Warning: Forced redefinition of \caption since the unsupported(!) package option `compatibility=false' was given., the resulting document does not display the optional text.

Is this some recent update of the caption package that no longer works with the given code ? Following, is a really minimal working example. I haven't included subcaption yet or any other customisations that may break it.

\documentclass[12pt]{report}

\usepackage{graphicx}
\usepackage[compatibility=false]{caption}
\usepackage{subcaption}

    \makeatletter
    \let\x@caption\caption % original \caption
    \def\x@@caption[#1]#2{\x@caption[{#1}]{#1 --- #2}} % with optional arg
    \def\x@@@caption#1{\x@caption[{#1}]{#1}} % without optional arg
    \def\caption{\@ifnextchar[\x@@caption\x@@@caption} % new \caption
    \makeatother


\begin{document}
\listoffigures


\begin{figure}
  \includegraphics[width=.5\linewidth]{example-image-a}
  \caption[Optional]{mandatory}
\end{figure}


\end{document}

enter image description here

2

The caption package has some \AtBeginDocument hook that detect if the \caption command has changed from its original definition.

A workaround is to also make the redefinition of \caption \AtBeginDocument:

\documentclass[12pt]{report}

\usepackage{graphicx}
\usepackage[compatibility=false]{caption}
\usepackage{subcaption}

\makeatletter
\let\x@caption\caption % original \caption
\long\def\x@@caption[#1]#2{\x@caption[{#1}]{#1 --- #2}}% with optional arg
\def\x@@@caption#1{\x@caption[{#1}]{#1}}% without optional arg
\AtBeginDocument{%
  \def\caption{\@ifnextchar[\x@@caption\x@@@caption}% new \caption
}
\makeatother

\begin{document}
\listoffigures

\begin{figure}
  \includegraphics[width=.5\linewidth]{example-image-a}
  \caption[Optional]{mandatory}
\end{figure}

\end{document}
| improve this answer | |
  • The \AtBeginDocument also fixes the [compatibility=false] requirement. Interestingly, modifying \caption@caption causes it to hang. – John Kormylo Mar 9 '18 at 15:08
  • It works ! So easy haha! Why did none of the others mention this, I wonder... What do you mean with modifying \caption@caption ? – Ciprian Tomoiagă Mar 9 '18 at 15:15
  • 1
    The caption package replaces \caption with \caption@caption, so I thought why not just replace \caption@caption? Looking closer at the code, the caption package compares the definition of \caption with a long list of possible variations, and generates an error if your \caption isn't on the list. If they repeat the test after replacement, and replace again... – John Kormylo Mar 9 '18 at 15:31
  • coming again to this, it seems I have some captions with multiple paragraphs (each refers to subfigures). Before the above modifications, I had blank line to make paragraph. Now, it fails with Runaway argument. It works if I replace blank line with \\ . What do you think ? Should I just use \\ ? – Ciprian Tomoiagă Mar 14 '18 at 14:09
  • 1
    @CiprianTomoiagă I edited the answer. The only thing that changed is that \x@@caption is now a \long\def instead of just \def. TeX doesn't allow paragraph breaks inside \defs, unless you make them \long. But be careful, you cannot put paragraph breaks in the optional argument of the \caption. More specifically, the caption entry that goes to the List of Figures cannot have paragraph breaks. This is only allowed in the caption that goes below the figure. – Phelype Oleinik Mar 14 '18 at 21:03

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.