Makro zur Auswertung der Anzahl von Argumenten ohne Begrenzung

Makro zur Auswertung der Anzahl von Argumenten ohne Begrenzung

Ist es möglich, ein Makro wie dieses zu haben:

\documentclass{article}
\usepackage{xparse}
\pagestyle{empty}
\ExplSyntaxOn
\NewDocumentCommand{\HowManyArguments}{ oooooooo }
    {
        \IfNoValueTF {#1} { 0 }{
            \IfNoValueTF {#2} { 1 }{
                \IfNoValueTF {#3}  { 2 }{
                     \IfNoValueTF {#4}  { 3 }{
                          \IfNoValueTF {#5}  { 4 }{
                               \IfNoValueTF {#6}  { 5 }{
                                    \IfNoValueTF {#7}  { 6 }{
                                         \IfNoValueTF {#8}  { 7 }{ So many }
                                    }
                               }
                          }
                     }
                }
            }
        }
    }
\ExplSyntaxOff
\begin{document}

The amount of arguments in this macro is \HowManyArguments[Cake][Chair][Stool][Bicycle] .

\end{document}

Außer einem, der keine Einschränkungen hat, ähnlich wie der \powertowerThread, bei dem das Ergebnis jedes Mal in den Rest eingebettet ist:Wie setze ich eine Zehnfachpotenzierung (einen Potenzturm) mit LaTeX?

Die Idee besteht dann darin, das erste Argument des Makros zu verwenden, um die Anzahl der Argumente zu definieren.

Der Grund, warum ich den „Powertower“-Ansatz brauche, ist Teil 2: Ich möchte basierend auf der Nummer des Arguments zwischen den Farben wechseln.

Versuchen Sie, Folgendes zu kompilieren:

\documentclass{article}
\usepackage{xparse}
\usepackage{xcolor}
\pagestyle{empty}
\ExplSyntaxOn
\NewDocumentCommand{\HowManyArguments}{ oooooooo }
    {
        \IfNoValueTF {#1} {}{
            \IfNoValueTF {#2} {\textcolor{blue}{#1(}...\textcolor{blue}{)}}{
                \IfNoValueTF {#3}  {\textcolor{blue}{#1(}\textcolor{green}{#2(}...\textcolor{green}{)}\textcolor{blue}{)}}{
                     \IfNoValueTF {#4}  {\textcolor{blue}{#1(}\textcolor{green}{#2(}\textcolor{red}{#3(}...\textcolor{red}{)}\textcolor{green}{)}\textcolor{blue}{)}}{
                          \IfNoValueTF {#5}  {\textcolor{blue}{#1(}\textcolor{green}{#2(}\textcolor{red}{#3(}\textcolor{blue}{#4(}...\textcolor{blue}{)}\textcolor{red}{)}\textcolor{green}{)}\textcolor{blue}{)}}{
                               \IfNoValueTF {#6}  {\textcolor{blue}{#1(}\textcolor{green}{#2(}\textcolor{red}{#3(}\textcolor{blue}{#4(}\textcolor{green}{#5(}...\textcolor{green}{)}\textcolor{blue}{)}\textcolor{red}{)}\textcolor{green}{)}\textcolor{blue}{)}}{
                                    \IfNoValueTF {#7}  {\textcolor{blue}{#1(}\textcolor{green}{#2(}\textcolor{red}{#3(}\textcolor{blue}{#4(}\textcolor{green}{#5(}\textcolor{red}{#6(}...\textcolor{red}{)}\textcolor{green}{)}\textcolor{blue}{)}\textcolor{red}{)}\textcolor{green}{)}\textcolor{blue}{)}}{
                                         \IfNoValueTF {#8}  {\textcolor{blue}{#1(}\textcolor{green}{#2(}\textcolor{red}{#3(}\textcolor{blue}{#4(}\textcolor{green}{#5(}\textcolor{red}{#6(}\textcolor{blue}{#7(}...\textcolor{blue}{)}\textcolor{red}{)}\textcolor{green}{)}\textcolor{blue}{)}\textcolor{red}{)}\textcolor{green}{)}\textcolor{blue}{)}}{\textcolor{blue}{#1(}\textcolor{green}{#2(}\textcolor{red}{#3(}\textcolor{blue}{#4(}\textcolor{green}{#5(}\textcolor{red}{#6(}\textcolor{blue}{#7(}\textcolor{green}{#8(}...\textcolor{green}{)}\textcolor{blue}{)}\textcolor{red}{)}\textcolor{green}{)}\textcolor{blue}{)}\textcolor{red}{)}\textcolor{green}{)}\textcolor{blue}{)}}
                                    }
                               }
                          }
                     }
                }
            }
        }
    }
\ExplSyntaxOff
\begin{document}

Try this one instead: \HowManyArguments[Mandatory][Argument][What][Have][You][Done]

\end{document}

Dies ist die vollständige Idee dessen, was ich zu bauen versuche, jedoch ohne Einschränkungen.

Antwort1

Es wäre konsistenter mit dem Design von LaTeX, eine durch Kommas getrennte Liste zu verwenden:

Bildbeschreibung hier eingeben

Bildbeschreibung hier eingeben

\documentclass{article}

\makeatletter
\newcommand\HowManyArguments[1]{{%
\count@\z@
\@for\tmp:=#1\do{\advance\count@\@ne}%
There are \the\count@\ arguments\par
\@for\tmp:=#1\do{(\tmp}%
\@for\tmp:=#1\do{)}}}
\makeatother


\begin{document}

\HowManyArguments{Lots,of,arguments,in,this,thread}

\end{document}

Oder mit Farbe

\documentclass{article}

\usepackage{color}
\makeatletter
\newcommand\HowManyArguments[1]{{%
\count@\z@
\@for\tmp:=#1\do{\advance\count@\@ne}%
There are \the\count@\ arguments\par
\@tempcnta\z@
\@for\tmp:=#1\do{%
\advance\@tempcnta\@ne
\textcolor[RGB]{\the\numexpr255*\@tempcnta/(\count@)\relax,
                 0,
                 \the\numexpr255-(255*\@tempcnta/(\count@))\relax}%
                {\tmp(}}%
\@tempcnta\count@
\@for\tmp:=#1\do{%
\textcolor[RGB]{\the\numexpr255*\@tempcnta/(\count@)\relax,
                 0,
                 \the\numexpr255-(255*\@tempcnta/(\count@))\relax}%
                {)}%
\advance\@tempcnta\m@ne
}}}
\makeatother


\begin{document}

\HowManyArguments{Lots,of,arguments,in,this,thread}

\end{document}

Antwort2

Eine expl3Lösung (nur um David eine Freude zu machen). Ich führe die Berechnung nur einmal durch, indem ich zwei Token-Listen ausfülle und übergebe.

\documentclass{article}
\usepackage{xparse,xcolor}

\ExplSyntaxOn
\NewDocumentCommand{\HMA}{m}
 {
  \leavevmode
  \group_begin:
  \hma_process_args:n { #1 }
  \group_end:
 }

\seq_new:N \l__hma_args_seq
\tl_new:N \l__hma_opening_tl
\tl_new:N \l__hma_closing_tl
\int_new:N \l__hma_step_int
\int_new:N \l__hma_args_int

\cs_new_protected:Npn \hma_process_args:n #1
 {
  \seq_set_split:Nnn \l__hma_args_seq { , } { #1 }
  \tl_clear:N \l__hma_opening_tl
  \tl_set:Nn \l__hma_closing_tl { \dots }
  \int_zero:N \l__hma_step_int
  \int_set:Nn \l__hma_args_int { \seq_count:N \l__hma_args_seq }
  \color[RGB]{0,0,255}
  \seq_map_inline:Nn \l__hma_args_seq
   {
    \int_incr:N \l__hma_step_int
    \tl_put_right:Nx \l__hma_opening_tl
     {
      \exp_not:n { ##1 ( }
      \group_begin:
      \exp_not:N \color[RGB] { \__hma_set_color: }
     }
    \tl_put_right:Nn \l__hma_closing_tl { \group_end: ) }
   }
  \tl_use:N \l__hma_opening_tl \tl_use:N \l__hma_closing_tl
 }

\cs_new:Npn \__hma_set_color:
 {
  \int_eval:n { 255 * \l__hma_step_int / \l__hma_args_int },
  0,
  \int_eval:n { 255 - ( 255 * \l__hma_step_int / \l__hma_args_int ) }
 }

\ExplSyntaxOff

\begin{document}

\HMA{Lots,of,arguments,in,this,thread}

\end{document}

Bildbeschreibung hier eingeben

Durch die Änderung nur einer Zeile können die Punkte vermieden und das letzte Element für die innerste Klammer verwendet werden.

\documentclass{article}
\usepackage{xparse,xcolor}

\ExplSyntaxOn
\NewDocumentCommand{\HMA}{m}
 {
  \leavevmode
  \group_begin:
  \hma_process_args:n { #1 }
  \group_end:
 }

\seq_new:N \l__hma_args_seq
\tl_new:N \l__hma_opening_tl
\tl_new:N \l__hma_closing_tl
\int_new:N \l__hma_step_int
\int_new:N \l__hma_args_int

\cs_new_protected:Npn \hma_process_args:n #1
 {
  \seq_set_split:Nnn \l__hma_args_seq { , } { #1 }
  \tl_clear:N \l__hma_opening_tl
  \seq_pop_right:NN \l__hma_args_seq \l__hma_closing_tl % <---- CHANGE
  \int_zero:N \l__hma_step_int
  \int_set:Nn \l__hma_args_int { \seq_count:N \l__hma_args_seq }
  \color[RGB]{0,0,255}
  \seq_map_inline:Nn \l__hma_args_seq
   {
    \int_incr:N \l__hma_step_int
    \tl_put_right:Nx \l__hma_opening_tl
     {
      \exp_not:n { ##1 ( }
      \group_begin:
      \exp_not:N \color[RGB] { \__hma_set_color: }
     }
    \tl_put_right:Nn \l__hma_closing_tl { \group_end: ) }
   }
  \tl_use:N \l__hma_opening_tl \tl_use:N \l__hma_closing_tl
 }

\cs_new:Npn \__hma_set_color:
 {
  \int_eval:n { 255 * \l__hma_step_int / \l__hma_args_int },
  0,
  \int_eval:n { 255 - ( 255 * \l__hma_step_int / \l__hma_args_int ) }
 }

\ExplSyntaxOff

\begin{document}

\HMA{Lots,of,arguments,in,this,thread}

\end{document}

Bildbeschreibung hier eingeben

Antwort3

Um dem Wunsch des OP nach etwas wie nachzukommen Lots(of(arguments(in(this(thread))))), habe ich das Makro erstellt \groupargs{{}{}{}{}}und gebe ein Beispiel.

Während ich die Farben diskret modulo der Verschachtelungsebene ändere (wie im MWE des OP), könnte jeder beliebige Algorithmus eingefügt werden, um die Farbe als Funktion der Verschachtelungsebene zu berechnen.

\documentclass{article}
\usepackage{readarray}
\usepackage{xcolor}
\newcounter{argindex}
\newcounter{colindex}
\def\groupargs#1{\def\lparen{}\def\rparen{}\getargsC{#1}%
  \setcounter{argindex}{0}\setcounter{colindex}{0}\nextarg}
\def\nextarg{\stepcounter{argindex}\stepcounter{colindex}%
  \ifnum\theargindex>\narg\else\lparen%
  \ifnum\thecolindex=1\def\mycolor{blue}\else%
    \ifnum\thecolindex=2\def\mycolor{green}\else%
      \ifnum\thecolindex=3\def\mycolor{red}\setcounter{colindex}{0}%
  \fi\fi\fi%
  \def\lparen{(}%
  \textcolor{\mycolor}{\csname arg\romannumeral\theargindex\endcsname%
  {\def\rparen{)}\nextarg}}\rparen\fi%
}
\begin{document}
\groupargs{Mandatory Argument What Have {You Indeed} Done ...}
\end{document}

Bildbeschreibung hier eingeben

verwandte Informationen