
我需要許多帶有關鍵數字參考的宏的同義詞?如何使用 \foreach 標記作為 \newcommand 名稱?我希望透過產生和使用巨集 \hello、\hola 和 \bonjour (都具有下標 1)和 \world、\mundo 和 \monde (都具有下標 2)來用 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
結束時關閉。 我建議在暫存令牌寄存器中累積指令,其中分配是全域完成的,並在迭代完成時傳遞該寄存器的內容。您需要一些- 技巧來正確擴展 -“variable”巨集。\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}