我正在尝试制作一个框,其中的换行符使得框中每行文本的长度大致相等。
例如:
\documentclass[11pt]{article}
\begin{document}
words words words words words words words words words words words words words words
\end{document}
在第一行会打印“单词”11次,但在第二行会打印3次。
我希望它在每一行打印“单词”7次,而不需要手动添加换行符。
目标是尽可能减少盒子中浪费的空间。
我需要这个应用程序是我有一个 2 列 tabularx,我希望第二列看起来尽可能好看。
\documentclass[11pt]{article}
\usepackage{tabularx}
\begin{document}
\begin{tabularx}{\textwidth}{ l X }
\bf Word & \bf Definition\\
\hline
\bf Apple & The usually round, red or yellow, edible fruit of a small tree, Malus sylvestris, of the rose family.\\
\hline
\bf Orange & A globose, reddish-yellow, bitter or sweet, edible citrus fruit.\\
\hline
\bf Banana & A tropical plant of the genus Musa, certain species of which are cultivated for their nutritious fruit.
\end{tabularx}
\end{document}
以下是我希望使用手动换行符的效果:
\documentclass[11pt]{article}
\usepackage{tabularx}
\begin{document}
\begin{tabularx}{\textwidth}{ l X }
\bf Word & \bf Definition\\
\hline
\bf Apple & The usually round, red or yellow, edible fruit of\\& a small tree, Malus sylvestris, of the rose family.\\
\hline
\bf Orange & A globose, reddish-yellow, bitter or sweet, edible citrus fruit.\\
\hline
\bf Banana & A tropical plant of the genus Musa, certain species\\& of which are cultivated for their nutritious fruit.
\end{tabularx}
\end{document}
我当前的解决方案是手动添加换行符,但是这很繁琐,而且我觉得有一种方法可以自动完成此操作。
那么有人有好办法来做到这一点吗?
答案1
这将尝试找到具有与全宽段落相同行数的最小宽度。
\documentclass[11pt]{article}
\usepackage{tabularx}
\begin{document}
% a couple of counters used in the tests below.
\newcount\zzc
\newcount\zzzc
%\zz{some text} is the main (only) macro defined.
\def\zz#1{{%
% the macro will set the text several times,
% will be very poor typographically
% so turn off all over/under full box warnings during tests
\hbadness\maxdimen\hfuzz\maxdimen
%
% first set the box at natural width with `\parfillskip` 0pt
% so the last line is full width, use \sloppy so the white space
% stretches. this box may look horrible (if you looked) eg
% \zz{hello world}
% would have hello at the left and world at the far right and a big
% gap in the middle.
% however save the number of lines (\prevgraf) that the paragraph took.
\setbox0\vbox{%
\sloppy \parfillskip 0pt
#1\par
\global\zzc\prevgraf}%
%
% Now set the same text in smaller and smaller widths
% reducing in steps of 5pt
\dimen0\hsize
\loop
\advance\dimen0 -5pt
\setbox0\vbox{%
\hsize\dimen0
\sloppy \parfillskip 0pt
#1\par
\global\zzzc\prevgraf}%
%
% The number of lines of this narrower paragraph is in zzzc
% if that is same as the original, we have less white space
% which is good. If if it is more than the original we have
% over-squeezed the paragraph so it is too narrow so taking more
% lines.
\ifnum\zzzc>\zzc\else
\repeat
%
% now the loop stops so \dimen0 is the first "bad" width
% so do the final setting with 5pt more than that which is
% the last good width with the tightest setting found
\noindent\strut\vtop{\hsize=\dimexpr\dimen0+5pt\relax
\sloppy \parfillskip 0pt
#1}}}
% example use in a table, as requested.
\noindent
\begin{tabularx}{\textwidth}{ lX }
\bfseries Word & \bfseries Definition\\
\hline
\bfseries Apple & \zz{The usually round, red or yellow, edible fruit of
a small tree, Malus sylvestris, of the rose family.}\\
\hline
\bfseries Orange & \zz{A globose, reddish-yellow, bitter or sweet, edible citrus fruit.}\\
\hline
\bfseries Banana &\zz{A tropical plant of the genus Musa, certain species
of which are cultivated for their nutritious fruit.}
\end{tabularx}
\end{document}