2

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:

\begin{align*}
\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 
\end{align*}

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
3

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

\documentclass{article}

\usepackage{eqparbox,xparse,amsmath,amsfonts}

% https://tex.stackexchange.com/a/34412/5764
\makeatletter
% \eqmathbox[<tag>][<align>]{<math>}
\NewDocumentCommand{\eqmathbox}{o O{c} m}{%
  \IfValueTF{#1}
    {\def\eqmathbox@##1##2{\eqmakebox[#1][#2]{$##1##2$}}}
    {\def\eqmathbox@##1##2{\eqmakebox{$##1##2$}}}
  \mathpalette\eqmathbox@{#3}
}
\makeatother

\begin{document}

\begin{align*}
  \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 
\end{align*}

\end{document}
  • 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
6

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

\documentclass[11pt]{book}
\usepackage{mathtools, amssymb, nccmath}

\begin{document}

\begin{alignat*}{2}
 & \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
\end{alignat*}

\end{document} 

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.