Easy LaTeX tables

Who wouldn’t want to type tables in LaTeX without worrying about how text is treated in each column. Something as simple as:

\begin{a_table} {|20%|60%|20%|}\hline

bla blaaaa   & bleee bleeeeh & babble babble \\

etc…

Well, it can be almost as simple!

I have taken the idea from a site about cheatsheets, an obvious application of this technique (I am actually writing a formulary, a formula cheatsheet to be handed out at exams).

In the beginning of the tex file, one needs some support for fancy tables:

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

The crucial definition is the following:

\makeatletter
\newcommand\ratio[2]{\strip@pt\dimexpr#1pt/#2\relax}
\newcolumntype{A}[2]
{
>{\begin{minipage}[t]{#2\linewidth-2\tabcolsep-#1\arrayrulewidth}%
\vspace{\tabcolsep}}%
c%
<{\vspace{\tabcolsep}\end{minipage}}%
}
\makeatother

This defines a “ratio” and the column style “A”.

Still in the preamble, you can adjust some features:

\pagestyle{empty}

\arrayrulewidth=1pt
\tabcolsep=5pt
\arrayrulecolor{gray}

Tables are now typed this way:

\begin{longtable}
{
|A{1.5}{\ratio{40}{100}}% 40%
|A{1.5}{\ratio{30}{100}}% 30%
|A{1.5}{\ratio{30}{100}}% 30%
|%
}\hline

bla \\ blaaaa   & bleee \\ bleeeeh & babble \\ babble

\tabularnewline\hline

\end{longtable}

Notice new tabular lines are introduced by “\tabularnewline”: “\\” is for line breaks inside the columns (whose contents are actually minipages, see the definition of column style “A” above).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s