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.

I know that tex.sx isn't the forum for bug-reporting, but I have a bit of a problem and I'm looking for a technical workaround. The following MWE produces odd labels:

\documentclass{article}

\usepackage{subcaption}
\usepackage{tabularx}

\begin{document}
\begin{figure*}
  \begin{subfigure}{0.3\textwidth}
    Figure 1a
    \caption{Fig1a}
    \label{fig:1a}
  \end{subfigure}
  \hfill
  \begin{subfigure}{0.5\textwidth}
\begin{tabularx}{0.5\textwidth}{r}
Figure 1b
\end{tabularx}
    \caption{Fig1b}
    \label{fig:1b}
  \end{subfigure}
  \caption{Fig1}
  \label{fig:1}
\end{figure*}
\begin{figure}
Figure 2
\caption{Fig2 caption}
\label{fig:2}
\end{figure}
Figures \ref{fig:1a}, \ref{fig:1b}, and \ref{fig:1} should be 1a, 1b
and 1; Figure \ref{fig:2} should be 2.
\end{document}

Specifically, the last line of text reads, "Figures 1a, 1b, and 2 should be 1a, 1b and 1; Figure 3 should be 2." (I know that the example doesn't use any of tabularx's features; merely the use of the tabularx environment is enough to throw off the labels.) If I move the \caption{Fig1}\label{fig:1} above the subfigures, then I get the right numbering, but of course the caption is now in the wrong position.

According to \listfiles, I'm using

 article.cls    2007/10/19 v1.4h 
  size10.clo    2007/10/19 v1.4h 
subcaption.sty    2011/10/30 v1.1c 
 caption.sty    2011/11/10 v3.2e 
caption3.sty    2011/11/01 v1.4a 
  keyval.sty    1999/03/16 v1.13 
tabularx.sty    1999/01/07 v2.07 
   array.sty    2008/09/09 v2.4c 

So my question is, what is tabularx doing that is not properly respecting the label counter, and how could I have deduced that myself?

share|improve this question
    
Not sure if this constitutes an answer, but if you change tabularx to tabular* then everything seems to work the way you want –  cmhughes Dec 16 '11 at 19:10
6  
This is clearly a bug in my subcaption package (I can't explain yet). I will take a closer look at it tomorrow, and try to offer a fix ASAP. –  Axel Sommerfeldt Dec 16 '11 at 19:16
    
@AxelSommerfeldt, thanks! –  Ben Lerner Dec 16 '11 at 19:17
1  
@AxelSommerfeldt The problem is in line 68 of tabularx.sty, where, according to the documentation, all values of counters are saved globally while, as I understand, subcaption steps figure only locally inside a subfigure environment. –  egreg Dec 16 '11 at 20:26
1  
I will build a new version 3.2f of my caption package on this week-end anyway, so I will include a fix for that one, too, and it should be available within the next days. –  Axel Sommerfeldt Dec 16 '11 at 21:34
show 7 more comments

1 Answer 1

up vote 5 down vote accepted

The problem is in the fact that subfigure steps the figure counter, in order to assign correctly the meaning to the \label commands inside it. However it does it only locally, so that the main \caption command will assign the correct figure number.

However, part of tabularx working is to save globally the value of all counters (this is because it processes twice its contents and counters need to be restored to their previous values)

A workaround seems issuing \addtocounter{figure}{-1} before the main \caption, after a subfigure environment containing tabularx.

Update 2013/01/09 (from Axel Sommerfeldt):

The (now available) version 3.3 of the caption package has a different counter handling, so counters will now neither be changed locally, nor decremented temporarily anymore. This will hopefully fix all issues regarding wrong figure or table counters, so updating the caption package should help.

share|improve this answer
    
Thanks! That workaround does indeed resolve my bug, but I'm going to hold off accepting this answer for a bit to give Axel a chance to address it properly in his package. Also, (and this just being slightly cheeky), the question was intended not just as a fix-my-bug question, but did ask, "how might I have diagnosed that myself?"... :) I can read through LaTeX code reasonably well, but I'm not familiar with the guts of either of these packages. Did you do anything particularly clever to find this global save, or just "read through the code"? –  Ben Lerner Dec 16 '11 at 21:16
1  
The problem is that the figure number is off by 1; so I put some \showthe\value{figure} around to figure out where it was changed. Then I read tabularx code commentary and found out what possibly is the cause. –  egreg Dec 16 '11 at 21:21
    
@egreg: Thanks for the analysis. I'm unhappy with this workaround since it will have a negative effect ones the problem was fixed. But I must confess that I can't offer a better one, therefore I just upvoted your answer. –  Axel Sommerfeldt Dec 16 '11 at 21:33
1  
@AxelSommerfeldt Just remember me when the package is fixed, or edit the answer yourself to tell everybody that the workaround has become obsolete. I'm sure that Ben will keep an eye on his question. Moreover, people finding this with search engines will see the updated answer. –  egreg Dec 16 '11 at 21:48
    
@egreg I just updated your answer, hope it's ok. –  Axel Sommerfeldt Jan 9 '13 at 21:12
show 2 more comments

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.