3

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}
\usepackage{hyperref}

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

yields

enter image description here

which is what I want. 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. How can I patch it to make it as I want?

I need a solution that works together with hyperref. An answer to this earlier question where hyperref isn't involved doesn't work with it, so it may complicate things.

pst
  • 4,674

1 Answers1

4

Well I think it is really the better way to typeset and measure the caption instead of looking at the tokens:

 \documentclass{scrartcl}

\usepackage{hyperref}

\usepackage{xpatch}
\makeatletter
\patchcmd\@@makecaption{\begingroup}
{%
 \settowidth{\@tempdima}{#3}%
 \ifdim\@tempdima=0pt
  \def\captionformat{}%
 \fi
  \begingroup
}{}{\fail}

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

Without hyperref this will also remove the colons for the caption with a space in it, but with hyperref this space counts.

enter image description here

Ulrike Fischer
  • 327,261
  • You still have the \usepackage{caption} line in there, which is what actually fixes it! – pst Feb 03 '19 at 12:11
  • Oh. Sorry. Will have to check again. – Ulrike Fischer Feb 03 '19 at 12:13
  • @pst I adapted the code. – Ulrike Fischer Feb 03 '19 at 12:24
  • Great! So now this is a better answer to my old question as well. For posterity I think it would be better if I remove that question and change this to be both without or with hyperref. What do you think? – pst Feb 03 '19 at 12:33
  • Are you OK with me doing as I suggested @Ulrike? (I don't want to go ahead otherwise since that question has an answer from you.) – pst Feb 06 '19 at 11:06
  • I would suggest that you leave a link at the older question refering to this one as a follow up.. – Ulrike Fischer Feb 06 '19 at 11:07
  • Yes, with two questions of course there should be links both ways. I haven't done that exactly because I'd prefer to remove it instead. For future visitors a single (slightly edited) question is much better, since the answer here is better than the answer there for "both questions". We want everyone who looks for an answer to find this question and this answer, and not the older one. – pst Feb 06 '19 at 11:46
  • I was looking for some answer where it says how to remove the colon after the number of the figure or table but I have not found anything. How could that be achieved in KOMA Script? Is it valid to ask it here? – Aradnix Feb 16 '19 at 03:40
  • @UlrikeFischer Measuring the width means adding another measurement but the already existing one ("Single-line-check") makes more than enough trouble, at least in my point of view. –  Feb 17 '19 at 20:32