💾 Archived View for bogwonch.net › latex_tables_column_separators captured on 2023-09-08 at 15:57:06. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2021-11-30)
-=-=-=-=-=-=-
I needed to make some fancy tables for a grant proposal, and had to use some fancy tricks to get the effects I wanted. I thought I'd write them up as somewhat useful tricks as it's been a while since I've seen them used.
The table I wanted to make was a combined Gantt-chart and work breakdown.
On the left of the table I'll have the Gantt-chart itself as 6 columns for each of the weeks of work. I'll fill in the blocks of work with a \rule{1em}{1em} to make a 1 em square box. On the right we'll have a one-line description of the work and a cost.
First problem: the Gantt chart columns are too far apart. We want them as close together as possible, but to maintain the normal separation for the other columns.
If you wanted to adjust the spacing of all columns in a table you can do it by setting the \tabcolsep length to whatever you want. For example:
\setlength{\tabcolsep}{2em}
But how do you do it individually? The answer lies in the tabular environment format.
When you normally Write a table with three left aligned columns you might specify the tabular environment like:
\begin{tabular}{lll}
Here the tabular separators are implicit, and will be expanded to the default (add a \tabcolsep's worth of horizontal space after it).
\begin{tabular}{l@{\hspace\tabcolsep} l@{\hspace\tabcolsep} l@{\hspace\tabcolsep}}
This gives us our first fix. For the columns we want to be closer, we just explicitly set the separator:
\begin{tabular}{l@{}l@{}l@{}l@{}l@{}l@{} ll}
Much better.
Next problem. The descriptions are rather separated from their costings. Let's add some dotted lines to help the eye follow to the right cost.
We could add this to each of the rows individually, but that gets tiresome as the table gets bigger. Luckily we can reuse the explicit separator trick to do this.
\begin{tabular}{l@{}l@{}l@{}l@{}l@{}l@{} l@{\hspace\tabcolsep\dotfill\hspace\tabcolsep}l}
Better, but now we have a new problem... the heading also gets the dots. Let's fix that with a macro.
\newcommand{\heading}[2]{\multicolumn{1}{#1}{#2}}
The heading command allows us to change the style of an individual field by creating a multicolumn that spans only 1 column and where we can specify its style explicitly. If we wrap the heading (and other problematic fields) with it we can tidy things up.
... & \heading{l}{Description} & ...
Nice!
Final tweak. We'd like to have our costs right aligned (so they're easier to compare), but have the \pounds symbol separated and vertically aligned so they're less distracting. Again we can use the explicit separator. Lets adjust that last column and append the \pounds on there, and make the costs column right aligned.
\begin{tabular}{... l@{\hspace\tabcolsep\dotfill\hspace\tabcolsep\pounds\hspace.5\tabcolsep}r}
Lovely! Couple more things to fix up... lets ensure the heading for the costs column is still left aligned like all the others by using that heading macro again.
\heading{l}{Cost}
And with a few more tiny tweaks we're done. Looks good!
It's amazing how you can improve you're tables with just a little bit of LaTeX finangling. I first came across the explicit column separator trick when trying to vertically align decimal points in numbers, e.g:
\begin{tabular}{r@{.}l} 1000 & 0 \\ 100 & 00 \\ 10 & 000 \\ 1 & 0000 \\ \end{tabular}
But you can use it for a variety of nice effects. Really neat.