7

Is it possible to define a glue that TeX will not stretch any further than a given limit--under no circumstances?

In the following MWE, I want to set a piece of text apart from the rest by half a line, and in case that piece of text lies on a page break, that half line should be allowed to be stretched to one full line, so that the whole text stays on the baseline grid and fills the page. However, I never want the glue to stretch more than one line, no matter if the page would become underfull or not.

\documentclass{memoir}

\usepackage{tikz, lipsum, everypage}

% show the baseline grid
\makeatletter
\newcommand{\left@margin}{}
\AddEverypageHook{
    \ifodd\value{page}\relax%
        \renewcommand{\left@margin}{\spinemargin}
    \else
        \renewcommand{\left@margin}{\foremargin}
    \fi
    % + \trimtop because it is also "\trimbottom"
    \tikz[overlay, remember picture, background rectangle/.style={fill=none},
            shift={(current page.south west)},xshift={\left@margin + \trimedge},
            yshift={\lowermargin + \trimtop}]%
    {%
        \draw[red!20!white,thin] (0,0) grid[step=\onelineskip] (\textwidth,
        \textheight);%
        \draw[red,thin] (0,0) rectangle (\textwidth,\textheight);
    }%
}%
\makeatother

\parskip=0pt

\begin{document}

\lipsum*[1-2]

\vskip .5\baselineskip plus .5\baselineskip % HERE should be the .5 limit

This is a test line of text to show my case, and I needed a few words to fill the line.

\vskip .5\baselineskip plus .5\baselineskip % HERE should be the .5 limit

\lipsum*[1-2]  Morbi auctor lorem non justo. Nam lacus libero, pretium at, lobortis
Morbi auctor lorem non justo. Nam lacus libero, pretium at, lobortis Morbi auctor
lorem non justo. Nam lacus libero, pretium at, lobortis Nam lacus libero, pretium at,
lobortis Morbi auctor lorem non justo. Nam

\section{Test}

\lipsum*[1]

\end{document}

The result looks like this:

enter image description here

You can see that the two lines including the space before and after it occupy 5 lines instead of just 4 lines.

MiB
  • 837
  • 4
  • 10
  • 1
    Glue will always stretch in continuous way, not in discrete steps. – egreg Feb 01 '14 at 16:42
  • Yes, indeed, but that is not exactly my question. My question is how to impose a limit on the amount it stretches. What, after all, is the use of giving a plus 1mm if you will then end up with 1cm? – MiB Feb 01 '14 at 17:25
  • You simply can't. Stretchable glue can stretch to infinity. – egreg Feb 01 '14 at 17:32
  • Darn! :( Is there an inherent reason for it? I mean, why then the possibility to specify an exact amount? Wouldn't it be more appropriate then to just use integers for glue so that one can specify relations of stretchability? Like 4pt plus 5 and 4pt plus 10 to get glue that can stretch twice as fast as the other glue? – MiB Feb 01 '14 at 17:42
  • Because Knuth so decided. – egreg Feb 01 '14 at 17:45
  • Ok, fair enough. so let me ask something else: is there a (variable, changable) penalty for TeX to stretch beyond the specified amount? – MiB Feb 01 '14 at 17:50
  • @MiB the \badness of a line is (mostly) a measure of how far glue has stretched beyond its limits – David Carlisle Feb 01 '14 at 19:04

1 Answers1

7

If the stretch component is non zero then the glue will stretch arbitrarily large (but with increasing badness) so if you want an upper limit then the plus component has to be zero. You can however use a length of

1\baselineskip minus 0.5\baselineskip

which is restricted to the finite range that you specify, however its natural length is the upper rather than lower limit, so you may need to "encourage" it to shrink.

Alternatively you can use fixed units and not allow stretch or shrink

\vskip 1\baselineskip
\penalty0
\vskip-0.5\baselineskip

will be a half baselineskip unless a page break happens at the penalty, in which case it will be a full baselineskip (with the -0.5\baselineskip glue discarded at the break)

David Carlisle
  • 757,742
  • Yeah, this is what I have been playing with already. I have no idea how to "encourage it to shrink", though. That seems to be more difficult than to manually fix all the pages where the glue has been stretched too far :-( This seems like a serious limitation of TeX to me. – MiB Feb 01 '14 at 19:05
  • @MiB TeX's page breaking algorithm (and its box and glue model generally) is not optimised (to put it politely) for grid based layouts. – David Carlisle Feb 01 '14 at 19:08
  • @MiB see updated answer for a plan b – David Carlisle Feb 01 '14 at 19:13
  • I really appreciate your creativity and you trying to help. Makes me realize new possibilities; however, in this case plan b doesn't solve the problem because the page break should cause the 1\baselineskip to be in the middle of the page, not at the end (otherwise the page looks too short). But I will experiment with the badness parameters, maybe I can find a satisfying solution that way. – MiB Feb 01 '14 at 19:41
  • 2
    @MiB yes I know it's only partial solution which is why I didn't slot it into your MWE, just pointing you in the direction of things you can do in TeX rather than things you can't:-) – David Carlisle Feb 01 '14 at 19:43