# Consistent spacing between caption and TikZ and EPS figures

The problem I have is that spacing between a TikZ figure and its caption is bigger that between any other type of pictures and their spacing:

Obviously, it can be solved by hardcoding \vspace{} or adjusting skip in the preamble or floatwise, which seems a bit tedious and not 'semantical'. But how can it be fixed globally without breaking spacing for other figures?

And, generally speaking, why is Tikz—caption spacing is so big (even without EPS to compare)?

(M)WE:

\documentclass[
oneside,
openright,
titlepage,
numbers=noenddot,
footinclude,
cleardoublepage=empty,
BCOR=5mm,
paper=b5,
fontsize=10pt
abstract=on
]{scrreprt}

\usepackage{classicthesis}
\usepackage{tikz,pgfplots}
\usepackage{subcaption}
\usetikzlibrary{backgrounds}
width=\textwidth,
height=0.75\textwidth,
%tick label style={font=\small},
%legend style={font=\small},
ylabel shift={-3pt},
xlabel shift={-3pt}
}

\begin{document}
\begin{figure}
\begin{subfigure}[b]{0.49\textwidth}
% This file was created by matplotlib2tikz v0.6.18.
\input{pic.tex}
\caption{Figure}
\end{subfigure}
~
\begin{subfigure}[b]{0.49\textwidth}
% This file was created by matplotlib2tikz v0.6.18.
\input{pic.tex}
\caption{Figure}
\end{subfigure}

\end{figure}
\end{document}


pic.tex:

% This file was created by matplotlib2tikz v0.6.18.
\begin{tikzpicture}[background rectangle/.style={fill=olive!45}, show background rectangle]

\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}
\definecolor{color1}{rgb}{1,0.498039215686275,0.0549019607843137}

\begin{axis}[
legend cell align={left},
legend entries={{$\lambda_{min}$},{$\lambda_{max}$}},
legend style={at={(0.03,0.03)}, anchor=south west, draw=white!80.0!black},
tick align=outside,
tick pos=left,
x grid style={white!69.01960784313725!black},
xlabel={$\omega$},
xmin=0, xmax=5.1,
xtick={0,1,2,3,4,5,6},
xticklabels={$0$,$1$,$2$,$3$,$4$,$5$,$6$},
y grid style={white!69.01960784313725!black},
ylabel={$\log_{10} ||\lambda_i| - 1|$},
ymin=-15, ymax=2,
ytick={-16,-14,-12,-10,-8,-6,-4,-2,0,2},
yticklabels={$-16$,$-14$,$-12$,$-10$,$-8$,$-6$,$-4$,$-2$,$0$,$2$}
]
table [row sep=\\]{%
0   -0.0150784734238995 \\
0.00340226817878586 -0.0150785247930231 \\
0.00680453635757171 -0.0150786789065911 \\
0.0102068045363576  -0.0150789357831926 \\
0.0136090727151434  -0.0150792954538207 \\
0.0170113408939293  -0.0150797579618575 \\
5.08639092728486    1.64658769206306 \\
5.08979319546364    1.65617254087203 \\
5.09319546364243    1.66573665825563 \\
5.09659773182121    1.67528018607781 \\
5.1 1.6848032640236 \\
};

\end{axis}
\end{tikzpicture}


Compiles to:

But \fbox{} fixes it:

• the spacing from caption will be the same, presumably you have white space in your tikz picture, but as you have provided no usable example it is impossible to say. It is always better to provide a complete test document and show the image of that document not output from a document that's not posted. compare \fbox{\includegraphics{...}} and \fbox{\input{...tikz..}} so you can see the size of the included graphic – David Carlisle Nov 16 '18 at 18:51
• @DavidCarlisle You're right. I've added a WE that compiles with huge spaces. – homocomputeris Nov 16 '18 at 19:21
• @DavidCarlisle It turns out that fbox reduces the spacing. – homocomputeris Nov 16 '18 at 19:24
• oh it's not vertical space it is a white line of a paragraph text tex does tell you it is maxmally bad: Underfull \hbox (badness 10000) in paragraph at lines 40--39 – David Carlisle Nov 16 '18 at 20:40

The image is too wide and forcing a white (underfull) line of text after the paragraph, if you change the last line of pic.tex from

\end{tikzpicture}


to

\end{tikzpicture}%


then the output changes from

to

Basically whenever TeX warns that

Underfull \hbox (badness 10000) in paragraph at lines 40--33


It means something is pretty bad and usually that there is a spurious empty line in the document (10000 is the maximum value of badness, so TeX is warning you that the output is as bad as it can be.....)

• Impressive, indeed. ;-) – user121799 Nov 16 '18 at 21:03
• Thanks! Overfull \hbox (22.30797pt too wide) in paragraph is what it tell me. Why does fbox fix it? Does it force TikZ to be contained and do not spawn that line? – homocomputeris Nov 16 '18 at 21:36
• @homocomputeris if you use fbox (or mbox) then line breaking at the space is prevented inside the box but actually the main line is then even more overfull, the real fix is to make the the tikz images a bit smaller so that they fit in the page. – David Carlisle Nov 16 '18 at 21:45