# How can I combine endfloat and chngcntr?

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{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}

• 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

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: The tables themselves move to the end of the document: ## Final MWE

\documentclass[11pt, a4paper]{article}

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\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.
% 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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8066903,"math_prob":0.82287204,"size":4445,"snap":"2019-26-2019-30","text_gpt3_token_len":1133,"char_repetition_ratio":0.16865571,"word_repetition_ratio":0.042087544,"special_character_ratio":0.22497188,"punctuation_ratio":0.066759385,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97088665,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-07-23T07:41:36Z\",\"WARC-Record-ID\":\"<urn:uuid:9cd3600f-4c57-4cb7-aadc-e7786e6d626b>\",\"Content-Length\":\"144571\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f6305db7-592c-451a-9783-88e1bfcba906>\",\"WARC-Concurrent-To\":\"<urn:uuid:68e95a9a-cf90-4ca6-ac9b-d72107265923>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://tex.stackexchange.com/questions/161857/how-can-i-combine-endfloat-and-chngcntr\",\"WARC-Payload-Digest\":\"sha1:NPTX7MIQV5PGBEN2C2KY4TQDVNHNYVZI\",\"WARC-Block-Digest\":\"sha1:5SR44PUB2VZ26RH3S36QJZZBQDVCFOE5\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-30/CC-MAIN-2019-30_segments_1563195529007.88_warc_CC-MAIN-20190723064353-20190723090353-00022.warc.gz\"}"}