# Align two equations vertically

I'm trying to vertically align two equations but I fail to get it right.

\begin{aligned} f_{s_{i}}=-\frac{w_{s}}{n_{i}}\sum_{j=1}^{n}g(d_{ij})d_{ij} \\ g(d_{ij}) = \begin{cases} 1 & d_{ij}\leq r_{h}\\ exp(-\frac{(d_{ij}-r_{h})^{2}}{\sigma^{2}}) & d_{ij}> r_{h} \end{cases} \end{aligned}


I get the following:

How can I align the equation 'f' and 'g' knowing that they are presented in a table?

You can use on these, depending on whether you want to align the = signs or the beginning of each line. Remember that aligned (or align) requires marking the alignment point with an ampersand:

\begin{aligned} f_{s_{i}} &=-\frac{w_{s}}{n_{i}}\sum_{j=1}^{n}g(d_{ij})d_{ij} \\ g(d_{ij}) &= \begin{cases} 1 & d_{ij}\leq r_{h}\\ \exp\Bigl(-\frac{(d_{ij}-r_{h})^{2}}{\sigma^{2}}\Bigr) & d_{ij}> r_{h} \end{cases} \end{aligned}

\begin{aligned} &f_{s_{i}} =-\frac{w_{s}}{n_{i}}\sum_{j=1}^{n}g(d_{ij})d_{ij} \\ &g(d_{ij}) = \begin{cases} 1 & d_{ij}\leq r_{h}\\ \exp\bigl(-\frac{(d_{ij}-r_{h})^{2}}{\sigma^{2}}\bigr) & d_{ij}> r_{h} \end{cases} \end{aligned}


I added two different sizes for the parentheses after exp in the two proposed codes.

Unrelated: like all usual functions having a name, the exp function should typed in upshape, which is obtained with the command \exp (like \sin, \cos, \log, &c.)

• +1. I would increase the size of the round parentheses around the fraction term in the second row of the cases environment, though.
– Mico
Jan 28, 2020 at 13:00
• @Mico: You're right. I didn't have time when I , posted to check this point, but I've added size commands (different in each proposed code, as none is perfect in this respect). Thanks for reminding me! Jan 28, 2020 at 18:42
• It's more usual to set such expressions in display form, not as in-line math. Jan 28, 2020 at 20:02
• @barbarabeeton: I don't think that, for the fraction in the cases environment, the display form looks so nice. Personally, I would rather use \mfrac, but I didn't want to complexify my answer. Jan 28, 2020 at 20:15
• Okay, legitimate personal preference. But then you're giving up the horizontal centering, "separation" above and below, and possibility of numbering, that come with a display environment. (I really hate to encourage the use of \\  to break lines, and just setting something like this as a separate paragraph would probably result in followup questions.) Jan 28, 2020 at 21:37

You just need to use the & charactere whereever you want to put the alignment.

For example, if you want to align at the left side of the column:

\begin{aligned} & f_{s_{i}}=-\frac{w_{s}}{n_{i}}\sum_{j=1}^{n}g(d_{ij})d_{ij} \\ & g(d_{ij}) = \begin{cases} 1 & d_{ij}\leq r_{h}\\ exp(-\frac{(d_{ij}-r_{h})^{2}}{\sigma^{2}}) & d_{ij}> r_{h} \end{cases} \end{aligned}


else, if you want to align in the = charactere, you do as:

\begin{aligned} f_{s_{i}} & =-\frac{w_{s}}{n_{i}}\sum_{j=1}^{n}g(d_{ij})d_{ij} \\ g(d_{ij}) & = \begin{cases} 1 & d_{ij}\leq r_{h}\\ exp(-\frac{(d_{ij}-r_{h})^{2}}{\sigma^{2}}) & d_{ij}> r_{h} \end{cases} \end{aligned}

• Welcome to TEX.SE! What difference is there between Bernard's answer and yours? Jan 28, 2020 at 14:00
• I actually didn't read his code (just now that you said), I just did with examples and explained the cases. Whereas he hasn't. Anyway, the two will give the same result. Jan 28, 2020 at 14:03