# Alingments in bordermatrix

I have a bordematrix with fractions and I want to adjust the alingment because numbers are overlaping. How can I do it? The example is this one:

\bordermatrix{~ & 1000 & 1001 & 1010 & 1011  & 1100 & 1101 & 1110 & 1111 \cr
A= & \frac{1}{8} & \frac{1}{8} & 0 & \frac{-1}{2} & \frac{1}{4} & \frac{1}{4} & \frac{1}{8} & \frac{-3}{8}  \cr
B= & \frac{-1}{4} & \frac{-1}{4} & \frac{-1}{4} & \frac{1}{4} & \frac{1}{8} & \frac{-7}{8} & \frac{1}{8}  & \frac{-3}{8} \cr
C= & \frac{1}{8} & \frac{1}{8} & \frac{1}{4} & \frac{1}{4} & \frac{-3}{8} & \frac{5}{8} & \frac{-1}{4} & \frac{3}{4} \cr }


and it looks like this:

• Welcome to TeX.SX! Align right?(of entries of the matrix) – Bobyandbob Nov 21 '17 at 15:01
• Yes, align of entries of the matrix. – Petra Nov 21 '17 at 15:04
• Problem solved? If yes, then you can accept my answer. – Dr. Manuel Kuehner Nov 21 '17 at 20:53

• Maybe not the cleverest solution.
• I provide a step-by-step guide to illustrate my approach.
• Basically, I use an invisible \rule that occupies only vertical space.

\documentclass{article}

\begin{document}

$$\bordermatrix{~ & 1000 & 1001 & 1010 & 1011 & 1100 & 1101 & 1110 & 1111 \cr A= & \frac{1}{8} & \frac{1}{8} & 0 & \frac{-1}{2} & \frac{1}{4} & \frac{1}{4} & \frac{1}{8} & \frac{-3}{8} \cr B= & \frac{-1}{4} & \frac{-1}{4} & \frac{-1}{4} & \frac{1}{4} & \frac{1}{8} & \frac{-7}{8} & \frac{1}{8} & \frac{-3}{8} \cr C= & \frac{1}{8} & \frac{1}{8} & \frac{1}{4} & \frac{1}{4} & \frac{-3}{8} & \frac{5}{8} & \frac{-1}{4} & \frac{3}{4} \cr }$$

% taken from https://www.tug.org/~hvoss/PDF/mathmode.pdf chapter 1.5
$$\bordermatrix{% & 0 & 1 & 2 \cr 0 & A & B & C \cr 1 & d & e & f \cr 2 & 1 & 2 & 3 \cr }$$

$$\bordermatrix{% & 0 & 1 & 2 \cr 0 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \cr 1 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \cr 2 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \cr }$$

$$\bordermatrix{% & 1000 & 1000 & 1000 \cr 0 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \cr 1 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \cr 2 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \cr }$$

$$\bordermatrix{% & 1000 & 1000 & 1000 \cr 0 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \rule[-4pt]{1pt}{14pt} \cr 1 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \rule[-4pt]{1pt}{14pt} \cr 2 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \rule[-4pt]{1pt}{14pt} \cr }$$

$$\bordermatrix{% & 1000 & 1000 & 1000 \cr 0 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \rule[-4pt]{0pt}{14pt} \cr 1 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \rule[-4pt]{0pt}{14pt} \cr 2 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \rule[-4pt]{0pt}{14pt} \cr }$$

$$\bordermatrix{% & 1000 & 1001 & 1010 & 1011 & 1100 & 1101 & 1110 & 1111 \cr A= & \frac{1}{8} & \frac{1}{8} & 0 & \frac{-1}{2} & \frac{1}{4} & \frac{1}{4} & \frac{1}{8} & \frac{-3}{8} \rule[-6pt]{1pt}{18pt} \cr B= & \frac{-1}{4} & \frac{-1}{4} & \frac{-1}{4} & \frac{1}{4} & \frac{1}{8} & \frac{-7}{8} & \frac{1}{8} & \frac{-3}{8} \rule[-6pt]{1pt}{18pt} \cr C= & \frac{1}{8} & \frac{1}{8} & \frac{1}{4} & \frac{1}{4} & \frac{-3}{8} & \frac{5}{8} & \frac{-1}{4} & \frac{3}{4} \rule[-6pt]{1pt}{18pt} \cr }$$

