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 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?

share|improve this question
    
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. –  Christian Hupfer Apr 4 at 16:39
    
\begin{figure}[ht]\centering $\begin{bmatrix}... –  ferahfeza Apr 4 at 16:41
    
@ferahfeza That's what I already have. –  sonicboom Apr 4 at 16:43
    
@ChristianH. The caption disappears if I remove the figure. –  sonicboom Apr 4 at 16:43
1  
Also please always post complete documents (as in the answers) not just a fragment. –  David Carlisle Apr 4 at 16:58

3 Answers 3

up vote 6 down vote accepted

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}
share|improve this answer
    
Sorry, it is working...{embarrassed!} –  sonicboom Apr 4 at 17:15

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}
share|improve this answer
    
Sorry, it is working...{embarrassed!} –  sonicboom Apr 4 at 17:14

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

\documentclass[10pt]{scrbook}

\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

share|improve this answer
    
I like this.... –  ferahfeza Apr 4 at 16:46
    
You may want to localise the effect of \centering with a group, or just use a displayed math environment instead. –  Torbjørn T. Apr 4 at 16:59
    
@TorbjørnT. Yes, you are right, but I want to keep my 'solution' similar to the post. –  Christian Hupfer Apr 4 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 at 17:17

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.