# tabular environment that is similar to html

How can I simulate a HTML table? That is: I want to give the table a maximum width (for example \textwidth) and the table should automatically distribute the necessary space according to the contents of each cell.

So something structured like this:

<table border="1">
<tr>
<td>foo</td><td>this is a longer text<br /> with a few lines that might be even without<br/> sense</td>
</tr>
<tr>
<td>bar</td><td>not so long</td>
</tr>
</table>


should result in something similar to that:

-
If you're just looking for the calculation of column widths, it seems tabulary and tabu offer this (maybe I'll sometime publish DocScape's table model as a LaTeX package, that would be the third option then...). –  Stephan Lehmke May 6 '13 at 18:03
@StephanLehmke how could I have missed these? They look very promising. –  topskip May 6 '13 at 18:10
@StephanLehmke tabu did the trick for me. If you don't write an answer, I'll post the code that I've used. Thanks! –  topskip May 6 '13 at 18:54
@topskip Please do. I'd like to see the LaTeX version too :-) –  Stephan Lehmke May 6 '13 at 18:59
@dıʞsdoʇ You should read the booktabs documentation – vertical lines are evil! ;) –  doncherry May 6 '13 at 19:23

@StephanLehmke gave me the hint:

\documentclass[paper=a4]{scrartcl}
\usepackage{tabu}

\begin{document}
\hline
foo & this is a longer text\par with a few lines that might be even without\par sense \\
bar & not so long \\
\hline
\end{tabu}

\end{document}


which gives:

-
Somehow the interline spacing looks odd... –  Stephan Lehmke May 6 '13 at 19:15
@StephanLehmke a known bug of tabu: it forgets a \strut at the end of its cells. And since »sense« has no descenders... –  clemens May 6 '13 at 22:21

Simulating HTML is the algorithm used by the tabulary package (named because y comes after x) by David Carlisle:

\documentclass{article}
\usepackage{tabulary}
\begin{document}
\begin{tabulary}{0.4\linewidth}{|LL|}
foo & this is a longer text\newline
with a few lines that might be even without\newline
sense\\
bar & not so long
\end{tabulary}
\end{document}


The vertical lines are just there to show the extent of the box. I should use this package more often; it looks like it does a good job.

-
Thanks for your answer, Will! This gets closer to what I need, but I have to know the width of the table in advance, which I don't know. –  topskip May 7 '13 at 5:55
@dıʞsdoʇ Ah, so you want like a regular tabular except that it allows line breaks and stretches each (non-"p") column to fit, but doesn't line-wrap so simply takes up as much space as the longest line in each column? I don't know of a canned solution off the top of my head, but using varwidth in each column preamble might help. –  Will Robertson May 9 '13 at 15:21

You can obtain this also with the tabularx package, but look better without vertical rules and the horizontal rules of booktabs package.

\documentclass{scrartcl}
\usepackage{tabularx,booktabs}
\begin{document}
\noindent\begin{tabularx}{.4\linewidth}{lX}
\toprule
foo & this is a longer text\par
with a few lines that might be even without\par
sense \\
\midrule
bar & not so long \\
\bottomrule
\end{tabularx}
\end{document}

-
But with tabulary I have to give the explicit widths of the whole table and which cells can expand. And this is not what I need. –  topskip May 7 '13 at 5:53
What you need exactly about the width? It width can be a any fixed measure ad 20cm, or any relative relative measure to the width of the main text as \linewidth (or anything else predefined measure in your document), that change automatically if you change change the paper size, the margins (or anything else related with your relative distance). Then, where is the problem? –  Fran May 7 '13 at 18:13
I don't want to specify any width at all. I don't know which column has more material. Therefore I'd have to specify XX as the table columns, which is clearly not what I want. But thanks anyway! –  topskip May 7 '13 at 21:03