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.




tabularraypackage. 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:40tabularwhere you are explicitly stopping line breaking by using a single column table withanlcolumn. – David Carlisle Feb 14 '24 at 15:12tabularis a workaround to add forced line breaks into anlcolumn, copied from this answer – Pybe Feb 15 '24 at 14:42