8

What would be the best way to use TeX to typeset basic arithmetic as it's done in school - for example, to typeset equations like

 356
+ 42
 ---
 398

written in this way?

Sean Allred
  • 27,421
  • Possibly of interest: http://ctan.org/pkg/fc-arith (arithmetic flash cards - addition subtraction division multiplication problems), http://ctan.org/pkg/longdiv (solves the problems, too, but only division). Actually, http://ctan.org/pkg/xlop might be better. – cfr Mar 10 '14 at 03:38

2 Answers2

9

You could define a command to handle this:

\documentclass{article}
\usepackage{array}
\makeatletter
\def\ae@arithmetic{\@ifnextchar[%]
  {\@ae@arithmetic}
  {\@ae@arithmetic[+]}}
\def\@ae@arithmetic[#1]#2#3{%%
  \def\ae@tmp{*}%%
  \def\ae@tmp@mult{x}%%
  \def\ae@operation{#1}%%
  \ifx\ae@tmp@mult\ae@operation\def\ae@operation{*}\fi
  \edef\ae@result{\number\numexpr#2\ae@operation#3\relax}%%
  \ifx\ae@tmp\ae@operation\def\ae@operation{\times}\fi
  \begin{tabular}{c@{}>{$}r<{$}}
                  & #2 \\
  $\ae@operation$ & #3 \\\hline
                  & \ae@result
  \end{tabular}
  }
\newcommand\arithmetic{\ae@arithmetic}
\makeatother
\pagestyle{empty}
\begin{document}

\begin{tabular}{rcr}
  \arithmetic{1234}{234}     &     &    \arithmetic[-]{1234}{234}   \\[1cm]
  \arithmetic[x]{1234}{234}  & vs. &    \arithmetic[*]{1234}{234}  
\end{tabular}

\end{document}

enter image description here

In the above code, I assume the default operation will be addition, but I allow an optional argument be provided to change the operation. I also allow multiplication be indicated by passing * or x in the optional argument.

A.Ellett
  • 50,533
2

One approach would be to have the addition sign on the right (which I have seen on occasions), and would look something like this:

\documentclass[]{article}

\usepackage{amsmath,tabu}
\begin{document}

\begin{equation*}
\begin{tabular}{r@{\;}l@{}}
356  \\
42 & +\\
\hline
398
\end{tabular}
\end{equation*}

\end{document}

gives:

math equation

The code is a bit clunkier to have the addition symbol on the left side:

\documentclass[]{article}

\usepackage{amsmath,tabu}
\begin{document}

\begin{equation*}
\begin{tabular}{r@{\;}r}
  & 356  \\
+ & 42\\
\hline
& 398
\end{tabular}
\end{equation*}

\end{document}

To get:

math with addition sign on left

cslstr
  • 6,545