[Tex/LaTex] pgfmath and doing some simple calculations using a variable

pgfmath

I'm creating addition worksheets for little oompa loompas.
It works using:

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

However, in the \newcommand \allan, what I'd really like is to be
able to eliminate addition problems that have a carry in them.

something like:

random1 = first random # from 10 to 99

random2 = 2nd random # from 0 to (9 – (random1 modulo 10))

Best Answer

The result of the calculation can be directly assigned to a macro using \pgfmathsetmacro.

Table cells are local groups, therefore the following definition of \allan first performs the calculations, defines a macro for the table rows with the expanded calculation results and calls the macro to actually set the rows:

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

Full example:

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

Result

Variant without tabularx, which fills the space on the page:

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

Improved result

Hint: \pgfmathsetseed{<number>} can be used to get reproducible results.

Related Question