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}

modifying \caption@caption? – Ciprian Tomoiagă Mar 09 '18 at 15:15Runaway 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\x@@captionis now a\long\definstead 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