[Tex/LaTex] Correctly left align a set of equations

equationshorizontal alignmentmath-mode

I have a set of equations and I want to have them left-aligned instead of center-aligned. Also, I want them all to be numbered. I searched a bit and found that using flalign we can achieve this. But the results are not what I want. I want all the equations to start from the left at same position. What am I missing?

Here's the minimal working example showing what I have tried:

\documentclass{article}
\usepackage[fleqn]{amsmath}

\begin{document}
\begin{flalign}
  f_1 = \sigma(W_{1}x_1+W_{2}h_{1}+W_{3}c_{1}+b_1)  \\
  f_2 = \sigma(W_{2}x_2+b_2) \\
  c = f_i c+i\; \mathrm{cos}(W_{3}x_1+W_{3}h_{1}+b_3) \\
  y=c\; \mathrm{tanh}(f_1.f_2) 
\end{flalign}
\end{document}

enter image description here

Best Answer

Under any of the align-like environments, each row follows a right-&-left alignment. As such, the first element in each row will necessarily be right-aligned, as you observed. If you want a left-alignment, consider adding "an empty first group" by just prepending each row by &.

Here are some options:

enter image description here

\documentclass{article}

\usepackage[fleqn]{amsmath}
\setlength{\mathindent}{0pt}

\begin{document}

\noindent X\dotfill X
\begin{flalign}
  & f_1 = \sigma(W_1 x_1 +W_2 h_1 + W_3 c_1 + b_1) \\
  & f_2 = \sigma(W_2 x_2 + b_2) \\
  &   c = f_i c + i \cos(W_3 x_1 + W_3 h_1 + b_3) \\
  &   y = c \tanh(f_1.f_2) 
\end{flalign}
\noindent X\dotfill X
\begin{flalign}
  f_1 &= \sigma(W_1 x_1 +W_2 h_1 + W_3 c_1 + b_1) \\
  f_2 &= \sigma(W_2 x_2 + b_2) \\
    c &= f_i c + i \cos(W_3 x_1 + W_3 h_1 + b_3) \\
    y &= c \tanh(f_1.f_2) 
\end{flalign}
\noindent X\dotfill X

\end{document}

You might be interested in the first representation. I prefer the second.

Note that there already exist operators for the math function you write in roman font: \cos and \tanh. If you wish to define additional ones (and therefore provide the appropriate spacing) use \operatorname and/or \DeclareMathOperator{<macro>}{<name>}. See What's the difference between \mathrm and \operatorname? and Define additional math operators to be typeset in roman.

Related Question