2

I like captions to normally have a colon, but not when the caption is empty. This is something that the package caption does automatically, so

\documentclass{scrartcl}
\usepackage{caption}
\begin{document}
\begin{figure}
  \caption{Test}
  \caption{}
\end{figure}
\end{document}

yields "Figure 1: Test / Figure 2". That is all very fine, but I want to use KOMA captions without the caption package, and then I get "Figure 1: Test / Figure 2:" with the extra colon at the end.

Here is my best try in fixing this, but it doesn't work. How can I do instead?

\documentclass{scrartcl}
\usepackage{xifthen}
\makeatletter
\renewcommand{\scr@@makesinglelinecaption}[3]{%
    \usekomafont{caption}{\strut\ignorespaces
      #1{{\usekomafont{captionlabel}{#2\ifthenelse{\isempty{#3}}{}{\captionformat}}}}%
      \ignorespaces #3\unskip}%
}
\makeatother

\begin{document}
\begin{figure}
  \caption{Test}
  \caption{}
\end{figure}
\end{document}
1

1 Answer 1

6

Your test doesn't work as KOMA doesn't pass an empty argument, it contains (for an unknown reason) \ignorespaces, so you should test for this:

\documentclass{scrartcl}
\usepackage{expl3,xpatch}
\ExplSyntaxOn
\makeatletter
\patchcmd\scr@@makesinglelinecaption{\captionformat}
 {\tl_if_eq:nnF {\ignorespaces}{#3}{\captionformat}}{}{\fail}
\ExplSyntaxOff

%\usepackage{caption}
\begin{document}
\begin{figure}
  \caption{Test}
  \caption{}
\end{figure}
\end{document}

enter image description here

7
  • Thanks! I guessed there was "something" there anyway, but I didn't know what. Would have liked some is-essentially-empty predicate (maybe there is?) that would work even if the innards changed.
    – pst
    Feb 1, 2019 at 14:41
  • 1
    You could put the argument in a box and measure its width - actually now that you mention it: this patch will only work if you use the oneline option, without it the singlelinecaption is not used. On the whole it is probably better to patch \@@makecaption, or to make a feature request ... Feb 1, 2019 at 14:47
  • Right. Normally an empty caption will be a oneliner, so that's why I went there. It doesn't work with captions=nooneline, but that's not a problem for me.
    – pst
    Feb 1, 2019 at 14:53
  • Actually now it didn't work for me in an actual more complicated environment. How can I log what is in #3 then? I naively tried \typeout{#3}.
    – pst
    Feb 1, 2019 at 15:15
  • 1
    The \ignorespaces is from the definition of \@caption in the LaTeX kernel. KOMA-Script does not redefine \caption or \@caption. And the \ignorespaces could make sense, e.g., to tolerate a line break after \caption{. Feb 2, 2019 at 15:30

You must log in to answer this question.

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