Starting a line where the previous line finishes

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:

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?

-
A quick fix, but not very elegant would be \phantom, like in: This is some\\ \phantom{This is some}text I would\\  –  Count Zero Apr 23 '13 at 17:54
Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you. –  Papiro Apr 23 '13 at 17:55
Just get rid of the granny! –  Ehsan Ershadi Apr 23 '13 at 19:57

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
\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:

If we uncomment the verse environment above and use

\renewcommand\magicomadjust{-4em}


we get the following:

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
-
Could you elaborate on what the code is doing? –  azetina Apr 23 '13 at 19:13
@azetina there is another example (with not much more explanation:-) here: tex.stackexchange.com/questions/107726/… –  David Carlisle Apr 23 '13 at 19:22
Thank you, I do not quite undertand how the code works, but it works... I had to tweak the \global\advance\indent@amount-2em to 7em for some reason... In any case, it makes minimalist an easy to read code which I like a lot. –  Quentin Geissmann Apr 23 '13 at 19:56
@azetina done, see above –  Frank Mittelbach Apr 23 '13 at 20:12
@QuentinGeissmann probably because you are in some verse env or similar. see my updated code version that ehlps you with that –  Frank Mittelbach Apr 23 '13 at 20:13

The tabbing environment provides this functionality where you designate the tabs via \= and skip to them using \>:

\documentclass{article}
\begin{document}
\begin{tabbing}
This is some \= text I would \\
\> text I would \\
like to \= format \\
\> format \\
How can I do this with \= \LaTeX? \\
\> \LaTeX?
\end{tabbing}
\end{document}


For completeness, I've doubled the content so you can see the tabbing is at the appropriate horizontal location. More help on the use of tabbing is available from TeX Blog.

-

I think it's simpler (and takes less markup) if you think of it as lowering the rest of the current line rather than making the next line start where the last one ended.

\documentclass{article}
{\obeylines%
\gdef\drop#1
{\smash{\lower\baselineskip\hbox{#1}}\par\vskip\baselineskip}}%
\begin{document}

\obeylines
This is some \drop text I would
like to \drop format
How can I do this with \drop \LaTeX?

\end{document}

-
nice but this doesn't support multi-line verses where TeX does its own breaking if no \\ is given (as in the verse environment) –  Frank Mittelbach Apr 23 '13 at 19:29
@FrankMittelbach I know:-) But it's hard to believe you want control over the final words but can let TeX break other lines automatically. And anyway needed a technique different from the ones posted so far:-) –  David Carlisle Apr 23 '13 at 19:36
I can agree on the last part of the comment, but for allowing for auto breaks that is quite useful the way verse does it (using extra indent) –  Frank Mittelbach Apr 23 '13 at 19:54

And, of course, there's a solution using \tikzmark. Use \Contline at the end of a line where the effect mut be applied; for example, your text can be produced simply with

\noindent This is some\Contline
text I would\\
like to\Contline
format\\
This is one way to do it with\Contline
\LaTeX.


The code needs two runs to stabilize:

\documentclass{article}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}

\newcommand\tikzmark{%
\tikz[remember picture,overlay] \coordinate (a);}
\begin{tikzpicture}[remember picture]
\path let \p1 = (current page text area.west), \p2=(a) in node[anchor=west,inner sep=0pt,outer sep=0pt,text width={\x2}] {};
\end{tikzpicture}}

\begin{document}

\noindent This is some\Contline
text I would\\
like to\Contline
format\\
This is one way to do it with\Contline
\LaTeX.

\end{document}


-
Like @FrankMittelbach 's answer, it is nice and easy to use. It seems however more robust to the alteration of the length parameter of the verse environment \begin{verse}[XX]. –  Quentin Geissmann Apr 23 '13 at 20:15
@QuentinGeissmann yes, you can use this solution inside virtually any construct and it will always produce the desired result. –  Gonzalo Medina Apr 24 '13 at 0:30
@GonzaloMedina: a very nice use of the tikzmark here :) –  Claudio Fiandrino Apr 24 '13 at 7:53

For example (you can comment the line beginning with \leavevmode). Let us observe, that no duplication of text is needed, as in some other solutions.

\documentclass{article}

\begin{document}

\def\magiccom#1{{#1}\\ \leavevmode\phantom{#1}}

\begin{verse}
Do you know how I could, for instance, write a magic command\\
\leavevmode\phantom{Do you know how I could,} for instance, write a magic command\\
\magiccom{Do you know how I could,} for instance, write a magic command

\end{verse}
\end{document}


-
nice and simple solution; the only catch is that you have to know where your line starts, i.e., the \phantom has to follow a \\. If that is not known the solution breaks. –  Frank Mittelbach Apr 23 '13 at 20:35

Perhaps using the verbatim environment?

% arara: pdflatex
\documentclass{article}
\begin{document}
\begin{verbatim}
This is some text I would
text I would
like to format
format
How can I do this with \= \LaTeX?
\LaTeX?
\end{verbatim}
\end{document}

-
Using \obeyspaces will be better than a full scale shift to verbatim environment. –  Aditya Apr 24 '13 at 6:24
@Aditya yes I agree- I wasn't really expecting this answer to be taken too seriously.... I'm glad it's at the bottom! –  cmhughes Apr 24 '13 at 15:24