我有以下代码:
\documentclass[12pt]{article}
\begin{document}
\begin{tabular}{|l|}
\hline
the title \\
\hline
very very very very long \\
\hline
\end{tabular}
\end{document}
代码绘制的表格如下(请忽略字体和背景颜色):
我想根据第一行的宽度换行第二行文本(这样“非常非常非常非常长”的文本就会显示在几行中)。有人知道怎么做吗?
答案1
像这样?
\documentclass[12pt]{article}
\newlength{\mylen}
\settowidth{\mylen}{the title}
\begin{document}
\begin{tabular}{|l|}
\hline
the title \\
\hline
\multicolumn{1}{|p{\mylen}|}{very very very very long} \\
\hline
\end{tabular}
\end{document}
如果您不想要那些未满的盒子,请加载array
包并使用>{\raggedright\arraybackslash}p{\mylen}
:
\documentclass[12pt]{article}
\usepackage{array}
\newlength{\mylen}
\settowidth{\mylen}{the title}
\begin{document}
\begin{tabular}{|l|}
\hline
the title \\
\hline
\multicolumn{1}{|>{\raggedright\arraybackslash}p{\mylen}|}{very very very very long} \\
\hline
\end{tabular}
\end{document}
答案2
长列情况的另一种尝试。需要使用widthof{<content>}
from包。calc
代码
\documentclass[border=20pt]{standalone}
\usepackage{array,calc}
\begin{document}
\noindent
\begin{tabular}{|p{\widthof{the title}}|} \hline
the title \\ \hline
very very very very long \\ \hline
very very very very very very long \\ \hline
\end{tabular}
\end{document}