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 Pasquet.
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:
1 2 3 |
\usepackage{pas-tableur} |
Now you can start drawing your spreadsheets
Drawing a Spreadsheet
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:
1 2 3 |
\tableur[rows]{columns} |
To create a 3 by 4 spreadsheet, we use:
1 2 3 4 5 |
\begin{tikzpicture} \tableur[3]{A,B,C,D} \end{tikzpicture} |
which looks like
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
% 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?
1 2 3 4 5 |
% Color of top part of header. \definecolor{grayTopCell}{cmyk}{0.00,0.00,1.00,0.07} |
which gives us the output:
and as Bermuda Sand is a wonderful color, we add the command:
1 2 3 4 5 |
% Color of bottom part of header. \definecolor{grayBottomCell}{cmyk}{0.00,0.18,0.17,0.04} |
and we get:
Now that you're adjusted the header colors, what if you needed to change the cell lines to a nice dark golden color?
1 2 3 4 5 |
% Color of line cell separation. \definecolor{graySepCell}{cmyk}{0.00,0.27,0.94,0.28} |
which looks like:
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.
1 2 3 4 5 |
% Minimum column width \def\colminwidth{15em} |
Our extra long spreadsheet looks like this:
We can do the same thing for the cell height:
1 2 3 4 5 |
% Minimum row height \def\lineminheight{2.8em} |
which looks like:
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.
1 2 3 |
\celtxt[alignment]{column}{row}{text} |
Now we can fill our spreadsheet:
1 2 3 4 5 6 7 8 9 |
\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:
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.
1 2 3 |
\selecCell{column}{row} |
So if we wanted to select the C2 cell of our spreadsheet:
1 2 3 4 5 6 7 8 9 10 11 12 |
\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: [caption id="attachment_515" align="aligncenter" width="625"]<a href="https://texnical-designs.com/wp-content/uploads/2014/01/pas-tableur_8.png"><img src="https://texnical-designs.com/wp-content/uploads/2014/01/pas-tableur_8-1024x364.png" alt="pas-tableur spreadsheet" width="625" height="222" class="size-large wp-image-515" /></a> The pas-tableur spreadsheet shows one cell selected.[/caption] |
Selecting Multiple Cells
Sometimes we need to highlight, not one, but a group of cells. For that we turn to the multiSelec command:
1 2 3 |
\multiSelec{column range}{row range} |
So if we wanted to highlight B2 to D4 in our spreadsheet:
1 2 3 4 5 6 7 8 9 10 |
\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:
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:
1 2 3 4 5 6 7 8 9 |
% 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:
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.