$$\bordermatrix{% & 1000 & 1001 & 1010 & 1011 & 1100 & 1101 & 1110 & 1111 \cr A= & \frac{1}{8} & \frac{1}{8} & 0 & \frac{-1}{2} & \frac{1}{4} & \frac{1}{4} & \frac{1}{8} & \frac{-3}{8} \rule[-6pt]{0pt}{18pt} \cr B= & \frac{-1}{4} & \frac{-1}{4} & \frac{-1}{4} & \frac{1}{4} & \frac{1}{8} & \frac{-7}{8} & \frac{1}{8} & \frac{-3}{8} \rule[-6pt]{0pt}{18pt} \cr C= & \frac{1}{8} & \frac{1}{8} & \frac{1}{4} & \frac{1}{4} & \frac{-3}{8} & \frac{5}{8} & \frac{-1}{4} & \frac{3}{4} \rule[-6pt]{0pt}{18pt} \cr }$$

\end{document}


In addition, I suggest prettifying the negative fractions using an invisible space with the size of a minus sign.

\documentclass{article}

\begin{document}

\section*{Ugly}

$$\bordermatrix{% & 0 & 1 & 2 \cr 0 & \frac{1}{-1} & \frac{1}{1} & \frac{1}{1} \cr 1 & \frac{1}{1} & \frac{-1}{1} & \frac{1}{1} \cr 2 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \cr }$$

\section*{Nice}

$$\bordermatrix{% & 0 & 1 & 2 \cr 0 & \frac{\phantom{-}1}{-1} & \frac{1}{1} & \frac{1}{1} \cr 1 & \frac{1}{1} & \frac{-1}{\phantom{-}1} & \frac{1}{1} \cr 2 & \frac{1}{1} & \frac{1}{1} & \frac{1}{1} \cr }$$

\end{document}


• Nice step-by-step guide. I think its very hepful. – Bobyandbob Nov 21 '17 at 16:09

You could use blkarray package for the border matrix and \dfrac instead of \frac to enlarge the fractions a bit.

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}
\usepackage{blkarray}

\begin{document}
\renewcommand{\arraystretch}{2.5}
$\begin{blockarray}{r*8c} & 1000 & 1001 & 1010 & 1011 & 1100 & 1101 & 1110 & 1111 \\ \begin{block}{r\{*8c\}} A= & \dfrac{1}{8} & \dfrac{1}{8} & 0 & \dfrac{-1}{2} & \dfrac{1}{4} & \dfrac{1}{4} & \dfrac{1}{8} & \dfrac{-3}{8} \\ B= & \dfrac{-1}{4} & \dfrac{-1}{4} & \dfrac{-1}{4} & \dfrac{1}{4} & \dfrac{1}{8} & \dfrac{-7}{8} & \dfrac{1}{8} & \dfrac{-3}{8} \\ C= & \dfrac{1}{8} & \dfrac{1}{8} & \dfrac{1}{4} & \dfrac{1}{4} & \dfrac{-3}{8} & \dfrac{5}{8} & \dfrac{-1}{4} & \dfrac{3}{4} \\[1ex] \end{block} \end{blockarray}$
\renewcommand{\arraystretch}{1}% to modify \arraystretch only for the above matrix
\end{document}


Edit: a little improvement inspired by Heiko Oberdiek's answer:

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}
\usepackage{blkarray}
\newcommand{\myp}{\phantom{-}}

\begin{document}
\renewcommand{\arraystretch}{2.5}
$\begin{blockarray}{r*8c<{\hspace{2pt}}} & 1000 & 1001 & 1010 & 1011 & 1100 & 1101 & 1110 & 1111\\ \begin{block}{r\{*8c<{\hspace{2pt}}\}} A= & \myp\dfrac{1}{8} & \myp\dfrac{1}{8} & \myp0 & -\dfrac{1}{2} & \myp\dfrac{1}{4} & \myp\dfrac{1}{4} & \myp\dfrac{1}{8} & -\dfrac{3}{8} \\ B= & -\dfrac{1}{4} & -\dfrac{1}{4} & -\dfrac{1}{4} & \myp\dfrac{1}{4} & \myp\dfrac{1}{8} & -\dfrac{7}{8} & \myp\dfrac{1}{8} & -\dfrac{3}{8} \\ C= & \myp\dfrac{1}{8} & \myp\dfrac{1}{8} & \myp\dfrac{1}{4} & \myp\dfrac{1}{4} & -\dfrac{3}{8} & \myp\dfrac{5}{8} & -\dfrac{1}{4} & \myp\dfrac{3}{4} \\[1ex] \end{block} \end{blockarray}$
\renewcommand{\arraystretch}{1}% to modify \arraystretch only for the above matrix
\end{document}


