3

I have a question regarding the topic: Place all floats last without changing numbering

How can I modify the solution so that it works for figures, inclusive a List of Figures? I have tried the package endfloat for my figures but have encountered certain issues hence why I am looking for an alternative way of putting all my figures in a separate Chapter at the end of my document but before the supplement.

Is there a possibility to include multiple figures in format of a table?

\begin{figure*}
\centering
\begin{tabular}{lc}
%Input image&
\includegraphics[width=1cm]{example-image}&
\includegraphics[width=1cm]{example-image}\\
%Aligned face&
\includegraphics[width=1cm]{example-image-a}&
\includegraphics[width=1cm]{example-image-a}\\
\end{tabular}}{Fifth image}{fig:tabfig}
\end{figure*}
2
  • For figures and tables, see tex.stackexchange.com/questions/430603/… and tex.stackexchange.com/questions/439236/… Commented Jan 8, 2022 at 22:47
  • Ad "Is there a possibility to include multiple figures in format of a table?" Seems your code-snippet creates a single figure consisting of a table (created via the tabular-environment) containing several images. Please specify precisely what you need: Do you need a sequence of floating objects? Is "floating" not needed but you just need a series of images with captions (see the “caption outside float” facility of the package "caption"), probably arranged in terms of a tablar/longtable-environment? Commented Jan 9, 2022 at 16:09

2 Answers 2

1

You can maintain a stack where tokens for creating floats are accumulated.

In the example below

  • a token-stack is created via \NewStack{⟨macro⟩}.

  • tokens are appended to a token-stack via \AppendToStack{⟨macro⟩}{⟨tokens⟩}.

  • the tokens saved in a stack are "flushed" and the stack is emptied via \FlushAndClearStack{⟨macro⟩}.

  • the command

    \StuffAndCountersToStack{⟨macro⟩}%
                          {⟨counters to step at the time of carrying out \StuffAndCountersToStack⟩}%
                          {⟨counters to reset and restore at the time of delivering the ⟨tokens⟩}%
                          {⟨tokens⟩}%

    adds to the stack ⟨macro⟩

    • directive to open a local scope
    • directives to reset ⟨counters to reset and restore at the time of delivering the ⟨tokens⟩,
    • directives to reset \the⟨counter⟩ and \theH⟨counter⟩ of ⟨counters to reset and restore at the time of delivering the ⟨tokens⟩,
    • directives to adjust \the⟨counter⟩ and \theH⟨counter⟩ of ⟨counters to step at the time of carrying out \StuffAndCountersToStack⟩
    • ⟨tokens⟩
    • directives to restore ⟨counters to reset and restore at the time of delivering the ⟨tokens⟩,
    • directive to close the local scope

    and steps ⟨counters to step at the time of carrying out \StuffAndCountersToStack⟩.

  • the command

    \VerbatimToScantokensStuffAndCountersToStack{⟨macro⟩}%
                          {⟨counters to step at the time of carrying out \StuffAndCountersToStack⟩}%
                          {⟨counters to reset and restore at the time of delivering the ⟨verbatimized material⟩ via \scantokens⟩}%
                          |⟨verbatimized material⟩|%

    does almost the same as the command \StuffAndCountersToStack. The difference is: The ⟨tokens⟩-argument is read and tokenized in verbatim-catcode-régime and therefore called ⟨verbatimized material⟩. Instead of just adding ⟨tokens⟩ to the stack ⟨macro⟩, directives are added to the stack ⟨macro⟩ to pass ⟨verbatimized material⟩ to \scantokens.

