LaTeX中如何避免文本溢出固定容器并裁剪文本
解决LaTeX文本固定高度裁剪问题
嘿,这个问题我熟!你原来用的\parbox[][100pt][t]{3cm}里的第二个参数是最小高度,不是最大高度——所以文本太长时,盒子会自动被撑大,根本拦不住溢出的内容。要把文本严格限制在100pt高度内,裁剪掉超出部分,这里有两个实用的方案:
方案1:用adjustbox包(推荐,语法更直观)
adjustbox是个功能强大的盒子调整工具,支持直接设置高度+裁剪,代码写起来很清爽:
\documentclass{article} \usepackage{adjustbox} % 导入adjustbox包 \begin{document} \fbox{ \adjustbox{height=100pt, clip, width=3cm, valign=t}{ The quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. } } \end{document}
参数说明:
height=100pt:把盒子的高度固定为100ptclip:开启裁剪模式,超出高度的内容会被自动剪掉width=3cm:和你原来的parbox一样固定宽度valign=t:让内容顶部对齐,和原代码的t参数行为一致
方案2:用trimclip包(手动控制裁剪范围)
如果你喜欢更精细的控制,可以用trimclip包手动计算并裁剪溢出部分:
\documentclass{article} \usepackage{trimclip} % 导入trimclip包 \begin{document} \fbox{ \clipbox{0pt {-\dimexpr\totalheight-100pt\relax} 0pt 0pt}{ \parbox[t]{3cm}{ The quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. the quick brown fox jumps right over the lazy dog. } } } \end{document}
参数说明:
\clipbox{左 下 右 上}:四个参数分别对应四个方向的裁剪量-\dimexpr\totalheight-100pt\relax:计算文本总高度超出100pt的部分,用负数值表示从底部向上裁剪掉这部分,只保留顶部100pt的内容
内容的提问来源于stack exchange,提问作者De Wolf Maxime
相关产品推荐
相关产品推荐

