Using the pas-tableur style for spreadsheets in LaTeX

One of LaTeX’s strengths is its ability to typeset publication quality tables using the booktabs but sometimes we need something that look like a spreadsheet. There are several solutions to this problem. If we are working in Excel, we can, for example, select the necessary cells of interest and create an image to be inserted into our document. There are some problems with this solution. As we are all too aware, things change during the course of writing and if our spreadsheet changes, we must go through the process of recreating our image and inserting the new image into our LaTeX code.

Though it takes some time and effort (in the beginning) there are several good reasons to rendering something that looks like a spreadsheet in code. For one, we can easily see the code to make sure everything is correct before we compile but the biggest advantage is that any changes can be made easily. We don’t have to fire up Excel (shudder) and so save time during the editing process. As ideal as this solution is, there is one problem. A huge one. It takes some planning and prep for it to move smoothly.

To get an idea of how you can do this you can take a look at the “Typesetting a table to look like a spreadsheet” discussion on StackExchange. It’s not difficult and we see one solution using the array and colortbl packages and another in TikZ. Of course, there is a problem of utility. These solutions are fine if you just need to show one spreadsheet image. What if you have many spreadsheets, for example, in the case where you’re writing a book? How can you make this easier? You can use the TikZ pas-tableur package by Stéphane Pas­quet.

Getting Started

Though the pas-tableur package documentation is written in French, the package is easy to utilize. To start you just need to load the package:


\usepackage{pas-tableur}

 1 \usepackage{pas-tableur} 

The tableur command automatically creates a spreadsheet with the tableur command. As this uses the TikZ package, the command must be enclosed within the tikzpicture environment. The command syntax is:


\tableur[rows]{columns}

 1 \tableur[rows]{columns} 

To create a 3 by 4 spreadsheet, we use:


\begin{tikzpicture}
\tableur[3]{A,B,C,D}
\end{tikzpicture}

 1 2 3 \begin{tikzpicture} \tableur[3]{A,B,C,D} \end{tikzpicture} 

which looks like

A pas-tableur spreadsheet with no options defined.

Changing colors up

This isn’t bad as things go but what if you need to customize things a bit? Turns out this is simple. You can define these options in the preamble.


% Color of top part of header.
\definecolor{grayTopCell}{cmyk}{0.08,0.05,0.06,0}
% Color the bottom part of headers.
\definecolor{grayBottomCell}{cmyk}{0.1,0.07,0.08,0}
% Color of line cell separation.
\definecolor{graySepCell}{cmyk}{0.29,0.21,0.21,0}
% Minimum width of the columns
\def\colminwidth{7em}
% Minimum row height
\def\lineminheight{1.4em}

  1 2 3 4 5 6 7 8 9 10 % Color of top part of header. \definecolor{grayTopCell}{cmyk}{0.08,0.05,0.06,0} % Color the bottom part of headers. \definecolor{grayBottomCell}{cmyk}{0.1,0.07,0.08,0} % Color of line cell separation. \definecolor{graySepCell}{cmyk}{0.29,0.21,0.21,0} % Minimum width of the columns \def\colminwidth{7em} % Minimum row height \def\lineminheight{1.4em} 

The above are the default options. What is we wanted to change the color of the top part of the header to yellow?


% Color of top part of header.
\definecolor{grayTopCell}{cmyk}{0.00,0.00,1.00,0.07}

 1 2 % Color of top part of header. \definecolor{grayTopCell}{cmyk}{0.00,0.00,1.00,0.07} 

which gives us the output:

A pas-tableur spreadsheey with the top part of the header colored in yellow.

and as Bermuda Sand is a wonderful color, we add the command:


% Color of bottom part of header.
\definecolor{grayBottomCell}{cmyk}{0.00,0.18,0.17,0.04}

 1 2 % Color of bottom part of header. \definecolor{grayBottomCell}{cmyk}{0.00,0.18,0.17,0.04} 

and we get:

A pas-tableur spreadsheet with the top part of the header colored in yellow and the bottom part colored in Bermuda Sand.

Now that you’re adjusted the header colors, what if you needed to change the cell lines to a nice dark golden color?


% Color of line cell separation.
\definecolor{graySepCell}{cmyk}{0.00,0.27,0.94,0.28}

 1 2 % Color of line cell separation. \definecolor{graySepCell}{cmyk}{0.00,0.27,0.94,0.28} 

which looks like:

A pas-tableur spreadsheet with the color of the cell lines changed.

Changing cell size

Now that we have had fun with changing the colors of our spreadsheet, what if we just wanted to change the cell width and height? For that we turn to the colminwidth and lineminheight commands.


% Minimum column width
\def\colminwidth{15em}

 1 2 % Minimum column width \def\colminwidth{15em} 

Our extra long spreadsheet looks like this:

Image showing a pas-tableur spreadsheet with a long cell width.

We can do the same thing for the cell height:


% Minimum row height
\def\lineminheight{2.8em}

 1 2 % Minimum row height \def\lineminheight{2.8em} 

which looks like:

A pas-tableur spreadsheet with extra tall cells

Filling the cells

