12

Is there any way to format a subtraction in LaTeX, the same way you would, if you were to calculate by hand? (see attached picture). btw the 1) and 2) does not have to be included.

enter image description here

naphaneal
  • 2,614

2 Answers2

20

The OP's title said substraction, but his/her image shows addition. I originally opted for the latter, but have EDITED to also perform subtractions. In the ADDENDUM, I have begin the process of implementing decimals, at this point for 2-digit addition.

SUBTRACTION

The one known flaw, if some might call it that, is that the difference will show leading zeroes.

Use \showdiff{<minuend>}{<subtrahend>}.

This approach uses my stringstrings package to extract a column at a time from the minuend and subtrahend, starting from the right (conveniently done with stringstrings feature of using $ to denote the end-of-string character location). It subtracts them, but if the value is negative, it executes a borrow. If the minuend's \borrowed value is less than zero, it bumps it to 9 and sets a \futureborrowed flag to apply to the next column. It then constructs and saves a \Longstack of those column digits (including any amount of borrowing into or out of the digit) under the name \thediff.

It then builds a global picture of the sum in \savediff, by prepending the latest \thediff to the existing \savediff. It discards the last digit from each of the addends (using \nextcolumn) and the it recursively goes back and does it again. When it determines that the truncated minuend and subtrahend are both zero, it tacks a minus in front of \savediff and presents it.

