Distance between caption and table differs (scrbook)

The KOMA-classes provide an option captions=nooneline. If this option is set, all captions of figures and tables are justified left; if it is not set, captions that are short enough to fit on one line are centered.

I have found that the distance between caption and the top of the table differs depending on whether this option is set or not: if the caption is centered, it will be set closer to the table than when it is justified. Here is an MWE:

\documentclass{scrbook}

\newlength{\tabwidth}                                                           % Länge für Tabellenbreite (wird bei den Tabellen gesetzt)

\KOMAoptions{%
captions=figurebelow,%
captions=nooneline,%
}

\setkomafont{caption}{\sffamily\mdseries\footnotesize}
\setkomafont{captionlabel}{\usekomafont{caption}\bfseries}

%----------------------------------
\begin{document}
\chapter{Test}
\setlength{\tabwidth}{.5\textwidth}

\begin{table}[!ht]
\centering
\setcapwidth{\tabwidth}
\caption{Table with short caption}\label{tab:01}
\footnotesize
\sffamily
\framebox[\tabwidth]{\centering Here would be a real table.}
\end{table}

\begin{table}[!hb]
\centering
\setcapwidth{\tabwidth}
\caption{This is a table with a very, very long caption that should span one line, and another line, perhaps even a third one.}\label{tab:02}
\footnotesize
\sffamily
\framebox[\tabwidth]{\centering Here would be another real table.}
\end{table}

\end{document}


Compile it once as it is, and you should get this result:

then comment out captions=nooneline, compile again, and you should get this:

In both pictures, the table on the top is the interesting one. Notice that there is a gap between the screenruler and the table top in the first picture, while the ruler fits snugly between caption and the table in the second? You can also see that this option has no effect with longer captions that span more than one line (as could be expected).

Is this really correct behavior, or is it a bug? For my taste, it would look better if all captions would have the same distance from the table. Imagine you had two floats on one page: if they were close enough together, one could think the head-caption of the table actually was the subcaption of a figure. Does anybody know a way to get the same alignment regardless of captions=nooneline is set?

• As a workaround: add \usepackage{caption}. – esdd May 28 '16 at 12:32
• @esdd As a workaround, this works fine. However, I thought that after switching to the KOMA-classes, I could ditch caption (which I regularly used before) as (most of) its functionality was implemented in the class. So the package has come back for the moment -- thanks for the advice! – Andreas May 28 '16 at 12:59
• That's funny, I didn't expect that adding my caption package would make a significant difference. However, if you think the outcome could be tweaked to be more consistent regarding spacing (without using the caption package) I would ask Markus Kohm (the author and maintainer of koma-script) to do so. – Axel Sommerfeldt May 29 '16 at 10:44
• @AxelSommerfeldt Something seems to be handled differently. See the manual (scrguien.pdf), page 120: "Of course KOMA - Script takes care of the heading and signature setting of option captions. But this feature may be lost, loading package capt-of or caption." I think I'll notify Markus Kohm of this issue and continue using your package for the time being. Not that I wasn't satisfied with caption in the first place (quite to the contrary!), I just wanted to reduce the number of packages loaded each time I run LaTeX. :-) – Andreas May 30 '16 at 12:47
• Markus knows this thread and the problem. I have informed him two days ago. – esdd May 31 '16 at 7:51

[lt;dr] When text has to be put into a box, don't check the distances ;-)

Long explanation:

KOMA-Script has to put the caption text into a box to provide the given functionality. But TeX only has one baseline per box, and when you put text into a box, you have decide where to put the baseline, at the top or at the bottom line. Whatever you choose, the other distance will be incorrect.

Example code:

\documentclass{article}
\newcommand\testtext{This is a table with a very, very long caption that should span one line, and another line, perhaps even a third one.}
\begin{document}
\testtext\testtext\testtext

\vtop{\testtext\testtext\testtext}

\testtext\testtext\testtext

\vbox{\testtext\testtext\testtext}

\testtext\testtext\testtext
\end{document}


As you see the \vtop will make the distance to the upper paragraph correct, but not the distance to the following paragraph. \vbox will do the opposite.

One could try to correct this with a so-called strut, but when simply added by \strut the outcome could be even worse:

\documentclass{article}
\newcommand\testtext{This is a table with a very, very long caption that should span one line, and another line, perhaps even a third one.}
\begin{document}
\testtext\AA{}\testtext\AA{}\testtext

\vtop{\testtext\AA{}\testtext\AA{}\testtext\strut}

\testtext\AA{}\testtext\AA{}\testtext

\vbox{\strut\testtext\AA{}\testtext\AA{}\testtext}

\testtext\AA{}\testtext\AA{}\testtext
\end{document}


The problem with \strut is that it's like using a hammer to correct things here: It makes the distance usually too large instead of too small since it has the height and depth of the biggest character in font. Furthermore it operates in both directions, therefore changing the distance between lines in the text, too. (To make this even more worse, I added some \AA{} to the text.) One can correct this by using \topstrut resp. \bottomstrut:

\documentclass{article}
\newcommand\testtext{This is a table with a very, very long caption that should span one line, and another line, perhaps even a third one.}
\makeatletter
\newcommand\topstrut{\vrule\@height\ht\strutbox\@width\z@}
\newcommand\bottomstrut{\@finalstrut\strutbox}
\makeatother
\begin{document}
\testtext\AA{}\testtext\AA{}\testtext

\vtop{\testtext\AA{}\testtext\AA{}\testtext\bottomstrut}

\testtext\AA{}\testtext\AA{}\testtext

\vbox{\topstrut\testtext\AA{}\testtext\AA{}\testtext}

\testtext\AA{}\testtext\AA{}\testtext
\end{document}


While this looks better that the previous example documents, we still have slightly different distances between the paragraphs. We can align this by using both in each box, \topstrut and \bottomstrut:

\documentclass{article}
\newcommand\testtext{This is a table with a very, very long caption that should span one line, and another line, perhaps even a third one.}
\makeatletter
\newcommand\topstrut{\vrule\@height\ht\strutbox\@width\z@}
\newcommand\bottomstrut{\@finalstrut\strutbox}
\makeatother
\begin{document}
\testtext\AA{}\testtext\AA{}\testtext

\vtop{\topstrut\testtext\AA{}\testtext\AA{}\testtext\bottomstrut}

\testtext\AA{}\testtext\AA{}\testtext

\vbox{\topstrut\testtext\AA{}\testtext\AA{}\testtext\bottomstrut}

\testtext\testtext\testtext
\end{document}


This looks best since the distance between the paragraphs are at least equal now. But they are (still) incorrect. And if you add another paragraph (without using a box) you'll see that the distance between the two ordinary paragraphs is slightly less that the distance between the other paragraphs. That's a major problem when using \strut or something similar, you can't fix the problem but only trying to make it less annoying.

So the main problem is build into TeX itself. But why TeX only handles one baseline per box, and not two, an upper and a lower baseline? Because of the processor power and RAM amount available when TeX was written. So what we see here is a relic of the stone age of computing.

And what has this to do with KOMA-Script? When looking at the \caption code of KOMA-Script one can see that it uses \vbox internally and tries to correct distances using \strut.

Update

The bug was fixed in KOMA-Script version 3.21 which is already on CTAN, in TeX Live 2016 and MiKTeX.

This is now a known issue of KOMA-Script version 3.20 (German). The official workaround at the moment is loading package caption as I suggested in a comment.