第二个单元格跨越三行:
\documentclass[12pt,landscape,a4paper]{article}
\usepackage{microtype}
\usepackage{multirow}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{longtable}
\usepackage[utf8]{inputenc}
\usepackage[english,czech]{babel}
\begin{document}
\begin{longtable}{p{2.1cm}p{3.4cm}}
\centering do not hyphen this & \centering please do hyphenate this longer one
\end{longtable}
\end{document}
这不会:
\usepackage[T1]{fontenc}
\usepackage{microtype}
\usepackage{lmodern}
\usepackage{longtable}
\usepackage[utf8]{inputenc}
\usepackage[english,czech]{babel}
\begin{document}
\begin{longtable}{p{2.1cm}p{3.4cm}}
\centering do not hyphen this & please do hyphenate this longer one
\end{longtable}
\end{document}
我是否可以以某种方式让 LaTeX 自己计算出我希望的文本适合两行,并且只有这样才可以将其居中?我实际上需要它用于表格(我通过编程生成,否则我可以根据需要添加和删除\centering
)。
答案1
一套microtype
尤其是当 pdfTeX 在 PDF 模式下使用时(pdflatex
),该软件包会增加一些灵活性(字符突出、字体扩展)。字体扩展尤其有用,因为它允许字母稍微拉伸或缩小以获得更好的换行效果。
以下示例显示,第二列中的条目由两行组成,甚至不需要连字符:
\documentclass[12pt,landscape,a4paper]{article}
\usepackage{microtype}
\usepackage{multirow}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{longtable}
\usepackage[utf8]{inputenc}
\usepackage[english,czech]{babel}
\begin{document}
\begin{longtable}{p{2.6cm}p{2.6cm}}
\centering do not hyphen this & \centering do hyphen this longer one
\end{longtable}
\end{document}
B. 增加\linepenalty
默认值为\linepenalty
10。如果增加该值,则换行符会受到更多惩罚,并且 TeX 会尝试查找行数较少的换行符:
\documentclass[12pt,landscape,a4paper]{article}
\usepackage{multirow}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{longtable}
\usepackage[utf8]{inputenc}
\usepackage[english,czech]{babel}
\begin{document}
\linepenalty=100
\begin{longtable}{p{2.6cm}p{2.6cm}}
\centering do not hyphen this & \centering do hyphen this longer one
\end{longtable}
\end{document}
C. 使用\looseness
更少的行
如果\looseness
设置为负值,则 TeX 会尝试用参数指定的更少的行数来排版当前段落。这里它成功了,并生成了与上例相同的两行:
\begin{document}
\begin{longtable}{p{2.6cm}p{2.6cm}}
\centering do not hyphen this
& \centering do hyphen this longer one\looseness=-1
\end{longtable}
\end{document}
D. 差异
两个版本的区别可以通过 看出\showlists
。第二行设置不同。
第一个版本带有microtype
,显示字符宽度缩小了百分之二:\T1/lmr/m/n/12 (-20)
。括号中的数字的单位是 ‰。
....\hbox(8.26648+4.35004)x73.97733, glue set - 0.17274
.....\glue(\leftskip) 0.0 plus 1.0fil
.....\T1/lmr/m/n/12 (-20) t
.....\T1/lmr/m/n/12 (-20) h
.....\T1/lmr/m/n/12 (-20) i
.....\T1/lmr/m/n/12 (-20) s
.....\glue 3.91661 plus 1.9583 minus 1.30554
.....\T1/lmr/m/n/12 (-20) l
.....\T1/lmr/m/n/12 (-20) o
.....\T1/lmr/m/n/12 (-20) n
.....\T1/lmr/m/n/12 (-20) g
.....\T1/lmr/m/n/12 (-20) e
.....\T1/lmr/m/n/12 (-20) r
.....\glue 3.91661 plus 1.9583 minus 1.30554
.....\T1/lmr/m/n/12 (-20) o
.....\T1/lmr/m/n/12 (-20) n
.....\T1/lmr/m/n/12 (-20) e
在其他情况下(\linepenalty
或\looseness
),词间空间缩小幅度更大(glue set - 0.69324
):
....\hbox(8.26648+4.35004)x73.97733, glue set - 0.69324
.....\glue(\leftskip) 0.0 plus 1.0fil
.....\T1/lmr/m/n/12 t
.....\T1/lmr/m/n/12 h
.....\T1/lmr/m/n/12 i
.....\T1/lmr/m/n/12 s
.....\glue 3.91661 plus 1.9583 minus 1.30554
.....\T1/lmr/m/n/12 l
.....\T1/lmr/m/n/12 o
.....\T1/lmr/m/n/12 n
.....\T1/lmr/m/n/12 g
.....\T1/lmr/m/n/12 e
.....\T1/lmr/m/n/12 r
.....\glue 3.91661 plus 1.9583 minus 1.30554
.....\T1/lmr/m/n/12 o
.....\T1/lmr/m/n/12 n
.....\T1/lmr/m/n/12 e
E. 总结
这三种方法可以结合起来:
\documentclass[12pt,landscape,a4paper]{article}
\usepackage{microtype}
\usepackage{multirow}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{longtable}
\usepackage[utf8]{inputenc}
\usepackage[english,czech]{babel}
\begin{document}
\begin{longtable}{p{2.6cm}p{2.6cm}}
\centering do not hyphen this &
\centering
\linepenalty=100
\looseness=-1\relax
do hyphen this longer one
\end{longtable}
\end{document}
F.\Centering
而不是\centering
使用时\centering
,由于行左右两侧的无限伸缩性,连字符实际上被禁用。软件包ragged2e
提供了 和 的变体\centering
,它们使用驼峰式命名,限制了无限伸缩性以平滑段落的粗糙度。软件包将\CenteringLeftskip
和设置为 ,而不是\CenteringRightskip
。比 更小的值可以在窄列中尝试,但它增加了出现未满警告的风险以及不必要的连字符。0pt plus 2em
0pt plus 1fil
2em
\hbox
但是,在这种情况下,这种方法可以减少线条的粗糙度,但会产生三条线条:
\documentclass[12pt,landscape,a4paper]{article}
\usepackage{microtype}
\usepackage{ragged2e}
\usepackage{multirow}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{longtable}
\usepackage[utf8]{inputenc}
\usepackage[english,czech]{babel}
\begin{document}
\begin{longtable}{p{2.6cm}p{2.6cm}}
\centering do not hyphen this &
\setlength{\CenteringLeftskip}{0pt plus 1em}%
\setlength{\CenteringRightskip}{0pt plus 1em}%
\Centering
do hyphen this longer one
\end{longtable}
\end{document}
答案2
如果正确理解了您的问题,那么包'ragged2e'会给出接近您所寻找的结果:
\documentclass[11pt,a4paper]{article}
\usepackage{microtype}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}% option "czech" is not relevant ...
\usepackage{ragged2e,array,longtable}%<-- added ragged2e and array
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{longtable}% <-------------
\setlength\PreviewBorder{1em}
\begin{document}
\begin{longtable}{>{\centering\arraybackslash}p{24mm}%<-- not allow hyphenation
>{\Centering\arraybackslash}p{24mm}}%<--- allow hyphenation
do not hyphen this & please do hyphenate this longer one
\end{longtable}
\end{document}
其中的代码和注释给出了基本的解释。