Graphics, Figures & TablesTable centering annoyance

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
BenBradley
Posts: 1
Joined: Tue Feb 21, 2012 9:20 pm

Table centering annoyance

Post by BenBradley »

Hi, I am new to Latex and have really enjoyed using it so far, I have however come across my first annoyance. I am trying to use a table to display 3 matrices with a label under each. My code is:

Code: Select all

\begin{tabularx}{\textwidth}{ X X X }
  \centering
 $\begin{matrix}
  2 & 0 & 0 & 0 & 0 & 0 \\
  0 & 2 & 0 & 0 & 0 & 0 \\
  0 & 0 & 2 & 0 & 0 & 0 \\
  0 & 0 & 0 & 2 & 0 & 0 \\
  0 & 0 & 0 & 0 & 2 & 0 \\
  0 & 0 & 0 & 0 & 0 & 2
 \end{matrix} $ \\ & 
 \centering
 $\begin{matrix}
  0 & 1 & 0 & 0 & 0 & 1 \\
  1 & 0 & 1 & 0 & 0 & 0 \\
  0 & 1 & 0 & 1 & 0 & 0 \\
  0 & 0 & 1 & 0 & 1 & 0 \\
  0 & 0 & 0 & 1 & 0 & 1 \\
  1 & 0 & 0 & 0 & 1 & 0
 \end{matrix} $ \\ &
 %\centering
 $\begin{matrix}
  2 & 1 & 0 & 0 & 0 & 1 \\
  1 & 2 & 1 & 0 & 0 & 0 \\
  0 & 1 & 2 & 1 & 0 & 0 \\
  0 & 0 & 1 & 2 & 1 & 0 \\
  0 & 0 & 0 & 1 & 2 & 1 \\
  1 & 0 & 0 & 0 & 1 & 2
 \end{matrix} $ \\ 
  \centering
  \textit{Degree Matrix}  & 
  \centering
  \textit{Adjacency Matrix} &
  \centering
  \textit{Laplacian Matrix} \\
\end{tabularx}
It works fine however I cannot get the commented out \centering for the 1st row 3 column to work correctly. When I uncomment it, I get the error message:
! Extra alignment tab has been changed to \cr.
I have tried a number of things none of which work, any help would be greatly appreciated.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
Stefan Kottwitz
Site Admin
Posts: 10350
Joined: Mon Mar 10, 2008 9:44 pm

Table centering annoyance

Post by Stefan Kottwitz »

Hi Ben,

welcome to the board!

\centering changes the meaning of \\. You can fix it if you add this redefinition, right after this `\centering`.

Code: Select all

\centering\let\\\tabularnewline
     $\begin{matrix}
     ...
Stefan
LaTeX.org admin
Post Reply