Wie können mit foreach synonyme Makros erstellt werden?

Wie können mit foreach synonyme Makros erstellt werden?

Ich brauche viele Synonyme für viele Makros mit wichtigen Abbildungsverweisen. Wie verwende ich das \foreach-Token als \newcommand-Namen? Ich versuche, die TATSÄCHLICHE Ausgabe durch die MOCKUP-Ausgabe zu ersetzen, indem ich die Makros \hello, \hola und \bonjour (alle mit Index 1) sowie \world, \mundo und \monde (alle mit Index 2) generiere und verwende, da sich die Synonyme alle auf dieselbe Abbildungsnummer beziehen. Wenn es einen einfacheren Weg gibt, dies zu tun, zeigen Sie ihn bitte. Sie alle durch Kopieren und Bearbeiten auszuschreiben, ist sehr mühsam.

KOMPILIEREN:

bash$ for i in 1 2 3 4; do pdflatex MWE.tex ; done

QUELLE:

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

Antwort1

TNX, Benutzer 202729. Eine Python-Lösung hat perfekt funktioniert.

Zuerst der Quellcode für 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)

Dann die Kommandozeile:

bash$ IDEAS.py > IDEAS.tex

Dann Latex:

\input{IDEAS}

Antwort2

Ein Problem ist, dass jede \foreachIteration in ihrem eigenen lokalen Bereich stattfindet.
Eine Definition, die in Bezug auf durchgeführt wird, \newcommandist also auf den lokalen Bereich beschränkt, der zu Beginn der Iteration geöffnet \foreachund am Ende der \foreachIteration geschlossen wird, während der die Definition durchgeführt wird.
Ich schlage vor, \newcommandDirektiven in einem Scratch-Token-Register zu sammeln, in dem Zuweisungen global vorgenommen werden, und den Inhalt dieses Registers nach Abschluss der Iteration zu übermitteln. Sie müssen einige \expandafterTricks anwenden, um die \foreach„Variablen“-Makros richtig zu erweitern.

Wahrscheinlich bewirkt der folgende Code, was Sie wünschen:

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

Bildbeschreibung hier eingeben


Alternativ können Sie statt der Akkumulation in einem Token-Register expl3s verwenden, \cs_new:cpxum Dinge global zu definieren:

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

Bildbeschreibung hier eingeben

verwandte Informationen