Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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 at 6:20
    
@AxelSommerfeldt This should interest you. Thanks for all your work. –  egreg Feb 19 at 12:35

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
    
This fixed the problem. Thanks! BTW, I checked the manual but couldn't find how to contact the author. –  krnk Feb 19 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 at 12:35

\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}
share|improve this answer
    
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 at 8:58
    
It works, because \ignorespaces triggers the expansion of \label and so it eats the spurious space. –  egreg Feb 19 at 10:19
    
@krnk Please see the edit. –  Harish Kumar Feb 19 at 10:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.