# How to write the equations (using alignat*) in the middle

The code is:

\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{wasysym, amsmath, amsfonts, amssymb, mathtools, mathrsfs, wasysym, cancel, extarrows, listings, fontspec}
\begin{document}
\begin{alignat*}{2}
(4x^3y+2y^3x)dx&+(y^4-4x^4)dy=0 \big/ :xy \\
4x^2+2y^2&+(\frac{y^3}x-4\frac{x^3}y)y'=0\big/ \smiley \\
4x^2+2x^2z^2&+(x^2z^3-4\frac{x^2}{z})y'=0\big/ :x^2; x\ne 0\\
4+2z^2&+(z^3-\frac 4z)(z'x+z)=0\Rightarrow 2z^3+4z+(z^4-4)(z'x+z)=0\\
2z^3+(z^4-4)z'x&+z^5=0\Rightarrow \frac{\overbrace{z^4-4}^{(z^2-2)(z^2+2)}}{\underbrace{-2z^3-z^5}_{-z^3(z^2+2)}}dz=\frac {dx}x\\
\int \frac{z^2-2}{-z^3}dz&=\int \frac{dx}x \Rightarrow -\ln|z|-\frac{1}{z^2}=\ln|x|+C\\
&\boxed{-\ln|\frac yx|-\frac{x^2}{y^2}=\ln|x|+C}
\end{alignat*}
\end{document}


The output is:

I want every line in the equation to be in the middle of the line. E.g. I wrote 31 characters in a line, so I want the 15th character to be in the middle of the line. The problem is that it takes time to "play" with the & so I thought maybe there is a way I could write it automatically in the middle.

• Why do you need to use alignat? {gather} will centre your equations...
– DrM
Commented Jun 15 at 22:09
• @DrM I know I wrote (using alignat*) in the title, but using gather is ok too. Commented Jun 16 at 3:39

## 1 Answer

LIke this:

You should be aware that align, alignat align equations to inserted anchors &, meanwhile equations, gather equation only horisontal center in text area. So, in your MWE youi only need to remove all ampersands and replace alignat* with gather* as was sugested in @DrM comment and done in MWE below:

\documentclass{article}
\usepackage{fontspec}

\usepackage{graphicx} % Required for inserting images
\usepackage{amssymb, mathtools, wasysym,}
\begin{document}
\begin{gather*}
(4x^3y+2y^3x)dx +(y^4-4x^4)dy=0 \big/ :xy \\
4x^2+2y^2 +(\frac{y^3}x-4\frac{x^3}y)y'=0\big/ \smiley \\
4x^2+2x^2z^2 +(x^2z^3-4\frac{x^2}{z})y'=0\big/ :x^2; x\ne 0\\
4+2z^2 +(z^3-\frac 4z)(z'x+z)=0\Rightarrow 2z^3+4z+(z^4-4)(z'x+z)=0\\
2z^3+(z^4-4)z'x +z^5=0\Rightarrow \frac{\overbrace{z^4-4}^{(z^2-2)(z^2+2)}}{\underbrace{-2z^3-z^5}_{-z^3(z^2+2)}}dz=\frac {dx}x\\
\int \frac{z^2-2}{-z^3}dz =\int \frac{dx}x \Rightarrow -\ln|z|-\frac{1}{z^2}=\ln|x|+C\\
\boxed{-\ln|\frac yx|-\frac{x^2}{y^2}=\ln|x|+C}
\end{gather*}
\end{document}

• Thank you! For my information, is there a way to do it using alignat*? Commented Jun 17 at 5:57
• No, alignat will anchor equation at ampersand, but over their position you not have control. Their are placed so, that whole equation system is centered, as you got with your code. Commented Jun 17 at 6:02