因此,我制作了一种打印卡片的格式(如下所示)。我可以从某种列表中填充每个卡片环境,而不必为每张卡片写出 \cards{foo} 吗?我可以使用脚本来执行此操作,但我认为在 LaTeX 中尝试它会很有趣。
\documentclass{article}
\usepackage[margin=2cm]{geometry}
\newlength\cardheight
\newlength\cardwidth
\setlength\cardheight{0.4\textheight}
\setlength\cardwidth{0.49\textwidth}
\newcommand\cards[1]{%
\fbox{\begin{minipage}[t][\cardheight][t]{\dimexpr\cardwidth-2\fboxsep-2\fboxrule\relax}
\vspace{\baselineskip}
\huge ~ #1
\vfill
\normalsize ~ Cards Against Humanity %I should insert the CAH logo in my final version
\end{minipage}}
\hspace{1pt}}
\pagestyle{empty}
\begin{document}
\centering
\cards{%
Classicist undertones.
} %
\cards{%
A windmill full of corpses.
} %
\cards{%
A tiny horse.
} %
\cards{%
One more card.
} %
\end{document}
所以,我现在要做的就是将所有想要的卡片写入一个换行符分隔的文件中,然后编写一个快速程序将每一行包装在“\cards{%\n\n}%”中
有没有办法用 LaTeX 完成所有这些操作,这样我只需要一个文件,而不是外部卡文件、转换程序和 LaTeX 文件?
答案1
纯 LaTeX 解决方案(不需要额外的包):
\documentclass{article}
\usepackage[margin=2cm]{geometry}
\newlength\cardheight
\newlength\cardwidth
\setlength\cardheight{0.4\textheight}
\setlength\cardwidth{0.49\textwidth}
\newcommand\card[1]{%
\fbox{\begin{minipage}[t][\cardheight][t]{\dimexpr\cardwidth-2\fboxsep-2\fboxrule\relax}
\vspace{\baselineskip}
\huge ~ #1
\vfill
\normalsize ~ Cards Against Humanity %I should insert the CAH logo in my final version
\end{minipage}}%
\hspace{1pt}}
\makeatletter
\newcommand{\cards}[1]{
\@for\@c:=#1\do{%
\card{\@c}%
}%
}
\makeatother
\pagestyle{empty}
\begin{document}
\centering
\cards{%
Classicist undertones.,%
A windmill full of corpses.,%
A tiny horse.,%
One more card.
}%
\end{document}
此处的\cards
命令接受带有标题的逗号分隔列表,并使用 LaTeX 循环遍历列表,\@for
其中每次迭代\@c
将包含当前标题。
答案2
使用任何列表处理包。下面是一个etoolbox
版本:
\documentclass{article}
\usepackage[margin=2cm]{geometry}% http://ctan.org/pkg/geometry
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\newlength\cardheight
\newlength\cardwidth
\setlength\cardheight{0.4\textheight}
\setlength\cardwidth{0.49\textwidth}
\newcommand{\card}[1]{%
\fbox{\begin{minipage}[t][\cardheight][t]{\dimexpr\cardwidth-2\fboxsep-2\fboxrule\relax}
\vspace{\baselineskip}
\huge ~ #1
\vfill
\normalsize ~ Cards Against Humanity %I should insert the CAH logo in my final version
\end{minipage}}%
\hspace{1pt}}
\newcommand{\cards}[1]{%
\renewcommand*{\do}[1]{\card{##1}}% How to process each item
\expandafter\docsvlist\expandafter{#1}}% Process list
\pagestyle{empty}
\begin{document}
\centering
\cards{%
Classicist undertones.,%
A windmill full of corpses.,%
A tiny horse.,%
One more card.
}%
\end{document}
该列表是以逗号分隔的列表。如果某个项目包含逗号,则需要将其括在括号中{..., ..}
。
等效的 LaTeX3 实现需要以下定义\cards
:
\ExplSyntaxOn
\DeclareDocumentCommand{\cards}{ > { \SplitList { , } } m }{%
\ProcessList {#1} { \card }}% Process list
\ExplSyntaxOff