# Why does \label create an undesired white space after \phantomsubcaption?

Usually \label does not create any white spaces. However, It creates an white space when \label is used after \phantomsubcaption from subcaption package.

• Why?
• How can I get rid of this white space?

Here is a MWE:

\documentclass{article}
\usepackage{subcaption}

\begin{document}

\section{a}
a\label{sec:a}b% no white space between a' and b'

\begin{figure}
\centering
{a\phantomsubcaption\label{fig:a}}%
{b\phantomsubcaption\label{fig:b}}
\caption{With label}% undesired white space between a' and b'
\end{figure}

\begin{figure}
\centering
{a\phantomsubcaption}%
{b\phantomsubcaption}
\caption{Without label}% no white space between a' and b'
\end{figure}

\end{document}

-
If one drops the relevant \phantomsubcaption statements in the \labeled figure environment there is no additional whitespace. However, the labels fig:a and fig:b are unknown outside. Why do you want to have such an arrangement? Perhaps it could be achieved with other methods. – Christian Hupfer Feb 19 '14 at 6:20
@AxelSommerfeldt This should interest you. Thanks for all your work. – egreg Feb 19 '14 at 12:35

There is an error in line 87 of subcaption.sty:

 87 \newcommand*\subcaption@label{
88   \caption@withoptargs\subcaption@@label}


At the end of line 87, after the brace, a % is missing.

If you add to your preamble, after loading subcaption,

\makeatletter
\renewcommand*\subcaption@label{%
\caption@withoptargs\subcaption@@label}
\makeatother


the output will be correct, without any spurious space.

Please, make this known to the package maintainer.

-
This fixed the problem. Thanks! BTW, I checked the manual but couldn't find how to contact the author. – krnk Feb 19 '14 at 10:57
@krnk He's active here, so he should already be aware of the problem, but I'll ping him – egreg Feb 19 '14 at 12:35
I'm deeply sorry. (Shame on me.) I filled the issue sourceforge.net/p/latex-caption/tickets/20 but totally forgot to fix this one. I have done so just now, and will upload the fixed version to CTAN tomorrow. If waiting is not an option grab the actual files from sourceforge.net/p/latex-caption/code/HEAD/tree/branches/3.3/tex – Axel Sommerfeldt Feb 20 at 20:20
I have just done the CTAN upload (caption package bundle release 2016-02-21), so the update should be available on CTAN, and for TeXlive, MikTeX and other TeX distributions within the next days. – Axel Sommerfeldt Feb 21 at 12:10
@AxelSommerfeldt Thank you! – egreg Feb 21 at 12:10

\phantomsubcaption and \phantomcaption do not have any arguments. Further, they do not generate any output, but give you an anchor for a \label command which can be placed afterwards. In addition, it increases the sub-figure and/or sub-table counter. It is noteworthy that – just like \subcaption – the \phantomsubcaption command must be applied inside its own group, box, or environment.

Here in your code, as you are not using any subfigure environment, you can put \phantomsubcaption inside braces like {\phantomsubcaption}.

\documentclass{article}
\usepackage{subcaption}

\begin{document}

\section{a}
a\label{sec:a}b% no white space between a' and b'

\begin{figure}
\centering
{a{\phantomsubcaption}\label{fig:a}}%
{b{\phantomsubcaption}\label{fig:b}}
\caption{With label}% undesired white space between a' and b'
\end{figure}

\begin{figure}
\centering
{a\phantomsubcaption}%
{b\phantomsubcaption}
\caption{Without label}% no white space between a' and b'
\end{figure}

\end{document}


Or you can also use \ignorespaces like

{a\phantomsubcaption\ignorespaces\label{fig:a}}%


Full code:

\documentclass{article}
\usepackage{subcaption}

\begin{document}

\section{a}
a\label{sec:a}b% no white space between a' and b'

\begin{figure}
\centering
{a\phantomsubcaption\ignorespaces\label{fig:a}}%
{b\phantomsubcaption\label{fig:b}}
\caption{With label}% undesired white space between a' and b'
\end{figure}

\begin{figure}
\centering
{a\phantomsubcaption}%
{b\phantomsubcaption}
\caption{Without label}% no white space between a' and b'
\end{figure}

\end{document}

-
Thanks, it worked. Is it possible to automate the insertion of \ignorespaces? I mean redefining something so that we don't have to insert \ignorespaces in every \phantomsubcaption\label. I also want to know why this happens only after \phantomsubcaption. – krnk Feb 19 '14 at 8:58
It works, because \ignorespaces triggers the expansion of \label and so it eats the spurious space. – egreg Feb 19 '14 at 10:19
@krnk Please see the edit. – Harish Kumar Feb 19 '14 at 10:19