我想要一个具有以下属性的表列
- 允许带有明确换行符的文本
- 自动断开最大宽度的长行
- 仅将列宽扩展到必要的最小宽度
我已经尝试过以下
\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}
所需的外观与示例 1 中的短/短线类似
和示例 4 中的短/长线类似
但是,我的每种方法仅适用于短/短或短/长的情况,但不能同时适用于这两种情况:
示例 2 超出了页面范围,而示例 3/5 使列不必要地宽。
答案1
tabulary
(默认情况下)如果表格自然宽度较小,则不会将其扩展到目标宽度
\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}
答案2
这DavidCarlisle 的回答几乎完成了,但需要进行一些调整。由于计算渲染宽度
的方式,短/短情况下的列仍然太宽。tabulary
\linebreak
我通过将每行渲染到单独的行中来替换单个单元格内的手册来解决此问题,如下所示
\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}