# Conflict between endfloat and figure floats included via macro

Although you cannot, as far as I can tell, configure endfloat to recognise the command \addfig appropriately, you can configure it to recognise additional floating environments such as addfig.

For example:

\documentclass{article}
\usepackage{graphicx}
\begin{figure}
\centering
\includegraphics[width=\textwidth]{#1}
}{\end{figure}}
\usepackage{endfloat}

\begin{document}
\end{document}


Here's one approach that writes each figure used in the command form \addfig[<opts>]{<image>} to a file tmpfig.tex and then immediately reads in that file:

\documentclass{article}

\usepackage{graphicx,newfile,endfloat}

\newoutputstream{tmpimg}

\openoutputfile{tmpfig.tex}{tmpimg}% Open tmpfig.tex for (over)writing
\closeoutputstream{tmpimg}% Close tmpfig.tex
\input{tmpfig}% Input tmpfig.tex
}

\begin{document}

Some text.

\begin{figure}[htbp]  % when float environment is included here
\centering
\includegraphics[width=\textwidth]{example-image-a}
\end{figure}

\end{document}


Last but not least a solution which tries to patch the endfloat package as less as possible:

\documentclass{article}
\usepackage{graphicx}
\usepackage{mwe}

\begin{figure}[htbp]  % when float environment is included here
\centering
\includegraphics[width=\textwidth]{#1}
\end{figure}
}

\usepackage{endfloat}[2011/12/25] % we need at least v2.5d
\usepackage{etoolbox}

% First of all we make \addfig known to the endfloat package as variant of the figure environment
\makeatletter
% Special variant of \[email protected] for commands (instead of environments):
% This macro will be expanded with every line read until \[email protected]@end is used.
% The original version tests if #2 (the actual line read) is "\end{}",
% and uses \[email protected] in case of no and \[email protected]@end in case of yes.
% Our version just writes the line (containing the command and the rest of the line)
% using \[email protected] and finishes the verbatim reading using \[email protected]@end afterwards.
{\catcode\^^M=12 \endlinechar=-1 %
\long\gdef\[email protected]@xfloat#1#2^^M{%
\[email protected]{#1}{#2}% write 1st line (containing the command)...
\[email protected]@end{#1}%  ...and end verbatim reading afterwards
\next}}              %  process next line
% Since \addfig is a command and not an environment we need to start a group for our own
% using \begingroup, so it behaves like we would have written \begin{addfig}.
% (Note: The endfloat package is designed to work with environments, not with commands.)
% Additionally we patch \[email protected] to use our own variant of it for this command.
\begingroup
\let\[email protected]\[email protected]@xfloat}{}{}
\makeatother

\begin{document}
\end{document}


But this solution has two caveats:

1. Everything written in the same line after usage of the command will be written to the file containing the floating environments as well. So it's not possible to write \addfig{example-image} Some text... in one line since otherwise "Some text..." would be delayed, too.

2. The command and its arguments must be written in the same line. So it's not possible to split its usage to more lines, e.g. \addfig{% as 1st and example-image} as 2nd line.

In total we are using the three internal commands \[email protected], \[email protected], and \[email protected]@end here.

Since endfloat v2.6 things get a little easier since the test "is this the last line of the environment" is now available as \[email protected]@end, so this is the only internal command we need to patch:

\documentclass{article}
\usepackage{graphicx}
\usepackage{mwe}

\begin{figure}[htbp]  % when float environment is included here
\centering
\includegraphics[width=\textwidth]{#1}
\end{figure}
}

\usepackage{endfloat}[2018/01/01] % we need at least v2.6
\usepackage{etoolbox}

% First of all we make \addfig known to the endfloat package as variant of the figure environment
\makeatletter
% Since \addfig is a command and not an environment we need to start a group for our own
% using \begingroup, so it behaves like we would have written \begin{addfig}.
% (Note: The endfloat package is designed to work with environments, not with commands.)
% Additionally we patch \[email protected]@end (usually expanding to \@firstoftwo or \@secondoftwo)
% so the very first command line will be written to the file and the verbatim reading will be
% finished afterwards.
\newcommand\[email protected]@[email protected][3]{#3#2}
\begingroup
\let\[email protected]@end\[email protected]@[email protected]}{}{}
\makeatother

\begin{document}
`