结合使用tikz
和 包varwidth
,我发现自己尝试使用不同的段落宽度值,以便实现“最佳”纵横比。理想情况下,我希望有一个类似的环境,varwidth
它会尽最大努力尝试生成具有给定纵横比的段落。
\begin{aspectparagraph}{2.5}
some text, which would automatically be formatted with
different widths, so as to find the width which is closest
to (in this case) 2.5. This could be approximated by computing the total
text ``natural'' length, and then computing an approximate width based on the
line height.
\end{aspectparagraph}
答案1
这是一个尝试:我计算面积,然后用一些数学运算设置盒子的宽度。
\documentclass{article}
\usepackage{environ,expl3}
\ExplSyntaxOn
\box_new:N \l_yossi_longline_box
\box_new:N \l_yossi_final_box
\dim_new:N \l_yossi_base_dim
\NewEnviron{aspectparagraph}[1]
{
\hbox_set:Nn \l_yossi_longline_box { \BODY }
\dim_set:Nn \l_yossi_base_dim
{
\fp_to_dim:n {
sqrt (
\dim_to_fp:n { \box_wd:N \l_yossi_longline_box } *
\dim_to_fp:n { \baselineskip } * #1
)
}
}
\vbox_set:Nn \l_yossi_final_box
{
\hsize = \l_yossi_base_dim
\emergencystretch = \hsize
\hyphenpenalty=0
\parindent = 0pt
\strut\BODY\strut
}
% the next two lines are just for debugging
\par Height~ = ~ \dim_eval:n { \box_ht:N \l_yossi_final_box }
\par Width ~ = ~ \dim_eval:n { \box_wd:N \l_yossi_final_box } \par
\leavevmode \box_use_drop:N \l_yossi_final_box
}
\ExplSyntaxOff
\begin{document}
\begin{aspectparagraph}{2.5}
some text, which would automatically be formatted with
different widths, so as to find the width which is closest
to (in this case) 2.5. This could be approximated by computing the total
text ``natural'' length, and then computing an approximate width based on the
line height.
\end{aspectparagraph}
\begin{aspectparagraph}{2}
some text, which would automatically be formatted with
different widths, so as to find the width which is closest
to (in this case) 2. This could be approximated by computing the total
text ``natural'' length, and then computing an approximate width based on the
line height.
\end{aspectparagraph}
\begin{aspectparagraph}{1}
some text, which would automatically be formatted with
different widths, so as to find the width which is closest
to (in this case) 1. This could be approximated by computing the total
text ``natural'' length, and then computing an approximate width based on the
line height.
\end{aspectparagraph}
\end{document}
也许人们可以想象做一些尝试,然后选择“最佳”的一个。