I want to have a horizontal table, like this:

Horizontal table

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


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
  • 1
    @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):

\caption{An easily defined tabular environment} \label{tab:easy}

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:


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





Factor sum & \textnormal{Number}\\ \hline
1 & 1\\ \hline
2 & 2\\ \hline
3 & 3\\ \hline
4 & 4\\ \hline
5 & 5\\ \hline




Factor sum & \textnormal{Number}\\ \hline
 6 &  6\\ \hline
 7 &  7\\ \hline
 8 &  8\\ \hline
 9 &  9\\ \hline
10 & 10\\ \hline




Factor sum & \textnormal{Number}\\ \hline
11 & 11\\ \hline
12 & 12\\ \hline
13 & 13\\ \hline
14 & 14\\ \hline
15 & 15\\ \hline




It looks like this:

Result of example output

rotating provides also an environment sidewaystable for landscape-form floating 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:

  \noalign{\hrule height 1pt\smallskip}
  1& 2& 3& 4& 5& 6& 7& 8& 9\cr
  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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.