[Tex/LaTex] Starting a line where the previous line finishes

horizontal alignmentindentationpoetryverse

I have been using LaTeX for standard things like letters and scientific reports.
I recently came to visit my grandmother who writes some poetry and told her I could make a nice pdf from her handwritten verses — which I start to regret :P…
I used the package verse and it is going all right, but in some specific poems, she wants a very 'exotic' kind of positioning. She wants some lines to start exactly where the former line finishes like in this example:

verse

Do you know how I could, for instance, write a magic command \magicom{} that would generate the text above when used like:

This is some\\
\magicom{}text I would\\
like to\\
\magicom{}format\\
How can I do this with\\
\magicom{}LaTeX?

Best Answer

Standard TeX has one way to measure the last line in a partial paragraph, it is a bit sneaky but there you go. At least it makes coding inside the document then nice and easy: normal line breaks with \\ and special ones with \magicom. This allows even for some lines to be automatically broken.

\documentclass{article}

\makeatletter
\def\magicomadjust{0em}  % a way to adjust if the spacing should be different
\newdimen\indent@amount
\def\magicom{\relax
  \ifhmode $$%
    \predisplaypenalty\@M \postdisplaypenalty\@M
    \abovedisplayskip-\baselineskip \belowdisplayskip\z@
    \abovedisplayshortskip\abovedisplayskip
    \belowdisplayshortskip\belowdisplayskip
    \global\indent@amount\predisplaysize
     $$\count@\prevgraf \advance\count@-\thr@@
         \prevgraf\count@
    \global\advance\indent@amount-2em  % correction for \predisplaysize indent
    \global\advance\indent@amount\magicomadjust  % correction for verse env, for example
    \hspace*\indent@amount
  \else\typeout{*Not in hmode*}\fi}
\makeatother


\begin{document}

%\begin{verse}
This is some\magicom
text I would\\
like to\magicom
format and it nicely allows for several lines  written with automatic lines breaks as well, as we can see here.
How can I do this with\magicom
\LaTeX?
%\end{verse}

\end{document}

This then gives us:

enter image description here

If we uncomment the verse environment above and use

\renewcommand\magicomadjust{-4em}

we get the following:

enter image description here

What does the code do?

  • test if we are in horizontal mode (if not warning)
  • start a display math formula (indeed :-)
  • set various variables for math displays, so that this display doesn't really show up and mess up spacing (vertial skip is a negative baseline above and zero below, penalties are set so this isn't introducing a break, ...)
  • then we store the \predisplaysize away (that is the width of the last line before the display + 2em) ... everything else was just done to get this variable set, i.e., doing the measurement for us
  • then we end the display (and it takes up one line (empty) and because of the settings above this is not visible
  • after the display we have to reduce the value of \prevgraf by 3 as a display takes up 3 lines (nominally in TeX's counting). This can be needed in some case when a \parshape is in effect
  • then we reduce the saved value by 2em and and we add \magicomadjust (which I used for the verse example
  • and finally we use the resulting value to produce the desired indentation after the display
Related Question