매크로를 통해 포함된 endfloat와 Figure float 간의 충돌

매크로를 통해 포함된 endfloat와 Figure float 간의 충돌

나는 \newcommand수치를 빠르게 포함하는 데 사용합니다. 하지만 와 결합하면 endfloat오류가 발생합니다. 이런 식으로 포함될 때 수레를 끝까지 보내는 방법에 대한 아이디어가 있습니까?

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

답변1

내가 아는 endfloat한 명령을 \addfig적절하게 인식하도록 구성할 수는 없지만 addfig.

예를 들어:

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

지연된 맞춤 부동 소수점 유형

답변2

다음은 명령 형식에 사용된 각 그림을 \addfig[<opts>]{<image>}파일에 쓴 tmpfig.tex다음 해당 파일에서 즉시 읽는 한 가지 접근 방식입니다.

여기에 이미지 설명을 입력하세요

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

답변3

endfloat마지막으로 패키지를 가능한 한 적게 패치하려는 솔루션은 다음과 같습니다 .

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

하지만 이 솔루션에는 두 가지 주의 사항이 있습니다.

  1. 명령을 사용한 후 같은 줄에 쓰여진 모든 내용은 부동 환경을 포함하는 파일에도 기록됩니다. 따라서 한 줄로 작성하는 것은 불가능합니다. \addfig{example-image} Some text...그렇지 않으면 "어떤 텍스트..."도 지연될 것이기 때문입니다.

  2. 명령과 해당 인수는 같은 줄에 작성되어야 합니다. 따라서 사용법을 더 많은 라인(예: \addfig{%첫 번째 라인과 두 번째 라인 )으로 분할하는 것은 불가능합니다 example-image}.

전체적으로 여기서는 , 및 3개의 내부 명령을 사용 \efloat@xfloat하고 \efloat@iwrite있습니다 \efloat@float@end.

부록 2018-03-25

endfloat v2.6에서는 "이것이 환경의 마지막 줄입니까?" 테스트가 이제 로 제공되므로 상황이 좀 더 쉬워졌습니다. \efloat@if@end따라서 패치해야 하는 유일한 내부 명령은 다음과 같습니다.

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

답변4

여기에 기반한 솔루션이 있습니다.베르너의 대답그러나 그것은 모두 사용을 금지합니다 endfloat. 어쨌든 외부 파일에 그림을 쓰고 나면 endfloat문서 끝에 있는 파일에서 그림을 읽을 수 있으므로 별로 필요하지 않은 것 같습니다.

구문을 사용하는 경우가 있지만 expl3실제로는 그렇습니다. expl3여기서는 특별한 것을 사지 않습니다. 이는 단지 대체 구문일 뿐입니다.

하나의 매크로가 있습니다:

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

분명히 선택적 인수는 에 전달될 옵션에 대한 것이고 \includegraphics, 두 번째 인수는 이미지 파일 이름에 대한 것이고, 세 번째 인수는 캡션에 대한 것입니다.

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

<code>endfloat</code> 없이 끝날 때까지 부동

관련 정보