5

Maybe a well known problem, how to avoid the trailing useless 0 for an integer ?

enter image description here

\documentclass{article}

\usepackage{tikz}
\usepgflibrary{fpu}

\begin{document}

\foreach \n [evaluate=\n as \m using int(\n+1)] in {1,...,3} {\m\ }

\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}

\foreach \n [evaluate=\n as \m using int(\n+1)] in {1,...,3} {\m\ }
\end{document} 
  • 1
    You could use \pgfmathprintnumber{\m}, but that is perhaps not what you were after? – Torbjørn T. Mar 21 '18 at 17:44
  • Exactly that. Could you write an answer please. – Tarass Mar 21 '18 at 17:51
5

You could feed \m into \pgfmathprintnumber.

enter image description here

\documentclass{article}

\usepackage{tikz}
\usepgflibrary{fpu}

\begin{document}

\foreach \n [evaluate=\n as \m using int(\n+1)] in {1,...,3} {\m\ }

\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}

\foreach \n [evaluate=\n as \m using int(\n+1)] in {1,...,3} {\pgfmathprintnumber{\m}\ }
\end{document}
1

Simpler syntax:

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

\ExplSyntaxOn
\NewDocumentCommand{\xforeach}{mO{1}mm}
 {
  \int_step_inline:nnnn { #1 } { #2 } { #3 } { #4 }
 }
\ExplSyntaxOff

\begin{document}

\xforeach{1}{3}{#1 }

\xforeach{1}{3}{\inteval{#1+1} }

\xforeach{1}{3}{\fpeval{round(sqrt(#1),4)} }

\end{document}

enter image description here

The optional argument is the step, you can try \xforeach{1}[2]{5}{#1 }. You can also pass \fpeval to \num of siunitx. For instance,

\xforeach{1}[3]{7}{\fpeval{round(pi^(#1),3)} }

will produce

enter image description here

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.