1

I'd like to have a table column with the following properties

  • allow text with explicit linebreaks
  • automatically break long lines at a maximum width
  • only extend column width to minimal necessary width

I have tried the following

\documentclass[a4paper]{article}
\usepackage{tabularx}

\begin{document}

\begin{table} \begin{tabular}{l|l} 1 & tabular l \ \hline & \begin{tabular}{@{}l@{}} short first line \ short second line \end{tabular} \end{tabular} \end{table}

\begin{table} \begin{tabular}{l|l} 2 & tabular l \ \hline & \begin{tabular}{@{}l@{}} short first line \ long second line which definitely needs a line break otherwise it runs off the page completely which would be a shame \end{tabular} \end{tabular} \end{table}

\begin{table} \begin{tabular}{l|p{0.6\textwidth}} 3 & tabular p \ \hline & short first line \newline short second line \end{tabular} \end{table}

\begin{table} \begin{tabular}{l|p{0.6\textwidth}} 4 & tabular p \ \hline & short first line \newline long second line which definitely needs a line break otherwise it runs off the page completely which would be a shame \end{tabular} \end{table}

\begin{table} \begin{tabularx}{0.7\textwidth}{l|X} 5 & tabulary X \ \hline & short first line \newline short second line \end{tabularx} \end{table}

\begin{table} \begin{tabularx}{0.7\textwidth}{l|X} 6 & tabulary X \ \hline & short first line \newline long second line which definitely needs a line break otherwise it runs off the page completely which would be a shame \end{tabularx} \end{table}

\end{document}

The desired appearance is like example 1 for the short/short lines

and like example 4 for the short/long lines

however, each of my methods only work for either the short/short or short/long case, but never both:
example 2 runs off the page, and example 3/5 make the column unnecessarily wide.

Pybe
  • 11
  • 2
  • If you are looking for the " jack of all trades" for tables, I recommend the tabularray package. For example is provides the varwidth library for columns with line breaks and automatic width. Nevertheless, I have my doubts that the same column specification can be the best one for all types of content. – cabohah Feb 14 '24 at 10:40
  • probably tabulary does what you want but I don't really understand your example with nested one column tabular where you are explicitly stopping line breaking by using a single column table withan l column. – David Carlisle Feb 14 '24 at 15:12
  • @DavidCarlisle the nested tabular is a workaround to add forced line breaks into an l column, copied from this answer – Pybe Feb 15 '24 at 14:42

2 Answers2

1

tabulary (by default) does not extend the table to the target width if it is naturally smaller

enter image description here

\documentclass[a4paper]{article}
\usepackage{tabulary}

\begin{document}

\begin{center}

\begin{tabulary}{\textwidth}{|l|J|} 1 & tabular l \ \hline & short first line \newline short second line \end{tabulary}

\bigskip

\begin{tabulary}{\textwidth}{|l|J|} 2 & tabular l \ \hline & short first line \newline long second line which definitely needs a line break otherwise it runs off the page completely which would be a shame \end{tabulary}

\end{center}

\end{document}

David Carlisle
  • 757,742
  • This is going in the right direction! Thanks for pointing out the J column type. The column in your example 1 still extends quite beyond what would be necessary for holding the two short lines (compare it to example 1 in the question). Do you know why this is the case and how to make it really the minimal necessary width? – Pybe Feb 15 '24 at 14:45
  • @Pybe tabulary has a parameter to set the minimum width of columns (to prevent it scrunching up whole paragraphs into columns 1 letter wide) but you can reduce it see the package doc. – David Carlisle Feb 15 '24 at 14:47
  • I checked the docs, but couldn't get tymin to work. Also, it seems connected to \newline - the final width of the column seems roughly the length of text without manual line break. If I put the two lines in separate rows & short first line \\ & short second line, the width is correct. Can I maybe add a new line differently so tabulary can infer the correct minimal width over all lines? – Pybe Feb 15 '24 at 16:43
  • @Pybe ah you are probably right, I wrote TY over a couple of weekends last century sometime: I may have forgotten some details:-) its measure of how much content a column has does, as you see, involve a first pass setting everything on one line. – David Carlisle Feb 15 '24 at 16:53
  • I solved it using the workaround above: instead of \newline I create line breaks by writing into additional rows. Should I add this to your answer so I can accept it? – Pybe Feb 16 '24 at 08:08
  • @Pybe or post (and accept) a complete answer yourself leaving this as the basic hint (I really can live without the 15 pt for a green tick, thanks:-) – David Carlisle Feb 16 '24 at 09:16
0

The answer of DavidCarlisle was almost there, but needed a little tweak.
The column in the short/short case was still too wide due to how tabulary calculates render width.

I fixed this by replacing the manual \linebreak inside a single cell by rendering each line in a separate row, like this

\documentclass[a4paper]{article}
\usepackage{tabulary}

\begin{document}

\begin{center}

\begin{tabulary}{\textwidth}{|l|J|} 1 & tabulary J \ \hline & short first line \ & short second line \end{tabulary}

\bigskip

\begin{tabulary}{\textwidth}{|l|J|} 2 & tabulary J \ \hline & short first line \ & long second line which definitely needs a line break otherwise it runs off the page completely which would be a shame \end{tabulary}

\end{center}

\end{document}

The result is then as desired in all cases: enter image description here

Pybe
  • 11
  • 2