Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

4 Answers

up vote 11 down vote accepted

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.

share|improve this answer
    
That did it! Thank you very much for this solution. It works just the way I want, at least for text. If used within math it does not work well with the automatic spacing, but I can work around that. –  SheridanVespo Aug 28 '12 at 19:29
add comment

Yet another option is the tabto package. It requires you to define the tab positions explicitly, but has the advantage to work over paragraph boundaries, so it can be used, for instance, with itemize lists:

\documentclass{article}
\usepackage{tabto}


\begin{document}
  This \tabto{4em}is the first line. \\
  \tabto{4em}One \tabto{7em}more line. \\
  \tabto{7em}And another.

  \begin{itemize}
    \TabPositions{1cm,2cm}
    \item This \tab is the first line. 
    \item \tab One \tab more line. 
    \item \tab\tab And another.
  \end{itemize}

\end{document}

enter image description here

share|improve this answer
add comment

For completeness, \phantom{<stuff>} is also a possibility:

enter image description here

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\setlength{\parindent}{0pt}% For this example.
\begin{document}
\lipsum[2]% Some dummy text.

This is the first line. \par
\leavevmode\phantom{This }One more line. \par
\leavevmode\phantom{This One more }And another.

\lipsum[2]% Some dummy text.
\end{document}

\leavevmode is required if the paragraph starts with \phantom.

share|improve this answer
1  
+1 for explaining the need for \leavevmode. –  Mico Aug 27 '12 at 23:31
add comment

Text mode

You could use the tabbing environment:

\documentclass{article}

\newenvironment{Tabbing}{% see http://tex.stackexchange.com/a/16389/16595
    \vspace{-\baselineskip}%
    \setlength{\topsep}{0pt}\setlength{\partopsep}{0pt}\tabbing%
}{\endtabbing}

\begin{document}
\noindent This is a line before the \verb|Tabbing| environment.
\begin{Tabbing}
    This \= One more \=\kill\\
    This \> is the first line. \\
         \> One more line. \\
         \>        \>And another.
\end{Tabbing}
This is a line after the \verb|Tabbing| environment.
\end{document}

Output:

Output for text mode

Math mode

For math I'd suggest the alignat environment:

\begin{alignat*}{2}
\textrm{This } & \rlap{is the first line.} & & \\
               & \textrm{One more }        & &\textrm{line.} \\
               &                           & &\textrm{And another.}
\end{alignat*}

Output:

Output for math mode

Use \(\displaystyle <math> \) inside \rlap to get back to math mode.

share|improve this answer
    
This helps. What can be a problem is that I need to encase the entire paragraph within the environment since it always adds a skip and a new line at the beginning of the environment. If someone finds a better solution it is always welcome. Until then I will see how I can work with this. –  SheridanVespo Aug 27 '12 at 21:19
    
Same problem with the alignat environment are the skips added before and after it. Is there a way to create a custom environment with the properties of either of the above, but without the skips before and after? –  SheridanVespo Aug 27 '12 at 21:30
    
See egreg's answer. –  Qrrbrbirlbel Aug 27 '12 at 21:43
    
I updated my answer for the text mode part. For the math mode you could change the length \abovedisplayshortskip, \belowdisplayshortskip, \abovedisplayskip and \belowdisplayskip as explained in Mathmode‌​. Or just use the array environment. Or the T/tabbing environment with a lots of \(...\) … Can you provide an example on how you want to align your formulas? –  Qrrbrbirlbel Aug 27 '12 at 22:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.