我用来\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}
但这个解决方案有两个注意事项:
使用该命令后,在同一行中写入的所有内容也将写入包含浮动环境的文件中。因此,不可能
\addfig{example-image} Some text...
在一行中写入,否则“某些文本...”也会被延迟。命令及其参数必须写在同一行。因此,无法将其用法拆分为多行,例如
\addfig{%
第一行和example-image}
第二行。
总的来说,我们在这里使用了三个内部命令\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}