Graphics, Figures & TablesAlign matrices and multi-line text

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
tonyjnel
Posts: 11
Joined: Thu May 12, 2011 3:51 pm

Align matrices and multi-line text

Post by tonyjnel »

I want to align these large blocks of stuff and I was wondering what the best way to do it is. I essentially want four columns: the first will be a multi-line description of the operation, the second will be a matrix, the third will be another multi-line description, and the fourth will be another matrix. I tried making minipages but it didn't react like I thought it would. Is there an aligning environment or command that will simplify this?

Here is my first attempt (which was an abysmal failure):

Code: Select all

\documentclass[12pt]{article}
\begin{document}
\begin{center}
\begin{minipage}{2cm}
{}
\end{minipage}
%
\begin{minipage}{1.4in}
\[
\left(\begin{array}{ccc|c}
2&6&1&7\\
1&2&-1&-1\\
5&7&-4&9\end{array}\right)
\]
\end{minipage}
%
\begin{minipage}{2cm}
\begin{center}
$R_{12}$ \\
$\Longrightarrow$
\end{center}
\end{minipage}
%
\begin{minipage}{1.4in}
\[
\left(\begin{array}{ccc|c}
1&2&-1&-1\\
2&6&1&7\\
5&7&-4&9\end{array}\right)
\]
\end{minipage} \\
%
%
\begin{minipage}{2cm}
\begin{center}
$-2R_{1}+R_2$\\
$-5R_1+R_3$\\
$\Longrightarrow$
\end{center}
\end{minipage}
%
\begin{minipage}{1.4in}
\[
\left(\begin{array}{ccc|c}
1&2&-1&-1\\
0&2&3&9\\
0&-3&1&14\end{array}\right)
\]
\end{minipage}
%
\begin{minipage}{2cm}
\begin{center}
$\frac{1}{2} R_{2}$ \\
$\Longrightarrow$
\end{center}
\end{minipage}
%
\begin{minipage}{1.4in}
\[
\left(\begin{array}{ccc|c}
1&2&-1&-1\\
0&1&\frac{3}{2}&\frac{9}{2}\\
0&-3&1&14\end{array}\right)
\]
\end{minipage}\end{center}
\end{document}
I thought I would get some input from some people with experience before I just started trying different methods of aligning it. I want to align the first column at the \Longarrow, the second column at the \right), the third column at the \Longarrow, and the fourth column at the \left(.

Thanks!

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

Align matrices and multi-line text

Post by localghost »

For math content you should choose math structures for alignment.

Code: Select all

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}  % loads »amsmath«

\begin{document}
  \begin{alignat*}{3}
    &&  &
    \left(\begin{array}{ccc|c}
      2&6&1&7\\
      1&2&-1&-1\\
      5&7&-4&9
    \end{array}\right) &
    \overset{\substack{R_{12}\\\mathstrut}}{\Longrightarrow}\quad &
    \left(\begin{array}{ccc|c}
      1&2&-1&-1\\
      2&6&1&7\\
      5&7&-4&9
    \end{array}\right) \\
    && \overset{\substack{-2R_1+R_2\\ -5R_1+R_3\\\mathstrut}}{\Longrightarrow}\quad &
    \left(\begin{array}{ccc|c}
      1&2&-1&-1\\
      0&2&3&9\\
      0&-3&1&14
    \end{array}\right)\quad &
    \overset{\substack{\frac{1}{2}\,R_2\\\mathstrut}}{\Longrightarrow}\quad &
    \left(\begin{array}{ccc|c}
      1&2&-1&-1\\
      0&1&\frac{3}{2}&\frac{9}{2}\\
      0&-3&1&14
    \end{array}\right)
  \end{alignat*}
\end{document}

Thorsten
tonyjnel
Posts: 11
Joined: Thu May 12, 2011 3:51 pm

Align matrices and multi-line text

Post by tonyjnel »

Thanks localghost, alignat works perfectly for this. I had tried using it before but with no success. Substack was the key ingredient I was missing. This is what I ended up with:

Code: Select all

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}
\begin{document}
\begin{alignat*}{4}
    &&  
    \left(\begin{array}{ccc|c}
      2&6&1&7\\
      1&2&-1&-1\\
      5&7&-4&9
    \end{array}\right)\quad &&
    \substack{R_{12}\vspace{.1cm}\\\Longrightarrow}\quad &&
    &\left(\begin{array}{ccc|c}
      1&2&-1&-1\\
      2&6&1&7\\
      5&7&-4&9
    \end{array}\right) \\
     \substack{-2R_1+R_2\\ -5R_1+R_3\vspace{.1cm}\\\Longrightarrow}\quad &&
    \left(\begin{array}{ccc|c}
      1&2&-1&-1\\
      0&2&3&9\\
      0&-3&1&14
    \end{array}\right)\quad &&
    \substack{\frac{1}{2}\,R_2\vspace{.1cm}\\\Longrightarrow}\quad &&&
    \left(\begin{array}{ccc|c}
      1&2&-1&-1\\
      0&1&\frac{3}{2}&\frac{9}{2}\\
      0&-3&1&14
    \end{array}\right)
  \end{alignat*}
\end{document}
This may not really be a question for this portion of the forum but I am not sure why you included these commands in the preamble:

Code: Select all

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
A google search gave me the impression that they were important for properly rendering special characters like letters with umlauts or accents. Should I just include them in every document?
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Align matrices and multi-line text

Post by localghost »

tonyjnel wrote:[…] This may not really be a question for this portion of the forum but I am not sure why you included these commands in the preamble:

Code: Select all

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
A google search gave me the impression that they were important for properly rendering special characters like letters with umlauts or accents. Should I just include them in every document?
An old habit of mine. These packages won't do any harm. Both are most useful when writing documents that are not in English.

The first one enables T1 font encoding for use with Postscript Type 1 fonts. It also enhances hyphenation in other languages than English because special characters (e. g. umlauts) are no longer made up of the known characters and accents (see below).

The second package allows inserting these characters directly in the source code instead of generating them by control sequences (ä instead of \"{a}). You can at least keep the first package if writing in English. For details refer to the package manuals.
Post Reply