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

Roland
  • 6,655
Léreau
  • 111
  • What you describe could be achived inside a table. Did you try it? – SebGlav Jun 26 '22 at 16:53
  • tables in general. In case of math alignat... – Zarko Jun 26 '22 at 16:53
  • See, if this can help you: https://en.wikibooks.org/wiki/LaTeX/Tables – Zarko Jun 26 '22 at 18:13
  • 2
    maybe the tabbing environment can help... https://tex.stackexchange.com/a/263766/38080 – Rmano Jun 26 '22 at 18:31
  • @Zarko I think that is on me. Yes the specific example could easily be done with tables. I will change it to maybe showcase a situation where a table is not that well suited. But for the specific problem, tables do not really seem as the best solution anyways, since you need to know beforehand how many columns you need and if you ever want to introduce another reference point for alignment, you would need to go add it in every line of the code at the right place. – Léreau Jun 26 '22 at 19:21
  • @Rmano That does seem promising, but I did not quite get my example to work (I have trouble getteing the intended alignment of the 4th line with respect to the 3rd). It also does not seem to be supported for math environments. – Léreau Jun 26 '22 at 19:48
  • 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 Jun 26 '22 at 20:06
  • As I say in my comment; use ąlignat ... I can show you when question will be reopen – Zarko Jun 27 '22 at 21:36

1 Answers1

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

Zarko
  • 296,517