pgfmath 并使用变量进行一些简单的计算

pgfmath 并使用变量进行一些简单的计算

我正在为小乌姆帕鲁姆帕人创建加法工作表。它的工作原理如下:

\documentclass{article}  

\usepackage{geometry}  
\geometry{letterpaper, portrait, margin=1.5cm, tmargin=2.5cm }  
\usepackage{tabularx}  
\usepackage{array}  
\usepackage{siunitx}  
\usepackage{tikz}  
\usepackage{pgf}  
\DeclareMathSizes{10.0}{17}{12}{12}  

\begin{document}  
\begin{flushleft}  
    
% fixed width, right justified column  
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}  



% print 2 numbers
%
%     42
%    + 7
%
\newcommand{\allan}{  
    & \pgfmathparse{random(10,99)}\pgfmathresult \\  
    + & \pgfmathparse{random(0,9)}\pgfmathresult \\  
}  

% create 7x9 grid of addition problems
\foreach \n in {0,...,8}{  
    \foreach \n in {0,...,6}{  
        \begin{tabularx}{1.8cm}{>{$}R{.3cm}<{$} >{$}R{.7cm}<{$}}  
            \allan  
            \hline  
        \end{tabularx}  
        \hspace{.4cm}  
    }  
    \vspace{1.5cm}  
}  


\end{flushleft} 
\end{document}

然而,在中\newcommand \allan,我真正想要的是能够消除带有进位的加法问题。

就像是:

random1 = 第一个随机数 # 从 10 到 99

random2 = 第二个随机数 # 从 0 到 (9 - (random1 modulo 10))

答案1

可以使用 将计算结果直接分配给宏\pgfmathsetmacro

表格单元格是本地组,因此下面的定义\allan首先执行计算,使用扩展的计算结果为表格行定义一个宏,并调用该宏来实际设置行:

\newcommand{\allan}{
    \pgfmathsetmacro\RandomA{random(10,99)}%
    \pgfmathsetmacro\RandomB{random(0, int(9 - mod(\RandomA,10)))}%
    \edef\next{%
      & \RandomA \noexpand\\%
      + & \RandomB \noexpand\\%
    }%
    \next
}

完整示例:

\documentclass{article}

\usepackage{geometry}
\geometry{letterpaper, portrait, margin=1.5cm, tmargin=2.5cm }
\usepackage{tabularx}
\usepackage{array}
\usepackage{siunitx}
\usepackage{tikz}
\usepackage{pgf}
\DeclareMathSizes{10.0}{17}{12}{12}

\begin{document}
\begin{flushleft}

% fixed width, right justified column
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}



% print 2 numbers
%
%     42
%    + 7
%
\newcommand{\allan}{
    \pgfmathsetmacro\RandomA{random(10,99)}%
    \pgfmathsetmacro\RandomB{random(0, int(9 - mod(\RandomA,10)))}%
    \edef\next{%
      & \RandomA \noexpand\\%
      + & \RandomB \noexpand\\%
    }%
    \next
}

% create 7x9 grid of addition problems
\foreach \n in {0,...,8}{
    \foreach \n in {0,...,6}{
        \begin{tabularx}{1.8cm}{>{$}R{.3cm}<{$} >{$}R{.7cm}<{$}}
            \allan
            \hline
        \end{tabularx}
        \hfill
    }
    \par
    \vspace{1.5cm}
}


\end{flushleft}
\end{document}

结果

不带 的变体tabularx,填充页面空间:

\documentclass{article}

\usepackage{geometry}
\geometry{letterpaper, portrait, margin=1.5cm, tmargin=2.5cm }
\usepackage{tabularx}
\usepackage{array}
\usepackage{siunitx}
\usepackage{tikz}
\usepackage{pgf}
\DeclareMathSizes{10.0}{17}{12}{12}

\begin{document}
\begin{flushleft}
    \setlength{\parskip}{0pt plus 1fill}
    \setlength{\parfillskip}{0pt}
    \setlength{\tabcolsep}{2\tabcolsep}% make the lines a little longer

    % print 2 numbers
    %
    %     42
    %    + 7
    %
    \newcommand{\allan}{
        \pgfmathsetmacro\RandomA{random(10,99)}%
        \pgfmathsetmacro\RandomB{random(0, int(9 - mod(\RandomA,10)))}%
        \edef\next{%
            & \noexpand\leavevmode
              \ifnum\RandomA<10 \noexpand\hphantom{0}\fi % if \RandomA can be smaller than ten
              \RandomA \noexpand\\%
            + & \RandomB \noexpand\\%
        }%
        \next
    }

    % create 7x9 grid of addition problems
    \foreach \n in {0,...,8}{
        \foreach \n in {0,...,6}{
            \begin{tabular}{>{$}l<{$} @{$\;$} >{$}r<{$}}
                \allan
                \hline
                \vadjust{\vspace{1.1cm}}% place for result
            \end{tabular}%
            \hfill
        }%
        \par
    }
    \flushbottom
    \newpage
\end{flushleft}
\end{document}

改善结果

暗示: \pgfmathsetseed{<number>}可用于获得可重复的结果。

相关内容