7

Is there any way to combine the packages endfloat and chngcntr?

I would like to have all tables at the end of the document (with endfloat) and a numbering that includes the section number (e.g., 1.1.).

The example below produces "3.1" (even though it should be "1.1"), since I guess endfloat builds a new section at the end of the document?

Thanks so much for any ideas!

MWE:

\documentclass[11pt, a4paper]{article}
\usepackage[nolists, fighead, tabhead]{endfloat}

\usepackage{chngcntr}               
\counterwithin{table}{section}      


\begin{document}

\section{test1}
\begin{table}[!htp]
\caption{This should be table 1.1}
\end{table}

xx

\section{test2}
\section{test3}
\end{document}
  • 2
    This question is about chapter numbers, but the idea is the same: tex.stackexchange.com/questions/158682/… – egreg Feb 22 '14 at 22:48
  • Thanks for the quick reply. The idea in the articel (use \processdelayedfloats) helps indeed to get the numbering right in case I place all tables at the end of a chapter. Still, it does not help to get the numbers correct if I have all tables after several sections at the end of the document (via endfloat)? – mathyou Feb 22 '14 at 23:32
2

This is a tricky problem, as the float environments are saved and literally transplanted from one section of your code to another. So when you finally have the table appear, if you have inserted another section, it will be as if the table only ever appeared in that last section.

To fix this issue, we can utilize a new counter that increments alongside the endfloat counter for a particular float type (in this case, I have done this with table, but you should be able to find/replace all table with figure to enable the same behavior for figure floats as well).

\newcounter{myposttable}

Then, etoolbox is used to run code every time a new table environment is started. Note that this occurs both when the table is initially encountered and when the table is finally printed at the end of the section/document.

\AtBeginEnvironment{table}{...

The first time through, we check that the custom control sequence name is not created yet, and then define a macro that prints the float number as we want it to appear.1

\ifcsdef{eftable\themyposttable}{}{ % Only runs first time through, not second time
   \stepcounter{table} % Get actual next table number
   \csxdef{eftable\themyposttable}{\thetable} % Create global variable \eftable<#>
   \addtocounter{table}{-1}% % reset table number so as not to disrupt operation
}

Then \thetable is updated, which controls how the table is actually labeled. This is run both times, but is only used the second time through when the table is actually output to the page.

\renewcommand{\thetable}{\csuse{eftable\themyposttable}}

Lastly, we must reset the myposttable counter to 0 when we go through the second time, as we need to count through the custom macros again.

\AtBeginTables{\setcounter{myposttable}{0}}

A few other items take care of completing the solution, including updating \tableplace which controls the text placed at the point of where the table would have appeared.

\renewcommand\tableplace{\begin{center}[\tablename~\csuse{eftable\themyposttable}\ referenced here.]\end{center}}

I also allowed multiple floats on one page to ease presentation of the result.

\renewcommand{\efloatseparator}{\mbox{}} 

Result

The sections with the original location of the tables appear as:

sections

The tables themselves move to the end of the document:

tables

Final MWE

\documentclass[11pt, a4paper]{article}

\usepackage{chngcntr}               
\counterwithin{table}{section}      
\usepackage[nolists, fighead, tabhead]{endfloat}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{etoolbox}

% For Tables:

% Make a new counter to step through the number of tables we have
% We use this to keep track of the "real" labels of the floats
\newcounter{myposttable}

% The following code will be run both when the table is initially encountered
% AND when the table is finally printed at the end of the section/document
\AtBeginEnvironment{table}{
  \stepcounter{myposttable} % Work with incremented myposttable count
  % If not defined, define a label for the table to be stored and displayed later.
  \ifcsdef{eftable\themyposttable}{}{\stepcounter{table}\csxdef{eftable\themyposttable}{\thetable}\addtocounter{table}{-1}}
  % Renew the definition of \thetable to use the updated number
  \renewcommand{\thetable}{\csuse{eftable\themyposttable}}
}

% Reset the myposttable counter when we start displaying the tables
% to cycle through the actual labels we want while we print them
\AtBeginTables{\setcounter{myposttable}{0}}

% Change definition of \tableplace to reflect actual table numbers with section
\renewcommand\tableplace{\begin{center}[\tablename~\csuse{eftable\themyposttable}\ referenced here.]\end{center}}

% --- End For Tables

% Allow multiple floats on one page
\renewcommand{\efloatseparator}{\mbox{}} 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\section{test1}
\begin{table}[!htp]
\caption{This should be table 1.1}
\end{table}

\section{test2}
\begin{table}[!htp]
\caption{This should be table 2.1}
\end{table}

\section{test3}
\begin{table}[!htp]
\caption{This should be table 3.1}
\end{table}
\end{document}

1 This means that the code would not work as written with endfloats at the end of each chapter with per-section float numbers. This could be overcome by either figuring out how to clear the relevant csname variable or (more simply) including the current chapter in the csname with something along the lines of \csxdef{eftable\thechapter chap\themyposttable}{\thetable}. The myposttable counter should also be reset between chapters in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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