![帮助:寻找通用(且优雅)数组(变量)映射语法来保存内容](https://linux22.com/image/391561/%E5%B8%AE%E5%8A%A9%EF%BC%9A%E5%AF%BB%E6%89%BE%E9%80%9A%E7%94%A8%EF%BC%88%E4%B8%94%E4%BC%98%E9%9B%85%EF%BC%89%E6%95%B0%E7%BB%84%EF%BC%88%E5%8F%98%E9%87%8F%EF%BC%89%E6%98%A0%E5%B0%84%E8%AF%AD%E6%B3%95%E6%9D%A5%E4%BF%9D%E5%AD%98%E5%86%85%E5%AE%B9.png)
如果这个问题已经在其他地方讨论过了,我深感抱歉,但如果有的话,我找不到它。
我是我所在大学学术论文模板的作者,但我绝对不是 TeX 专家。在这个模板中,我大量使用了使用临时(自)定义数组映射的配置文件。例如:
\arraymay{cover}
\cover{phd}={phd_cover_file.pdf}
\cover{msg}={msc_cover_file.pdf}
\arraymap{margin}
\margin{cover,left}={5cm}
\margin{cover,right}={5cm}
\margin{cover,top}={4cm}
\margin{cover,bottom}={4cm}
\margin{main,left}={3cm}
\margin{main,right}={3cm}
\margin{main,top}={2cm}
\margin{main,bottom}={2cm}
键中的逗号用于视觉效果,并被视为普通字符,即在“ \margin{cover,bottom}={2cm}
”中,键为“ cover,bottom
”。
稍后我可以按如下方式访问这些值:
\def\manuscripttype{phd}
The cover file is ``\thecover[\manuscripttype]'' and the margins for the cover are ``\themargin[cover,left], \themargin[cover,right], \themargin[cover,top], and \themargin[cover,bottom].''
将产生
The cover file is “phd_cover_file.pdf” and the margins for the cover are “3cm, 3cm, 2cm, and 2cm.”
有没有其他 (可靠的) 软件包可以实现此功能?如果没有,是否有可靠的方法来实现它(可能基于某个 KV 软件包)?谢谢。
答案1
你可以这样做pgfkeys
:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{pgfkeys}
\pgfkeys{
lourenco/.cd,
cover/phd/.initial={},
cover/msc/.initial={},
margin/cover/left/.initial=0pt,
margin/cover/right/.initial=0pt,
margin/cover/top/.initial=0pt,
margin/cover/bottom/.initial=0pt,
margin/main/left/.initial=0pt,
margin/main/right/.initial=0pt,
margin/main/top/.initial=0pt,
margin/main/bottom/.initial=0pt,
}
\newcommand{\setlourenco}[1]{\pgfkeys{lourenco/.cd,#1}}
\newcommand{\thecover}[1]{%
\pgfkeysvalueof{/lourenco/cover/#1}%
}
\newcommand{\themargin}[1]{%
\pgfkeysvalueof{/lourenco/margin/#1}%
}
%% settings
\setlourenco{
cover/phd=phd-cover-file.pdf,
cover/msc=msc-cover-file.pdf,
margin/cover/left=5cm,
margin/cover/right=5cm,
margin/cover/top=4cm,
margin/cover/bottom=4cm,
margin/main/left=3cm,
margin/main/right=3cm,
margin/main/top=2cm,
margin/main/bottom=2cm,
}
\def\manuscripttype{phd}
\begin{document}
The cover file is ``\thecover{\manuscripttype}'' and the margins
for the cover are ``\themargin{cover/left}, \themargin{cover/right},
\themargin{cover/top}, and \themargin{cover/bottom}.''
\end{document}