2

I have a matrix that I would like to be centered in the middle of the page, however it is showing up left-aligned.

\begin{figure}[ht]$
\centering
    \begin{bmatrix}
        0       & C_l       & 0         & 0         & \dots     & \dots     & \dots     & 0         \\
        0       & 0         & C_l       & 0         & \dots     & \dots     & \dots     & \vdots    \\
        0       & C_r       & 0         & C_l       & \dots     & \dots     & \dots     & \vdots    \\
        0       & 0         & C_r       & 0         & \ddots    & \dots     & \dots     & \vdots    \\
        \vdots  & \hdots    & \hdots    & \ddots    & \ddots    & \ddots    & \hdots    & \vdots    \\
        \vdots  & \hdots    & \hdots    & \hdots    & \ddots    & \ddots    & C_l       & 0         \\
        \vdots  & \hdots    & \hdots    & \hdots    & \hdots    & \ddots    & \ddots    & 0         \\
        0       & \hdots    & \hdots    & \hdots    & \hdots    & \hdots    & C_r       & 0         \\
    \end{bmatrix}$
\caption{$n \times n$ transition matrix for a classical random walk.}
\end{figure}

How can I center this matrix?

10
  • Omit the figure environment and use \usepackage{caption} if you need to make legend for that matrix. Change to captionof also. Please post a MWE also.
    – user31729
    Apr 4, 2014 at 16:39
  • \begin{figure}[ht]\centering $\begin{bmatrix}...
    – user31034
    Apr 4, 2014 at 16:41
  • @ferahfeza That's what I already have.
    – sonicboom
    Apr 4, 2014 at 16:43
  • @ChristianH. The caption disappears if I remove the figure.
    – sonicboom
    Apr 4, 2014 at 16:43
  • 1
    Also please always post complete documents (as in the answers) not just a fragment. Apr 4, 2014 at 16:58

3 Answers 3

6

Just move the \centering out of the math material enclosed in $...$.

\documentclass[a4paper]{article}
\usepackage{amsmath}
\begin{document}
\begin{figure}[ht]
\centering
    $\begin{bmatrix}
        0       & C_l       & 0         & 0         & \dots     & \dots     & \dots     & 0         \\
        0       & 0         & C_l       & 0         & \dots     & \dots     & \dots     & \vdots    \\
        0       & C_r       & 0         & C_l       & \dots     & \dots     & \dots     & \vdots    \\
        0       & 0         & C_r       & 0         & \ddots    & \dots     & \dots     & \vdots    \\
        \vdots  & \hdots    & \hdots    & \ddots    & \ddots    & \ddots    & \hdots    & \vdots    \\
        \vdots  & \hdots    & \hdots    & \hdots    & \ddots    & \ddots    & C_l       & 0         \\
        \vdots  & \hdots    & \hdots    & \hdots    & \hdots    & \ddots    & \ddots    & 0         \\
        0       & \hdots    & \hdots    & \hdots    & \hdots    & \hdots    & C_r       & 0         \\
    \end{bmatrix}$
\caption{$n \times n$ transition matrix for a classical random walk.}
\end{figure}
\end{document}
1
  • Sorry, it is working...{embarrassed!}
    – sonicboom
    Apr 4, 2014 at 17:15
4

If you want a figure (floating), replace $\begin{bmatrix} ... $ with \begin{equation*}\begin{bmatrix} ... \end{equation*}:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\begin{figure}[ht]
\centering
\begin{equation*}
    \begin{bmatrix}
        0       & C_l       & 0         & 0         & \dots     & \dots     & \dots     & 0         \\
        0       & 0         & C_l       & 0         & \dots     & \dots     & \dots     & \vdots    \\
        0       & C_r       & 0         & C_l       & \dots     & \dots     & \dots     & \vdots    \\
        0       & 0         & C_r       & 0         & \ddots    & \dots     & \dots     & \vdots    \\
        \vdots  & \hdots    & \hdots    & \ddots    & \ddots    & \ddots    & \hdots    & \vdots    \\
        \vdots  & \hdots    & \hdots    & \hdots    & \ddots    & \ddots    & C_l       & 0         \\
        \vdots  & \hdots    & \hdots    & \hdots    & \hdots    & \ddots    & \ddots    & 0         \\
        0       & \hdots    & \hdots    & \hdots    & \hdots    & \hdots    & C_r       & 0         \\
    \end{bmatrix}
\end{equation*}

\caption{$n \times n$ transition matrix for a classical random walk.}
\end{figure}


\end{document}
0
1

My solution is suited for 'fixed' positioning of content with a caption, i.e. the content should not float around.

\documentclass[10pt]{book}

\usepackage{caption}
\usepackage{amsmath}

\begin{document}

\chapter{My very sophisticated matrix}
\begingroup
\centering%
\(%
    \begin{bmatrix}%
        0       & C_l       & 0         & 0         & \dots     & \dots     & \dots     & 0         \\
        0       & 0         & C_l       & 0         & \dots     & \dots     & \dots     & \vdots    \\
        0       & C_r       & 0         & C_l       & \dots     & \dots     & \dots     & \vdots    \\
        0       & 0         & C_r       & 0         & \ddots    & \dots     & \dots     & \vdots    \\
        \vdots  & \hdots    & \hdots    & \ddots    & \ddots    & \ddots    & \hdots    & \vdots    \\
        \vdots  & \hdots    & \hdots    & \hdots    & \ddots    & \ddots    & C_l       & 0         \\
        \vdots  & \hdots    & \hdots    & \hdots    & \hdots    & \ddots    & \ddots    & 0         \\
        0       & \hdots    & \hdots    & \hdots    & \hdots    & \hdots    & C_r       & 0         \\
    \end{bmatrix}%
\)%
\captionof{figure}{$n \times n$ transition matrix for a classical random walk.}%
\endgroup%
\end{document}

enter image description here

4
  • I like this....
    – user31034
    Apr 4, 2014 at 16:46
  • 1
    You may want to localise the effect of \centering with a group, or just use a displayed math environment instead. Apr 4, 2014 at 16:59
  • @TorbjørnT. Yes, you are right, but I want to keep my 'solution' similar to the post.
    – user31729
    Apr 4, 2014 at 17:02
  • Why is it still showing up left aligned on mine...and it's also complaining about missing math symbols when I use your code. Edit: Sorry, it is working...{embarrassed!}
    – sonicboom
    Apr 4, 2014 at 17:17

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.