foreach를 사용하여 동의어 매크로를 만드는 방법은 무엇입니까?

foreach를 사용하여 동의어 매크로를 만드는 방법은 무엇입니까?

주요 수치 참조와 함께 많은 매크로의 동의어가 많이 필요합니까? \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끝날 때 닫히는 로컬 범위로 제한됩니다. 할당이 전역적으로 수행되는 스크래치 토큰 레지스터 내에 -지시문을 축적하고 반복이 완료되면 해당 레지스터의 내용이 전달되도록 하는 것이 좋습니다 . -"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}

여기에 이미지 설명을 입력하세요


또는 토큰 레지스터 내에서 누적하는 대신 expl3을 사용하여 \cs_new:cpx전역적으로 정의할 수 있습니다.

% 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}

여기에 이미지 설명을 입력하세요

관련 정보