2

I am trying to use the tweak provided in Display short title and long title (both) in a caption? together with the subcaption package.

Unfortunatelly it breaks:

\documentclass{scrartcl}
\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}
\centering
\rule{5cm}{5cm}
\caption[Optional text.]{Obligatory text.}
\end{figure}

\begin{figure}
\centering
\rule{5cm}{5cm}
\caption{Obligatory text only.}
\end{figure}

\end{document}

I am using many subfigures in my document, so going away from the subcaption package is no alternative.

Edit:

Using the caption package with option \usepackage[compatibility=false]{caption} does produce figure captions of the intended format (Optional text. -- Obligatory text.) with the corresponding list of figures entry (Optional text.). Thanks to TeXnician for the remark.

2 Answers 2

1

There is more than one way to cause caption to behave differently in different floats. The usual way is to set and test \@captype, but one can also simply redefine \caption inside the float. This appears to be the approach used by KOMA. So you just need to reset it back again.

Note: the ##1 is to not confuse it with the #1 argument for \captionreset.

\documentclass{scrartcl}
\usepackage[compatibility=false]{caption}
\usepackage{subcaption}

\makeatletter
\newcommand{\captionreset}{%
  \let\x@caption\caption % original (caption package) \caption
  \renewcommand{\caption}[2][\empty]{\ifx\empty##1\relax\x@caption{##2}%
    \else\x@caption[##1]{##1 -- ##2}%
    \fi}% new \caption
}
\let\x@figure=\figure % original figure
\renewcommand{\figure}{\x@figure\captionreset}
\makeatother

\begin{document}

\listoffigures

\begin{figure}
\centering
\rule{5cm}{5cm}
\caption[Optional text.]{Obligatory text.}
\end{figure}

\begin{figure}
\centering
\rule{5cm}{5cm}
\caption{Obligatory text only.}
\end{figure}

\end{document}

From the KOMA source I found that \caption was being replaced by either \captionabove or \captionbelow. So a simpler solution would be to replace those in the preamble.

\makeatletter
\let\x@caption\caption% original (caption package) \caption
\newcommand{\x@@caption}[2][\empty]{%
  \ifx\empty#1\relax\x@caption{#2}%
  \else\x@caption[#1]{#1 -- #2}%
  \fi}% modified caption
\let\caption\x@@caption% new global \caption for other float types
\renewcommand{\captionabove}[2][\empty]{\captionsetup{position=above}%
  \x@@caption[#1]{#2}%
}% new \captionabove
\renewcommand{\captionbelow}[2][\empty]{\captionsetup{position=below}%
  \x@@caption[#1]{#2}%
}% new \captionbelow
\makeatother
4
  • Only remaining problem: I have no clue how this might work with lstlistings with enabled float... but for figures and tables this is perfect!
    – BR123
    Feb 16, 2017 at 0:14
  • I found a simpler solution. Feb 16, 2017 at 18:18
  • how can we make this work with report class ? Mar 8, 2018 at 21:51
  • @CiprianTomoiagă - The OP code should work with report class. It is KOMA that makes it difficult. Mar 9, 2018 at 2:04
0

You simply have to disable compatibility mode. This works:

\documentclass{scrartcl}
\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}
\centering
\rule{5cm}{5cm}
\caption[Optional text.]{Obligatory text.}
\end{figure}

\begin{figure}
\centering
\rule{5cm}{5cm}
\caption{Obligatory text only.}
\end{figure}

\end{document}
3
  • 1
    Sorry, I forgot to mention that I already tried this: Then the [Optional text] does not show up in the figure caption as intended
    – BR123
    Feb 15, 2017 at 11:48
  • Than: \caption[Text in LOF.]{Text in LOF. Some more text.} ...
    – Zarko
    Feb 15, 2017 at 11:55
  • I don't like this approach since then I have to write the text twice. This is vulnerable to errors
    – BR123
    Feb 21, 2017 at 13:29

You must log in to answer this question.

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