# align: control where equations are stretched

I would like to format equations in a very particular way, the desired result would be:

        f(x)           = 42       (1)
g_something(x) =  0       (2)

1. the function names are left aligned (but the entire equations remain floating, and there is no extensive stretch)
2. the equations are numbered (reason against array as there is no easy way to add the numbers*
3. the numbers are right aligned to align decimal places (Bonus since currently not an issue)

Below you find a MWE with the basic equation and me trying to achieve my alignment by adding additional &

MWE:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align}
f(x) &= 42 \\
g_{\text{something}}(x) &= 1
\end{align}

%incorrect alignment of parts
\begin{align}
&f(x) &= 42& \\
&g_{\text{something}}(x) &= 1&
\end{align}

%too much spacing
\begin{align}
&f(x) &=&& 42& \\
&g_{\text{something}}(x) &=&& 1&
\end{align}
\end{document}


I believe neither fleqn nor nccmath solve my issue. I came across nccmath in this answer however, the = signs are missaligned in that answer.

* (if I understand the answer correctly)

• Please not that \text should never be used to make something upright like you are doing here. It will be italic under the right circumstances, use something like \textup instead. This is a very common mistake. – daleif Aug 30 '18 at 19:05
• @daleif: thank you for pointing out my mistake, do you know where I can find details about why this is bad? – ted Aug 30 '18 at 19:19
• You don't need to, try \textit{gggg $\text{A}$ bbb} , here A comes out italic. For something like this you want a solution that always works, that is always upright. – daleif Aug 30 '18 at 20:06
• @daleif the following is from page 7(11 in the pdf) of amsldoc "Line-by-line annotations on an equation can be done by judicious application of \text inside an align environment:" so I guess, \text can be fine but one has to be aware of the drawback you pointed out. – ted Aug 31 '18 at 13:27
• as you can see in the example given in amsldoc, those are textual comments in displayed math. Your example is not. This is a textual subscript or a subscript using a teaxual word as a subscript (not a math variable), this needs careful typesetting as to never be confused with many math variables being multiplicated together. This is just as wrong: \text{Var}(X), again this is not a textual comment. it is a math operator and should be treated as such. – daleif Aug 31 '18 at 13:46

It's  alignat which gives control of the spacing between columns of alignment. Here you need 3 columns, hence 5 ampersands:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{alignat}{3}
&f(x) & & ={}& 42& \\
&g_{\text{something}}(x) & & =& 1&
\end{alignat}

\end{document}


• While I noticed that {} is needed to fix the spacing between the = and 42, I do not understand why it is needed. So far I assumed that \begin{mathenviornment} guranteed (more or less) proper spacing around the diferent operators, including =. Could you enlighten me? (I understand it as a way to place an empty token, but cant see how this helps with alignment. – ted Aug 30 '18 at 19:16
• I suppose that being surrounded by two & makes = ordinary math instead of mathrel. You have the same problem when you break a line of equation, and the second line begins with + or –: without {}+…, you don't get the math bin spacing. – Bernard Aug 30 '18 at 19:24

like this?

above system of equation is obtained by us of the alignat math environment:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{alignat}{3}
&f(x)                    & = & 42 \\
&g_{\text{something}}(x) & = & 1
\end{alignat}
\end{document}

• Isn't the spacing after the = wrong? And even if the OP used it, don't promote using \text like this. Plus the OP wanted the 1 under the 2 – daleif Aug 30 '18 at 20:07
• @daleif, you are right. meantime Bernard provide better solution (exploitation of the alignat environment ... – Zarko Aug 30 '18 at 20:56