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?

  • Could you use a tabular? – cmhughes Feb 7 '12 at 22:03

With tabbing one can set dynamically the tab stops:

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

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 \=.


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


  • 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.



$\Swap : \forall\alpha,\beta.\;\Tuple\;\alpha\;\beta\to\Tuple\;\beta\;\alpha$

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

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

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

\>$\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'$

\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'$
  • 1
    \begin{tabbing}...\end{tabbing}? – egreg Feb 7 '12 at 22:02
  • 1
    @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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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