# White space with subfigures using a macro

EDIT

The answer proposed by egreg doesn't work for me:

When changing the outer sep to outer sep=-10pt, instead of shifting the images, just the labels get shifted and the white space persists.

I guess this indicates the white space is generated somewhere outside the tikzpicture environment.

Any ideas?

Initial question

I want to place several subfigures within a figure in multiple rows. Since I don't want the several rows of images being separated by the subcaption of the subfigures, I defined a macro, placing a label on the images with tikz and using the \phantomsubcaption command:

\newcommand{\customcaption}[4]%
{%
{%
\tikz%
{%
\node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[#1]{#2}};%
\begin{scope}[x={(image.south east)},y={(image.north west)}]%
\draw[white] (0,0) node [font=\footnotesize, anchor=south west] {#3};%
\end{scope}%
}%
\phantomsubcaption\label{#4}%
}%
}%
% Macro to typeset a label onto a picture and putting a \phantomlabel for reference
% Arguments are: #1 Dimension of the graphic, #2 file path, #3 drawn label, #4 reference label


However, the macro produces white space arround the images, which I can't get rid of.

What do I need to do, to prevent the white space?

Here is a MWE replacing \includegraphics by an rectangle drawn with tikz:

\documentclass[12pt,oneside,a4paper]{book}
%
\usepackage{tikz}
\usepackage[font+=footnotesize, subrefformat=parens]{subcaption}
%
\newcommand{\customcaption}[2]%
{%
{%
\tikz%
{%
\node[anchor=south west,inner sep=0, fill=black, shape=rectangle, minimum size=3cm] (image) at (0,0) {};%
\begin{scope}[x={(image.south east)},y={(image.north west)}]%
\draw[white] (0,0) node [font=\footnotesize, anchor=south west] {#1};%
\end{scope}%
}%
\phantomsubcaption\label{#2}%
}%
}%
%
\begin{document}%
%
\begin{figure}%
\centering
\customcaption{(a)}{a}%
\customcaption{(b)}{b}%
\customcaption{(c)}{c}%
\end{figure}%
%
\end{document}

• It seems like word space. But we cannot reproduce these spaces using your MWE. I.e., there is a problem in your inputted files. – wipet May 20 '16 at 13:16
• @wipet There are no input files in my MWE and the pictures I am posting are generated with my MWE or the code given by egreg in his answer. – JMP May 20 '16 at 13:33
• When I run your MWE then there are 113 another input files. See the log file: book.cls, bk12.clo, tikz.sty, pgf.sty, pgfrcs.sty, pgfutil-common.tex, pgfutil-common-lists.tex, etc, etc. And something similar is at your side in your computer. One of your input file is bad. – wipet May 20 '16 at 13:43
• @wipet As already mentioned in my comment under egregs answer, all my LaTex packages are up to date! – JMP May 20 '16 at 13:52

## 1 Answer

You're forgetting outer sep.

Don't be obsessed by end-of-lines! ;-)

\documentclass[12pt,oneside,a4paper]{book}

\usepackage{tikz}
\usepackage[font+=footnotesize, subrefformat=parens]{subcaption}

\newcommand{\customcaption}[2]{{%
\begin{tikzpicture}
\node[
anchor=south west,
inner sep=0,
outer sep=-0.2pt,
fill=black,
shape=rectangle,
minimum size=3cm
] (image) at (0,0) {};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\draw[white] (0,0) node [font=\footnotesize, anchor=south west] {#1};
\end{scope}
\end{tikzpicture}%
\phantomsubcaption\label{#2}%
}}

\begin{document}

\begin{figure}
\centering
\customcaption{(a)}{a}%
\customcaption{(b)}{b}%
\customcaption{(c)}{c}%
\end{figure}

\end{document}


Spaces inside a tikzpicture are ignored (but they are necessary for syntactic purposes, of course). So you just need % after \end{tikzpicture}.

No % is necessary after \begin{figure} or \end{figure}.

• This is strange. When compiling your code I get exactly the same result as in my question. The white space persists! Looks like there is something different wrong as well. :-/ – JMP May 20 '16 at 11:24
• @JMP Possibly an issue with outdated software? – egreg May 20 '16 at 12:46
• When thinking about your answer twice, you just corrected for half the linewidth of the outer boundary, which was not drawn. When saying filldraw=black instead of fill=black the whitespace should vanish for you, without using outer sep=-0.2pt. But then the question is, why is the white space created for me, but it works for you? – JMP May 20 '16 at 12:48
• First thing I did, after you proposed your answer and it didn't worked for me was updating my latex packages. However, the problem persists. So I don't think it's a software issue. – JMP May 20 '16 at 12:49
• Could be this bug: sourceforge.net/p/latex-caption/tickets/20 However, it should be resolved when using the latest version of the caption package bundle available on CTAN. So double-check the version of subcaption.sty actually used when compiling your document, it should be from 2016-02-20. – Axel Sommerfeldt May 20 '16 at 16:46