[Tex/LaTex] Caption makes Table move in statsoc class

auxiliary-filescaptionstables

I am working in statsoc.cls (you can find it here). Here I show you a working file for the problem I am facing in my paper.

I have a table which has a long-ish caption (2-3 lines). Every time I compile my .tex file, the table moves a little to the right of the page. I think it is because the captions stop wrapping at some point.

\documentclass{statsoc}

%%%% Packages to be used
\usepackage[a4paper]{geometry}
\usepackage{amssymb, amsmath}

\usepackage{graphicx}
\usepackage{subfig} 
\usepackage{enumerate}
\usepackage{comment}
\usepackage{natbib, float}

\title{``Table keeps moving every time I compile''}
\begin{document}

\maketitle

\begin{table}
  \caption{Table moves a little everytime I compile. I think this is because I have a long caption, but I cannot figure out how to resolve this problem. As you can see I am using statsoc.cls. I can reset the table if I delete the .aux file and recompile}
  \fbox{%
    \begin{tabular}{c c c c c c c c c c c c c c c c c c}
    \hline
    1 & 2 & 3 & 4 & 5 & 2 & 3 & 4 & 5 & 1 & 2 & 3 & 4 & 5 & 2 & 3 & 4 & 5\\
    1 & 2 & 3 & 4 & 5 & 2 & 3 & 4 & 5 & 1 & 2 & 3 & 4 & 5 & 2 & 3 & 4 & 5\\
    1 & 2 & 3 & 4 & 5 & 2 & 3 & 4 & 5& 1 & 2 & 3 & 4 & 5 & 2 & 3 & 4 & 5\\
    \hline
    \end{tabular}}
\end{table}
\end{document}

When I delete the .aux file and re-compile, it starts off as this:

enter image description here

Then after one compilation becomes

enter image description here

Then after many more

enter image description here

As I recompile, I make no changes inside the table environment, and work outside it. I believe this question is exactly the same as mine, but the question got closed.

I am not sure if you need more information here. Let me know if you do.

There is an overleaf user who writes here

Note that if your document contains other LaTeX errors, the tables
will not be processed correctly. Therefore if you're getting an error
message about Missing number and \statsocwidth@b etc, comment out your
tables and correct other errors in your code first. Then uncomment
your table code, and hit "recompile from scratch" at the bottom of the
error/warning message window.

So apparently, every time there is an error, the tables go haywire, and you need to get rid of the aux file.

Best Answer

It's a very obscure bug, due to a spurious space that gets inserted somewhere I couldn't find out. Due to this spurious space, every time the document is compiled, the width assigned to the caption increases by 3.65pt, which is exactly the width of a normal interword space in the selected font.

Here's a workaround.

\documentclass{statsoc}

%%%% Packages to be used
\usepackage[a4paper]{geometry}
\usepackage{amssymb, amsmath}
\usepackage{etoolbox}

\makeatletter
\patchcmd{\@makecaption}
  {\parbox}
  {\advance\@tempdima-\fontdimen2\font\parbox} % decrease the width!
  {}{}
\makeatother  

\usepackage{graphicx}
\usepackage[caption = false]{subfig} 
\usepackage{enumerate}
\usepackage{comment}
\usepackage{natbib, float}

\title{``Table keeps moving every time I compile''}
\author{Don't move!}
\begin{document}

\maketitle

\begin{table}
\caption{Table moves a little everytime I compile. 
  I think this is because I have a long caption, 
  but I cannot figure out how to resolve this problem.
  As you can see I am using statsoc.cls. I can reset
  the table if I delete the .aux file and recompile}
  \fbox{%
    \begin{tabular}{c c c c c c c c c c c c c c c c c c}
    \hline
    1 & 2 & 3 & 4 & 5 & 2 & 3 & 4 & 5 & 1 & 2 & 3 & 4 & 5 & 2 & 3 & 4 & 5\\
    1 & 2 & 3 & 4 & 5 & 2 & 3 & 4 & 5 & 1 & 2 & 3 & 4 & 5 & 2 & 3 & 4 & 5\\
    1 & 2 & 3 & 4 & 5 & 2 & 3 & 4 & 5& 1 & 2 & 3 & 4 & 5 & 2 & 3 & 4 & 5\\
    \hline
    \end{tabular}}
\end{table}
\end{document}

enter image description here

Related Question