# Force all floats to the end of the document without using endfloat, compatible with htlatex

What is the easiest way to have all floats forcibly appear at the end of the document? The solution should work with htlatex, the floats should appear at the end of the HTML document in this case. (The original question, Force all floats to the end of the document without using endfloat, didn't have the htlatex requirement, and the solution doesn't work for htlatex. Other than that it's a copy, sorry for that. I didn't want to edit the original question, but I could.)

The endfloat package is not an option, see below. The following questions are related but not helpful:

How to place all floats (tables in particular) at a certain point in the document

Place all floats last without changing numbering

## Details

In our environment we have wrappers for creating figures, tables etc.. The wrappers are implemented as environments to allow "everything" inside and furthermore accept, among others, the long and short caption name, the label and the contens of the figure. The implementation of the wrappers can be altered for different document layouts (caption above/below, figure separated by rule yes/no, ...).

One particular layout requires each figure to be placed after the main document.

## Own attempts

The endfloat package comes to mind. However, I was unable to get it to work, as the figures are created by a wrapper.

I have tried implementing this using \gappto: The figure-creating command would be just appended to a global hook which is executed just before \end{document}. This worked when the figure-creating wrapper was just a command, but now it's an environment to allow "everything" inside. I have added my attempt to the MnWE below, but this results in the following error message:

! Extra }, or forgotten \endgroup.
\environment_richfigure_end_aux:w ...gure}\egroup

l.39       \end{richfigure}


How would you implement this?

## MnWE

\documentclass{scrartcl}
\pagestyle{empty}
\usepackage{caption}
\usepackage{xparse}
\usepackage{etoolbox}

\newcommand{\delayedfigures}{}

% Comment the following line to get working code
\newcommand{\dofigure}[1]{\gappto{\delayedfigures}{\clearpage#1}}

% The example works with the default implementation of \dofigure
\providecommand{\dofigure}[1]{#1}

%%   \begin{richfigure}
%%     [<placement>, e.g. htp (h=here, t=top, p=page)]
%%     {<short caption>}
%%     {<long caption>}
%%     {<\label{label}>}
%%       <\includegraphics[...]{figure}>
%%   \end{richfigure}
\NewDocumentEnvironment{richfigure}{O{tbp} m m m}{%
\dofigure\bgroup%{%
\begin{figure}[#1]%
\caption[#2]{#3}#4%
}{% Here, the contents of the environment are placed
\end{figure}%
\egroup%{
}

\gpreto{\enddocument}{\delayedfigures}

% Usage example
\begin{document}
Main document contents.

\begin{richfigure}{Short caption}{Long caption}{\label{fig:1}}
Figure contents.
\end{richfigure}

All figures are to appear on separate pages, one per page.
\end{document}

-
What does "working with htlatex" mean? You want them by the end in the HTML document, or they can appear at their place in the HTML document, just in must be compilable? –  tohecz Aug 30 '12 at 12:59
The former: All figures should appear at the end of the HTML document. –  krlmlr Aug 30 '12 at 13:11
It looks to me as if you are thinking \bgroup will work as a substitute for { in the beginning of the environment (and \egroup in the end): that will not work and you will need to use a body-collection approach. Could you give an example showing why endfloat is not an option? –  Joseph Wright Aug 30 '12 at 13:23
Section 10.3 of texdoc endfloat explains this: The package explicitly looks for the \end{float} string, and this is embedded in the wrapper code. Also, I'd like to keep the environment-type interface -- my approach works with a macro-type interface, i.e. if the figure contents is just another argument to a macro. (The contents of the richfigure environment might be autogenerated and contain verbatim code.) –  krlmlr Aug 30 '12 at 13:43
Could you + use the default implementation of \dofigure? + place a \AtEndDocument{\clearpage} after \begin{document}? + wrap each \begin{richfigure}...\end{richfigure} with a \AtEndDocument{ ... }? If no: sorry, if yes: does that solve your problem? –  Stephen Aug 30 '12 at 15:10
show 3 more comments

You can use a combination of rigfigure and \AtEndDocument:
1. Place \AtEndDocument{\clearpage} just after \begin{document}
2. Wrap each richfigure into \AtEndDocument{...}