Creating A LaTeX Package

last updated 2020-11-22 21:10:34 by Simon Vandevelde

Why my own package?

Recently, I was working on a paper in which we had to list multiple so-called decision tables. A decision table is a visual representation of an assignment of outputs based on a set of inputs. In the Decision Model and Notation standard, such a table looks as follows:

Decide what to eat
U Season NumberOfGuests Dish
1 Summer > 10 Light Salad
2 Summer ≤ 10 Beans Salad
3 Spring < 10 Stew
4 Spring ≥ 10 Steak

Basically, it is a table in which the output (column in blue) is decided based on a set of inputs (columns in green). Every row represents a rule of the table: if it's input conditions are met, then the output is set accordingly. For example, the above table decides that if the season is Summer, and there are 8 guests we eat "Beans Salad" (because of the second rule).

The paper which we were writing was chock-full of these decision tables. Our initial approach was to simply insert screenshots of the tables into the text, as we had already modelled all tables in a spreadsheet editor. There were multiple issues with this approach.
  • Every time a table changed, we had to take a new screenshot.
  • No easy way to ensure consistent layout
  • Most importantly: No easy way to ensure consistent font size
The reason for that last point is that the tables had different sizes, and thus the screenshots were different sizes. When adding the image to our document, we had to guess the right print size in order to keep a consistent font size.

Decision Tables in LaTeX

Annoyed at all the inconsistencies, I wanted to figure out if I could add decision tables directly to LaTeX. After all, it is possible to create normal tables in LaTeX, so I figured it couldn't be that hard to modify them into decision tables.

I used the colortbl package to color in the cells, and the booktabs package to create the first row of the table, as it has a special size. The LaTeX code for creating the earlier table then looks like this:

\begin{figure}[h]
    \centering
    {
    \dmnfont
    \begin{tabular}{|r|l|l|l|l|}
        \cmidrule{1-3}
        \multicolumn{3}{|l|}{Decide what to eat} & \multicolumn{1}{c}{}\\
        \hline
        U & \inputcol \textbf{Season} & \inputcol \textbf{NumberOfGuests}
          & \outputcol \textbf{Dish}\\
        \hline
        1 & Summer & $> 10$ & Light Salad\\
        \hline
        2 & Summer & $\leq 10$ & Beans Salad\\
        \hline
        3 & Spring & $< 10$ & Stew\\
        \hline
        4 & Spring & $\geq 10$ & Steak\\
        \hline
    \end{tabular}
    }
    \caption{Decision table to decide dinner.}
\end{figure}

(Note that I stuck them in a figure environment instead of a table, because a decision table is not a table in the literature sense.)

While this code delivers a nice clean looking decision table, it is very hard to read. Furthermore, if you want multiple such tables in your text, you have to copy paste the entire snippet and change it to represent the new table, which is a lot of work.

Creating a package!

To overcome this inefficiency, I figured I would try to implement the table in the form of a package. The main idea was to take the above code, and put it in a package! It's just that simple, right?

Wrong. Turns out creating macro's in LaTeX isn't that easy. In my case, the most difficult part was that tables can be of all sizes, and the macro should support this. The macro arguments are as follows:
\dmntable{title}{hitpolicy}{input}{output}{values}
The example table can be created as follows:
\dmntable{Decide what to eat}{U}
         {Season,NumberOfGuests}{Dish}
         {Summer,$> 10$, Light Salad,
          Summer,$\leq 10$, Beans Salad,
          Spring,$< 10$, Stew,
          Spring,$\geq 10$, Steak}
I had to figure out a way to extract the number of inputs, number of outputs, and the number of rows. Initially I thought this would be easy, but as it turns out, traditional programming in LaTeX doesn't exist.

Still, after fiddling around for a day and reading numerous StackOverflow posts, I managed to build a LaTeX package which can create decision tables! It is aptly named decision-table, and is avaible on CTAN and through texlive-science.

And this is how the table looks in LaTeX!
Example decision table