![更新](https://linux22.com/image/426391/%E6%9B%B4%E6%96%B0.png)
我想准备一些卡片。例如:
\documentclass[12pt]{article}
\usepackage[paperwidth=32pc, paperheight=18pc, margin=5pc]{geometry}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}
\usepackage{fancyhdr}
\pagestyle{empty}
\newenvironment{vertcent}
{\newpage\topskip0pt\vspace*{\fill}}
{\vspace*{\fill}\newpage}
\begin{document}
\begin{vertcent}
\begin{center}
\Huge
a book
\end{center}
\end{vertcent}
\newpage
\begin{vertcent}
\begin{center}
\Huge
a chair
\end{center}
\end{vertcent}
\end{document}
是否可以自动化此过程?假设我有一个单词列表(以逗号分隔,或每行一个新条目等)并且每个条目都经过处理?
答案1
像这样?用法,以逗号分隔的列表:\makecards{card 1, card 2}:
\documentclass[12pt]{article}
\usepackage[paperwidth=32pc, paperheight=18pc, margin=5pc]{geometry}
\usepackage{fontspec,pgffor}
%\setmainfont{Linux Libertine O}
\usepackage{fancyhdr}
\pagestyle{empty}
\newenvironment{vertcent}
{\newpage\topskip0pt\vspace*{\fill}}
{\vspace*{\fill}\newpage}
\newcommand{\makecards}[1]{%
\foreach \c in {#1}{%
\begin{vertcent}
\begin{center}
\Huge
\c
\end{center}
\end{vertcent}%
}%
}
\begin{document}
\makecards{a book,a chair}
\end{document}
更新
如果所需的逗号分隔列表位于外部文件中(在本例中为 listtext.txt,其中包含逗号分隔列表),则此方法有效(感谢 RmanoTikz foreach 不适用于 \input 或 \directlua):
\documentclass[12pt]{article}
\usepackage[paperwidth=32pc, paperheight=18pc, margin=5pc]{geometry}
\usepackage{fontspec,pgffor,catchfile}
%\setmainfont{Linux Libertine O}
\usepackage{fancyhdr}
\pagestyle{empty}
\newenvironment{vertcent}
{\newpage\topskip0pt\vspace*{\fill}}
{\vspace*{\fill}\newpage}
\newcommand\loaddata[1]{\CatchFileDef\loadeddata{#1}{\endlinechar=-1}}
\newcommand{\makecards}[1]{%
\loaddata{#1}
\foreach \c in \loadeddata{%
\begin{vertcent}
\begin{center}
\Huge
\c
\end{center}
\end{vertcent}%
}%
}
\begin{document}
\makecards{listtext.txt}
\end{document}
更新 2
\foreach
使用正斜杠分隔元素可以很容易地获得更复杂的语句。该文件listtext2.txt
包含以下内容:
a chair/living room,
a table/kitchen,
a lamp/bedroom,
something else/elsewhere,
and some more/some unknown place,
a horse/on a farm
排版代码如下:
\documentclass[12pt]{article}
\usepackage[paperwidth=32pc, paperheight=18pc, margin=5pc]{geometry}
\usepackage{fontspec,pgffor,catchfile}
%\setmainfont{Linux Libertine O}
\usepackage{fancyhdr}
\pagestyle{empty}
\newenvironment{vertcent}
{\newpage\topskip0pt\vspace*{\fill}}
{\vspace*{\fill}\newpage}
\newcommand\loaddata[1]{\CatchFileDef\loadeddata{#1}{\endlinechar=-1}}
\newcommand{\makecards}[1]{%
\loaddata{#1}%
\foreach \c/\d in \loadeddata{%
\begin{vertcent}
\begin{center}
\Huge
\c\par
\vspace{.2in}
\d
\end{center}
\end{vertcent}%
}%
}
\begin{document}
\makecards{listtext2.txt}
\end{document}
...并产生以下内容:
我一直想知道如何做到这一点——现在我知道了!谢谢。
答案2
csvsimple
允许您将包含条目的文件处理为 CSV:
\begin{filecontents*}{\jobname.csv}
entry ,
a book ,
a chair ,
a very long term ,
\end{filecontents*}
% This is a CSV file with only one column: each entry must be on it's own line **and** be followed by a comma
% The first line is the header: the name of the column ("entry") will be assigned to command `\entry` below
\documentclass[12pt]{article}
\usepackage[paperwidth=32pc, paperheight=18pc, margin=5pc]{geometry}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}
\usepackage{fancyhdr}
\pagestyle{empty}
\usepackage{csvsimple}
\newenvironment{vertcent}
{\newpage\topskip0pt\vspace*{\fill}}
{\vspace*{\fill}\newpage}
\begin{document}
\csvreader{\jobname.csv}{entry=\entry}{%
\begin{vertcent}
\begin{center}
\Huge\entry
\end{center}
\end{vertcent}}
\end{document}
如果您的条目中包含乳胶代码,请将其括在括号中{ \somecommand{text} } ,
:
\begin{filecontents*}{\jobname.csv}
entry ,
{ \arb{ muslimuN} } ,
{ \arb{muslimAni} } ,
{ \arb{muslimUna} } ,
\end{filecontents*}
% This is a CSV file with only one column: each entry must be on it's own line **and** be followed by a comma
% The first line is the header: the name of the column ("entry") will be assigned to command `\entry` below
\documentclass[12pt]{article}
\usepackage[paperwidth=32pc, paperheight=18pc, margin=5pc]{geometry}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}
\usepackage[fullvoc]{arabluatex}
\usepackage{fancyhdr}
\pagestyle{empty}
\usepackage{csvsimple}
\newenvironment{vertcent}
{\newpage\topskip0pt\vspace*{\fill}}
{\vspace*{\fill}\newpage}
\begin{document}
\csvreader{\jobname.csv}{entry=\entry}{%
\begin{vertcent}
\begin{center}
\Huge\entry
\end{center}
\end{vertcent}}
\end{document}