\documentclass{article}
\usepackage{stringstrings,stackengine}
\newcommand\showdiff[2]{%
  \let\savediff\relax%
  \def\borrow{}%
  \xdef\borrowed{0}%
  \ifnum#1>32\relax\showdiffengine{#1}{#2}\else
    {\ttfamily\Longstack[r]{#1\,{} \underline{-#2\,} {< 0\,}}}\fi
}
\newcommand\showdiffengine[2]{%
  \xdef\opA{#1}%
  \xdef\opB{#2}%
  \lastdigits{\opA}{\opB}%
  \ifnum\borrowed=1\relax\xdef\XdigA{\the\numexpr\digA-1}\else\xdef\XdigA{\digA}\fi%
  \ifnum\XdigA<0\relax\xdef\XdigA{9}\xdef\futureborrowed{1}\else
    \xdef\futureborrowed{0}\fi
  \edef\coldiff{\the\numexpr\XdigA-\digB\relax}%
  \ifnum\coldiff<0\relax%
    \xdef\borrow{1}%
    \edef\coldiff{\the\numexpr\borrow\XdigA-\digB\relax}%
  \else
    \xdef\borrow{}%
  \fi
  \ifnum\borrowed=0\relax%
    \savestack{\thediff}{\ttfamily\Longstack[l]{%
      \tiny\bllap[1.5\baselineskip]{\borrow} \XdigA{} \underline{\VdigB\kern3pt} %
        \coldiff}}%
  \else
    \savestack{\thediff}{\ttfamily\Longstack[l]{%
     \tiny\brlap[.7\baselineskip]{\borrow\the\numexpr\XdigA} \crossout{\digA} %
        \underline{\VdigB\kern3pt} \coldiff}}%
  \fi
  \if\relax\borrow\relax\xdef\borrowed{0}\else\xdef\borrowed{1}\fi
  \savestack{\savediff}{\thediff\savediff}%
  \ifnum\futureborrowed>0\relax\xdef\borrowed{1}\xdef\fugureborrowed{0}\fi
  \nextcolumn{\opA}{\opB}%
  \ifnum\numexpr0\opA+0\opB>0\relax\showdiffengine{\opA}{\opB}%
  \else
    \Longstack{\underline{\ttfamily-\,} {}}\savediff\fi%
}
\newcommand\crossout[1]{\stackengine{0pt}{#1}{/}{O}{c}{F}{F}{L}}
\newcommand\lastdigits[2]{%
  \substring[q]{#1}{$}{$}%
  \xdef\digA{\thestring}%
  \xdef\VdigA{\digA}\if0\digA\ifnum#1=0\relax\xdef\VdigA{~}\fi\fi%
  \substring[q]{#2}{$}{$}%
  \xdef\digB{\thestring}%
  \xdef\VdigB{\digB}\if0\digB\ifnum#2=0\relax\xdef\VdigB{~}\fi\fi%
}
\newcommand\nextcolumn[2]{%
  \substring[q]{#1}{1}{$-1}%
  \xdef\opA{0\thestring}%
  \substring[q]{#2}{1}{$-1}%
  \xdef\opB{0\thestring}%
}
\begin{document}
\showdiff{567}{243}\qquad
\showdiff{37}{9}\qquad
\showdiff{135}{49}\qquad
\showdiff{2134}{49}\bigskip

\showdiff{107}{23}\qquad
\showdiff{100}{1}\qquad
\showdiff{1000}{999}\qquad
\showdiff{10}{70}\qquad\bigskip
\end{document}

enter image description here

2-TERM ADDITION

Use \showsum{<addend-1>}{<addend-2>}.

This approach also extracts a column at a time from each addend, starting from the right. It adds them, extracts the right-most digit of the sum, and saves the remainder. It then constructs and saves a \Longstack of those column digits (including any prior remainder) under the name \thesum.

It then builds a global picture of the sum in \savesum, by prepending the latest \thesum to the existing \savesum. It discards the last digit from each of the addends (using \nextcolumn) and the it recursively goes back and does it again. When it determines that the remainder and truncated addends are all zero, it tacks a plus in front of \savesum and presents it.

(NOTE: the \nextcolumn and \lastdigits macros here is identical to those used in the subtraction code above)

\documentclass{article}
\usepackage{stringstrings,stackengine}
\newcommand\showsum[2]{%
  \xdef\remainder{0}%
  \xdef\Vremainder{~}%
  \let\savesum\relax%
  \showsumengine{#1}{#2}%
}
\newcommand\showsumengine[2]{%
  \xdef\opA{#1}%
  \xdef\opB{#2}%
  \lastdigits{\opA}{\opB}%
  \edef\colsum{\the\numexpr\digA+\digB+0\remainder\relax}%
  \substring[q]{\colsum}{$}{$}%
  \savestack{\thesum}{\ttfamily\Longstack[l]{%
    \tiny\brlap[.7\baselineskip]{\Vremainder} \VdigA{} \underline{\VdigB\,} \thestring}}%
  \substring[q]{\colsum}{1}{$-1}%
  \xdef\remainder{\thestring}%
  \if\relax\remainder\relax\xdef\Vremainder{}\else\xdef\Vremainder{\remainder}\fi%
  \savestack{\savesum}{\thesum\savesum}%
  \nextcolumn{\opA}{\opB}%
  \ifnum\numexpr0\opA+0\opB+0\remainder>0\relax\showsumengine{\opA}{\opB}\else
    \Longstack{\underline{\ttfamily+\,} {}}\savesum\fi%
}
\newcommand\lastdigits[2]{%
  \substring[q]{#1}{$}{$}%
  \xdef\digA{\thestring}%
  \xdef\VdigA{\digA}\if0\digA\ifnum#1=0\relax\xdef\VdigA{~}\fi\fi%
  \substring[q]{#2}{$}{$}%
  \xdef\digB{\thestring}%
  \xdef\VdigB{\digB}\if0\digB\ifnum#2=0\relax\xdef\VdigB{~}\fi\fi%
}
\newcommand\nextcolumn[2]{%
  \substring[q]{#1}{1}{$-1}%
  \xdef\opA{0\thestring}%
  \substring[q]{#2}{1}{$-1}%
  \xdef\opB{0\thestring}%
}
\begin{document}
\showsum{14}{7}\qquad
\showsum{56}{98}\qquad
\showsum{9236}{895}\qquad
\showsum{9236}{825}\bigskip

\showsum{9236}{455}\qquad
\showsum{98669}{985}\qquad
\showsum{98069}{905}
\end{document}

enter image description here

A VERSION WITH 3 ADDENDS

Use, with capital "S", \Showsum{<addend-1>}{<addend-2>}{<addend-3>}

\documentclass{article}
\usepackage{stringstrings,stackengine}
\newcommand\Showsum[3]{%
  \xdef\remainder{0}%
  \xdef\Vremainder{~}%
  \let\savesum\relax%
  \Showsumengine{#1}{#2}{#3}%
}
\newcommand\Showsumengine[3]{%
  \xdef\opA{#1}%
  \xdef\opB{#2}%
  \xdef\opC{#3}%
  \Lastdigits{\opA}{\opB}{\opC}%
  \edef\colsum{\the\numexpr\digA+\digB+\digC+0\remainder\relax}%
  \substring[q]{\colsum}{$}{$}%
  \savestack{\thesum}{\ttfamily\Longstack[l]{%
    \tiny\brlap[.7\baselineskip]{\Vremainder} \VdigA{} \VdigB{} \underline{\VdigC\,} %
      \thestring}}%
  \substring[q]{\colsum}{1}{$-1}%
  \xdef\remainder{\thestring}%
  \if\relax\remainder\relax\xdef\Vremainder{}\else\xdef\Vremainder{\remainder}\fi%
  \savestack{\savesum}{\thesum\savesum}%
  \Nextcolumn{\opA}{\opB}{\opC}%
  \ifnum\numexpr0\opA+0\opB+0\opC+0\remainder>0\relax%
    \Showsumengine{\opA}{\opB}{\opC}\else
    \Longstack{\underline{\ttfamily+\,} {}}\savesum\fi%
}
\newcommand\Lastdigits[3]{%
  \substring[q]{#1}{$}{$}%
  \xdef\digA{\thestring}%
  \xdef\VdigA{\digA}\if0\digA\ifnum#1=0\relax\xdef\VdigA{~}\fi\fi%
  \substring[q]{#2}{$}{$}%
  \xdef\digB{\thestring}%
  \xdef\VdigB{\digB}\if0\digB\ifnum#2=0\relax\xdef\VdigB{~}\fi\fi%
  \substring[q]{#3}{$}{$}%
  \xdef\digC{\thestring}%
  \xdef\VdigC{\digC}\if0\digC\ifnum#3=0\relax\xdef\VdigC{~}\fi\fi%
}
\newcommand\Nextcolumn[3]{%
  \substring[q]{#1}{1}{$-1}%
  \xdef\opA{0\thestring}%
  \substring[q]{#2}{1}{$-1}%
  \xdef\opB{0\thestring}%
  \substring[q]{#3}{1}{$-1}%
  \xdef\opC{0\thestring}%
}
\begin{document}
\Showsum{16}{7}{39}\qquad
\Showsum{898}{7}{439}\qquad
\Showsum{898032}{70405}{40309}\qquad
\Showsum{7040}{898032}{4030}
\end{document}

enter image description here

ADDENDUM

Here is 2-digit addition that automatically handles decimals or integers:

\documentclass{article}
\usepackage{stringstrings,stackengine}
%%%%%% stringstrings BUG FIX (MISSING %)
\makeatletter
\renewcommand\testmatchingchar[3]{%
  \setbox0=\hbox{%
  \substring[e]{#1}{#2}{#2}\+%
  \isnextbyte[q]{\EscapeChar}{#3}%
  \if T\theresult%
    \isnextbyte[q]{\EscapeChar}{\thestring}%
    \if T\theresult%
      \edef\@testcode{\expandafter\@DiscardNextChar\expandafter{#3}}%
      \edef\@teststring{\@DiscardNextChar{\thestring}}%
      \if \@teststring\@testcode\matchingchartrue\else\matchingcharfalse\fi
    \else
      \global\matchingcharfalse%
    \fi
  \else
    \if \thestring#3\global\matchingchartrue\else\global\matchingcharfalse\fi
  \fi}%
\?}
\makeatother
%%%%%
\edef\decchar{.}
\newcounter{digits}
\newcounter{decrements}
\newcommand\showsum[2]{%
  \setcounter{digits}{0}%
  \xdef\remainder{0}%
  \xdef\Vremainder{~}%
  \let\savesum\relax%
  \whereischar[q]{#1}{\decchar}%
  \xdef\locA{\theresult}%
  \whereischar[q]{#2}{\decchar}%
  \xdef\locB{\theresult}%
  \ifnum\numexpr\locA+\locB=0\relax%
    \def\decloc{-1}%
    \showsumengine{#1}{#2}%
  \else
    \ifnum\locA=0\relax%
      \xdef\numA{#1\decchar}%
      \whereischar[q]{\numA}{\decchar}%
      \xdef\locA{\theresult}%
    \else
      \xdef\numA{#1}%
    \fi
    \stringlength[q]{\numA}%
    \xdef\lenA{\theresult}%
    \substring[q]{\numA}{1}{\numexpr\locA-1\relax}%
    \xdef\preA{\thestring}%
    \substring[q]{\numA}{\numexpr\locA+1\relax}{$}%
    \xdef\postA{\thestring}%
    \ifnum\locB=0\relax%
      \xdef\numB{#2\decchar}%
      \whereischar[q]{\numB}{\decchar}%
      \xdef\locB{\theresult}%
    \else
      \xdef\numB{#2}%
    \fi
    \stringlength[q]{\numB}%
    \xdef\lenB{\theresult}%
    \substring[q]{\numB}{1}{\numexpr\locB-1\relax}%
    \xdef\preB{\thestring}%
    \substring[q]{\numB}{\numexpr\locB+1\relax}{$}%
    \xdef\postB{\thestring}%
    \def\decsA{\numexpr\lenA-\locA}%
    \def\decsB{\numexpr\lenB-\locB}%
    \ifnum\decsA=\decsB%
    \xdef\decloc{\decsA}%
    \else\ifnum\decsA>\decsB%
        \xdef\decloc{\decsA}%
        \setcounter{decrements}{\the\numexpr\decsA-\decsB}%
        \paddecimal{\postB}%
      \else%
        \xdef\decloc{\decsB}%
        \setcounter{decrements}{\the\numexpr\decsB-\decsA}%
        \paddecimal{\postA}%
      \fi%
    \fi%
    \showsumengine{\preA\postA}{\preB\postB}%
  \fi%
}
\newcommand\paddecimal[1]{%
  \xdef#1{#1 }%
  \addtocounter{decrements}{-1}%
  \ifnum\value{decrements}>0\relax\paddecimal{#1}\fi%
}
\newcommand\showsumengine[2]{%
  \ifnum\value{digits}=\decloc\relax\savestack\savesum{%
    \Longstack{. \underline{.} .}\savesum}\fi
  \stepcounter{digits}%
  \xdef\opA{#1}%
  \xdef\opB{#2}%
  \lastdigits{\opA}{\opB}%
  \edef\colsum{\the\numexpr\digA+\digB+0\remainder\relax}%
  \substring[q]{\colsum}{$}{$}%
  \savestack{\thesum}{\ttfamily\Longstack[l]{%
    \tiny\brlap[.7\baselineskip]{\Vremainder} \VdigA{} \underline{\VdigB\,} \thestring}}%
  \substring[q]{\colsum}{1}{$-1}%
  \xdef\remainder{\thestring}%
  \if\relax\remainder\relax\xdef\Vremainder{}\else\xdef\Vremainder{\remainder}\fi%
  \savestack{\savesum}{\thesum\savesum}%
  \nextcolumn{\opA}{\opB}%
  \ifnum\numexpr0\opA+0\opB+0\remainder>0\relax\showsumengine{\opA}{\opB}\else
    \Longstack{\underline{\ttfamily+\,} {}}\savesum\fi%
}
\newcommand\lastdigits[2]{%
  \substring[q]{#1}{$}{$}%
  \xdef\digA{\thestring}%
  \xdef\VdigA{\digA}\if0\digA\ifnum#1=0\relax\xdef\VdigA{~}\fi\fi%
  \substring[q]{#2}{$}{$}%
  \xdef\digB{\thestring}%
  \xdef\VdigB{\digB}\if0\digB\ifnum#2=0\relax\xdef\VdigB{~}\fi\fi%
}
\newcommand\nextcolumn[2]{%
  \substring[q]{#1}{1}{$-1}%
  \xdef\opA{0\thestring}%
  \substring[q]{#2}{1}{$-1}%
  \xdef\opB{0\thestring}%
  \if.\thestring\nextcolumn{\opA}{\opB}\fi%
}
\begin{document}
\showsum{14}{7}\qquad
\showsum{56}{98}\qquad
\showsum{9236}{895}\qquad
\showsum{9236}{825}\bigskip

\showsum{9236}{455}\qquad
\showsum{98669}{985}\qquad
\showsum{98069}{905}\bigskip

\showsum{1.}{3.}\qquad
\showsum{1}{3.96}\qquad
\showsum{.2}{.94}\qquad
\showsum{121.24}{39.7}\qquad
\showsum{94.8}{121.2479}
\end{document}

enter image description here

p.s. I feel a package coming on...

  • Steven, did you ever get that package written? Another cool feature would be to include binary and/or hex arithmetic. Great solution! – Rico Picone Jan 30 '18 at 21:45
  • 3
    @RicoPicone Thank you for your interest, but I have not made the jump into turning this into a package. Perhaps I should reconsider... – Steven B. Segletes Jan 31 '18 at 10:42
  • I think educators like me would find it useful. I'm teaching an embedded computing course in which we explore binary and hex arithmetic. Including those would probably be relatively easy (he said), and would probably find users in CS and engineering education circles. – Rico Picone Feb 01 '18 at 17:50
8

you can use the xlop package

\documentclass[border=5pt,tikz]{article}

\usepackage{xlop}

\begin{document}

\opadd{485,05}{178,4}


\opsub{245.25}{72.003}

\opsub[style=text]{72}{245}

\opsub[carrysub,lastcarry]{72}{245}

\end{document}

enter image description here

rpapa
  • 12,350
  • I had never studied xlop enough to know that it could show carries and borrows. But I have to say, I can't quite wrap my head around the output of \opsub[carrysub,lastcarry]{49}{135}. It is certainly not the way I learned subtraction. But +1 for showing the method anyhow. – Steven B. Segletes May 17 '16 at 16:22