Consider documents with the following preamble:
\documentclass[12pt]{article}
\usepackage{amsmath}
Question #1.
Why do we get bad alignment with the following code?
\noindent
$-3x$\\[-\baselineskip]%
$\begin{aligned}\!
\phantom{-3x} (x+1) & = 4 \\ & = 2^2
\end{aligned}$
I cannot figure out an appropriate amount of negative space to use in order to achieve alignment between the inline math and the aligned
environment, using any plausible combination of \baselineskip
and \jot
. (The dimension \dimexpr-\baselineskip+0.5\jot
almost works, for reasons which are opaque to me, but still doesn't do the job.)
Question #2.
What is the correct spacing to achieve the proper alignment in this case, independent of font size etc.?
Background
I'm puzzling over how to produce a robust solution to this problem on equation alignment, and have come to the conclusion that I don't understand what LaTeX is doing with vertical spacing when it comes to aligned
.
I would like to describe how to undo one line of vertical space, but this does not appear to be straightforward when the thing on the next line is a multiline equation.
Test cases which work properly
The following are my little tests to try to asses what is going on with the spacing. The latter two cases only served to make the failure in the above example extra confusing.
Undoing vertical spacing of two lines in textmode: easy. The following code illustrates roughly the effect that I would like to achieve:
\noindent Testing \phantom{testing} 1 \phantom{2} 3 \\[-\baselineskip]% \phantom{Testing} testing \phantom{1} 2 \phantom{3}
This appears as a single line of text, saying "Testing testing 1 2 3" with appropriate alignment and spacing, as it should.
Undoing vertical spacing involving math and an
aligned
environment: nothing inherently changes about the spacing.\noindent $-3x$ \\[-\baselineskip]% $\begin{aligned}\! \phantom{-3x} (x+1) \end{aligned}$
This gives output which looks like −3x(x+1) with appropriate alignment and spacing.
Multiline equations without additional vertical spacing: If we make the
aligned
environment into a multiline equation — but remove the vertical spacing between the two lines of the equation — then the vertical alignment still functions properly.\noindent $-3x$ \\[-\baselineskip]% $\begin{aligned}\! \phantom{-3x} (x+1) & + 4 \\[-\baselineskip] & + 4 \end{aligned}$
I must admit that I don't understand why I don't have to involve the unit
\jot
to make this work properly, but once again we obtain proper vertical alignment, with something that looks like −3x(x+1)+4.
I would like to see a way of making this work in an obvious and portable way, regardless of what other things are going on. Naturally, if I were using align
instead of aligned
, I might also have to involve \abovedisplayskip
, which I'm comfortable with; though if there are "gotchas" in the spacing beyond that, I'd be again a little annoyed. But in any case, I think the first step is to understand how it's supposed to work with an environment which lives inside of mathmode, such as aligned
.