有算術表達式的 pgffor 循環

有算術表達式的 pgffor 循環

我越來越喜歡這個xintexpr包,因為它有很好的語法。我想重複一個指令 M 次,另一個指令 5-M = N 次。由於後者的算術表達式在foreach循環中,我收到錯誤。我認為使用\letcs 應該計算表達式?我還認為這個問題可能與擴展/

MWE如下:

\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}

答案1

您想要對表達式進行完全擴展:

\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}

這將列印

MMMM神經網路

採用不同的方法:

\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}

相關內容