Für jedes Element einer Liste einen neuen Befehl bereitstellen

Für jedes Element einer Liste einen neuen Befehl bereitstellen

Ich habe ein Makro , das mit dem Paket lVarsdefiniert ist (Teil vonforeachpgfforPGF) als:

\newcommand{\lVars}[1]{\foreach \x [count=\ii] in {#1}{
  \ifnum\ii=1{} \else {,} \fi \lVar[\x]}
}

Die beabsichtigte Verwendung erfolgt in Ausdrücken wie \exists \lVars{a, b, c, d}, was das Ergebnis erzeugt \exists \lVar[a], \lVar[b], \lVar[c], \lVar[d]. Der Befehl \lVar[a]ist ein weiterer benutzerdefinierter Befehl der Form \newcommand{\lVar}[1][x]{\textsc{#1}}, kann aber in Kürze komplexer werden. Ich verwende dann die neu quantifizierten \lVarElemente in größeren Foren.

Ich bin jedoch extrem faul. Ich hätte gern, dass dieses Makro jedes Listenelement als neuen Befehl in der Form \lA, \lBusw. anzeigt, der sich wiederum zu den eigentlichen zugrunde liegenden \lVar[A], \lVar[B]... erweitert. Diese neuen Befehle würden meine Formelnotation deutlich weniger wortreich machen. Diese neuen Befehle sollten bis zum nächsten Mal gültig sein, wenn a, b,... im Listenparameter des lVarsMakros verwendet werden. Die allgemeine Verwendung wird also Situationen wie die folgenden sein:

\begin{displaymath}
  \begin{array}{l}
    \exists \lVars{a, b, c}.~ \lA = \lB \implies \lA = \lC \\
    \lC = 0 \implies \lA = 0
    \exists \lVars{a, b}.~ \lA \neq \lB
  \end{array}
\end{displaymath}

And, as we see in the term $\lA = \lB$, the implication...

\newcommandIch habe mit verschiedenen Permutationen am und \defim Hauptteil des experimentiert foreach, ohne Erfolg. Irgendwelche Ratschläge oder Tipps?

Antwort1

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\lVars}{m}
 {
  % cycle through the argument
  \clist_map_inline:nn { #1 }
   {
    % print the variable
    \lVar[##1]
    % if the variable is a, define \lA
    \tl_to_uppercase:n
     {
      \cs_gset_protected:cpn { \c_wright_prefix_tl ##1 }
     }
     { \lVar[##1] }
   }
 }
% We need to store the `l' prefix in a control sequence to hide it from \uppercase
\tl_const:Nn \c_wright_prefix_tl { l }
\ExplSyntaxOff

\newcommand{\lVar}[1][x]{\textsc{#1}}

\begin{document}
\begin{displaymath}
  \begin{array}{l}
    \exists \lVars{a, b, c}.~ \lA = \lB \implies \lA = \lC \\
    \lC = 0 \implies \lA = 0
    \exists \lVars{a, b}.~ \lA \neq \lB
  \end{array}
\end{displaymath}

And, as we see in the term $\lA = \lB$, the implication...
\end{document}

Die Definition \lCbleibt jedoch für immer erhalten. Wenn Sie sie in verschiedenen Zellen verwenden möchten, arrayist eine globale Definition erforderlich.

Bildbeschreibung hier eingeben


Eine Version, die Variablen mit mehreren Buchstaben akzeptiert und \lFooFolgendes definiert \lVars{foo}:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\lVars}{m}
 {
  % cycle through the argument
  \clist_map_inline:nn { #1 }
   {
    % print the variable
    \lVar[##1]
    % build the name with the first letter uppercase
    \wright_make_var_name:n { ##1 }
    \cs_gset_protected:cpn { \l__wright_var_name_tl } { \lVar[##1] }
   }
 }
\tl_const:Nn \c_wright_prefix_tl { l }
\cs_new:Npn \wright_make_var_name:n #1
 {
  % get the first letter and uppercase it
  \tl_to_uppercase:n
   {
    \tl_set:Nx \l__wright_var_name_tl { \tl_head:n {#1} }
   }
  % add the rest
  \tl_put_right:Nx \l__wright_var_name_tl { \tl_tail:n {#1} }
  % add the prefix
  \tl_put_left:Nx \l__wright_var_name_tl { \c_wright_prefix_tl }
 }
\ExplSyntaxOff
\newcommand{\lVar}[1][x]{\textsc{#1}}

\begin{document}
\begin{displaymath}
  \begin{array}{l}
    \exists \lVars{a, b, c}.~ \lA = \lB \implies \lA = \lC \\
    \lC = 0 \implies \lA = 0
    \exists \lVars{a, b}.~ \lA \neq \lB
  \end{array}
\end{displaymath}

And, as we see in the term $\lA = \lB$, the implication...

Now $\forall\lVars{foo}\;\lFoo=0$
\end{document}

Bildbeschreibung hier eingeben

verwandte Informationen