1

I am looking for a way to align separate lines but always relative to some marker in the previous line. My use-case is to align equations in a math environment in a specific way but for demonstration purposes let me first showcase it with the following lines:

----a----
a------b
c--b
d---c
e--d  

In this case, I want to have align every line relative to the previous one, such that the letters match up:

----a----
    a------b
        c--b
    d---c
 e--d     

I am sure it is possible to do this using tables. But to make this work by using tables, it seems to me that you need to already know in advance how many columns you are going to need (which might change when you ever want to add more lines), while I am seeking a solution which is flexible, since every line is only supposed to be aligned in reference to the previous one.

Or am I missing some feature about tables, which resolves my problem in exactly that way?


To make this very concrete, here is one instance where I would like to use it. This is the code:

\documentclass{article}


\begin{document}
    $$
    \phi(G\,a\,n) 
    \\ \phi \circ g (~\phi (a) [\overline n] ~)
    \\ \phi (a) [\overline n] 
    \\ \phi \circ g (\, \forall y. \, \phi(d)[z,y] \to \varphi[y] \, ) [\overline n]
    \\ (\forall y. \, \phi(d)[z,y] \to \varphi[y] \, )[\overline  n]
    \\ \forall y. \, \phi(d)[\overline n,y] \to \varphi[y]
    \\ \forall y. \, \overline{\Delta \,n} = y \to \varphi[y]
    \\ \varphi[\overline{\Delta \,n}]
    \\ \varphi[\overline{G \,n \,n}]
    $$  
\end{document}

How it looks:

enter image description here

And how I would like it to look:

Yes, this is done using a lot of "~". And I hope that makes it clear why I am looking for a better solution

8
  • What you describe could be achived inside a table. Did you try it?
    – SebGlav
    Commented Jun 26, 2022 at 16:53
  • tables in general. In case of math alignat...
    – Zarko
    Commented Jun 26, 2022 at 16:53
  • See, if this can help you: en.wikibooks.org/wiki/LaTeX/Tables
    – Zarko
    Commented Jun 26, 2022 at 18:13
  • 2
    maybe the tabbing environment can help... tex.stackexchange.com/a/263766/38080
    – Rmano
    Commented Jun 26, 2022 at 18:31
  • 3
    @Léreau I don't understand really your requirement (nor you've cited the need for math mode). The second line --- I don't understand what you really want. I am suspecting this is an XY problem.
    – Rmano
    Commented Jun 26, 2022 at 20:06

1 Answer 1

2
  • As I mentioned in my comment, try with use of amsmath environments align and/or alignat:
\documentclass{article}
\usepackage{mathtools}

\begin{document}
With \verb+align+ and insert spaces before last two rows:
\begin{align*}
    &   \phi(G\,a\,n)                       \\
    &   \phi \circ g (~\phi (a) [\bar{n}])  \\ 
    &   \phi (a) [\overline n]              \\ 
\phi \circ g (\, \forall y. \, \phi(d)[z,y] 
    & \to  \varphi[y] \, ) [\bar{n}]                   \\ 
(\forall y. \, \phi(d)[z,y]
    & \to  \varphi[y] \, )[\bar{n}]                    \\ 
\forall y. \, \phi(d)[\bar{n},y]
    & \to  \varphi[y]\quad                             \\ 
\forall y. \, \overline{\Delta \,n} = y
    & \to  \varphi[y]\                                   \\ 
    &\quad\ \varphi[\overline{\Delta \,n}]              \\ 
    &\quad\ \varphi[\overline{G \,n \,n}]
\end{align*}
or with \verb+alignat+
\begin{alignat*}{3}
    &   \mathrlap{\phi(G\,a\,n)}                        \\
    &   \mathrlap{\phi \circ g (~\phi (a) [\bar{n}])}   \\
    &   \mathrlap{\phi (a) [\overline n]}               \\
\phi \circ g (\, \forall y. \, \phi(d)[z,y]
    & \to && \varphi[y] \, ) [\bar{n}]                  \\
(\forall y. \, \phi(d)[z,y]
    & \to {} && \varphi[y] \, ) [\bar{n}]                  \\
\forall y. \, \phi(d)[\bar{n},y]
    & \to && \varphi[y]\quad                            \\
\forall y. \, \overline{\Delta \,n} = y
    & \to && \varphi[y]\                                \\
    &&& \varphi[\overline{\Delta \,n}]                  \\
    &&& \varphi[\overline{G \,n \,n}]
\end{alignat*}

\end{document}
  • for mathrlap you need to load mathtools package
  • in LaTeX document don't use $$` for math delimiters

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .