Konflikt zwischen Endfloat und Figure Floats per Makro eingebunden

Konflikt zwischen Endfloat und Figure Floats per Makro eingebunden

Ich verwende \newcommandes, um schnell Zahlen einzufügen. Aber in Kombination mit endfloatwird ein Fehler ausgegeben. Irgendwelche Ideen, wie Floats ans Ende gesendet werden können, wenn sie auf diese Weise eingefügt werden?

\documentclass{article}
\usepackage{graphicx}
%\usepackage{endfloat}  % this causes an error

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

\begin{document}
\addfig{fig1}
\end{document} 

Antwort1

Obwohl Sie, soweit ich das beurteilen kann, die Konfiguration nicht so vornehmen können, endfloatdass der Befehl \addfigrichtig erkannt wird, können Sie ihn so konfigurieren, dass zusätzliche Floating-Umgebungen wie erkannt werden addfig.

Zum Beispiel:

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

verzögerter benutzerdefinierter Float-Typ

Antwort2

Hier ist ein Ansatz, der jede im Befehlsformular verwendete Zahl \addfig[<opts>]{<image>}in eine Datei schreibt tmpfig.texund diese Datei dann sofort einliest:

Bildbeschreibung hier eingeben

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

Antwort3

Zu guter Letzt noch eine Lösung, die versucht, das endfloatPaket mit so wenig Patches wie möglich zu versehen:

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

Diese Lösung weist jedoch zwei Einschränkungen auf:

  1. Alles, was nach Verwendung des Befehls in die gleiche Zeile geschrieben wird, wird auch in die Datei mit den schwebenden Umgebungen geschrieben. Es ist also nicht möglich, \addfig{example-image} Some text...in einer Zeile zu schreiben, da sonst auch „Ein Text...“ verzögert würde.

  2. Der Befehl und seine Argumente müssen in derselben Zeile stehen. Eine Aufteilung auf mehrere Zeilen, z.B. \addfig{%als 1. und example-image}2. Zeile, ist daher nicht möglich.

Insgesamt verwenden wir hier die drei internen Befehle \efloat@xfloat, \efloat@iwrite, und \efloat@float@end.

Nachtrag 25.03.2018

Seit endfloat v2.6 ist alles etwas einfacher, da der Test „Ist dies die letzte Zeile der Umgebung?“ nun als verfügbar ist \efloat@if@end. Dies ist also der einzige interne Befehl, den wir patchen müssen:

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

Antwort4

Hier ist eine Lösung basierend aufWerners Antwortaber das macht die Verwendung von endfloatvollständig überflüssig. Wenn wir die Zahlen sowieso in eine externe Datei schreiben, scheint es nicht viel Bedarf dafür zu geben, endfloatda wir die Zahlen einfach am Ende des Dokuments aus der Datei einlesen können.

Es verwendet zufällig die expl3Syntax, aber das ist wirklich nebensächlich. expl3Es bringt Ihnen hier nichts Besonderes – es ist nur eine alternative Syntax.

Es gibt ein Makro:

\addfig[<options>]{<filename>}{<caption>}

Offensichtlich ist das optionale Argument für zu übergebende Optionen \includegraphics, das zweite Argument für den Namen der Bilddatei und das dritte für die Beschriftung.

\documentclass{article}
\usepackage{graphicx,xparse,kantlipsum}
\ExplSyntaxOn
\AtBeginDocument
{
  \iow_new:N \l_strongbad_addfigs_stream
  \iow_open:Nn \l_strongbad_addfigs_stream { \jobname.addfigs }
}
\AtEndDocument
{
  \iow_close:N \l_strongbad_addfigs_stream
  \listoffigures
  \file_input:n { \jobname.addfigs }
}
\cs_new_protected:Nn \strongbad_addfig:nnn
{
  \group_begin:
  \str_set:Nn \l_tmpa_str { \setcounter{figure} }
  \str_put_right:Nx \l_tmpa_str { \c_left_brace_str }
  \str_put_right:Nx \l_tmpa_str { \thefigure }
  \str_put_right:Nx \l_tmpa_str { \c_right_brace_str }
  \iow_now:Nx \l_strongbad_addfigs_stream { \l_tmpa_str }
  \iow_now:Nn \l_strongbad_addfigs_stream
  {
    \begin{ figure }
      \centering
      \includegraphics [ #1 ] { #2 }
      \caption { #3 }
    \end{ figure }
  }
  \stepcounter{figure}
  \skip_vertical:N \bigskipamount
  \centering
  [~\figurename{}~\thefigure{}~about~here~]
  \skip_vertical:N \bigskipamount
  \group_end:
}
\NewDocumentCommand \addfig { O {} m +m }
{
  \strongbad_addfig:nnn { #1 } { #2 } { #3 }
}
\ExplSyntaxOff
\begin{document}
\kant[1]
\addfig{example-image-a}{first end figure}
\kant[2]
\addfig  {example-image-b}{second end figure}
\kant[3]
\addfig [width=.25\textwidth] {tiger} {third end figure}
\kant[4-5]
\end{document}

Floats zum Beenden ohne <code>endfloat</code>

verwandte Informationen