5

Consider this example code which creates a 2x6 table where the 6 columns are formed from three name/value pairs:

\documentclass{article}
\usepackage[svgnames,table]{xcolor}
\usepackage{tabu}
\usepackage{booktabs}

\begin{document}

\begin{center}
\taburowcolors[1]{ForestGreen!40..ForestGreen!40}
\begin{tabu}{p{1cm}|X|p{1cm}|X|p{1cm}|X}
\tabucline-
name1 & value1 & name3 & value3 & name5 & value6 \\
name2 & value2 & name4 & value4 & name6 & value6 \\
\tabucline-
\end{tabu}
\end{center}

\end{document}

which looks like this:

screenshot

It works but we had to explicitly lay out the 2 rows by 3 pairs of columns. Is there any way to specify it like this:

name1 & value1 
name2 & value2
name3 & value3
name4 & value4 
name5 & value5
name6 & value6

and then just tell it to wrap it into three (or whatever number we wish to specify) of column pairs?

EDIT:

Thanks, ArTourterless. Here is my attempt and a further question on how to complete it so that it looks like the tabu package approach.

First we try with the multicols and itemenum packages. Following that we have done it the hard way using the tabu package. The key thing that still seems missing from the multicols/itemenum approach is the horizontal blue lines. Anyone know how to get those?

\documentclass{article}
\usepackage{multicol}
\usepackage{enumitem}
\usepackage{tabu}
\usepackage[table]{xcolor}

\begin{document}

Using multicols/enumitem:

\begin{center}
\setlength{\columnseprule}{.4pt}
\renewcommand\columnseprulecolor{\color{blue}}
\begin{multicols}{2}
\begin{description}[font=\bfseries, leftmargin=2cm, style=nextline]
\item[Name1] Value1.
\item[Name2] Value2.
\item[Name3]  Value3.
\end{description}
\end{multicols}
\end{center}

Using tabu (manually doing the layout ourself):

\begin{center}\small
\tabulinesep=1.5mm
\begin{tabu} {>{\bfseries}p{1cm}X |[blue] >{\bfseries}p{1cm}X }
\everyrow{\tabucline[blue]-}
Name1 & Value1. & Name3 & Value3. \\
Name2 & Value2. & & \\
\tabucline[blue]-
\end{tabu}
\end{center}

\end{document}

which looks like this:

screenshot

EDIT 2: Have come up with another variation of the approach in EDIT 1. We can create a separate tabu table for each row and then put that in the multicols. If the number of columns do not evenly divide into the length of the data then the dividing lines will be different from the one tabu approach but otherwise seems very similar. Although there is extra entry for each row it does mean that we can get out of layout-specific code so its more flexible.

\documentclass{article}
\usepackage{multicol}
\usepackage{enumitem}
\usepackage{tabu}
\usepackage[table]{xcolor}

\begin{document}

\begin{center}\small
\setlength{\columnseprule}{.4pt}
\renewcommand\columnseprulecolor{\color{blue}}
\begin{multicols}{2}

\begin{tabu} {>{\bfseries}p{1cm}X}
Name1 & Value1. \\
\tabucline[blue]-
\end{tabu}

\begin{tabu} {>{\bfseries}p{1cm}X}
Name2 & Value2. \\
\tabucline[blue]-
\end{tabu}

\begin{tabu} {>{\bfseries}p{1cm}X}
Name3 & Value3. \\
\tabucline[blue]-
\end{tabu}

\end{multicols}
\end{center}

\end{document}

which looks like this:

screenshot

EDIT 3:

Added screenshots.

1
  • 1
    I have used the itemize or descriptions environments inside a multicols in the past to get that sort of effect. but the colouring doesn't come with it.
    – ArTourter
    Commented Jul 8, 2012 at 17:35

2 Answers 2

0

Perhaps a bit overkill, but I like to use TikZ for this kind of layout problems. Next is a "solution" with an easy to use interface. Look at the final part of the code which provides the usage example. You can insert \NewColumn anywhere in you sequence of \Row (try uncommenting the last \NewColumn.

Note that each \Row is in fact a tabular, used in the macro \RowContent. You can customize there the column types or widths. The use of tikz allows for endless possibilities for the blue lines, row backgrounds, etc.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\thispagestyle{empty}
\usetikzlibrary{positioning}

\def\RowWidth{4cm}

\def\RowContent#1{\begin{tabular}{ll}
#1\\
\end{tabular}}

\def\Row#1{
\node[text width=\RowWidth,
      below=0mm of previous row, 
      name=previous row] {\RowContent{#1}};
\draw[blue] (previous row.south west) -- (previous row.south east);
}

\def\NewColumn{
\path (previous column) +(\RowWidth,0) coordinate[name = previous column] {};
\coordinate[right=of previous column] (previous column) {};
\coordinate (previous row) at (previous column) {};
}

\def\Start{
\coordinate (previous column) at (0,0) {};
\coordinate (previous row) at (0,0) {};
}

\begin{tikzpicture}
\Start  % This Start is mandatory, it defines the nodes to which the rest is positioned
\Row{ Name1 & Value 1}
\Row{ Name2 &  Value 2}
\Row{ Name3 &  Value 3}

\NewColumn
\Row{ Name4 & Value 4}
\Row{ Name5 &  Value 5}

% \NewColumn
\Row{ Name6 & Value 6}
\Row{ Name7 &  Value 7}
\end{tikzpicture}

\end{document}

The above code results in:

Output

2
  • Thanks. Although switching from 2 to 3 columns is not just a matter of changing a parameter from 2 to 3 it does seem like a very flexible infrastructure nonetheless. Have you noticed if the run time of actually drawing out tables is significant? I have many tables, images, etc. Commented Jul 9, 2012 at 11:18
  • If you refer to the fact of each row being a table, I think the overhead is negligible. However, TikZ is a big monster, and any document using TikZ takes more time to compile, if only to load it. But if you are using TikZ for other figures in your document, you already have that overhead.
    – JLDiaz
    Commented Jul 9, 2012 at 11:28
0

ArTourterless commented that a solution would be to put a list in a multicols environment and I have been pursuing the implementation of this idea. The main problem I came across was wanting to put a blue line between successive items. This is easy to do with tabu but seems not to fit in well with lists. In reading over the tabu package documentation I came across \savetabu and \usetabu. These save and reuse the column widths and other settings of a tabu table. This seems a perfect application for that since we can make each row a separate tabu table which has column widths identical to the others:

\documentclass{article}
\usepackage{multicol}
\usepackage{enumitem}
\usepackage{tabu}
\usepackage[table]{xcolor}

\begin{document}

\begin{center}\small
\setlength{\columnseprule}{.4pt}
\renewcommand\columnseprulecolor{\color{blue}}
\begin{multicols}{2}

\begin{tabu} {>{\bfseries}lX}\savetabu{mytabu}
Name1 & Value1. \\
\tabucline[blue]-\end{tabu}

\begin{tabu} {\usetabu{mytabu}}
Name2 & Value2. \\
\tabucline[blue]-\end{tabu}

\begin{tabu} {\usetabu{mytabu}}
Name3 & Value3. \\
\tabucline[blue]-\end{tabu}

\end{multicols}
\end{center}

\end{document}

The output looks like this:

screenshot

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .