[Tex/LaTex] indent the next line by an exactly specified position

indentation

I am typesetting an entire math lecture script, where readability often requires me to indent just a line or two for a certain amount, which is ALWAYS different. I would like to have some way of controlling the indent of the next line by an & like in tables, or any other control character. I do not want to use tables though, I want it to look like normal text. It obviously has to work inside and outside of math environments, since they appear in text quite often for this project.

It should look something like this:

This is the first line.
     One more line.
              And another.

With the control charcters being in front of "is" and "line".

Is there an easy-to-use solution? I don't care if it is complicated to configure once, as long as the use is simple every time.

Best Answer

Most TeX compilers provide the feature \pdfsavepos that allows to record the position on the shipout page. It was introduced in pdfTeX and is nowadays available in pdfTeX, both modes DVI and PDF, XeTeX, and LuaTeX.

Because the position is not known, before the page is output, some kind of reference system is needed. Package zref-savepos of project zref provides an interface to the \pdfsavepos feature.

\documentclass{article}
\usepackage{zref-savepos}
\makeatletter
% \zsaveposx is defined since 2011/12/05 v2.23 of zref-savepos
\@ifundefined{zsaveposx}{\let\zsaveposx\zsavepos}{}
\makeatother
\newcounter{hposcnt}
\renewcommand*{\thehposcnt}{hpos\number\value{hposcnt}}
\newcommand*{\SP}{% set position
  \stepcounter{hposcnt}%
  \zsaveposx{\thehposcnt s}%
}
\makeatletter
\newcommand*{\UP}{% use previous position
  \zsaveposx{\thehposcnt u}%
  \zref@refused{\thehposcnt s}%
  \zref@refused{\thehposcnt u}%
  \kern\zposx{\thehposcnt s}sp\relax
  \kern-\zposx{\thehposcnt u}sp\relax
}
\makeatother

\begin{document}

This \SP is the first line\\
     \UP One more \SP line\\
                  \UP And another.

\end{document}

Result

Some remarks:

  • The label names are automatically chosen via the help of a counter. That makes the usage easier, because the user don't need to invent unique label names.

  • The internal position data are integer numbers with implicit unit sp.

  • \zref@refused marks the reference as used to allow LaTeX the notification for undefined references.