Drawing empty spreadsheets are fun but generally we fill spreadsheets with text and formulas. This can be done with the celtxr command.


\celtxt[alignment]{column}{row}{text}

 1 \celtxt[alignment]{column}{row}{text} 

Now we can fill our spreadsheet:


\begin{tikzpicture}
\tableur[3]{A,B,C,D}
\celtxt{A}{1}{Some text placed here}
\celtxt[r]{C}{2}{=3*C1}
\celtxt[l]{A}{3}{=C2*D1}
\celtxt[c]{D}{3}{More text}
\end{tikzpicture}

 1 2 3 4 5 6 7 \begin{tikzpicture} \tableur[3]{A,B,C,D} \celtxt{A}{1}{Some text placed here} \celtxt[r]{C}{2}{=3*C1} \celtxt[l]{A}{3}{=C2*D1} \celtxt[c]{D}{3}{More text} \end{tikzpicture} 

which looks like:

The pas-tableur spreadsheet has been filled with text and formulas.

Selecting and Highlighting Cells

Selecting a Single Cell

You may need to select or highlight cells to illustrate your point. For that, we turn to the selecCell command.


\selecCell{column}{row}

 1 \selecCell{column}{row} 

So if we wanted to select the C2 cell of our spreadsheet:


\begin{tikzpicture}
\tableur[3]{A,B,C,D}
\celtxt{A}{1}{Some text placed here}
\celtxt[r]{C}{2}{=3*C1}
\celtxt[l]{A}{3}{=C2*D1}
\celtxt[c]{D}{3}{More text}
\selecCell{C}{2}
\end{tikzpicture}
which looks like:
The pas-tableur spreadsheet shows one cell selected.

  1 2 3 4 5 6 7 8 9 10 \begin{tikzpicture} \tableur[3]{A,B,C,D} \celtxt{A}{1}{Some text placed here} \celtxt[r]{C}{2}{=3*C1} \celtxt[l]{A}{3}{=C2*D1} \celtxt[c]{D}{3}{More text} \selecCell{C}{2} \end{tikzpicture} which looks like: The pas-tableur spreadsheet shows one cell selected. 

Selecting Multiple Cells

Sometimes we need to highlight, not one, but a group of cells. For that we turn to the multiSelec command:


\multiSelec{column range}{row range}

 1 \multiSelec{column range}{row range} 

So if we wanted to highlight B2 to D4 in our spreadsheet:


\begin{tikzpicture}
\tableur[5]{A,B,C,D,E,F,G}
\celtxt{A}{1}{Some text placed here}
\celtxt[r]{C}{2}{=3*C1}
\celtxt[l]{A}{3}{=C2*D1}
\celtxt[c]{D}{3}{More text}
\multiSelec{B,C,D}{2,3,4}
\end{tikzpicture}

 1 2 3 4 5 6 7 8 \begin{tikzpicture} \tableur[5]{A,B,C,D,E,F,G} \celtxt{A}{1}{Some text placed here} \celtxt[r]{C}{2}{=3*C1} \celtxt[l]{A}{3}{=C2*D1} \celtxt[c]{D}{3}{More text} \multiSelec{B,C,D}{2,3,4} \end{tikzpicture} 

which looks like:

Here we can see the B2 and D4 cells highlighted.

Changing the colors

But what if you wanted to change the color of the highlighted headers? For that we define the blueSelecCellTop and the blueSelecCellBottom colors in the preamble:


% Color of the top part of the header of the column/row selected
\definecolor{blueSelecCellTop}{cmyk}{0.00,0.30,0.98,0.06}

% Color of the bottom part of the header of the column/row selected
\definecolor{blueSelecCellBottom}{cmyk}{0.00,0.32,0.14,0.67}

 1 2 3 4 5 % Color of the top part of the header of the column/row selected \definecolor{blueSelecCellTop}{cmyk}{0.00,0.30,0.98,0.06} % Color of the bottom part of the header of the column/row selected \definecolor{blueSelecCellBottom}{cmyk}{0.00,0.32,0.14,0.67} 

which looks like:

Here we change the colors of the top and bottom parts of the highlighted cells.

Conclusion

The pas-tableur package is an easy to use LaTeX package for generating spreadsheet images. Of course, you can use your favorite spreadsheet, capture it as an image and integrate it into your document but the package offers some advantages. As everything is generated in LaTeX code, you can add spreadsheets and make edits easily–the process becomes a part of your writing which is something we all love about LaTeX.

I found the package easy to use and understand and would recommend it to anyone who needs to generate spreadsheet images into their document.

• hlosukwakha

Is there a way to rename the rows to something like ‘Attrbute1′, Attribute2,..,etc’ instead of simply 1,2,3,..,etc. I can rename the column headers easily from A,B,C,..,etc to whatever values I need.

• http://www.texnical-designs.com/ David Latchman

Hi, yes it is possible. Yes, you can by using the command:

tableur[3]{Attribute 1, Attribute 2, Attribute 3, Attribute 4}

Which will look like the image shown. Hope that helps

• hlosukwakha

Thanks a lot. That’s exactly what I need.