TransWikia.com

pgffor loop with arithmetic expression

TeX - LaTeX Asked on March 1, 2021

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-Mrelax}}}
%   edefN{printnumber{xintiexpr5-Mrelax}}
%   newcommand{Na}{xintiexpr5-Mrelax}  % intermediate value
%   letNNa % tr
    
    foreach m in {1,...,M}{M}
    foreach n in {1,...,N}{N} % but the math wont work here
end{document}

One Answer

You want to do full expansion of the expression:

documentclass[11pt,a4paper]{article}

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

begin{document}

newcommand{M}{3}

edefN{xintthexintiexpr5-Mrelax}

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}

Correct answer by egreg on March 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP