Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am having some trouble in displaying characters in LaTeX. Here is what I am doing.

I am performing a binary addition and would like to have all the characters aligned one below the other.

Here is the small example :

1010
1000 
----
0010

However, the problem I am facing is that the characters are not aligned in a vertical line. If it was a table, I could align left-edge or right edge, but not here.

I would prefer, here, 1-1-0, 0-0-0, 1-0-1, 0-0-0 are aligned in a single line vertically.

I am trying to give proper spacing, but it does not look elegant in the pdf. It looks misaligned with a long sequence of binary number.

P.S : Attached is the screen shot of the pdf file. enter image description here

\documentclass[twoside,english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\setcounter{secnumdepth}{2}
\setcounter{tocdepth}{2}
\usepackage{babel}
\begin{document}
\newdimen\digitwidth 
\settowidth\digitwidth{0} 
\def~{\hspace{\digitwidth}}
\def\divrule#1#2{% 
\noalign{\moveright#1\digitwidth% 
\vbox{\hrule width#2\digitwidth}}} 
\begin{tabular}[b]{@{}r@{}} 
%10010 
\\ \hline 
\big|\begin{tabular}[t]{@{}l@{}} 
1010 1111\\ 
1000 1000 \\ 
\divrule{0}{10} 
~~ 1000 1000 \\ 
\end{tabular} 
\end{tabular}
\end{document}
share|improve this question
    
Did you try using a fixed width font? –  Peter Grill Sep 29 '11 at 20:09
3  
Characters that are aligned on a vertical line share the same horizontal alignment. (And yes, it took me some time to figure this out. :-)) –  lockstep Sep 29 '11 at 20:10
    
Yes, I am using the default font. –  www.sapnaedu.in Sep 29 '11 at 20:11
add comment

4 Answers

Perhaps defining a new environment

\newlength{\digitwidth}
\newcommand\divrule[2]{\noalign{\moveright#1\digitwidth\vbox{\hrule width#2\digitwidth}}}
\newenvironment{bdivision}[1]
  {\settowidth\digitwidth{0}\setlength{\tabcolsep}{\digitwidth}%
   \def~{\hspace{\digitwidth}}%
   \hphantom{#1~\vrule}\begin{tabular}{l@{}}
   \hline\relax\llap{#1~\vrule\strut\hspace{\tabcolsep}\kern-0.4pt}\ignorespaces}
  {\end{tabular}}

and inputting data as

\begin{bdivision}{10010}
1010~1111~1111~1111~0000~0010\\
1000~1000~0001~0000~1 \\
\divrule{1}{21}
~~10~0111~1110~1111~100 \\
~~10~0010~0000~0100~001 \\
\divrule{3}{27}
~~~~~~101~1110~1011~1010~00
\end{bdivision}

(the digits are almost at random, but this should be the idea).

enter image description here

share|improve this answer
    
Very nice and intuitive to use! –  Werner Sep 29 '11 at 23:18
    
Amazing, works really nice :) –  www.sapnaedu.in Sep 30 '11 at 8:08
add comment

Update (old solution moved to bottom)

Now that I MWE was provided, I think one way to do this is to adapt the solution from Visualization in LaTeX of hamming distance which used the listings package and define a style that iterates over each character.

Each character is set in the width of the digit 0, and other properties can be adjusted based on the character, such as changing the color of the digits separately if you want. For normal math purposes you can just remove the color change code.

One problem that comes about is the leading spacing as TeX wants to gobble that up. For this, I adapted your solution by replacing a ~ with a space of the appropriate width. This only needs to be inserted for leading spaces. Any spaces after the first binary digit can just be a normal space (or a ~).

With this solution, the use of the \ttfamily is optional, since each digit (and space) is set to be the width of a 0, but the output looked the better with \ttfamily and the image shown reflects that.

enter image description here

Here is the modified version of your code:

\documentclass[twoside,english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{babel}

\usepackage{listings}
\usepackage{xcolor}% can remove this if you don't want color in output

\setcounter{secnumdepth}{2}
\setcounter{tocdepth}{2}

\newdimen\digitwidth%
\settowidth\digitwidth{0}%
\def~{\hspace{\digitwidth}}%
\def\divrule#1#2{%
\noalign{\moveright#1\digitwidth% 
\vbox{\hrule width#2\digitwidth}}}%

\newcommand*{\Resize}[1]{\makebox[\digitwidth]{#1}}%
\lstdefinestyle{BinaryNumber}{%
    literate={1}{\textcolor{blue}{\Resize{1}}}{1}%
             {0}{\textcolor{red}{\Resize{0}}}{1}%
             {,}{\Resize{\phantom{ }}}{1}%
             {\ }{\Resize{\phantom{ }}}{1},%
   basicstyle=\ttfamily,%  Optional to use this
}
\newcommand{\Binary}[1]{%
    \lstinline[style=BinaryNumber]{#1}%
}


\begin{document}
\Binary{10010}
\begin{tabular}[b]{@{}r@{}} 
\\ \hline 
\big|%
\begin{tabular}[t]{@{}l@{}} 
\Binary{1010 1111}\\ 
\Binary{1000 1000}\\ 
\divrule{0}{10} 
\Binary{,,,1 0000 1000} \\ 
\end{tabular} 
\end{tabular}
\end{document}

Old Solution:

Does this produces the results you ask for:

\documentclass{article} 
\begin{document}
With textt:
\texttt{\newline%
1010\newline
1000\newline
----\newline
0010\newline
}
\end{document} 

enter image description here

share|improve this answer
    
Yes it does, But, if the binary sequence has a space in between, the alignment is lost :( Please have a look at the screen shot –  www.sapnaedu.in Sep 29 '11 at 20:36
    
It'd be easier if you would just post a MWE. –  Peter Grill Sep 29 '11 at 20:44
    
Adding spaces does not seem to effect the example code provided here. Things still line up. It appears from your new image that the issue is with the vertical line shifting the alignment, not the characters. If you post a MWE can help more. –  Peter Grill Sep 29 '11 at 21:29
1  
Definitely, I am attaching the working example –  www.sapnaedu.in Sep 29 '11 at 22:17
1  
Thanks. Now that I know what you want I can proceed. I have an idea for a good solution for you... –  Peter Grill Sep 29 '11 at 22:50
show 2 more comments

You have a couple of options here:

  • Using a tabular; yes, this is possible:

    \documentclass{article}
    \begin{document}
    \begin{tabular}{*{4}{@{}c@{}}}% 4 centered columns without interspaced gaps
      1&0&1&0 \\
      1&0&0&0 \\ \hline
      0&0&1&0
    \end{tabular}
    \end{document}
    

    tabular for binary addition

    Extending this to incorporate more than just a 4-bit binary addition is also possible. Using the array package to define a new column type B, we have:

    \documentclass{article}
    \usepackage{array}% http://ctan.org/pkg/array
    \newcolumntype{B}{*{4}{@{}c@{}}}% One byte/4 bits
    \begin{document}
    \begin{tabular}{B@{~}B@{~}B@{~}B@{~}B}% 5 centered bytes with ~ as gaps
      1&0&1&0 & 1&1&1&1 & 1&1&1&1 & 1&1&1&1 & 0&0&0&0 \\
      1&0&0&0 & 1&0&0&0 & 0&0&0&1 & 0&0&0&0 & 1&1&0&1 \\ \hline
      0&0&1&1 & 1&0&0&0 & 0&0&0&0 & 1&1&1&1 & 1&1&0&1
    \end{tabular}
    \end{document}
    

    larger binary addition

  • Or you could fix the character width by using a mono-spaced font:

    \documentclass{article}
    \begin{document}
    {\noindent\ttfamily
      1010 \\
      1000 \\
      ---- \\
      0010
    }
    \end{document}
    

    mono-spaced font "table"

    You could also use this mono-spaced in a tabular for better horizontal line representation, thereby combining the aforementioned examples:

    \documentclass{article}
    \begin{document}
    \ttfamily\begin{tabular}{@{}c@{}}
      1010 \\
      1000 \\ \hline
      0010
    \end{tabular}
    \end{document}
    

    mono-spaced tabular

Similar extensions to larger binary additions are also possible for the last two examples that use mono-spaced \ttfamily.

share|improve this answer
    
Thank you so much, I am trying you first approach. If I insert just space, the binary sequences are not aligned on a vertical line. with & character, it is working pretty fine. Thank you. BTW, I tried with "~" as well. I am not an expert in this. –  www.sapnaedu.in Sep 29 '11 at 20:29
    
Inserting a space at the end would cause a problem, for sure. That is because you're increasing the width of the final element. You would have to modify the tabular column specification for this. I'll update my answer to include something like this, for what it's worth. –  Werner Sep 30 '11 at 5:11
add comment

I'm not sure what you want, so here's some suggestions.

Matrices

Maybe you'll be happy using matrices as in:

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\[
\begin{matrix}
  1 & 0 & 1 & 0 \\
  1 & 0 & 0 & 0 \\
  \hline
  0 & 0 & 1 & 0
\end{matrix}
\]

\end{document}

A matrix

If you want more options check out mathtools.

Verbatim

The following is probably not what you want and it's not pretty. The idea is just throw everything in verbatim.

\documentclass{article}

\usepackage{verbatim}

\begin{document}

\begin{verbatim}
1010
1000 
----
0010
\end{verbatim}

\end{document}

Verbatim digits

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.