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}
pst
  • 4,674

1 Answers1

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

Ulrike Fischer
  • 327,261
  • 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 01 '19 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 ... – Ulrike Fischer Feb 01 '19 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 01 '19 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 01 '19 at 15:15
  • \typeout should work, put it before the \tl_if_eq. – Ulrike Fischer Feb 01 '19 at 15:17
  • Oh, now I see it does work for the simple example. When it didn't work in my actual file I assumed I was doing it wrong. It was interference with hyperref that made it not work. I've tried just adding that to the small example and don't get it to work, regardless of loading order. So now I want to amend my question (or maybe make a new one)... – pst Feb 01 '19 at 15:36
  • 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{. – Schweinebacke Feb 02 '19 at 15:30