2

In a typical paragraph, the first line is indented by some amount (let's say, 1em).

For some optical effect, I would like the last line of each paragraph to be also indented by, say, 1em. The rest of the last line should be left aligned and the lines between the first and last lines to be justified as usual.

An optical example would be like this:

  This paragraph is indented by 1em    <- this line is indented 1em
at the first line. After that some     <-\
text and some more to give you the     <-|
impression that all lines are justi-   <-+- all these lines justified 
fied and left aligned and then the     <-|
last line is indented by the same      <-/
  amount as the first.                 <- this line indented and left aligned
^
This small amount of space is what I am after!

I tried the following with various values for leftskip and rightskip but those affect all lines, not just the last.

\documentclass{article}
\usepackage{lipsum} 
\begin{document}
 
\setlength{\parindent}{1em}
\setlength{\leftskip}{1em plus -1fil}
\setlength{\rightskip}{0pt plus 1fil}
\par
\lipsum[1-20]

\end{document}
3
  • I'd imagine the difficulty here is when the last line has a width >= \linewidth - 1em. – Werner Jan 18 at 19:47
  • do you want the middle lines to be left aligned (ragged right) or justified. You say "left aligned as usual" but the usual style is justified? – David Carlisle Jan 18 at 22:05
  • @DavidCarlisle I meant: middle lines justified, last line left aligned. I'll clarify the question. – David Woitkowski Jan 19 at 8:09
3

With the help of some methods presented here and by using grabbox we can at least introduce this manually on a per paragraph basis (grabbox so that the paragraph can still contain verbatim material).

I don't think this is possible in LaTeX to be automatically applied to every normal text paragraph.

It works by first collecting the paragraph into box register 0 as a \vbox. This is done with \parfillskip=\parindent plus 1fil so that TeX tries to have at least one \parindent space in the last line. Next step is to store into a box register again (and again as a \vbox), this is done so that we can do some alterations. First put the original contents back, then remove the last box (being the horizontal box of the last line) and store it in a box register. Next output that box register again but with a \hskip\parindent before it, so that it'll be correctly indented.

We can do all this with a single box register 0 as the assignments are done locally and scoped within each nested use of a box.

Also we use some code from https://tex.stackexchange.com/a/463739/117050 to fix the space between consecutive \indentlastlinepars (which unfortunately needs another box register, the code would be way more confusing if it had only used register 0).

Beware: The current implementation creates paragraphs that are not page breakable.

\documentclass[]{article}

\usepackage{grabbox}

\newcount\myprevgraf

\newcommand\indentlastlinepar
  {%
    \par
    \begingroup
    \parfillskip\parindent plus 1fil\relax
    % save the number of lines of the paragraph typeset in the box
    \grabbox0\vbox[\par\global\myprevgraf\prevgraf]
      {%
        % measurement as in https://tex.stackexchange.com/a/463739/117050
        \setbox0\vtop{\unvcopy0}%
        \setbox2\vbox{\unvcopy0}%
        \dimen0\dimexpr\dp0-\dp2\relax
        % only make changes if the paragraph had more than one line
        \ifnum\myprevgraf>1
          % changing the last line as in
          % https://tex.stackexchange.com/a/280035/117050
          \setbox0\vtop
            {%
              \unvbox0
              \setbox0\lastbox
              \hbox to \linewidth{\hskip\parindent\unhbox0\uskip\unskip\hfill}%
            }%
        \fi
        % introducing additional space and setting the correct depth as in
        % https://tex.stackexchange.com/a/463739/117050
        \dp0=\dp2
        \box0
        \vskip\dimen0
        \endgroup
      }%
  }

\usepackage{duckuments}

\begin{document}
\indentlastlinepar{\blindduck[1]}
\indentlastlinepar{\blindduck[2]}
\indentlastlinepar{\blindduck[3]}
\indentlastlinepar{\blindduck[4]}
\indentlastlinepar{\blindduck[5]}
\indentlastlinepar{This is a paragraph containing \verb|\verbatim| material.}
\end{document}

An alternative that works if multiple consecutive \indentlastlinepar are used and that is page breakable. You can suppress the additional space added above the paragraph by using the starred variant \indentlastlinepar*.

\documentclass[]{article}

\usepackage{grabbox}

\newcount\myprevgraf

\NewDocumentCommand\indentlastlinepar{s}
  {%
    \par
    \begingroup
    \parfillskip\parindent plus 1fil\relax
    % save the number of lines of the paragraph typeset in the box
    \grabbox0\vtop[\par\global\myprevgraf\prevgraf]
      {%
        % only make changes if the paragraph had more than one line
        \ifnum\myprevgraf>1
          % changing the last line as in
          % https://tex.stackexchange.com/a/280035/117050
          \setbox0\vtop
            {%
              \unvbox0
              \setbox0\lastbox
              \hbox to \linewidth{\hskip\parindent\unhbox0\unskip\unskip\hfill}%
            }%
        \fi
        % introducing additional space dependent on the current vtops height
        \IfBooleanF{#1}
          {%
            \ifdim\ht0<\baselineskip
              \vskip\dimexpr\baselineskip-\ht0\relax
            \fi
          }%
        % unbox the contents
        \unvbox0
        \endgroup
      }%
  }

\usepackage{duckuments}

\begin{document}

\indentlastlinepar{\blindduck[-]}
\indentlastlinepar{This is a paragraph containing \verb|\verbatim| material.}
\indentlastlinepar{\blindduck[-]}
\indentlastlinepar{\blindduck[-]}
\indentlastlinepar{\blindduck[-]}
\indentlastlinepar{\blindduck[-]}
\indentlastlinepar{\blindduck[-]}
\indentlastlinepar{\blindduck[-]}

\end{document}

enter image description here

3

This takes two runs of latex, the first run counts the number of lines of any paragraph prefixed by \zz and on the second pass uses that information to construct a \parshape If the parshape changes the line count, an extra run may be needed.

enter image description here

\documentclass[twocolumn]{article}

\newcount\zzpar

\makeatletter
\def\zz{%
\global\advance\zzpar\@ne
\expandafter\let\expandafter\zzn\csname zzcnt\the\zzpar\endcsname
\ifx\zzn\relax
\else
\parshape \zzn
\zzloop\zzn
\parindent \dimexpr\linewidth-\parindent\relax
\fi
\def\par{\endgraf
\edef\tmp{\immediate\write\@auxout{%
\global\string\@namedef{zzcnt\the\zzpar}{\the\prevgraf}}}%
\tmp
\let\par\endgraf
}%
}

\def\zzloop#1{%
\ifnum#1>\@ne
\z@\linewidth
\zzloop{\numexpr#1-1\relax}\fi}


\makeatother

\begin{document}



\zz
This paragraph is indented by 1em
at the first line. After that some
text and some more to give you the
impression that all lines are justi-
fied and left aligned and then the
last line is indented by the same
amount as the first.



\end{document}
1
  • Setting \parfillskip=\parindent plus 1fil might reduce the number of cases where more than two runs are necessary. – Skillmon Jan 19 at 8:52

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.