Math & ScienceLong Addition in Base 16

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
ewald
Posts: 2
Joined: Tue Sep 13, 2011 11:58 pm

Long Addition in Base 16

Post by ewald »

I am trying to do long addition in base 16, and I wish to show my steps in Latex. That is, I want to align the two numbers I'm adding, so that each digit lines up (which is difficult because the letters and numbers are different sizes), and I want to be able to show my carries in addition by putting a small number above the top number and borrows in subtraction by putting a cross through the digit on the top number and again adding a smaller number in the neighboring column. Any help would be appreciated. This is all I currently have:

Code: Select all

\begin{align*}
	\mathrm{ABBABA_{16}}&\\
	+ \quad \quad \mathrm{981CD_{16}}&\\
	\line(1,0){80}\\
	\mathrm{B64C98_{16}}&
\end{align*}
I'm relatively new to Latex, so any help would be appreciated. Thanks much!
Last edited by cgnieder on Sun Feb 03, 2013 1:45 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Long Addition in Base 16

Post by localghost »

Please get used to always providing a minimal example that is compilable out of the box. This is essential when a problem is more complicated. And please use the »Code« environment when posting code to keep your post clear an legible.

Modify your approach slightly and you will get what you want.

Code: Select all

\documentclass[11pt]{article}
\usepackage{mathtools}

\begin{document}
  \begin{alignat*}{2}
    &&\text{ABBABA}_{16} \\
    &+\quad& \text{981CD}_{16} \\[-2\jot] \cline{1-3}
    &&\text{B64C98}_{16}
  \end{alignat*}
\end{document}
You probably know the \cline command only from the array or tabular environment, respectively. It works here as well because the align(at)* environment is a similar structure. The negative space is just for adjustment.


Best regards and welcome to the board
Thorsten
ewald
Posts: 2
Joined: Tue Sep 13, 2011 11:58 pm

Re: Long Addition in Base 16

Post by ewald »

Thank you much for the timely and helpful response - I will keep in mind the guidelines for future posts.

I implemented your code, and it indeed made the addition look how I wanted it to. However, I'm still trying to "show my work" for "carrying" in long addition (e.g. 8 + 9 = 7 with a carry of 1, in base 10). I want to put a small "1" above each column where there is a carry for addition. Similarly for subtraction, I want to be able to put a diagonal cross through the digit from which I am borrowing (e.g. 15 - 8 in base 10, the 5 "borrows" 10 from the 10's place digit), and to also put a small number above a column. Is there a way to achieve these two things?

I don't have any code for this - I couldn't find anything online. All I have is what you have above.

Thanks again.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Long Addition in Base 16

Post by localghost »

In this case I think you are better with an array environment. But since I have no idea what you are after I can't provide a code sample at the moment. Perhaps you can give a visual example.
Post Reply