How to adjust alignment within Math ($$)?

As you can see in the picture, I have a system of equations and I want the same items(for example y_1, + and =) to be vertically aligned (so as to be viewed easy for Linear Algebra)

For system of linear equations, 
$ cases(
    2y_1    &+& y_2     &=  x_1\
    y_1     &+& 2y_2    &=  x_2\
    -2y_1   &+& 3y_2    &=  x_3
) $

$
    2y_1    &+& y_2     &=  x_1\
    y_1     &+& 2y_2    &=  x_2\
    -2y_1   &+& 3y_2    &=  x_3
$

rich text editor image

I want to use the case function, within which all the terms are aligned left (How to change?). I want the terms to be aligned to the right, just like the ones without the case function.

With LaTeX, I am able to achieve this by creating an array with the {lr} modification,

$$
\begin{array}{lr}

1                    &= &a_1  &+& a_2    &+& a_3    &+& a_4    \\
1 + 2^3              &= &2a_1 &+& 2^2a_2 &+& 2^3a_3 &+& 2^4a_4 \\
1 + 2^3 + 3^3        &= &3a_1 &+& 3^2a_2 &+& 3^3a_3 &+& 3^4a_4 \\
1 + 2^3 + 3^3 + 4^4  &= &4a_1 &+& 4^2a_2 &+& 4^3a_3 &+& 4^4a_4

\end{array}
$$

How to adjust alignment within Math ($$) anyway?

Hello, how about this?

#set page(paper: "a6")
#align(left, $
& 1                   &&=& a_1  &+& a_2    &+& a_3    &+& a_4    \
& 1 + 2^3             &&=& 2a_1 &+& 2^2a_2 &+& 2^3a_3 &+& 2^4a_4 \
& 1 + 2^3 + 3^3       &&=& 3a_1 &+& 3^2a_2 &+& 3^3a_3 &+& 3^4a_4 \
& 1 + 2^3 + 3^3 + 4^4 &&=& 4a_1 &+& 4^2a_2 &+& 4^3a_3 &+& 4^4a_4
$)

image

This is not very different from the LaTeX code you gave. Although I do not recommend using cases for this, you can also write the following. I don’t seem to be able to right align the terms on the rhs though (if someone has an idea?)

$ 
cases(
1                   & = thin a_1  &+ a_2    &+ a_3    &+ a_4   ,
1 + 2^3             & = thin 2a_1 &+ 2^2a_2 &+ 2^3a_3 &+ 2^4a_4,
1 + 2^3 + 3^3       & = thin 3a_1 &+ 3^2a_2 &+ 3^3a_3 &+ 3^4a_4,
1 + 2^3 + 3^3 + 4^4 & = thin 4a_1 &+ 4^2a_2 &+ 4^3a_3 &+ 4^4a_4,
)
$

image

How to adjust alignment within Math ($$) anyway?

From the documentation,

When equations include multiple alignment points (& ), this creates blocks of alternatingly right- and left-aligned columns.

Notice that in the first solution, I used double && before the equal sign to right-align the summation terms.

I tried using “&&” as in the document. It works for most of the case, but when the equations are inside the cases function, then “&&” lost its function, like these:

== 1.3

$ cases(
     2y_1 +&&  y_2 =&&    4x_1\
      y_1 +&& 2y_2 =&&  -12x_2\
    -2y_1 +&& 3y_2 =&&     x_3
) $

$
     2y_1 +&&  y_2 =&&    4x_1\
      y_1 +&& 2y_2 =&&  -12x_2\
    -2y_1 +&& 3y_2 =&&     x_3
$

These will give:
Screenshot 2024-09-26 at 20.13.51

The syntax of cases is rather specific, as it is for mat, vec. The behaviour of alignment points for cases is specifically written as to not alternate between left and write. I think the best you can achieve is what I showed above. Oh! and wrapping cases in align does not seem to do anything. My bad, I made a mistake when copy pasting.

Cases intentionally does not alternate between left and right.

I finally find a way to solve it (temporarily)

$ cases(
&2&y_1 &+ &y_2 &= x_1\
& &y_1 &+ 2&y_2 &= x_2\
-&2&y_1 &+ 3&y_2 &= x_3
) $

This code will force any element to align vertically even in cases,

rich text editor image

1 Like

Another way might be using op(); function to create a “space” operator:

$ cases(
    &1 + 2^3 + 3^3 + 4^4
	&= 4a_1 + 4^2a_2 + 4^3a_3 + 4^4a_4\

    &1 + 2^3 + 3^3
	&= 3a_1 + 3^2a_2 + 3^3a_3 + 3^4a_4\

	&1 + 2^3
	&= 2a_1 + 2^2a_2 + 2^3a_3 + 2^4a_4\
	
	&1
	&=  op(" ")a_1 +   op(" ")op(" ")a_2 +   op(" ")op(" ")a_3 +   op(" ")op(" ")a_4
) $

this will make:

That’s great! if your current solution satisfies you, don’t hesitate to select an answer and mark the post as resolved :smiley:!

1 Like

You don’t have to use the alignment point after everything, you can use only the amount that is enough for it to look good. Additionally, in cases you should use , as a case delimiter (instead of using explicit line breaks):

$ cases(
   &2&y_1 +  &y_2 = x_1,
   & &y_1 + 2&y_2 = x_2,
  -&2&y_1 + 3&y_2 = x_3,
) $

image

I’d argue that using op creates unnecessary complexity in code and it’s better to insert a few alignment point in previous cases:

$ cases(
  & 1 + 2^3 + 3^3 + 4^4
  & = 4 & a_1 + 4^2 & a_2 + 4^3 & a_3 + 4^4 & a_4,
    
  & 1 + 2^3 + 3^3
  & = 3 & a_1 + 3^2 & a_2 + 3^3 & a_3 + 3^4 & a_4,
    
  & 1 + 2^3
  & = 2 & a_1 + 2^2 & a_2 + 2^3 & a_3 + 2^4 & a_4,
    
  & 1
  & =   & a_1 +     & a_2 +     & a_3 +     & a_4,
) $

image

Note that you can also add spaces in the code to make source more readable too.