[Tex/LaTex] the best way to space an equation

equationsformattingspacing

Recently I have chosen to change the typical way of writing in math mode for one in which before certain operators or symbols there is a space in between (think about arrows, =, :, etc.).

Consider this MWE:

\documentclass{article}
\usepackage[spanish]{babel}
\selectlanguage{spanish}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}

\begin{document}
Normal writing: \[x+2=0\Rightarrow x=-2.\]

Own writing: \[\begin{matrix}x+2 & = & 0 && \Rightarrow && x & = & -2.\end{matrix}\]
\end{document}

Example of writing

Another example:

\[\begin{matrix}
\begin{cases}y\quad=&xk_1,\; k_1\in\mathbb Z\\z\quad=&xk_2,\; k_2\in\mathbb Z\end{cases}
&\Rightarrow&
y+z&=&xk_1+xk_2
&\Rightarrow&
y+z=x\underbrace{(k_1+k_2)}_{k_3\in\mathbb Z}&\Rightarrow&
y+z=xk_3
&\Rightarrow&
x\mid y+z.
\end{matrix}\]

Note that it produces an extra alignment message but the point is that I don't want to manually set space to not make that mistake again.

I use matrix environment because:

  • it's easy to program;
  • automatic centering when the \\ command is used;
  • requires a single package to work.

Anyway, when there are certain types of equations keeping them centered it becomes a bit visually annoying. In addition, I use a personalized space for each environment (not a general number):

Example 1: \[\begin{matrix}x+2+1+52&=&2\\y&=&2\\z&=&1\end{matrix}\]

Example 2 (same as Example 1 but with vertical space): \[\def\arraystretch{1.5}\begin{matrix}x+2+1+52&=&2\\y&=&2\\z&=&1\end{matrix}\]

Example 3 (with more vertical space): \[\def\arraystretch{3.0}\begin{matrix}\displaystyle\int_2^2{x\;\text dx}&=&2+\dfrac x2\\\displaystyle\int_2^2{x\;\text dx}&=&2+\dfrac x2\end{matrix}\]

Spacing example

Note that I typed manually the convenient array strecth to match the other spaces as best as possible. I do it for every equation in the document…

Also in a matrix environment there could be more substructures, such as cases,array, etc.

I would like to know if there is any environment that does this work or how it could be automated as best as possible to keep the horizontal spaces vertical (maybe using a personalized command, maybe a package, etc.).

Any kind of contribution is appreciated.

Thanks!

Best Answer

The tabstackengine package allows things to be set with a uniform vertical gap (see "short stacks" in stackengine package documentation), and with math tabbing with customized horizontal spacing. What it won't allow is the numbering of individual equations.

I created \CtabbedShortstack to mean centered \tabbedShortstack, as the packages \Centerstack, \Vectorstack and \Matrixstack employ "long stacks" which have a constant baselineskip, rather than a constant gap.

In the MWE below, there is a 12pt gap between equations, and an extra 7pt of horizontal gap added to the horizontal spacing around tabs. Because the OP's example seemed to show it, I retained centered alignment of columns, rather than something akin to align (which in this case would be rcl alignment).

\documentclass{article}
\usepackage{tabstackengine}
\TABstackMath
\TABstackMathstyle{\displaystyle}
\setstackgap{S}{12pt}
\setstacktabbedgap{7pt}
\TABbinary
\newcommand\CtabbedShortstack[2][c]{%
  \setbox0=\hbox{\tabbedShortstack[#1]{#2}}%
  \vcenter{\box0}%
}
\begin{document}
\noindent Example 2:
\[
  \tabbedShortstack{
  x + 2 + 1 + 52 &=& 2\\
  y &=& 2\\
  z &=& 1
  }
\]
Example 3:
\[
  \tabbedShortstack{
  \int_2^2 x \,dx &=& 2 + \frac{x}{2}\\
  \int_2^2 x \,dx &=& 2 + \frac{x}{2}
  }
\]
First in stack numbered
\begin{equation}
  \tabbedShortunderstack{
  x + 2 + 1 + 52 &=& 2\\
  \int_2^2 x \,dx &=& 2 + \frac{x}{2}
  }
\end{equation}
Last in stack numbered
\begin{equation}
  \tabbedShortstack{
  \int_2^2 x \,dx &=& 2 + \frac{x}{2}\\
  x + 2 + 1 + 52 &=& 2
  }
\end{equation}
Middle of stack numbered
\begin{equation}
  \CtabbedShortstack{
  \int_2^2 x \,dx &=& 2 + \frac{x}{2}\\
  \int_2^2 x \,dx &=& 2 + \frac{x}{2}
  }
\end{equation}
\end{document}

enter image description here

Related Question