# Typesetting a table to look like a spreadsheet

Continuing in my efforts to create particular computing environments in LaTeX, I'm now try to coerce a table to look like a spreadsheet. It needs

• All columns other than the first having equal width (say 3cm)
• First row and column having a gray background
• First column and first row right aligned
• All other rows and columns left aligned

I can probably do this with a mess of stuff from the array and colortbl packages, but so far my efforts have been less than successful. (If I get the alignment right, then the background doesn't fill the cell properly, for example).

Here's what I have so far, which is almost good, except for the formatting:

\begin{tabular}[h]{>{\columncolor[gray]{.9}}c|*{5}{>{\hfill}p{2cm}|}}
\hline
\rowcolor[gray]{.9}&A&B&C&D&E\\
\hline
1&0&1&2&3&4\\
\hline
2&185&&&&\\
\hline
3&-31&&&&\\
\hline
4&-39&&&&\\
\hline
5&-367&&&&\\
\hline
6&-1159&&&&\\
\hline
\end{tabular}


The main difficulty seems to be formatting the first row differently from all other rows. Is there a LaTeX environment which allows you to changing formatting mid-table?

One possibility using TikZ:

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\tikzset{
table/.style={
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={rectangle,draw=black,text width=3cm,align=left},
text depth=0.5ex,
text height=1.75ex,
nodes in empty cells
},
row 1/.style={nodes={fill=gray!10,align=right}},
column 1/.style={nodes={fill=gray!10,text width=1cm,align=right}}
}

\begin{tikzpicture}

\matrix (mat) [table]
{
& 20  & 30 & 40 & 50 & 60 \\
80 & 78 & 79 & 80 & 81 & 82 \\
80 & 78 & 79 & 80 & 81 & 82 \\
80 & 78 & 79 & 80 & 81 & 82 \\
80 & 78 & 79 & 80 & 81 & 82 \\
80 & 78 & 79 & 80 & 81 & 82 \\
};
\end{tikzpicture}

\end{document}


And a possibility using longtable, array and colortbl:

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{array}
\usepackage{longtable}
\usepackage[table]{xcolor}

\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}

\begin{document}

{
\renewcommand\arraystretch{1.25}
\begin{longtable}{|>{\columncolor{gray!10}}R{1cm}*{5}{|L{3cm}}|}
\hline
\rowcolor{gray!10}& \hfill20  & \hfill30 & \hfill40 & \hfill50 & \hfill60 \\
\hline
80 & 78 & 79 & 80 & 81 & 82 \\
\hline
80 & 78 & 79 & 80 & 81 & 82 \\
\hline
80 & 78 & 79 & 80 & 81 & 82 \\
\hline
80 & 78 & 79 & 80 & 81 & 82 \\
\hline
80 & 78 & 79 & 80 & 81 & 82 \\
\hline
\end{longtable}
}

\end{document}


In the last code I used longtable just in case a multi-page table is required (if this is not so, one can simply use tabular).

• That's a great idea - I never would have thought of using TiKZ for typesetting matrices! – Alasdair Jun 24 '13 at 2:43
• @Alasdair I've updated my answer with a TikZ-free option. – Gonzalo Medina Jun 24 '13 at 2:46
• Very nice indeed. However, I'm happy to use TiKZ - it's my preferred option for diagrams now. Why do you need longtable? – Alasdair Jun 24 '13 at 2:59
• @Alasdair longtable is just in case the table should span more than one page. – Gonzalo Medina Jun 24 '13 at 3:02

An approach using "cals":

\documentclass{minimal}
\usepackage{cals}
\usepackage{xcolor}
\begin{document}
\begin{calstable}
\makeatletter
\colwidths{{5mm}{20mm}{20mm}{20mm}{20mm}{20mm}}
\alignR
\brow
\def\bgcolor{gray!20}
\def\cals@bgcolor{\bgcolor}
\cell{}\cell{A}\cell{B}\cell{C}\cell{D}\cell{E}
\erow
\brow
\def\cals@bgcolor{\bgcolor}\cell{1}\def\cals@bgcolor{}
\cell{0}\cell{1}\cell{2}\cell{3}\cell{4}
\erow
\brow
\def\cals@bgcolor{\bgcolor}\cell{2}\def\cals@bgcolor{}
\cell{185}
\cell{}\cell{}\cell{}\cell{}
\erow
\brow
\def\cals@bgcolor{\bgcolor}\cell{3}\def\cals@bgcolor{}
\cell{-31}
\cell{}\cell{}\cell{}\cell{}
\erow
\brow
\def\cals@bgcolor{\bgcolor}\cell{4}\def\cals@bgcolor{}
\cell{-39}
\cell{}\cell{}\cell{}\cell{}
\erow
\brow
\def\cals@bgcolor{\bgcolor}\cell{5}\def\cals@bgcolor{}
\cell{-367}
\cell{}\cell{}\cell{}\cell{}
\erow
\brow
\def\cals@bgcolor{\bgcolor}\cell{6}\def\cals@bgcolor{}
\cell{-1159}
\cell{}\cell{}\cell{}\cell{}
\erow
\end{calstable}
\end{document}