我想在布局中打印图像,
我当前的代码:
\documentclass{article}
\usepackage{graphicx}
\usepackage{blindtext}
\usepackage{showframe}
\begin{document}
\begin{center}
\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a}\quad
\\[\baselineskip]% adds vertical line spacing
\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a} \quad
\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a} \quad
\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a} \quad
\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a} \quad
\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a} \quad
\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-a}
\end{center}
\end{document}
我的输出:
如何简化我的代码,这样我就不需要\includegraphics
多次复制和粘贴。一个例子是循环或 for-each 命令。
答案1
这是一个相当通用的\repeatthings
命令。它有五个参数:
- 重复次数
- (可选)模数
- 需要重复的事情
- 物品之间放什么
- (可选)模数中要放置什么
最后没有添加任何内容。
\documentclass{article}
\usepackage{graphicx}
\ExplSyntaxOn
\NewDocumentCommand{\repeatthings}{m o m +m +O{}}
{% #1 = number of repeats
% #2 = optional modulo
% #3 = thing to repeat
% #4 = what to put in between
% #5 = what to put at chunks specified by #2
\IfNoValueTF { #2 }
{
\latexforti_repeat:nnn { #1 } { #3 } { #4 }
}
{
\latexforti_repeat:nnnnn { #1 } { #2 } { #3 } { #4 } { #5 }
}
}
\cs_new_protected:Nn \latexforti_repeat:nnn
{% no modulo
\int_step_inline:nn { #1 - 1 }
{
#2 #3
}
#2
}
\cs_new_protected:Nn \latexforti_repeat:nnnnn
{
\int_step_inline:nn { #1 - 1 }
{
#3
\int_compare:nTF { \int_mod:nn { ##1 } { #2 } = 0 } { #5 } { #4 }
}
#3
}
\ExplSyntaxOff
\begin{document}
\begin{center}
\repeatthings{18}[3]{\includegraphics[width=.3\linewidth]{example-image-a}}{\quad}[\\[1ex]]
\end{center}
\clearpage
\repeatthings{20}{I must not drive the principal's car}{\par}
\bigskip
\repeatthings{20}[5]{I must not crash the principal's car}{\par}[\par\medskip]
\end{document}
答案2
\newcommand
如果所有图形都与代码中显示的相同,则创建一个。我创建了\addfig
没有参数的命令。
新MWE
\documentclass{article}
\usepackage{graphicx}
\usepackage{blindtext}
\newcommand{\addfig}{\includegraphics[width=.3\linewidth]{example-image-a}}
\usepackage{showframe}
\begin{document}
\begin{center}
\addfig\quad\addfig\quad\addfig\quad
\\[\baselineskip]% adds vertical line spacing
\addfig\quad\addfig\quad\addfig \quad
\addfig\quad\addfig\quad\addfig \quad
\addfig\quad\addfig\quad\addfig \quad
\addfig\quad\addfig\quad\addfig \quad
\addfig\quad\addfig\quad\addfig \quad
\addfig\quad\addfig\quad\addfig
\end{center}
\end{document}
添加
如果您的图形不同,则向\addfig
命令添加一个参数。例如\addfig{example-image-a}
。在这里,您可以根据需要更改图形名称(example-image-a)。
\documentclass{article}
\usepackage{graphicx}
\usepackage{blindtext}
\usepackage{showframe}
\newcommand{\addfig}[1]{\includegraphics[width=.3\linewidth]{#1}}
\begin{document}
\begin{center}
\addfig{example-image-a}\quad\addfig{example-image-b}\quad\addfig{example-image-c}\quad
\\[\baselineskip]% adds vertical line spacing
\addfig{example-image-a}\quad\addfig{example-image-c}\quad\addfig{example-image-a} \quad
\addfig{example-image-c}\quad\addfig{example-image-a}\quad\addfig{example-image-a} \quad
\addfig{example-image-b}\quad\addfig{example-image-a}\quad\addfig{example-image-a} \quad
\addfig{example-image-a}\quad\addfig{example-image-a}\quad\addfig{example-image-a} \quad
\addfig{example-image-a}\quad\addfig{example-image-a}\quad\addfig{example-image-a} \quad
\addfig{example-image-a}\quad\addfig{example-image-a}\quad\addfig{example-image-a}
\end{center}
\end{document}
答案3
您可以使用非常基本的\loop
......\if
构造\repeat
并定义您自己的重复宏:
\documentclass{article}
\usepackage{graphicx}
\usepackage{showframe}
\newcounter{repeatcount}
\newcommand{\repeatthis}[2]{%
\setcounter{repeatcount}{0}%
\loop%
{#2}%
\stepcounter{repeatcount}%
\ifnum\value{repeatcount}<#1%
\repeat%
}
\begin{document}
\begin{center}
\repeatthis{3}{\includegraphics[width=.3\linewidth]{example-image-a}\quad}
\\[\baselineskip] % vertical line spacing
\repeatthis{14}{\includegraphics[width=.3\linewidth]{example-image-a}\quad}%
\includegraphics[width=.3\linewidth]{example-image-a} % last item without `\quad`
\end{center}
\end{document}
然后,此宏将基本上重复其第二个参数中的所有内容,次数与第一个参数中所述次数相同。例如,\repeatthis{10}{Hello }
将输出以下内容:
更新
egreg 的好主意是用一个(可选)参数来定义应该放在项目之间的东西,这里也可以实现:
\documentclass{article}
\usepackage{graphicx}
\usepackage{showframe}
\newcounter{repeatcount}
\newcommand{\repeatthis}[3][]{%
\setcounter{repeatcount}{0}%
\loop%
{#3}%
\stepcounter{repeatcount}%
\ifnum\value{repeatcount}<#2%
{#1}%
\repeat%
}
\begin{document}
\begin{center}
\repeatthis[\quad]{3}{\includegraphics[width=.3\linewidth]{example-image-a}}
\\[\baselineskip] % vertical line spacing
\repeatthis[\quad]{15}{\includegraphics[width=.3\linewidth]{example-image-a}}%
\end{center}
\repeatthis[, ]{10}{Hello} % the "stuff between" is not placed after the last item
\end{document}
输出结果如下: