Katex: aligned doesn't produce enough spacing

Created on 15 Sep 2016  Â·  14Comments  Â·  Source: KaTeX/KaTeX

http://tex.stackexchange.com/questions/329787/how-to-get-katex-aligned-equations-to-have-proper-spacing

\begin{aligned}
 x &= \frac{K_1M_2 - K_2M_1}{K_1K_2 + M_1M_2} \cr
 y &= \frac{K_1M_1 + K_2M_2}{K_1K_2 + M_1M_2} \cr
 z &= \frac{N^2}{K_1K_2 + M_1M_2}
\end{aligned}

I get this:

image

Is there a way to fix?

bug

Most helpful comment

David Carlisle's comment in tex.stackexchange is relevant except that KaTeX uses ex (_height of letter x in current font_) as the unit rather than pt, which is good as I like to use ex in LaTeX documents.

\begin{aligned}
 x &= \frac{K_1M_2 - K_2M_1}{K_1K_2 + M_1M_2} \\[3ex]
 y &= \frac{K_1M_1 + K_2M_2}{K_1K_2 + M_1M_2} \\[2ex]
 z &= \frac{N^2}{K_1K_2 + M_1M_2}
\end{aligned}

looks nice to me but you can adjust the vertical spacing to suit you.

All 14 comments

David Carlisle's comment in tex.stackexchange is relevant except that KaTeX uses ex (_height of letter x in current font_) as the unit rather than pt, which is good as I like to use ex in LaTeX documents.

\begin{aligned}
 x &= \frac{K_1M_2 - K_2M_1}{K_1K_2 + M_1M_2} \\[3ex]
 y &= \frac{K_1M_1 + K_2M_2}{K_1K_2 + M_1M_2} \\[2ex]
 z &= \frac{N^2}{K_1K_2 + M_1M_2}
\end{aligned}

looks nice to me but you can adjust the vertical spacing to suit you.

Is there a way to get around this without \\ ? I'm using Sphinx which automatically wraps the whole thing in a \begin{split}\end{split} if it sees \\.

I don't know anything about Sphinx but you can use repeated \cr in KaTeX

\begin{aligned}
 x &= \frac{K_1M_2 - K_2M_1}{K_1K_2 + M_1M_2} \cr\cr
 y &= \frac{K_1M_1 + K_2M_2}{K_1K_2 + M_1M_2} \cr\cr
 z &= \frac{N^2}{K_1K_2 + M_1M_2}
\end{aligned}

It's definitely a little tight. Here's the LaTeX rendering.
screen shot 2016-09-16 at 4 07 20 pm

But fractions are okay...

\begin{aligned}
 x &= \frac{\frac{1}{a}+\frac{1}{b}}{\frac{1}{a+b}} \cr\cr
 y &= \frac{\frac{1}{a}+\frac{1}{b}}{\frac{1}{a+b}}\cr\cr
\end{aligned}

hmm... so it's just fractions that contain subscripts.

actually, no. @PeterBocan has two \crs. If we have only one, the fractions overlap:

screenshot

@sthoch yup, I did not test that. Just tinkered @stevem6 example.

You can write \cr[3ex] without problems, since \cr and \\ are synonymous.

One thing worth knowing is that the argument in the square brackets works differently for positive and for negative values. For negative values, it will simply reduce the actual line depth by the given amount. For positive line heights, however, it will add the given gap to the _default_ line depth. So if the actual depth is already greater than the default, you won't see any effect for small gap values. You need to match the difference between default depth and actual depth before you see any effect. The benefit is that you can easily get the same height for rows with different content, as long as the parameter exceeds the maximal depth for all of them. This is also how LaTeX does things.

That said, the comment and screenshot by @kevinbarabash indicate that vertical spacing in KaTeX doesn't match that of LaTeX, which I also would consider a bug. I should probably revisit https://github.com/Khan/KaTeX/issues/61#issuecomment-113551368 and check the value for \normallineskiplimit in what LaTeX is doing.

I'm using this approach to look at the boxes of the equation at the top of this issue here.

