6

Attention: This is an advanced TeX problem.

I usually dislike lines that are too small, and so I tend to add

\parfilskip=2em plus 0.6\textwidth

to my documents (apart from small lines, this also avoids last lines that end too close to the right margin, making sure the paragraph ends at least 2ems from the margin).

My problem is on figures, in which I also like to have a centred last line. Memoir and other classes/packages provide a macro for this effect, eg \centerlastline from Memoir, which is defined as:

\leftskip=0pt plus 1fil
\rightskip=0pt plus -1fil
\parfilskip=0pt plus 2fil

This clever code allows the last line to be centred, but sometimes the last line is short (for example, only one word). What I would like to do is tweak that macro so that the stretchability is not an infinite stretch. My first approach was to try

\leftskip=0pt plus 0.3\textwidth
\rightskip=0pt plus -0.3\textwidth
\parfillskip=0pt plus 0.6\textwidth

My intuition was that for mid-paragraph lines, stretchability would be calculated as 0 and they would therefore be written in their right place, while the last like would have enough stretchability on the left and right to be absolutely centred. Unfortunately, this does not work, as the paragraph is then typeset in a wobbly way

\documentclass[10pt,a4paper]{article}
\usepackage{lipsum}
\begin{document}

\leavevmode \vskip-10em \parindent=0pt \hsize=12cm

\textbf{REGULAR}\par
\lipsum[1] \vskip\baselineskip

\textbf{REGULAR / NOT SMALL}\par
{\parfillskip=2em plus 0.6\textwidth
 \lipsum[1]} \vskip\baselineskip

\textbf{CENTRED}\par
{\leftskip=0pt plus 1fil
 \rightskip=0pt plus -1fil
 \parfillskip=0pt plus 2fil
 \lipsum[1]} \vskip\baselineskip

\textbf{CENTRED / NOT SMALL}\par
{\leftskip=0pt plus 0.3\textwidth
 \rightskip=0pt plus -0.3\textwidth
 \parfillskip=0pt plus 0.6\textwidth
 \lipsum[1]} \vskip\baselineskip

\end{document}

Result of code above

The problem (as far as I can see) is that spaces also contribute to the stretchability of a line. This means that:

  1. the total stretchability of the mid-paragraph lines is not 0
  2. and so it gets distributed,
  3. which means that the left margin gets a positive value and the right margin the corresponding negative value
  4. finally leading to loose lines being shifted to the right (tight lines do not stretch at all and so they are set in place).

Any thoughts on this problem?

  • 2
    Don't put code on links that will probably expire in the feature. Copy your examples in the question. – Ulrike Fischer Nov 25 '15 at 19:20
  • not sure I understand but may be this question help you http://tex.stackexchange.com/questions/206955/how-to-access-the-last-line-of-a-paragraph-with-luatex – touhami Nov 25 '15 at 19:54
  • @UlrikeFischer Sorry, changes done. I also added the resulting PDF I'm getting as a PNG image. – jdferreira Nov 25 '15 at 21:06
  • @touhami I've looked at that and I have indeed found a solution. I'll post it in a few moments. – jdferreira Nov 25 '15 at 21:16

1 Answers1

6

touhami's suggestion in the comments made me find an answer for this admittedly niche problem. The solution is to construct the paragraph as if it was being normally typeset and then handle the positioning of the last paragraph manually. Here's how it goes:

\textbf{CENTRED / NOT SMALL}\par
{
    % Typeset as we did previously for the "regular / not small" case
    % by specifying a custom \parfillskip
    \parfillskip=2em plus 0.6\textwidth
    % Create a vbox with the content needed
    \setbox0=\vbox{\lipsum[1]}
    \setbox1=\vbox{%
        % Unwind the previously build box
        \unvbox0
        % put the last box (which is the last line of the paragraph)
        % in its own register
        \setbox2=\lastbox
        % place this box on a new hbox but remove the two last
        % glues, which are the parfillskip and the right margin
        % this box also has \hfill on both sides to centre it
        \hbox to \textwidth{\hfill\unhcopy2 \unskip\unskip\hfill}
    }
    % Unwind the vertical box previously constructed
    \unvbox1
}

The result is that the last line, indeed, is not as short as in the uncustomised paragraph and is also centred:

The result of the code above