29

I was wondering why elastic lengths like \fill seem to work only under certain conditions.

For instance if I write

A \hspace{\fill} B

I get A left aligned, B right aligned as wanted. If however I write

A \rule{\fill}{1mm} B

is results in a zero width rule. Just to be sure that I didn't just mix up parameters I also tried \rule{1mm}{\fill}. With fixed widths like \linewidth or 2cm \rule works fine.

Similiarily when I write

\begin{minipage}{\fill}
  Test. Test. Test. Test. Test. Test. Test. Test. Test. Test. Test. Test. 
\end{minipage}

I get output that looks like

Test.
Test.
Test.
Test.
Test.
...

i.e. presumably again zero width.

It is easy enough to find some solution for each specific problem on Google, usually coming down to "write these lines into your file" or "import that package". I want to understand why there is anything to solve to begin with so I can come up with my own solutions in the future.


Some minimal compileable example for demonstrating some cases:

\documentclass{article}

\parindent=0cm
\parskip=\fill % use of fill for vertical spacing.

\begin{document}

A \hspace{\fill} B 

C \rule{2cm}{1mm} D

E \rule{\fill}{1mm} F

\begin{minipage}{\textwidth}
  Test. Test. Test. Test. Test. Test. Test. Test. Test. Test. Test.
  Test. Test. Test. Test. Test. Test. Test. Test. Test. Test. Test.
\end{minipage}

\begin{minipage}{\fill}
  Test. Test. Test. Test. Test. Test. 
\end{minipage}

\end{document}

Compiled Screenshot

20

Rubber (elastic) lengths work for \hspace or \vspace, but not when setting a length needed by commands such as \rule, \parbox, \makebox or the minipage environment.

The \fill command is equivalent to 0pt plus 1fill, so when a rigid length is needed TeX coerces it to a dimension, in this case 0pt. To the contrary, \parskip accepts a rubber length as its argument, because it gives vertical spacing similar to \vspace. With

\setlength{\parskip}{\fill}

you're telling TeX that glue of zero width is wanted, but this glue can be used to fill the \textheight in case of need. The usual value is 0pt plus 1pt, so this space will not increase too much, unless page breaking requires building an underfull box according to all the parameters that govern page breaking.

Why \parbox or minipage can not be given rubber length values for their argument? Because TeX wants to typeset paragraphs in them, and for this it needs to know in advance the required line width.

On the other hand you can get “rubber rules”, but only resorting to lower level commands. With

A \leaders\hrule\hskip\fill{} B

(and \hskip\fill can be also the equivalent \hfill), you get the same as

A \hspace{\fill} B

but the space is filled with a rule. Here's an example:

\documentclass{article}

\setlength{\textwidth}{5cm} % just for the example

\begin{document}
\noindent A \hspace{\fill} B

\noindent A \leaders\hrule\hskip\fill{} B

\noindent A \leaders\hrule height -1pt depth 1.4pt\hfill{} B

\end{document}

Note how the second rule is lowered.

enter image description here

| improve this answer | |
  • So if I a paragraph Some Text \begin{minipage} ... \end{minipage} latex cannot calculate dynamically the width needed for the minipage? Or can the \leaders .. technique be adapted to that case? – kdb Aug 8 '13 at 16:25
  • @kdb No; but with the linegoal package you can achieve it. – egreg Aug 8 '13 at 17:50

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.