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}
\newenvironment{addfig}[1]{%
\begin{figure}
\centering
\includegraphics[width=\textwidth]{#1}
}{\end{figure}}
\usepackage{endfloat}
\DeclareDelayedFloatFlavor{addfig}{figure}
\begin{document}
\begin{addfig}{example-image-a}
\end{addfig}
\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}
\newcommand{\addfig}[2][width=\textwidth]{%
\openoutputfile{tmpfig.tex}{tmpimg}% Open tmpfig.tex for (over)writing
% Add figure environment...
\addtostream{tmpimg}{\protect\begin{figure}[htbp]}
\addtostream{tmpimg}{\protect\centering}
\addtostream{tmpimg}{\protect\includegraphics[#1]{#2}}
\addtostream{tmpimg}{\protect\end{figure}}
\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}
\addfig{example-image-b}
\addfig[width=.5\textwidth]{example-image-c}
\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}
\newcommand{\addfig}[1]{%
\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}
% Adapt \addfig to the endfloat package
% First of all we make \addfig known to the endfloat package as variant of the figure environment
\DeclareDelayedFloatFlavour{addfig}{figure}
\makeatletter
% Special variant of \efloat@xfloat for commands (instead of environments):
% This macro will be expanded with every line read until \efloat@found@end is used.
% The original version tests if #2 (the actual line read) is "\end{}",
% and uses \efloat@iwrite in case of no and \efloat@found@end in case of yes.
% Our version just writes the line (containing the command and the rest of the line)
% using \efloat@iwrite and finishes the verbatim reading using \efloat@found@end afterwards.
{\catcode`\^^M=12 \endlinechar=-1 %
\long\gdef\my@efloat@xfloat#1#2^^M{%
\efloat@iwrite{#1}{#2}% write 1st line (containing the command)...
\efloat@found@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 \efloat@xfloat to use our own variant of it for this command.
\pretocmd\addfig{%
\begingroup
\let\efloat@xfloat\my@efloat@xfloat}{}{}
\makeatother
\begin{document}
\addfig{example-image}
\end{document}
But this solution has two caveats:
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.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 andexample-image}
as 2nd line.
In total we are using the three internal commands \efloat@xfloat
, \efloat@iwrite
, and \efloat@float@end
here.
Addendum 2018-03-25
Since endfloat v2.6 things get a little easier since the test "is this the last line of the environment" is now available as \efloat@if@end
, so this is the only internal command we need to patch:
\documentclass{article}
\usepackage{graphicx}
\usepackage{mwe}
\newcommand{\addfig}[1]{%
\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}
% Adapt \addfig to the endfloat package
% First of all we make \addfig known to the endfloat package as variant of the figure environment
\DeclareDelayedFloatFlavour{addfig}{figure}
\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 \efloat@if@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\my@efloat@if@end[3]{#3#2}
\pretocmd\addfig{%
\begingroup
\let\efloat@if@end\my@efloat@if@end}{}{}
\makeatother
\begin{document}
\addfig{example-image}
\end{document}