Second edit: as required by pzorba75 in his comment, the following is a solution with a macro for fractions adding a \phantom{-} when the numerator is positive.

Of course, it could be improved testing both the numerator and denominator signs, and I'm sure some TeXpert should have done it better.

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}
\usepackage{blkarray}
\usepackage{ifthen}
\newcommand{\myp}{\phantom{-}}
\newcommand{\myfrac}[2]{%
\ifthenelse{#1<0}{% numerator < 0
-\dfrac{\the\numexpr#1*-1\relax}{#2}%
}{% numerator >= 0
\myp\dfrac{#1}{#2}%
}%
}

\begin{document}
\renewcommand{\arraystretch}{2.5}
$\begin{blockarray}{r*8c<{\hspace{2pt}}} & 1000 & 1001 & 1010 & 1011 & 1100 & 1101 & 1110 & 1111\\ \begin{block}{r\{*8c<{\hspace{2pt}}\}} A= & \myfrac{1}{8} & \myfrac{1}{8} & \myp0 & \myfrac{-1}{2} & \myfrac{1}{4} & \myfrac{1}{4} & \myfrac{1}{8} & \myfrac{-3}{8} \\ B= & \myfrac{-1}{4} & \myfrac{-1}{4} & \myfrac{-1}{4} & \myfrac{1}{4} & \myfrac{1}{8} & \myfrac{-7}{8} & \myfrac{1}{8} & \myfrac{-3}{8} \\ C= & \myfrac{1}{8} & \myfrac{1}{8} & \myfrac{1}{4} & \myfrac{1}{4} & \myfrac{-3}{8} & \myfrac{5}{8} & \myfrac{-1}{4} & \myfrac{3}{4} \\[1ex] \end{block} \end{blockarray}$
\renewcommand{\arraystretch}{1}% to modify \arraystretch only for the above matrix
\end{document}


The result is, obviously, the same as the previous one.

• Much better than mine, +1 – Dr. Manuel Kuehner Nov 22 '17 at 21:20
• @Dr.ManuelKuehner Thank you! I think it could also be improved with some \phantoms like in Heiko Oberdiek's amswer (that is writing \phantom{-}\dfrac... for positive fractions and -\dfrac... for negative ones. – CarLaTeX Nov 22 '17 at 21:27
• I also used phantom's :( (just kidding) – Dr. Manuel Kuehner Nov 22 '17 at 21:39
• @Dr.ManuelKuehner You're right, I didn't noticed it :) – CarLaTeX Nov 22 '17 at 21:43
• @pzorba75 Done, see my second edit. – CarLaTeX Nov 23 '17 at 6:39

The following example shows an approach with "horizontal" fractions with the slash symbol. This avoids the problems with the vertical space. The digits are not reduced in size.

In the second matrix, the positive fractions are preceded by the space of a minus sign to improve the vertical alignment and increase the readability.

\documentclass{article}
\begin{document}
$\bordermatrix{~ & 1000 & 1001 & 1010 & 1011 & 1100 & 1101 & 1110 & 1111 \cr A = & 1/8 & 1/8 & 0 & -1/2 & 1/4 & 1/4 & 1/8 & -3/8 \cr B = & -1/4 & -1/4 & -1/4 & 1/4 & 1/8 & -7/8 & 1/8 & -3/8 \cr C = & 1/8 & 1/8 & 1/4 & 1/4 & -3/8 & 5/8 & -1/4 & 3/4 \cr }$
$\def\M{\hphantom{-}} \bordermatrix{~ & 1000 & 1001 & 1010 & 1011 & 1100 & 1101 & 1110 & 1111 \cr A = & \M1/8 & \M1/8 & \M0 & -1/2 & \M1/4 & \M1/4 & \M1/8 & -3/8 \cr B = & -1/4 & -1/4 & -1/4 & \M1/4 & \M1/8 & -7/8 & \M1/8 & -3/8 \cr C = & \M1/8 & \M1/8 & \M1/4 & \M1/4 & -3/8 & \M5/8 & -1/4 & \M3/4 \cr }$
\end{document}