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 want to typeset a piece of code containing line breaks. New lines should indent to a specified point in the previous line. Here is a monospaced example to illustrate what I mean. Notice how 'case' and 'of' line up below, as well as 'let' and 'in':

swap : forall a, b. Tuple a b -> Tuple b a
swap a b x = case x
             of tuple a b y z. let x' = tuple b a z y
                               in x'

The code I want to typeset is not monospaced, so I can't just use a verbatim environment to get what I want. The actual LaTeX of the first line, for instance, is

\mathsf{swap} : \forall\alpha,\beta.\;\mathsf{Tuple}\;\alpha\;\beta\to\mathsf{Tuple}\;\beta\;\alpha

How can I align some text to a specific point in the previous line?

share|improve this question
    
Could you use a tabular? –  cmhughes Feb 7 '12 at 22:03
add comment

2 Answers

up vote 2 down vote accepted

With tabbing one can set dynamically the tab stops:

\begin{tabbing}
$\mathsf{swap} : \forall\alpha,\beta.\;\mathsf{Tuple}\;\alpha\;\beta\to\mathsf{Tuple}\;\beta\;\alpha$\\
$\mathsf{swap}\;\alpha\;\beta\;x={}$%
  \=$\mathsf{case}\;x$\\
  \>$\mathsf{of}\;\mathsf{Tuple}\;\alpha\;\beta\;x\;z.\;$%
    \=$\mathsf{let}\;x'=\mathsf{Tuple}\;\beta\;\alpha\;z\;y$\\
  \>\>$\mathsf{in}\;x'$
\end{tabbing}

With \= one sets a tab stop valid from that line onwards, with \> we go to the next tab stop.

enter image description here

It's also possible to cancel tab stops previously set by using again \=.

share|improve this answer
add comment

There are various ways to accomplish this:

  1. You can use \phantom{<arg>} to leave as much space as taken up by <arg>. So in this case you would do:

    \phantom{swap a b x = }of tuple a b y z. let x' = tuple b a z y

    \phantom{swap a b x = of tuple a b y z. }in x'

    \phantom{} will take up as much horizontal and vertical space as its parameter. There is also a \vphantom{} which takes up as much vertical space as its parameter but zero horizontal space, and a corresponding \hphantom{} that takes up as much horizontal space as its parameter but zero vertical space.

  2. As @egreg commented, you can also use the tabbing environment as shown below.

  3. As @cmhughes commented you could also use tabular.


Each of these yield similar results:

enter image description here

Notes:

  • Although the tab spaces are hard coded below, they could easily be computed using \widthof{} available with the calc package. Or better still, see solution provided by egreg which shows how to use tabbing properly.
  • Since a MWE was not provided, I guessed at the definitions.

Code:

\documentclass{article}
\usepackage{amsmath}

\newcommand{\Case}{\mathsf{case}}%
\newcommand{\Let}{\mathsf{let}}%
\newcommand{\Swap}{\mathsf{swap}}%
\newcommand{\Tuple}{\mathsf{Tuple}}%
\begin{document}
\noindent
$\Swap : \forall\alpha,\beta.\;\Tuple\;\alpha\;\beta\to\Tuple\;\beta\;\alpha$

\noindent
$\Swap : \alpha,\beta x = \Case\; x$

\noindent
$\phantom{\Swap : \alpha,\beta x ={}}of \;\Tuple\; \alpha \beta y z. \;\Let\; x' = \Tuple\; \beta \alpha z y$

\noindent
$\phantom{\Swap : \alpha,\beta x = of \;\Tuple\; \alpha \beta y z.}\in x'$

\begin{tabbing}
\hspace{0em}\=\hspace{6.5em}\=\hspace{6.8em}\=\kill
%
\>$\Swap : \forall\alpha,\beta.\;\Tuple\;\alpha\;\beta\to\Tuple\;\beta\;\alpha$\\

\>$\Swap : \alpha,\beta x ={}$\>$\Case\; x$\\

\>\>$of \;\Tuple\; \alpha \beta y z. \;\Let\; x' = \Tuple\; \beta \alpha z y$\\

\>\>\>$\in x'$
\end{tabbing}

\noindent
\begin{tabular}{l@{}l@{}l@{}}
\multicolumn{3}{l}{$\Swap : \forall\alpha,\beta.\;\Tuple\;\alpha\;\beta\to\Tuple\;\beta\;\alpha$}\\
$\Swap : \alpha,\beta x ={}$&$\Case\; x$\\
&$of \;\Tuple\; \alpha \beta y z. \;$&$\Let\; x' = \Tuple\; \beta \alpha z y$\\
&&$\in x'$
\end{tabular}
\end{document}
share|improve this answer
1  
\begin{tabbing}...\end{tabbing}? –  egreg Feb 7 '12 at 22:02
    
@egreg: Well, ummm..... yes that would be simpler, but then how would I be known for providing the overkill solution? :-) I never use that environment so it never occurs to me. –  Peter Grill Feb 7 '12 at 22:15
    
Neither do I use it. :) I can delete my comment to cover your tracks. :) –  egreg Feb 7 '12 at 22:27
    
@egreg: Sure. Might as well... I use \phantom so often, perhaps that means I am doing things wrong (well at least not in the most efficient way). –  Peter Grill Feb 7 '12 at 22:41
    
Your solution with tabbing could be much improved. :) –  egreg Feb 7 '12 at 23:02
show 3 more comments

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.