[Tex/LaTex] Latex table error

tables

I can not see what i wrong with my table code

\begin{tabular}{l*{11}{c}r}
  & A & B & C & D & E & F & G & H\\
\hline
A & 0_A & 4_A & \infty & 5_A & 6_A & \infty & \infty & \infty\\
B & & 4_A & 14_B & 5_A & 6_A & \infty & \infty & \infty\\
D & & & 10_D & 5_A & 6_A & 12_D & 9_D & \infty\\
E & & & 10_D & & 6_A & 8_E & 7_E & \infty\\
G & & & 10_D & & & 8_E & 7_E & 12_G\\
F & & & 9_F & & & 8_E & & 12_G\\
C & & & 9_F & & & & & 11_C\\
H & & & & & & & & 11_C
\end{tabular}

Best Answer

Your given code results in several warnings that an $ is missing. So your given code can not work fine.

You have two possibilities to solve the issue:

  1. Add $ arround the mathematical parts in the table, like $\infty$ and $0_A$ etc.
  2. Use environment array, which has to be used in mathematical mode, started with \[, ended with \]. To keep the letters in the first row and column upright, I used command \mathrm{A} etc.

The following MWE shows both solutions:

\documentclass{article}


\begin{document}
First possibility:

\begin{tabular}{l*{7}{c}r}
  & A     & B     & C        & D     & E     & F        & G        & H\\
\hline
A & $0_A$ & $4_A$ & $\infty$ & $5_A$ & $6_A$ & $\infty$ & $\infty$ & $\infty$\\
B &       & $4_A$ & $14_B$   & $5_A$ & $6_A$ & $\infty$ & $\infty$ & $\infty$\\
D &       &       & $10_D$   & $5_A$ & $6_A$ & $12_D$   & $9_D$    & $\infty$\\
E &       &       & $10_D$   &       & $6_A$ & $8_E$    & $7_E$    & $\infty$\\
G &       &       & $10_D$   &       &       & $8_E$    & $7_E$    & $12_G$\\
F &       &       & $9_F$    &       &       & $8_E$    &          & $12_G$\\
C &       &       & $9_F$    &       &       &          &          & $11_C$\\
H &       &       &          &       &       &          &          & $11_C$
\end{tabular}

Second possibility:

\[
\begin{array}{l*{7}{c}r}
           & \mathrm{A} 
                 & \mathrm{B} 
                       & \mathrm{C} 
                                & \mathrm{D} 
                                      & \mathrm{E} 
                                            & \mathrm{F} 
                                                     & \mathrm{G} 
                                                              & \mathrm{H}\\
\hline
\mathrm{A} & 0_A & 4_A & \infty & 5_A & 6_A & \infty & \infty & \infty\\
\mathrm{B} &     & 4_A & 14_B   & 5_A & 6_A & \infty & \infty & \infty\\
\mathrm{D} &     &     & 10_D   & 5_A & 6_A & 12_D   & 9_D    & \infty\\
\mathrm{E} &     &     & 10_D   &     & 6_A & 8_E    & 7_E    & \infty\\
\mathrm{G} &     &     & 10_D   &     &     & 8_E    & 7_E    & 12_G\\
\mathrm{F} &     &     & 9_F    &     &     & 8_E    &        & 12_G\\
\mathrm{C} &     &     & 9_F    &     &     &        &        & 11_C\\
\mathrm{H} &     &     &        &     &     &        &        & 11_C
\end{array}
\]
\end{document}

resulting in the following pdf page:

first page