I'm trying to write a key:value environment which would allow me to create a dual columns tables where the columns are separated by colons.
Typically, I've implemented this using parbox in my own class file:
\makeatletter
\newenvironment{keylist}{\global\setlength{\parindent}{0pt}}{}
\newlength{\@keyvalueXposition}
\setlength{\@keyvalueXposition}{0.5\textwidth}
\newcommand{\keyvalue}[2]{
\parbox[t]{\@keyvalueXposition}{#1}
:
\parbox[t]{\@keyvalueXposition}{#2}
\\
}
\makeatother
Which is typically used like this on the user-side:
\begin{keylist}
\keyvalue{Foo que lorem ipsum } {Foofoo }
\keyvalue{Bar foobar } {Bar bar ipsum }\\
\keyvalue{Baz norf } {Bazbaz }
\keyvalue{Qux lorem ipsum } {Qux lorem qux }
\end{keylist}
Soon I realized my implementation was lacking this use case: allow lines to have only one element without printing the colon. Also, I began to use, in my class file, tabularx
with booktabs
to have great spaced lines in my tables.
\begin{tabularx}{\textwidth}{ X@{ : }X }\toprule
Foo que lorem ipsum & Foofoo\\\midrule
Bar foobar & Bar bar ipsum\\[\baselineskip]
Baz norf & Bazbaz\\
Qux lorem ipsum & Qux lorem qux\\\bottomrule
\end{tabularx}
* where [\baselineskip]
is used to simulate linebreaks, implemented simply using \\
in the previous implementation using parbox.
While this was working nice up to now, this was quite a hassle to use. I decided to replace the above solution by an environment alias, defined like this:
\newenvironment{keylisting}
{\tabularx{\textwidth}{ X@{ : }X }}
{\endtabularx}
which would give me
\begin{keylisting}\toprule
Foo que lorem ipsum & Foofoo\\\midrule
Bar foobar & Bar bar ipsum\\[\baselineskip]
Baz norf & Bazbaz\\
Qux lorem ipsum & Qux lorem qux\\\bottomrule
\end{keylisting}
In the case where a line has only one item, we need to create an empty item for the second column, like this
Foo bar &\\
However, a colon is printed between both columns. According to what I found in the documentation, using \multicolumn{2}{l}{Foo bar}\\
is the solution. However, for a reason I don't know when we prefix that statement by [\baselineskip]
, the latter isn't interpreted by the LaTeX kernel. If you use the following example, you will see the differences between the two lines with [\baselineskip]]
: the first one is not interpreted. I prefixed the statement by 10
to clearly see it isn't interpreted.
\documentclass{report}
\usepackage[margin=2cm]{geometry}
\linespread{1.2}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{float}
\newenvironment{keyval}
{ \tabularx{\textwidth}{ X@{ : }X } }
{ \endtabularx }
\begin{document}
\begin{keyvalue}
Foo bar lorem ipsum & Lorem ipusm que \\
Some number & 12 3456 789 8 \\
\multicolumn{2}{l}{This is some comment} \\[10\baselineskip]
Yet some number & 987 65432 1 \\
Foo bar & 163883495778 \\[\baselineskip]
Bar lorem ipsum & 2013-04-12 \\
\end{keyvalue}
\end{document}
Thanks for your help.
\begin{tabularx}
in an environment definition, you can with lots restrictions, use\tabularx
\end{tabularx
\global\setlength
is not supported syntax (and will blow up spectacularly if the standardcalc
package is used).