1

I've grown fond of this xintexpr package for its nice syntax. I'd like to repeat a command M times and another one 5-M = N times. I'm getting an error due to the arithmetic expression of the latter being in the foreach loop. I thought using the \let cs was supposed to compute the expression? I also thought maybe the issue was related to expansion/

MWE below:

\documentclass[11pt,a4paper]{article}

\usepackage{pgffor}
\usepackage{xint}
\usepackage{xintexpr}

\begin{document}

    \newcommand{\M}{3}  
     
    % my attempts below
    \newcommand{\N}{\printnumber{{\xintiexpr5-\M\relax}}}
%   \edef\N{\printnumber{\xintiexpr5-\M\relax}}
%   \newcommand{\Na}{\xintiexpr5-\M\relax}  % intermediate value
%   \let\N\Na % tr
    
    \foreach \m in {1,...,\M}{M}
    \foreach \n in {1,...,\N}{N} % but the math wont work here
\end{document}
2

You want to do full expansion of the expression:

\documentclass[11pt,a4paper]{article}

\usepackage{pgffor}
\usepackage{xint}
\usepackage{xintexpr}

\begin{document}

\newcommand{\M}{3}

\edef\N{\xintthe\xintiexpr5-\M\relax}

\foreach \m in {1,...,\M}{M}
\foreach \n in {1,...,\N}{N}

\end{document}

This will print

MMM NN

With a different approach:

\documentclass[11pt,a4paper]{article}

\usepackage{pgffor}
\usepackage{xfp}

\begin{document}

\newcommand{\M}{3}

\foreach \m in {1,...,\M}{M}
\foreach \n in {1,...,\inteval{5-\M}}{N}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.