I have a list of equations and I want them to be centered and aligned to the beginning of the equation and by the equal sign. This is what I tried so far:

\mathcal{L}_{a2c} &= \mathbb{E}_{s_t,a_t\sim\pi_{\theta}}[\mathcal{L}_{a2c_{policy}} + \frac{1}{2}\mathcal{L}_{a2c_{value}}]\\
\mathcal{L}_{a2c_{policy}} &= -\log{\pi_{\theta}(a_t|s_t)}(V_t^n-V_\theta(s_t)) - \alpha\mathcal{H}_t^{\pi_\theta}\\
\mathcal{L}_{a2c_{value}} &= {(V_t^n-V_\theta(s_t))}^2 

but unfortunately, I get:

enter image description here

As the image shows, the equations are aligned by "=" but start in different positions.

  • To be honest, I would either align like this or to the left, not keeping the equal signs aligned. – egreg Sep 11 at 17:34

One option is to place each of the left-hand sides of te equations in equally-sized boxes, and <align> each element to the left. \eqmathbox[LHS][l]{<lhs>}, as defined below, will help with that:

enter image description here



% https://tex.stackexchange.com/a/34412/5764
% \eqmathbox[<tag>][<align>]{<math>}
\NewDocumentCommand{\eqmathbox}{o O{c} m}{%


  \eqmathbox[LHS][l]{\mathcal{L}_{a2c}} &= 
    \mathbb{E}_{s_t, a_t \sim \pi_{\theta}}[\mathcal{L}_{a2c_{\text{policy}}} + \tfrac{1}{2}\mathcal{L}_{a2c_{\text{value}}}] \\
  \eqmathbox[LHS][l]{\mathcal{L}_{a2c_{\text{policy}}}} &= 
    -\log{\pi_{\theta}(a_t|s_t)}(V_t^n - V_\theta(s_t)) - \alpha\mathcal{H}_t^{\pi_\theta} \\
  \eqmathbox[LHS][l]{\mathcal{L}_{a2c_{\text{value}}}} &= {(V_t^n - V_\theta(s_t))}^2 

  • works perfectly!! thanks :) – Shani Gamrian Sep 11 at 16:32
  • @ShaniGamrian: Bernard's solution is far cleaner with alignat... – Werner Sep 11 at 16:42

A solution with alignat, and some improvements (in particular, fractional coefficients in medium size look better, in my opinion):

\usepackage{mathtools, amssymb, nccmath}


 & \mathcal{L}_{a2c} & &= \mathbb{E}_{s_t,a_t\sim\pi_{\theta}}\Bigl[\mathcal{L}_{a2c_\text{policy}} + \mfrac{1}{2}\mathcal{L}_{a2c_\text{value}}\Bigr]\\
 & \mathcal{L}_{a2c_\text{policy}} & &= -\log{\pi_{\theta}(a_t\mid s_t)}\bigl(V_t^n-V_\theta(s_t)\bigr) - \alpha\mathcal{H}_t^{\pi_\theta}\\
 & \mathcal{L}_{a2c_\text{value}} & &= \bigl(V_t^n-V_\theta(s_t)\bigr)^2


enter image description here

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.