1

do anyone of you knows the codes for subtracting two whole numbers with borrowing/carry?

I have tried

$$\begin{aligned}
296 \\
- \\
129 \\\hline 
167
\end{aligned}$$
Mensch
  • 65,388
jenn kim
  • 11
  • 1
  • 1
    Welcome to TeX.SE! – Mensch Nov 25 '21 at 16:30
  • 4
    Welcome! Can you be a bit more specific about what you want to achieve? And can you please include a complete document, starting with \documentclass and ending with \end{document}? There are several ways of doing calculations in LaTeX automatically. Which solution is best depends on your setting (that we don't know). Maybe related: https://tex.stackexchange.com/questions/87824/how-to-do-simple-calculation-in-latex, https://tex.stackexchange.com/questions/167222/perform-simple-calculations-on-user-defined-variables – gernot Nov 25 '21 at 17:03

1 Answers1

1

If you're willing and able to use LuaLaTeX, the following solution may be of interest to you:

enter image description here

\documentclass{article}
\usepackage{luacode} % for "\luaexec" macro
\luaexec{%
    function dosubtraction ( n1 , n2 )
       tex.sprint ( "\\begin{array}{@{}r@{}}" )
       tex.sprint ( n1 .. "\\\\ \\llap{$-$}" ..  n2 .. "\\\\ \\hline" ) 
       tex.sprint ( n1-n2 )
       tex.sprint ( "\\end{array}" )
    end
}
% LaTeX "wrapper" macro to access the 'dosubtracton' Lua function:
\newcommand\dosubtraction[2]{\luaexec { dosubtraction ( #1 , #2 ) }}

\begin{document}

[ \dosubtraction{2}{1} \qquad \dosubtraction{296}{129} \qquad \dosubtraction{1961}{2021} ]

\end{document}

Mico
  • 506,678