logo

📂Academic Writing

How to Automatically Break Long Equations and Align by Equals in LaTeX

Code

To automatically align equations that grow long, use the breqn package.

dmath

An equation can be placed between the opening and closing of dmath; when the equation becomes long, it is automatically broken into lines and aligned by the equals sign.

\documentclass{article}
\usepackage{breqn}

\begin{document}

\begin{dmath}
dx = -0.018 x^{2} + 0.023 x y - 0.008 y^{2} + 0.033 x^{3} - 0.083 x^{2} y + 0.065 x y^{2} - 0.012 y^{3} + 0.02 x^{4} - 0.093 x^{3} y + 0.169 x^{2} y^{2} - 0.137 x y^{3} + 0.038 y^{4} - 0.037 x^{5} + 0.192 x^{4} y - 0.412 x^{3} y^{2} + 0.447 x^{2} y^{3} - 0.244 x y^{4} + 0.054 y^{5} + 0.011 x^{6} - 0.066 x^{5} y + 0.173 x^{4} y^{2} - 0.241 x^{3} y^{3} + 0.188 x^{2} y^{4} - 0.077 x y^{5} + 0.013 y^{6}
\end{dmath}

\begin{dmath}
dy = 0.009 + 0.006 x - 0.003 y + 0.005 x^{2} - 0.005 y^{2} - 0.025 x^{3} + 0.073 x^{2} y - 0.086 x y^{2} + 0.034 y^{3} - 0.025 x^{4} + 0.109 x^{3} y - 0.157 x^{2} y^{2} + 0.094 x y^{3} - 0.018 y^{4} + 0.068 x^{5} - 0.362 x^{4} y + 0.771 x^{3} y^{2} - 0.82 x^{2} y^{3} + 0.439 x y^{4} - 0.095 y^{5} - 0.027 x^{6} + 0.175 x^{5} y - 0.472 x^{4} y^{2} + 0.677 x^{3} y^{3} - 0.547 x^{2} y^{4} + 0.236 x y^{5} - 0.042 y^{6}
\end{dmath}

\end{document}

alt text

dgroup

Systems of equations or ordinary differential equations require several equals signs to be aligned, and in that case use dgroup. Simply place several dmath environments inside the opening and closing of dgroup.

\documentclass{article}
\usepackage{breqn}

\begin{document}

\begin{dgroup*}
\begin{dmath*}
\dot{x} = 5.1747 - 17.932x + 9.5378y - 11.764z + 23.656x^{2} + 6.5559xy + 30.334xz - 23.801y^{2} - 6.3831yz + 7.976z^{2} - 9.7456x^{3} - 9.9892x^{2}y - 18.991x^{2}z + 38.4xy^{2} + 6.9991xyz - 10.694xz^{2} - 19.787y^{3} + 6.3091y^{2}z + 5.0707yz^{2} - 1.9369z^{3}
\end{dmath*}
\begin{dmath*}
\dot{y} =  1.0x - 1.0y + 1.0z
\end{dmath*}
\begin{dmath*}
\dot{z} = -15.0y
\end{dmath*}
\end{dgroup*}

\begin{dgroup*}
\begin{dmath*}
\dot{x} = 5.031 - 17.617x + 9.5957y - 11.543z + 23.464x^{2} + 5.8539xy + 30.084xz - 21.77y^{2} - 6.6587yz + 7.9488z^{2} - 9.727x^{3} - 9.3318x^{2}y - 18.975x^{2}z + 35.698xy^{2} + 7.6657xyz - 10.732xz^{2} - 17.426y^{3} + 5.0784y^{2}z + 5.2821yz^{2} - 1.9521z^{3}
\end{dmath*}
\begin{dmath*}
\dot{y} =  1.0x - 1.0y + 1.0z
\end{dmath*}
\begin{dmath*}
\dot{z} = -15.0y
\end{dmath*}
\end{dgroup*}

\end{document}

alt text

[spread=1pt]

Using the breqn package in the first place presumably means dealing with equations that cannot be handled by hand. When an equation becomes extremely long, the line spacing may need to be tightened, and the spacing between lines can be controlled by appending [spread=...pt] after dgroup or dgroup*.

\documentclass{article}
\usepackage{breqn}

\begin{document}

\begin{dgroup*}[spread=-3pt]
\begin{dmath*}
\dot{x} = 5.1747 - 17.932x + 9.5378y - 11.764z + 23.656x^{2} + 6.5559xy + 30.334xz - 23.801y^{2} - 6.3831yz + 7.976z^{2} - 9.7456x^{3} - 9.9892x^{2}y - 18.991x^{2}z + 38.4xy^{2} + 6.9991xyz - 10.694xz^{2} - 19.787y^{3} + 6.3091y^{2}z + 5.0707yz^{2} - 1.9369z^{3}
\end{dmath*}
\begin{dmath*}
\dot{y} =  1.0x - 1.0y + 1.0z
\end{dmath*}
\begin{dmath*}
\dot{z} = -15.0y
\end{dmath*}
\end{dgroup*}

\begin{dgroup*}[spread=-3pt]
\begin{dmath*}
\dot{x} = 5.031 - 17.617x + 9.5957y - 11.543z + 23.464x^{2} + 5.8539xy + 30.084xz - 21.77y^{2} - 6.6587yz + 7.9488z^{2} - 9.727x^{3} - 9.3318x^{2}y - 18.975x^{2}z + 35.698xy^{2} + 7.6657xyz - 10.732xz^{2} - 17.426y^{3} + 5.0784y^{2}z + 5.2821yz^{2} - 1.9521z^{3}
\end{dmath*}
\begin{dmath*}
\dot{y} =  1.0x - 1.0y + 1.0z
\end{dmath*}
\begin{dmath*}
\dot{z} = -15.0y
\end{dmath*}
\end{dgroup*}

\end{document}

alt text