Conflito entre endfloat e figure floats incluídos via macro

Conflito entre endfloat e figure floats incluídos via macro

Eu uso \newcommandpara incluir números rapidamente. Mas combinado com endfloat, gera um erro. Alguma idéia de como enviar carros alegóricos para terminar quando incluídos desta forma?

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

Responder1

Embora você não possa, até onde eu sei, configurar endfloatpara reconhecer o comando\addfig adequadamente, você pode configurá-lo para reconhecer ambientes flutuantes adicionais, como addfig.

Por exemplo:

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

tipo flutuante personalizado atrasado

Responder2

Aqui está uma abordagem que grava cada figura usada no formulário de comando \addfig[<opts>]{<image>}em um arquivo tmpfig.texe depois lê imediatamente esse arquivo:

insira a descrição da imagem aqui

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

Responder3

Por último, mas não menos importante, uma solução que tenta corrigir o endfloatpacote o menos possível:

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

Mas esta solução tem duas ressalvas:

  1. Tudo escrito na mesma linha após o uso do comando será gravado também no arquivo que contém os ambientes flutuantes. Portanto não é possível escrever \addfig{example-image} Some text...em uma linha, caso contrário "Algum texto..." também seria atrasado.

  2. O comando e seus argumentos devem ser escritos na mesma linha. Portanto não é possível dividir seu uso em mais linhas, por exemplo, \addfig{%como 1ª e example-image}2ª linha.

No total, estamos usando os três comandos internos \efloat@xfloat, \efloat@iwritee \efloat@float@endaqui.

Adendo 25/03/2018

Desde o endfloat v2.6 as coisas ficam um pouco mais fáceis já que o teste "esta é a última linha do ambiente" agora está disponível como \efloat@if@end, então este é o único comando interno que precisamos corrigir:

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

Responder4

Aqui está uma solução baseada emA resposta de Wernermas que elimina endfloatcompletamente o uso de. De qualquer forma, uma vez que estamos gravando as figuras em um arquivo externo, não parece haver muita necessidade, endfloatpois podemos apenas ler as figuras do arquivo no final do documento.

Acontece que usa a expl3sintaxe, mas isso é realmente um detalhe. expl3não compra nada de especial aqui - é apenas uma sintaxe alternativa.

Existe uma macro:

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

Obviamente, o argumento opcional é para as opções serem passadas \includegraphics, o segundo argumento é para o nome do arquivo de imagem e o terceiro é para a legenda.

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

flutua para terminar sem <code>endfloat</code>

informação relacionada