我有这个命令
\newcommand{\pdfappendix}[1]{
\includegraphics[scale=0.6]{pdf/#1.pdf}
}
当我有 1 页 pdf 但不包括其他页面时,它工作得很好。现在我决定更改命令以接受第二个参数,即页数,并将重复\includegraphics
该次数。
我想要类似的东西(以下不是乳胶代码):
\newcommand{\pdfappendix}[2]{
for index=1 to #2
{
\includegraphics[scale=0.6]{pdf/#1index.pdf}
}
}
示例:MyPDF.pdf 有 4 页。我将把它拆分为 4 个 pdf,每个 1 页:MyPDF1.pdf、MyPDF2.pdf、MyPDF3.pdf、MyPDF4.pdf。该命令的输出将是:
然后我将写入命令:\pdfappendix{MyPDF}{4}
它将输出:
\includegraphics[scale=0.6]{pdf/MyPDF1.pdf}
\includegraphics[scale=0.6]{pdf/MyPDF2.pdf}
\includegraphics[scale=0.6]{pdf/MyPDF3.pdf}
\includegraphics[scale=0.6]{pdf/MyPDF4.pdf}
可以这样做吗?
附言:我不想使用,\includepdf
因为我对节标题有一些问题。
答案1
\documentclass{article}
\usepackage{graphicx}
\usepackage{pgffor}
\def\pdfappendix#1#2{%
\foreach \index in {1, ..., #2} {%
\includegraphics[scale=0.6]{#1\index.pdf}\par%
}}
\begin{document}
\pdfappendix{pdf/test}{4}
\end{document}
上面的代码就是你想要的。不过我还没有进行过广泛的测试。我只是测试了这个最小示例,它有效。
答案2
为了完整起见,在 Luatex 中:
\newcommand{\pdfappendix}[2]{
\directlua{
for index=1,#2 do
tex.print("\includegraphics[scale=0.6]{pdf/#1"..index..".pdf}")
end}
答案3
无需任何额外软件包的解决方案
\documentclass{article}
\usepackage{graphicx}
% introduce a dummy counter, initially 0
\newcount\tmp
\newcommand{\pdfappendix}[2]{% need this to prevent extra vertical space
% #1: image path and core part of name
% #2: maximum number
\tmp=0
\loop
% increment dummy counter
\advance\tmp by 1
% include the image
\includegraphics[scale=0.6]{#1\number\tmp.pdf}
% repeat the loop provided the counter is within specified bound
\ifnum\tmp<#2
\repeat
}
\begin{document}
\pdfappendix{pdf/test}{5}
\newpage
\pdfappendix{pdf/test}{3}
\newpage
\pdfappendix{pdf/test}{2}
\newpage
\pdfappendix{pdf/test}{1}
\end{document}
答案4
只是用不同的方式,通过递归:
\newcommand\pdfappendix[2]{%
%% initialize the "container" macro
\def\pdfappendixcommand{}
%% store the common prefix
\def\pdfappendixcommon{#1}%
%% start the recursion
\dopdfappendix{1}{#2}}
\newcommand\dopdfappendix[2]{%
\ifnum#1>#2
%% terminate the recursion
\expandafter\pdfappendixcommand
\else
%% append to \pdfappendixcommand
\edef\pdfappendixcommand{%
\unexpanded\expandafter{\pdfappendixcommand}\space
\noexpand\includegraphics[scale=0.6]{\pdfappendixcommon#1.pdf}}%
%% call \dopdfappendix{#1+1}{#2} (but computing "#1+1" and expanding \fi)
\expandafter\dopdfappendix\expandafter
{\number\numexpr#1+1\expandafter}% First argument to \dopdfappendix
\expandafter{\number#2\expandafter}% Second argument to \dopdfappendix
\fi}
然后
\pdfappendix{pdf/MyPDF}{5}
将执行所需的操作。诀窍是递归构建一个包含所有指令的宏。例如,\pdfappendix{pdf/MyPDF}{2}
将构建\pdfappendixcommand
扩展为
\includegraphics[scale=0.6]{pdf/MyPDF1.pdf} \includegraphics[scale=0.6]{pdf/MyPDF2.pdf}
所有的都\ifnum
在嵌套级别 1 中进行评估,因为对的调用\dopdfappendix
是在\fi
扩展之后进行的。
所发生的情况是依次调用以下命令(第二个参数 5):
\dopdfappendix{1}{5}
\dopdfappendix{2}{5}
\dopdfappendix{3}{5}
\dopdfappendix{4}{5}
\dopdfappendix{5}{5}
\dopdfappendix{6}{5}
最后一个将执行
\pdfappendixcommand
而不是继续递归。