3

There are a couple of questions asking for a description environment where the width of the label-portion is the width of the widest label:

Label:         Text text text text text text
               text text text text.
Longer label:  Text text text text text text
               Text text text text.

One solution is to use enumitem to set the label width manually (also here). Another approach is to specify the widest label manually (also here).

I am working on a dictionary that will be hundreds of pages in length. Presumably, the widest word in the whole dictionary will be something of an outlier. I would like instead the label width to be appropriate for the longest word on the page. (I'm guessing this would take two LaTeX runs, along the lines of longtable or something like that.)

So for example, the alignment of ‘Nam gui ligula’ would match on each page in the example below. It would match the width of ‘Lorem ipsum’ on the first page, but ‘Lorem ipsum dolor sit’ on the second page, etc.

\documentclass[11pt,a4paper]{article}
\usepackage{enumitem,lipsum}
\begin{document}
\begin{description}
\item[Lorem] \lipsum[2-3]
\item[Lorem ipsum] \lipsum[2-3]
\item[Lorem ipsum dolor] \lipsum[2-3]
\item[Lorem ipsum dolor sit] \lipsum[2-3]
\item[Lorem ipsum dolor sit amet] \lipsum[2-3]
\end{description}
\end{document}

I have tagged this for the description environment, but I'm open to any solution. Here is an MWE without any list-like environment. I'd be looking for a way to compute \ComputedWidth for each page.

\documentclass[11pt]{article}
\usepackage{enumitem,lipsum}
\begin{document}
\parindent=0pt
\newdimen\ComputedWidth
\ComputedWidth=9em
\def\FixedWidthBox#1{\fbox{\hbox to \ComputedWidth{#1\hfill}}}

\FixedWidthBox{Lorem} \lipsum[2-3]

\FixedWidthBox{Lorem ipsum} \lipsum[2-3]

\FixedWidthBox{Lorem ipsum dolor} \lipsum[2-3]

\end{document}
  • 1
    It would be helpful if you could add a minimal working example that people can work from. – Andrew May 2 '16 at 4:15
  • if you expect description of items (i.e. \lipsum[2-3]) to be breakables cross pages this will be difficult. – touhami May 2 '16 at 7:43
  • Imho it will look odd if the text width differs on every page, even more if the document is twoside. Beside this you will run into problems if page breaks can happen in the description text, as paragraphs are made before tex decides where to insert the page break. – Ulrike Fischer May 2 '16 at 7:54
  • Yes, I agree with you there. This is for a dictionary, though, and the idea is to have all of the pronunciations aligned, no matter the width of the head word. If I could get the maximum width of each word per page, I could even just put all the headwords into hboxs with that width. – adam.baker May 2 '16 at 9:51
1

Here is a working solution. After the first run things aren't aligned. The second time through they are.

\documentclass[11pt]{article}
\usepackage{enumitem,lipsum,atbegshi,ifthen}
\parindent=0pt

\IfFileExists{\jobname .ali}{\input{\jobname .ali}}{}

\newwrite\AlignmentOutput
\immediate\openout\AlignmentOutput=\jobname .ali

\newlength\LatestWidth
\newlength\WidestWidth
\setlength\WidestWidth{0pt}

\newsavebox{\mybox}

\def\FixedWidthBox#1{%
    \settowidth{\LatestWidth}{#1}%
    \ifthenelse{\lengthtest{\LatestWidth>\WidestWidth}}{\global\setlength\WidestWidth{\LatestWidth}}{}%
    \newdimen\Width
    \ifcsname WidestBoxOnPage\thepage\endcsname% 
        \setlength\Width{\csname WidestBoxOnPage\thepage\endcsname}%
    \else%
        \sbox{\mybox}{#1}
        \settowidth{\Width}{\usebox{\mybox}}
    \fi%
    \fbox{\hbox to \Width{#1\hfill}}%
}

\AtBeginShipout{\immediate\write\AlignmentOutput{\string\expandafter\string\def\string\csname\space WidestBoxOnPage\thepage\string\endcsname{\the\WidestWidth}}%
\global\setlength\WidestWidth{0pt}}

\begin{document}
\FixedWidthBox{Lorem} \lipsum[2-3]

\FixedWidthBox{Lorem ipsum} \lipsum[2-3]

\FixedWidthBox{Lorem ipsum dolor} \lipsum[2-3]

\FixedWidthBox{Lorem} \lipsum[2-3]

\FixedWidthBox{Lorem ipsum} \lipsum[2-3]

\FixedWidthBox{Lorem} \lipsum[2-3]
\end{document}

Update This solution ‘works’ in the sense of answering the question. I've found that practically, however, it's unworkable. The (occasionally) large changes in the size of the box change the number of lines in the paragraphs, which changes the pagination, which leads to TeX remembering the wrong box sizes between runs. I would guess that this solution could work if the page consisted of short paragraphs (especially one-liners), but I had to abandon this approach for my dictionary, which included longer paragraphs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.