0

I want to create a new command, that has more than 9 arguments. Therefore, i am using \NewDocumentComannd and comma separated lists. With this new command i use \pgfmathsetmacro, too. A simplified (working) example code looks like this:

\documentclass[12pt]{article}

\usepackage{pgfmath}

\ExplSyntaxOn
\NewDocumentCommand{\test}{mm}
{
\exp_args:Nne\pgfmathsetmacro{\expression} % Instead of naming the macro “\expression”, i want to give the macro a name depending on an element of a comma separated list
{
\clist_item:nn{#2}{1}
}
\expression
}
\ExplSyntaxOff

\begin{document}

\test{pa}{1,2,3}

\end{document}

I am trying to do something like this:

\documentclass[12pt]{article}

\usepackage{pgfmath}

\ExplSyntaxOn
\NewDocumentCommand{\test}{mm}
{
\exp_args:Nne\pgfmathsetmacro{\clist_item:nn{#1}{1}x} % Here i try to create a macro named “\pax” (“pa” is an element of a list (see below: \test{pa}{1,2,3}) and “x” is just added to this element)
{
\clist_item:nn{#2}{1}
}
\csname\clist_item:nn{#1}{1}x\endcsname % Here i try to use/access the created macro named “\pax” (This code works, but is there another method provided by the expl3-package/LaTeX3 to use/access the macro without using \csname \endcsname?)
}
\ExplSyntaxOff

\begin{document}

\test{pa}{1,2,3}

\end{document}

How can i name a pgfmath macro depending on an element of a comma separated list?

Edit 1: Again, thanks a lot for the answers.

2 Answers 2

0

Your \exp_args:Nne is redundant in either case, because \pgfmathsetmacro does full expansion anyway.

For the code you have in mind, \exp_args:Nc is what you need.

\documentclass[12pt]{article}

\usepackage{pgfmath}

\ExplSyntaxOn
\NewDocumentCommand{\test}{mm}
 {
  \exp_args:Nc \pgfmathsetmacro{\clist_item:nn{#1}{1}x}{\clist_item:nn{#2}{1}}
  \use:c {\clist_item:nn{#1}{1}x}
 }
\ExplSyntaxOff

\begin{document}

\test{pa}{1,2,3} \pax

\test{pa}{pi^2/6,2,3} \pax

\end{document}

enter image description here

0

As with your last question it is not clear why you use a pgf definition here, but this defines \pax to be 1 and immediately expands it which seems to be the intention.

\documentclass[12pt]{article}

\usepackage{pgfmath}

\ExplSyntaxOn
\NewDocumentCommand{\test}{mm}
{
\use:e{
\exp_not:N\exp_args:Nc\exp_not:N\pgfmathsetmacro
{\clist_item:nn{#1}{1}x} % Here i try to create a macro named “\pax” (“pa” is an element of a list and “x” is just added to this element)
{
\clist_item:nn{#2}{1}
}
}
\use:c{\clist_item:nn{#1}{1}x}
}
\ExplSyntaxOff

\begin{document}

\test{pa}{1,2,3}


\end{document}
2
  • Thanks for your help. I use \pgfmathsetmacro to calculate. E. g., \pgfmathsetmacro{\numericalvalue}{cos(\clist_item:nn{#2}{1})*(\clist_item:nn{#2}{2})}. I need the results to be saved as numerical values, because i will access them later within the document. I do not know, if there is a easier way. Commented Jan 15, 2023 at 18:48
  • @usuallyiamhungry expl3 would calculate cos a lot more acurately than pgf Commented Jan 15, 2023 at 19:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .