也许是一个众所周知的问题,如何避免0
整数的尾随无用?
\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
你可以\m
输入\pgfmathprintnumber
。
\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}
答案2
更简单的语法:
\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}
可选参数是步骤,你可以尝试\xforeach{1}[2]{5}{#1 }
。你也可以传递\fpeval
给\num
of siunitx
。例如,
\xforeach{1}[3]{7}{\fpeval{round(pi^(#1),3)} }
将产生