1

The following is a command that does basic arithmetic with nine variables.

\documentclass{article}
\usepackage{xfp}

\newcommand\functesta[9]{ \edef\myvara{#1} \edef\myvarb{#2} \edef\myvarc{#3} \edef\myvard{#4} \edef\myvare{#5} \edef\myvarf{#6} \edef\myvarg{#7} \edef\myvarh{#8} \edef\myvari{#9} $\inteval{\myvara + \myvarb - \myvarc + \myvard - \myvare + \myvarf - \myvarg + \myvarh - \myvari}$ }

\begin{document} \functesta{1}{2}{3}{4}{5}{6}{7}{8}{100} \end{document}

-94

I tried shrinking the nine lines of definition into one for loop but it didn't work. My intention was that as a loop iterates through 1 to 9, it would expand \alphalph{\iteration} first, which would be combined with \myvarx to become \myvarxa, \myvarxb, ..., \myvarxi. In the same way #\iteration would expand to #1 to #9. However I probably got the syntax wrong.

\documentclass{article}
\usepackage{xfp}
\usepackage{pgffor}
\usepackage{alphalph}

\newcommand\functestb[9]{ \foreach \iteration in {1,...,9} { \edef\expandafter\myvarx\alphalph{\iteration}{#\iteration} } $\inteval{\myvarxa + \myvarxb - \myvarxc + \myvarxd - \myvarxe + \myvarxf - \myvarxg + \myvarxh - \myvarxi}$ }

\begin{document} \functestb{1}{2}{3}{4}{5}{6}{7}{8}{100} \end{document}

! Illegal parameter number in definition of \functestb.

Then I replaced #\iteration with a constant to see if I got the rest of the code right. Nope. What is the right way to achieve what I want? The reason I am not directly accessing #1 to #9 is because I will write a more complex command in which the nine numbers given need to be saved and changed multiple times. I am open to alternatives such as passing a list as a parameter or generating a list inside the command if it is possible to do so in LaTeX.

\documentclass{article}
\usepackage{xfp}
\usepackage{pgffor}
\usepackage{alphalph}

\newcommand\functestb[9]{ \foreach \iteration in {1,...,9} { \edef\expandafter\myvarx\alphalph{\iteration}{17} } $\inteval{\myvarxa + \myvarxb - \myvarxc + \myvarxd - \myvarxe + \myvarxf - \myvarxg + \myvarxh - \myvarxi}$ }

\begin{document} \functestb{1}{2}{3}{4}{5}{6}{7}{8}{100} \end{document}

! Undefined control sequence.
  • 1
    use \csname primitive to concat strings to form a control token. See https://tex.stackexchange.com/questions/39380/what-exactly-do-csname-and-endcsname-do – LdBeth Jul 30 '23 at 17:11
  • Do you need to remember the value of the variables? – egreg Jul 30 '23 at 18:10

2 Answers2

1

This probably isn't how I would do this, but you could use \pgffor's facility to expand the list of arguments and just count them.

\documentclass{article}
\usepackage{xfp}
\usepackage{pgffor}
\usepackage{alphalph}

\newcommand\functesta[9]{% \edef\myvara{#1}% \edef\myvarb{#2}% \edef\myvarc{#3}% \edef\myvard{#4}% \edef\myvare{#5}% \edef\myvarf{#6}% \edef\myvarg{#7}% \edef\myvarh{#8}% \edef\myvari{#9}% $\inteval{\myvara + \myvarb - \myvarc + \myvard - \myvare + \myvarf - \myvarg + \myvarh - \myvari}$% }

\newcommand\functestb[9]{% \foreach [count=\ino,expand list=true] \iteration in {#1,#2,#3,#4,#5,#6,#7,#8,#9} {% \xdef is needed as we want to use the definitions outside the loop \expandafter\xdef\csname myvarx\alphalph{\ino}\endcsname{\iteration}% }% $\inteval{\myvarxa + \myvarxb - \myvarxc + \myvarxd - \myvarxe + \myvarxf - \myvarxg + \myvarxh - \myvarxi}$% }

\begin{document} \functestb{1}{2}{3}{4}{5}{6}{7}{8}{100} \functesta{1}{2}{3}{4}{5}{6}{7}{8}{100} \end{document}

-94 twice

cfr
  • 198,882
0

I'm not sure you need to name variables.

\documentclass{article}
%\usepackage{xfp} % no longer needed
\usepackage{alphalph}

\ExplSyntaxOn

\NewDocumentCommand\functesta{mm} { \tl_set:Nn \l_tmpa_tl { #1 } \regex_replace_all:nnN { \cP#([0-9]+) } % find #<digits> { \c{seq_item:Nn} \c{l_tmpa_seq} \cB{\1\cE} } % replace with the seq item \l_tmpa_tl % make the sequence from the input \seq_set_from_clist:Nn \l_tmpa_seq { #2 } % evaluate $\int_eval:n { \tl_use:N \l_tmpa_tl }$ }

\ExplSyntaxOff

\begin{document}

\functesta{#1+#2-#3+#4-#5+#6-#7+#8-#9}{1,2,3,4,5,6,7,8,100}

\functesta{(#1#2+#3#4)*#5}{1,2,3,4,5}

\functesta{#1+#2+#3+#4+#5+#6+#7+#8+#9+#10+#11}{1,2,3,4,5,6,7,8,9,10,11}

\end{document}

As you see, the function gets any definition you like and the values are given as a list.

The trick is to replace #<digits> with \seq_item:N \l_tmpa_tl { <digits> }, so we have a syntax similar, but not restricted to nine parameters, to a macro definition.

enter image description here

egreg
  • 1,121,712