1

I would like to construct a table look like the picture below in latex:

enter image description here

1 Answer 1

3

Just use multicolumn with the tabular environment like this:

\documentclass{article}
 \usepackage[table]{xcolor}
\begin{document}

\begin{tabular}{c|c|c|c|c}
 \multicolumn{5}{c}{ input}\\
 \multicolumn{1}{c}{state}&\multicolumn{1}{c}{a}&\multicolumn{1}{c}{b}&    \multicolumn{1}{c}{c}&\multicolumn{1}{c}{\textit{P}}\\
\cline{2-4}
0&\cellcolor{gray}1&0&0&a\\
\cline{2-4}
1&1&\cellcolor{gray}2&0&b\\
\cline{2-4}
2&\cellcolor{gray}3&0&0&a\\
\cline{2-4}
3&1&\cellcolor{gray}4&0&b\\
\cline{2-4}
\end{tabular}
\end{document}

enter image description here

An improved version

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{calc}

\begin{document}

\begin{tabular}{c|c|c|c|c}
 \multicolumn{1}{c}{}&
 \multicolumn{3}{c}{input}\\
 \multicolumn{1}{c}{state}&
 \multicolumn{1}{c}{\texttt{a}}&
 \multicolumn{1}{c}{\texttt{b}}&
 \multicolumn{1}{c}{\texttt{c}}&
 \multicolumn{1}{c}{\makebox[\widthof{state}]{$P$}}\\
\cline{2-4}
0 & \cellcolor{gray}1 & 0 & 0 & \texttt{a}\\
\cline{2-4}
1 & 1 & \cellcolor{gray}2 & 0 & \texttt{b}\\
\cline{2-4}
2 & \cellcolor{gray}3 & 0 & 0 & \texttt{a}\\
\cline{2-4}
3 & 1 &\cellcolor{gray}4 & 0 & \texttt{b}\\
\cline{2-4}
\end{tabular}

\end{document}

enter image description here

7
  • when i run this code it give error "missing \cr inserted" May 27, 2015 at 7:07
  • Please check if you have copied completely. I have attached the output too. May 27, 2015 at 7:14
  • @AmbikaVanchinathan I took the liberty of adding a variation to your (good) solution, so to match more closely the OP's picture.
    – egreg
    May 27, 2015 at 7:15
  • thanks for the code, but when i copied code completely it give error "Undefined control sequence. 0&\cellcolor{grey}1&0&0&a\\" May 27, 2015 at 7:20
  • check if you have the \usepackage[table]{xcolor} included. May 27, 2015 at 7:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .