我需要类似这样的 LaTeX:
while x mod 10 != 0:
x++
我的 LaTeX 翻译可以无限编译,如下所示:
\newcommand{\lfn}{
\loop \ifnum \pgfmathresult<1
\stepcounter{x}
\pgfmathparse{Mod(\arabic{x},2)==0?1:0}
\repeat
}
答案1
这有效:
\documentclass{article}
\usepackage{pgfmath}
\newcounter{x}
\begin{document}
\pgfmathparse{0}% initialize
\loop
\ifnum\pgfmathresult<1
\stepcounter{x}%
\pgfmathparse{Mod(\value{x},10)==0?1:0}
\thex
\repeat
\end{document}
更通用的循环宏。在模板中,#1
代表循环中的当前值。可选参数是模数(默认为 10)。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\lfn}{ O{10} m +m }
{% #1 (optional) is the modulo
% #2 is the starting point
% #3 is a template for what to do
\cs_set:Nn \__noname_lfn_do:n { #3 }
\noname_lfn:nn { #1 } { #2 }
}
\int_new:N \l__noname_lfn_index_int
\cs_new_protected:Nn \noname_lfn:nn
{
\int_set:Nn \l__noname_lfn_index_int { #2 }
\int_do_until:nn
{ \int_mod:nn { \l__noname_lfn_index_int } { #1 } = 0 }
{
\__noname_lfn_do:V \l__noname_lfn_index_int
\int_incr:N \l__noname_lfn_index_int
}
\__noname_lfn_do:V \l__noname_lfn_index_int
}
\cs_new:Nn \__noname_lfn_do:n {}
\cs_generate_variant:Nn \__noname_lfn_do:n { V }
\ExplSyntaxOff
\begin{document}
\verb|\lfn{1}{The current value is #1\par}|
\lfn{1}{The current value is #1\par}
\bigskip
\verb|\lfn[6]{2}{The current value is #1\par}|
\lfn[6]{2}{The current value is #1\par}
\end{document}
答案2
欢迎来到 TeX.SE!一种可能性是使用pgffor
with \breakforeach
。只要确保\pgfmathparse
正确初始化,您的代码也能正常工作。
\documentclass{article}
\usepackage{pgffor}
\begin{document}
\foreach \X [evaluate=\X as \Y using {int(mod(\X,10))}] in {1,...,15}
{\X
\ifnum\Y=0
\breakforeach
\fi
}
\newcounter{x}\setcounter{x}{0}
\newcommand{\lfn}{\pgfmathparse{0}
\loop \ifnum \pgfmathresult<1
\stepcounter{x}\thex,
\pgfmathparse{Mod(\arabic{x},10)==0?1:0}
\repeat
}
\lfn
\end{document}