...\vbox(40.0902+35.09018)x88.00406
....\hbox(13.59839+8.3595)x88.00406
.....\glue(\tabskip) 0.0
.....\hbox(13.59839+8.3595)x5.71527
......\glue 0.0 plus 1.0fil
......\hbox(8.39996+3.60004)x0.0
.......\vbox(8.39996+3.60004)x0.0
........\kern 0.0
........\hbox(8.39996+3.60004)x0.0
.........\rule(8.39996+3.60004)x0.0
......\mathon
......\OML/cmm/m/it/10 x
......\mathoff
.....â‹®
....\glue 0.0
....\glue(\lineskip) 4.0
....\hbox(13.59839+8.3595)x88.00406
.....â‹®
....\glue 0.0
....\glue(\lineskip) 4.0
....\hbox(14.9051+8.3595)x88.00406
.....â‹®

I'd say this (8.39996+3.60004)x0.0 box is the expansion of \strut@. This confirms we have still have zero \normallineskiplimit. But the \lineskip is the relevant part here. If the distance between adjacent rows is less than \lineskiplimit, then instead of using \baselineskip as the distance between baselines, TeX will use \lineskip as the distance between the line boxes.

This is what's happening here. We have a \lineskiplimit of 3pt and a \lineskip of 4pt. But why? Here is the code setting this up:

% amsmath.sty, definition of \start@aligned
        \spread@equation % no-op if already called

% amsmath.sty
\def\spread@equation{\reset@strutbox@
  \openup\jot \let\spread@equation\@empty}

% latex.ltx
\def\openup{\afterassignment\@penup\dimen@}

% latex.ltx
\def\@penup{\advance\lineskip\dimen@
  \advance\baselineskip\dimen@
  \advance\lineskiplimit\dimen@}

% various places in latex.ltx and amsmath.sty
\lineskip\normallineskip

% latex.ltx
\newskip\normallineskip \normallineskip=1pt

% latex.ltx
\jot=3pt

Not sure if we missed some \openup in other environments as well, but we should definitely handle it for the aligned environment. If the distance between boxes is less than 3pt we should make that distance exactly 4pt.

I get the same for horizontal spacing:

\begin{aligned}
x =
\begin{bmatrix}
x_0 \\
x_1 \\
x_2 \\
... \\
x_n
\end{bmatrix}
\in \text{ }\mathbb{R}^{n+1}

&

\theta =
\begin{bmatrix}
\theta_0 \\
\theta_1 \\
\theta_2 \\
... \\
\theta_n
\end{bmatrix}
\in \text{ }\mathbb{R}^{n+1}
\end{aligned} 

image

Any way to specify the horizontal spacing?

aligned alternates between right-aligned and left-aligned columns, with some space between _pairs_ of columns. The idea is that you align at the = or similar. Try this:

\begin{aligned}
x &= \begin{bmatrix} x_0 \\ x_1 \\ x_2 \\ \vdots \\ x_n \end{bmatrix}
\in \text{ }\mathbb{R}^{n+1}
&
\theta &= \begin{bmatrix} \theta_0 \\ \theta_1 \\ \theta_2 \\ \vdots \\ \theta_n \end{bmatrix}
\in \text{ }\mathbb{R}^{n+1}
\end{aligned} 

I replaced your = by &=. I also replaced ... by \vdots as I think that makes more sense in this context, but feel free to not take that change as well. I also condensed the code since we are diverging from the main subject of this thread here.

Using:

\begin{aligned}  
x &= \frac{K_1M_2 - K_2M_1}{K_1K_2 + M_1M_2} \\  
y &= \frac{K_1M_1 + K_2M_2}{K_1K_2 + M_1M_2} \\  
z &= \frac{N^2}{K_1K_2 + M_1M_2} 
\end{aligned}

now produces the following on master:
screen shot 2017-06-10 at 5 22 58 pm

Thanks to @edemaine for fixing this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sophiebits picture sophiebits  Â·  3Comments

janosh picture janosh  Â·  4Comments

q2apro picture q2apro  Â·  3Comments

pyramation picture pyramation  Â·  4Comments

trollanfer picture trollanfer  Â·  5Comments