What is the proper way to create a horizontal table?

I want to have a horizontal table, like this:

However, when using the usual method of creating a table, it quickly becomes a pain having to pre-define the number of columns (especially since I want to generate them using the data tool):

\begin{tabular}{ | l | l | l | l | l | l | l | l | l | l | l | l | l | l | }


I also want it to be multi-line. Is there a better way?

-
Are you asking for a better way of creating many columns without writing l | l | l, etc? –  Alenanno Mar 31 '12 at 21:05
@Alenanno: That would be one possible solution, but I was also wondering if there was a "proper" way to create horizontal tables, as the system I mentioned above seems designed for creating vertical tables. –  Gelatin Mar 31 '12 at 21:14
It works like any other table: if you input more columns than rows, you'll get a horizontal question. –  Alenanno Mar 31 '12 at 21:16

Nothing much bad happens if you specify more columns than you use, so if you use

 {|*{50}{l|}}


and no row has more than 3 entries latex doesn't use the extra col spec and just makes a 3 column table.

-

(La)TeX tabulars do not require a predefined number for the rows. For the columns, however, this is required in the general setup. You can combine column specifications that are similar though using a *{<num>}{<col spec>} specification. This would repeat <col spec> a total of <num> times. Therefore, defining

\begin{tabular}{ | *{14}{l|} }


is equivalent to

\begin{tabular}{ | l | l | l | l | l | l | l | l | l | l | l | l | l | l | }


and much better in terms of code readability.

-
This seems more practical, but since I'm using the data tool, I'd still have to edit my Latex document every time I change the number of items in my CSV file. –  Gelatin Mar 31 '12 at 21:18
@SimonBrown: Do you have a minimal example that you can include in your question to show what you're working with (keep it minimal) and also for other to have something to fiddle around with? –  Werner Mar 31 '12 at 21:21

I don't know if I understand fully what you mean by "having to pre-define the number of rows" (did you mean to write columns?). As long as you know, for instance, that the table should contain a left-justified header column and n centered data columns -- with all columns separate by vertical lines, according to the example you give -- the following will work without too much setup cost (I'll assume that n = 10):

\begin{table}
\caption{An easily defined tabular environment} \label{tab:easy}
\begin{tabular}{|l*{10}{|c}|}
...
\end{tabular}
\end{table}


Addendum: Suppose that the table (other than the first or header row) will be filled with a datatool and that you don't know in advance the exact number of data columns. However, as long as you know the maximum possible number -- say, 15 -- you can set up the tabular environment with that information. I.e., you'd set \begin{tabular}{|l*{15}{|c}|}. If the actual table ends up containing, say, only [!] 12 data columns, no harm was done by having specified an excessive number of centered columns.

-

Edit: I must confess I forgot, that you want to use datatool, which I don't know. To work the way I provided below it seems you need \DTLforeach or the starred variant.

The package rotating could be your friend:

\documentclass{article}

\usepackage[english]{babel} % needed for "blindtext"
\usepackage[pangram]{blindtext}

\usepackage{array}
\usepackage{rotating}

\begin{document}

\blindtext[1]

\bigskip

\begin{tabular}{|>{\begin{turn}{-90}}c<{\end{turn}}|>{\bfseries\begin{turn}{-90}}c<{\end{turn}}|}
\hline
Factor sum & \textnormal{Number}\\ \hline
1 & 1\\ \hline
2 & 2\\ \hline
3 & 3\\ \hline
4 & 4\\ \hline
5 & 5\\ \hline
\end{tabular}

\bigskip

\blindtext[1]

\bigskip

\begin{turn}{180}
\begin{tabular}{|>{\begin{turn}{-90}}c<{\end{turn}}|>{\bfseries\begin{turn}{-90}}c<{\end{turn}}|}
\hline
Factor sum & \textnormal{Number}\\ \hline
6 &  6\\ \hline
7 &  7\\ \hline
8 &  8\\ \hline
9 &  9\\ \hline
10 & 10\\ \hline
\end{tabular}
\end{turn}

\bigskip

\blindtext[1]

\bigskip

\begin{sideways}
\begin{tabular}{|>{\begin{turn}{-90}}c<{\end{turn}}|>{\bfseries\begin{turn}{-90}}c<{\end{turn}}|}
\hline
Factor sum & \textnormal{Number}\\ \hline
11 & 11\\ \hline
12 & 12\\ \hline
13 & 13\\ \hline
14 & 14\\ \hline
15 & 15\\ \hline
\end{tabular}
\end{sideways}

\bigskip

\blindtext[1]

\end{document}


It looks like this:

rotating provides also an environment sidewaystable for landscape-form ﬂoating tables.

Another way would be the document class option landscape or the packages lscape/pdflscape, which provide the environment landscape.

-

The underlying horizontal alignment primitive, \halign, has support for a repeating column definition:

\halign{\strut#\hfil&&\quad#\hfil\cr
\noalign{\hrule height 1pt\smallskip}
1& 2& 3& 4& 5& 6& 7& 8& 9\cr
\noalign{\hrule}
1& 3& 4& 7& 6& 12& 8& 15& 13\cr}


There's no vertical rules in the above, though (adding them would complicate the example). The first line up until a \cr inside the \halign{ is a preamble which tells how to format the column cells. When it contains a &&, it tells to repeat the following definition(s). A #-character indicates where the content will go in the definition.

In my opinion, \halign is a great way to create tables – I don't feel it needs a higher-level macros/API/abstraction at all.

-