# Aligning a linear system of differential equations

How do I align a system of linear differential equations in latex? I can use

\begin{cases}
x_1'(t) = x_1(t)-x_2(t)+2x_3(t)\\
x_2'(t) = -2x_1(t)+5x_2(t)+2x_3(t)\\
x_3'(t) = 8x_1(t)-5x_2(t)-2x_3(t)\\
\end{cases}


But I want the variables to line up better vertically with each x_i in its own column. I usually use the systeme package for this, but

\systeme{x_1'(t) = x_1(t)-x_2(t)+2x_3(t), x_2'(t) = -2x_1(t)+5x_2(t)+2x_3(t), x_3'(t) = 8x_1(t)-5x_2(t)-2x_3(t)}


gives me an error.

• I don't think that systeme supports having the alignments after the equals sign. It's possible to have alignment if you are willing to have x_1(t)-x_2(t)_2x_3(t)=x_1'(t) Commented May 11, 2023 at 8:45

systeme is not apt to the job. You might use alignat, but it's cumbersome. I propose iEEEeqnarray.

\documentclass{article}
\usepackage{IEEEtrantools}

\begin{document}

\begin{IEEEeqnarray*}{rCrCrCr}
x_1'(t) & = &   x_1(t) & - &  x_2(t) & + & 2x_3(t) \\
x_2'(t) & = & -2x_1(t) & + & 5x_2(t) & + & 2x_3(t) \\
x_3'(t) & = &  8x_1(t) & - & 5x_2(t) & - & 2x_3(t) \\
\end{IEEEeqnarray*}

\end{document}


r means a right aligned column, while C denotes a centered column with appropriate spaces around the operation or relation symbol.

A solution is to use \syssubstitute. Below, for example A will be replaced by \mkern-22mu x_{1}'(t) and a will be replaced by x_{1}(t).

The \mkern-22mu and \mkern-7mu create a negative horizontal space.

Note the % at the end of the line to remove unwanted space.

Furthermore, \sysdelim.. is used to remove the delimiters for the second \systeme.

\documentclass[border=6pt]{standalone}
\usepackage{systeme}
\begin{document}
\syssubstitute{A{\mkern-22mu x_{1}'(t)}B{\mkern-22mu x_{2}'(t)}C{\mkern-22mu x_{3}'(t)}a{x_{1}(t)}b{x_{2}(t)}c{x_{3}(t)}}%
$\systeme{A=,B=,C=}\mkern-7mu \sysdelim..\systeme{a-b+2c,-2a+5b+2c,8a-5b-2c}$
\end{document}

• You should put \syssubstitute after $, so the setting is local. Commented Dec 13, 2023 at 18:32 • Thank you for this remark. If the settings provided by \syssubstitute are only desired in this one formula then it is indeed best to put \syssubstitute after $. But if more such formulas follow then, in that case, these settings would need to be repeated each time. Of course a macro can be created to simplify this. Another possibility is to place all those formulas in a group, if possible. I think that whether it is more efficient to keep these settings local or not depends on the situation. Commented Dec 13, 2023 at 18:57

You can structure the alignment using alignat:

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\begin{cases} \begin{alignedat}{4} x_1'(t) ={} && x_1(t) & {}-{} & x_2(t) & {}+{} & 2x_3(t) \\ x_2'(t) ={} && -2x_1(t) & {}+{} & 5x_2(t) & {}+{} & 2x_3(t) \\ x_3'(t) ={} && 8x_1(t) & {}-{} & 5x_2(t) & {}-{} & 2x_3(t) \end{alignedat} \end{cases}

\end{document}


Additions of {} ensure proper spacing around binary relations/operators.