Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

5 Answers

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.

share|improve this answer
add comment

(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.

share|improve this answer
    
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
add comment

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.

share|improve this answer
add comment

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:

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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