2

I am trying to create a 'matrix' using the array environment. I have multiple of these lines each with text besides it of varying length. I would like to have the array aligned; however, in example below, the differing length of text changes that alignment

\documentclass{article}
\usepackage{gensymb}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{dcolumn}
\newcolumntype{R}{D{.}{.}{2,6}}
\begin{document}
\[
\left[
    \begin{array}{@{}RR|R@{}}
        0.28 &0.72 &45\\
        0.72 &0.28 &55\\
    \end{array}
\right]
test
\]
\[
\left[
    \begin{array}{@{}RR|R@{}}
        0.28 &0.72 &45\\
        0.2016 &0.0784 &15.4\\
    \end{array}
\right]
test1
\]
\[
\left[
    \begin{array}{@{}RR|R@{}}
        0.2016 &0.5184 &32.4\\
        0.2016 &0.0784 &15.4\\
    \end{array}
\right]
test12
\]
\end{document}

I have tried using /hfill and the align/* environment with no success; however, my knowledge of latex is extremely limited.

1

I'd use aligned (and siunitx rather than dcolumn)

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{siunitx}
\newcolumntype{R}[1]{S[table-format=#1]}

\begin{document}
\[
\begin{aligned}
\left[
    \begin{array}{@{}R{2.4}R{2.4}|R{2.1}@{}}
        0.28 &0.72 &45\\
        0.72 &0.28 &55\\
    \end{array}
\right]
&\quad\text{test}
\\
\left[
    \begin{array}{@{}R{2.4}R{2.4}|R{2.1}@{}}
        0.28 &0.72 &45\\
        0.2016 &0.0784 &15.4\\
    \end{array}
\right]
&\quad\text{test1}
\\
\left[
    \begin{array}{@{}R{2.4}R{2.4}|R{2.1}@{}}
        0.2016 &0.5184 &32.4\\
        0.2016 &0.0784 &15.4\\
    \end{array}
\right]
&\quad\text{test12}
\end{aligned}
\]

\end{document}

enter image description here

1

You could place the right-hand text material into a fixed-width \parbox:

enter image description here

The fixed width chosen in the code below is 2cm; you're obviously free to adapt this width to your typesetting needs. Note also that the contents of a \parbox are typeset in text mode by default; this is true even if the \parbox occurs inside math mode.

\documentclass{article}
\usepackage{gensymb}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{dcolumn}
\newcolumntype{R}{D{.}{.}{2,6}}
\begin{document}
\[
\left[
    \begin{array}{@{}RR|R@{}}
        0.28 &0.72 &45\\
        0.72 &0.28 &55\\
    \end{array}
\right]
\parbox{2cm}{test}
\]
\[
\left[
    \begin{array}{@{}RR|R@{}}
        0.28   &0.72   &45\\
        0.2016 &0.0784 &15.4\\
    \end{array}
\right]
\parbox{2cm}{test1}
\]
\[
\left[
    \begin{array}{@{}RR|R@{}}
        0.2016 &0.5184 &32.4\\
        0.2016 &0.0784 &15.4\\
    \end{array}
\right]
\parbox{2cm}{test12}
\]
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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