5

I using tikz together with package varwidth, I find myself playing out with different values for paragraph width, so as to achieve an "optimal" aspect ratio. Ideally, I would have liked to have an environment similar to varwidth which would make a best effort attempt to produce a paragraph with a given aspect ratio.

\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}
David Carlisle
  • 757,742
Yossi Gil
  • 15,951

1 Answers1

7

Here's an attempt: I compute the area and then with some math I set the box width.

\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}

enter image description here

Maybe one can imagine doing some tentative attempts and then choose the “best” one.

egreg
  • 1,121,712