\makeatletter
% With older LaTeX-releases uncomment the following line:
%\RequirePackage{xparse}
\ExplSyntaxOn
\@ifdefinable\MapCommaListToMacro{%
  \let\MapCommaListToMacro=\clist_map_function:nN 
}%
\ExplSyntaxOff
\@ifdefinable\stopromannumeral{\chardef\stopromannumeral=`\^^00}%
\newcommand\unexpandedlocalsetcounter[1]{%
  \unexpanded{\localsetcounter{#1}}{\number\value{#1}}%
  \def\expandafter\noexpand\csname theH#1\endcsname{\csname theH#1\endcsname}%
  \def\expandafter\noexpand\csname the#1\endcsname{\csname the#1\endcsname}%
}%
\newcommand\unexpandedadjustcounter[1]{%
  \def\expandafter\noexpand\csname theH#1\endcsname{\csname theH#1\endcsname}%
  \def\expandafter\noexpand\csname the#1\endcsname{\csname the#1\endcsname}%
}%
\newcommand\unexpandedsetcounter[1]{%
  \unexpanded{\setcounter{#1}}{\number\value{#1}}%
}%
\@ifdefinable\localsetcounter{%
  \DeclareRobustCommand\localsetcounter[2]{%
    \csname c@#1\endcsname=#2\relax
  }%
}%
\@ifdefinable\localstepcounter{%
  \newcommand\localstepcounter[1]{%
    \advance\csname c@#1\endcsname by 1 %
  }%
}%
\newcommand\PassFirstToSecond[2]{#2{#1}}%
\newcommand\Exchange[2]{#2#1}%
%
% If this was for a package, checking whether stack-macro is defined
% could be implemented for the sake of error-checking.
\NewDocumentCommand\NewStack{m}{\newcommand#1{}}%
\NewDocumentCommand\FlushAndClearStack{m}{#1\global\let#1\empty}%
\NewDocumentCommand\AppendToStack{mm}{\xdef#1{\unexpanded\expandafter{#1#2}}}%
\newcommand\StuffAndCountersToStack[4]{%
  \@bsphack
  \begingroup
  \protected@edef\@tempa{\MapCommaListToMacro{#3}{\unexpandedlocalsetcounter}}%
  \MapCommaListToMacro{#2}{\localstepcounter}%
  \protected@edef\@tempb{\MapCommaListToMacro{#2}{\unexpandedadjustcounter}}%
  \expandafter\endgroup\expandafter\PassFirstToSecond\expandafter{%
    \romannumeral
    \expandafter\Exchange\expandafter{\expandafter{\expandafter\begingroup
    \romannumeral\expandafter\expandafter\expandafter\stopromannumeral\expandafter
     \@tempa\@tempb#4}}{%
       \stopromannumeral
       \begingroup
       \protected@edef\@tempa{\MapCommaListToMacro{#3}{\unexpandedsetcounter}}%
       \expandafter\endgroup\expandafter\Exchange\expandafter{\@tempa}%
    }%
    \endgroup
  }{\AppendToStack{#1}}%
  \MapCommaListToMacro{#2}{\stepcounter}%
  \@esphack
}%
%=== \VerbatimToScantokensStuffAndCountersToStack ========================
\NewDocumentCommand\VerbatimToScantokensStuffAndCountersToStack{mmm}{%
  \@bsphack
  \begingroup
  \catcode`\^^I=12\relax
  \InnerVerbatimToScantokensStuffAndCountersToStack{#1}{#2}{#3}%
}%
\begingroup
\def\InnerVerbatimToScantokensStuffAndCountersToStack#1{%
  \endgroup
  \NewDocumentCommand\InnerVerbatimToScantokensStuffAndCountersToStack{mmm+v}{%
    \endgroup
    \expandafter\@gobble\StuffAndCountersToStack{##1}{##2}{##3}{%
      \begingroup\newlinechar=\endlinechar
      \scantokens{\endgroup##4#1}%
    }%
  }%
}%
\catcode`\%=12\relax
\InnerVerbatimToScantokensStuffAndCountersToStack{%}%
%=== End of code of \VerbatimToScantokensStuffAndCountersToStack =========
\makeatother

\documentclass{article}
\usepackage{graphicx}
\usepackage{float}
% Load hyperref _after_ float !
\usepackage{hyperref}

\NewStack{\laterfloats}

\renewcommand{\thefigure}{\thesection.\arabic{figure}}
\csname @ifundefined\endcsname{theHfigure}{}{%
  \renewcommand{\theHfigure}{\theHsection.\arabic{figure}}%
}%
\csname @addtoreset\endcsname{figure}{section}

% \ref without hyperlink:
\csname @ifpackageloaded\endcsname{hyperref}{%
  \newcommand*\myref{\ref*}%
}{%
  \newcommand*\myref{\ref}%
}%

\begin{document}

\listoffigures

\section*{Introduction}
Please see figure~\ref{fig:one} and \ref{fig:two} and \ref{fig:three}
and \ref{fig:four} in Appendix~\ref{app:allimg}.

\section{First section}
Delayed figure~\ref{fig:one} and delayed figure~\ref{fig:two} are defined in this section.

\StuffAndCountersToStack{\laterfloats}{figure}{section,figure}{%
  \begin{figure}[H]
    \centering
    \includegraphics[width=50pt]{example-image-a}%
    \caption{First image}%
    \label{fig:one}%
  \end{figure}%
}%

\StuffAndCountersToStack{\laterfloats}{figure}{section,figure}{%
  \begin{figure}[H]
    \centering
    \includegraphics[scale=0.5]{example-image-b}%
    \caption{Second image}%
    \label{fig:two}%
  \end{figure}%
}%

\section{Second section}
Delayed figure~\ref{fig:three} is defined in this section.

\VerbatimToScantokensStuffAndCountersToStack{\laterfloats}%
                                            {figure}%
                                            {section,figure}%
                                            ?%<-start of verbatim-argument
  \begin{figure}[H]
    \centering
    \begin{tabular}{|c|}
    \hline\\
    \begin{minipage}{.27\textwidth}
% Indenting of source code does matter with verbatimized arguments!
\begin{verbatim*}
1 AB % & _^~# } {
2 AB % & _^~# } {
\end{verbatim*}
    \end{minipage}
    \\\\\hline
    \end{tabular}
    \caption{figure with verbatim}%
    \label{fig:three}%
  \end{figure}%
? %<-end of verbatim-argument

\section{Third section}
Delayed figure~\ref{fig:four} is defined in this section.

\StuffAndCountersToStack{\laterfloats}{figure}{section,figure}{%
  \begin{figure}[H]
    \centering
    \includegraphics[height=240pt]{example-image-c}%
    \caption{Third image}%
    \label{fig:four}%
  \end{figure}%
}%

\appendix

\section{An appendix}\label{app:a}

\begin{figure}[H]
\centering
A figure in appendix \myref{app:a}
\caption{A figure in appendix \myref{app:a}}
\end{figure}

\subsection{A subsection of appendix \myref{app:a}}\label{app:allimg}

Here come all the delayed floats.

\FlushAndClearStack{\laterfloats}

\end{document}

enter image description here

enter image description here


I suppose it would be only a few lines in expl3, but I am not yet so fluent in expl3 that I could type the appropriate expl3 code in a few minutes.

There are many expl3-luminaries here, whom I could not hold a candle and who would of the cuff do a much better job than I could do by fiddling around and reading interface3.pdf for hours.
But I "borrowed" expl3's \clist_map_function:nN for mapping elements of comma-lists as arguments to macros. ;-)
(I also have my own "old school code" for this sort of things, but pasting that here would imply another huge load of confusing code.)

1

Try this code.

UPDATE Begin numbering the figures with .1 in each section.

f

g

Move all the figures to the end using the command figfrom (one optional parameter and three mandatory: #1 (opt) figure configuration, #2 figure name, #3 caption, #4 label) within the environment fromsection{<section number of the figures>}

\documentclass[a4paper]{article}    

%%*****************  adapted from <<<<<<<<<<<<<<<<<<
%%  https://tex.stackexchange.com/questions/3491/place-all-floats-last-without-changing-numbering
\usepackage{graphicx}
\usepackage{float}

\newcounter{savesection}
\newenvironment{fromsection}[1]{% #1 section number
    \setcounter{figure}{0}%
    \setcounter{savesection}{\value{section}}%
    \setcounter{section}{#1}%
    \renewcommand{\thesection}{\arabic{section}}}
{\setcounter{section}{\value{savesection}}}

\newcommand{\figfrom}[4][]{% #1 (opt) figure specs, #2 figure name, #3 caption, #4 label
\begin{figure}[H]
    \centering
    \includegraphics[#1]{#2}
    \caption{#3}
    \label{#4}
\end{figure}
}
\renewcommand{\thefigure}{\arabic{section}.\arabic{figure}}
%% ***************************************** 

\begin{document}
    
    \listoffigures
    
    \section{Introduction}
    Please see figures~\ref{fig:one} and \ref{fig:two} in section~\ref{app:allimg}.
    
    \section{Other stuff}
    See also figure~\ref{fig:three} on page~\pageref{fig:three}.        

    \section{All figures together}\label{app:allimg}
    
    \begin{fromsection}{1}
        \figfrom[width=50pt]{example-image-a}{First image}{fig:one}
        \figfrom[scale=0.5]{example-image-b}{Second image}{fig:two}
    \end{fromsection}
    
    \begin{fromsection}{2}
        \figfrom[height=270pt]{example-image-c}{Third image}{fig:three}
    \end{fromsection}

    \appendix
    \section{Supplement}
    
\end{document}
3
  • The numbering is 1.1, 1.2, 2.3. - How do I change the code to obtain 1.1, 1.2, 2.1, so that the numbering starts with .1 for each section? Otherwise, your code works great! Thanks, Simon!
    – KMFWeb
    Commented Jan 8, 2022 at 21:11
  • 1
    @KMFWeb In the definition of the environment fromsection behind \setcounter{section}{#1}% add \setcounter{figure}{0}%. If using hyperref, which must be loaded after the package float, ensure uniqueness of destination-names by also adding \renewcommand{\theHsection}{\arabic{section}} and after \renewcommand{\thefigure}{\arabic{section}.\arabic{figure}} adding \renewcommand{\theHfigure}{\theHsection.\arabic{figure}}. With fromsection you may wish to add restoring the figure-counter at the end of the environment in the same way in which the section-counter is restored. Commented Jan 9, 2022 at 11:36
  • @KMFWeb Please see the updated code to correct the figure numbers. You can follow Ulrich Diez's very good instructions for adding hyperlinks if you want. Commented Jan 9, 2022 at 18:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .