我需要许多宏的同义词,这些宏具有关键图号引用?如何使用 \foreach 标记作为 \newcommand 名称?我希望通过生成和使用宏 \hello、\hola 和 \bonjour(所有宏的下标均为 1)以及 \world、\mundo 和 \monde(所有宏的下标均为 2)将 ACTUAL 输出替换为 MOCKUP 输出,因为这些同义词都引用相同的图号。如果有更简单的方法,请说明。通过复制和编辑将它们全部拼写出来非常繁琐。
编译:
bash$ for i in 1 2 3 4; do pdflatex MWE.tex ; done
来源:
% filename: MWE.tex
\documentclass[10pt,twoside]{book}
\usepackage{tikz}
\begin{document}
\begin{center}{\bf\LARGE MWE}\end{center}
\begin{figure}\tikz{\node at (0,0) {hello};}\caption{hello}\label{hello}\end{figure}
\begin{figure}\tikz{\node at (0,0) {world};}\caption{world}\label{world}\end{figure}
GOAL: produce synonymous variant macros referencing a key figure.\\\\
EXPECT:\\
{\bf key:} $hello_1$ {\bf variants:} $hola_1$ $bonjour_1$\\
{\bf key:} $world_2$ {\bf variants:} $mundo_2$ $monde_2$\\
MOCKUP:\\
% NOTICE: this exercises the loops needed, but how do I generate named macros?
% PROBLEM: \foreach defines \key and \variant so \newcommand can't reuse them.
% FAILURE: \expandafter\newcommand\csname\variant\endcsname{$\variant~\ref{\key}}
\foreach\key/\variants in {hello/{hola,bonjour},world/{mundo,monde}}{
{\bf key:} $\key_{\ref{\key}}$ {\bf variants:}
\foreach\variant in \variants{$\variant_{\ref{\key}}$\;} \\
}
ACTUAL:\\
TODO
% TODO uncomment these next two lines when the \newcommands work correctly.
%key: \hello variants: \hola \bonjour
%key: \world variants: \mundo \monde
\end{document}
答案1
TNX 用户 202729。Python 解决方案非常有效。
首先是IDEAS.py的源代码:
#!/usr/bin/env python3
(BS, OB, CB) = ("\\", "{", "}")
IDEAS = {
"hello": {"hola", "bonjour"},
"world": {"mundo", "monde"}
}
def newcommand(num, key, syn):
print(
BS + "newcommand" +
BS + syn +
OB + BS + "large " + syn +
BS + "raisebox{-2pt}" +
OB + BS + "footnotesize" +
BS + "ref{" + key + "}" +
CB + CB
)
def synonyms(num, key):
newcommand(num + 1, key, key)
for syn in IDEAS[key]:
newcommand(num + 1, key, syn)
for num, key in enumerate(IDEAS):
synonyms(num, key)
然后命令行:
bash$ IDEAS.py > IDEAS.tex
然后乳胶:
\input{IDEAS}
答案2
一个问题是每次\foreach
迭代都在其自己的本地范围内进行。
因此,根据执行的定义\newcommand
仅限于本地范围,该范围在迭代开始时打开 ,并在执行该定义的迭代\foreach
结束时关闭。 我建议在临时令牌寄存器中累积指令,其中分配是全局完成的,并在迭代完成时传递该寄存器的内容。您需要一些技巧才能正确扩展“变量”宏。\foreach
\newcommand
\expandafter
\foreach
以下代码可能可以实现您的期望:
% filename: MWE.tex
\documentclass[10pt,twoside]{book}
\newtoks\scratchtoks
\usepackage{tikz}
\begin{document}
\begin{center}{\bf\LARGE MWE}\end{center}
\begin{figure}\tikz{\node at (0,0) {hello};}\caption{hello}\label{hello}\end{figure}
\begin{figure}\tikz{\node at (0,0) {world};}\caption{world}\label{world}\end{figure}
GOAL: produce synonymous variant macros referencing a key figure.\\\\
EXPECT:\\
{\bf key:} $hello_1$ {\bf variants:} $hola_1$ $bonjour_1$\\
{\bf key:} $world_2$ {\bf variants:} $mundo_2$ $monde_2$\\
MOCKUP:\\
% NOTICE: this exercises the loops needed, but how do I generate named macros?
% PROBLEM: \foreach defines \key and \variant so \newcommand can't reuse them.
% FAILURE: \expandafter\newcommand\csname\variant\endcsname{$\variant~\ref{\key}}
\global\scratchtoks{\global\scratchtoks{}}%
\foreach\key/\variants in {hello/{hola,bonjour},world/{mundo,monde}}{%
%---------------------------------------------------------------------------
\global\scratchtoks\expandafter{%
\the\expandafter\scratchtoks
\expandafter\newcommand
\csname\key\expandafter\expandafter\expandafter\endcsname
\expandafter\expandafter\expandafter{%
\expandafter\expandafter\expandafter$%
\expandafter\key
\expandafter_%
\expandafter{%
\expandafter\ref
\expandafter{\key}}$}%
}%
%---------------------------------------------------------------------------
{\bf key:} $\key_{\ref{\key}}$ {\bf variants:}
\foreach\variant in \variants{%
%-------------------------------------------------------------------------
\global\scratchtoks\expandafter{%
\the\expandafter\scratchtoks
\expandafter\newcommand
\csname\variant\expandafter\expandafter\expandafter\endcsname
\expandafter\expandafter\expandafter{%
\expandafter\expandafter\expandafter$%
\expandafter\variant
\expandafter_%
\expandafter{%
\expandafter\ref
\expandafter{\key}}$}%
}%
%-------------------------------------------------------------------------
$\variant_{\ref{\key}}$\;%
}\\
}%
\the\scratchtoks
ACTUAL:\\
% TODO uncomment these next two lines when the \newcommands work correctly.
{\bf key:} \hello { \bf variants:} \hola\; \bonjour\;\\
{\bf key:} \world { \bf variants:} \mundo\; \monde\;\\
%\show\hello
%\show\hola
%\show\bonjour
%\show\world
%\show\mundo
%\show\monde
\end{document}
\cs_new:cpx
另外,你可以使用 expl3来全局定义事物,而不是在标记寄存器中累积:
% filename: MWE.tex
\documentclass[10pt,twoside]{book}
\usepackage{tikz}
\begin{document}
\begin{center}{\bf\LARGE MWE}\end{center}
\begin{figure}\tikz{\node at (0,0) {hello};}\caption{hello}\label{hello}\end{figure}
\begin{figure}\tikz{\node at (0,0) {world};}\caption{world}\label{world}\end{figure}
GOAL: produce synonymous variant macros referencing a key figure.\\\\
EXPECT:\\
{\bf key:} $hello_1$ {\bf variants:} $hola_1$ $bonjour_1$\\
{\bf key:} $world_2$ {\bf variants:} $mundo_2$ $monde_2$\\
MOCKUP:\\
% NOTICE: this exercises the loops needed, but how do I generate named macros?
% PROBLEM: \foreach defines \key and \variant so \newcommand can't reuse them.
% FAILURE: \expandafter\newcommand\csname\variant\endcsname{$\variant~\ref{\key}}
\ExplSyntaxOn
\foreach\key/\variants in {hello/{hola,bonjour},world/{mundo,monde}}{
%---------------------------------------------------------------------------
\cs_new:cpx{\key}
{\c_math_toggle_token\exp_not:o {\key}\c_math_subscript_token{\exp_not:N\ref{\exp_not:o {\key}}}\c_math_toggle_token}
%---------------------------------------------------------------------------
{\bf key:}~\c_math_toggle_token\key\c_math_subscript_token{\ref{\key}}\c_math_toggle_token~{\bf variants:}~
\foreach\variant in \variants{
%-------------------------------------------------------------------------
\cs_new:cpx{\variant}
{\c_math_toggle_token\exp_not:o {\variant}\c_math_subscript_token{\exp_not:N\ref{\exp_not:o {\key}}}\c_math_toggle_token}
%-------------------------------------------------------------------------
\c_math_toggle_token\variant\c_math_subscript_token{\ref{\key}}\c_math_toggle_token\;
}\\
}
\ExplSyntaxOff
ACTUAL:\\
% TODO uncomment these next two lines when the \newcommands work correctly.
{\bf key:} \hello { \bf variants:} \hola\; \bonjour\;\\
{\bf key:} \world { \bf variants:} \mundo\; \monde\;\\
%\show\hello
%\show\hola
%\show\bonjour
%\show\world
%\show\mundo
%\show\monde
\end{document}