Math Spacing in the presence of a trailing/non-trailing comma

I thought I knew how to use a \hphantom{} but don't know of an easy way to handle this case due to the comma. I want to get the text aligned for the X row. I added the appropriate \hphantom{{}+1} after the comma on the X row and yet the spacing is not correct. If I add the \hphantom{{}+1} before the comma, as in the Z row the spacing is ok, but I want the comma on the left.

But surprisingly, if I add an additional phantom comma on the right hand side to all three rows, then things align properly:

Questions:

• What is the appropriate \hphantom to use to adjust for the spacing issues caused by the comma?
• Why does adding an additional phantom comma to all three rows fix things?

Notes:

• Yes I could add an additional alignment point just before the that is text, but I want the entire text to be able to wrap properly if the text is longer than the width of the column.

Code:

\documentclass{article}
\usepackage{tabularx}

\begin{document}
\begin{tabularx}{\linewidth}{r@{}X}
$X$~ &represents $n = 4k, \hphantom{{}+1}$ that is \ldots,\\
$Y$~ &represents $n = 4k+1,$ that is \ldots,\\
$Z$~ &represents $n = 4k \hphantom{{}+1},$ that is \ldots,\\
\end{tabularx}

\bigskip
\smallskip

\begin{tabularx}{\linewidth}{r@{}X}
$X$~ &represents $n = 4k, \hphantom{{}+1,}$ that is \ldots,\\
$Y$~ &represents $n = 4k+1, \hphantom{,}$ that is \ldots,\\
$Z$~ &represents $n = 4k \hphantom{{}+1}, \hphantom{,}$ that is \ldots,\\
\end{tabularx}
\end{document}

-
Ok, hope this makes up for the hard question I posted earlier as this one seems that it should have an easy answer. – Peter Grill Feb 20 '13 at 3:49
The X comma sees the \hphantom as Ord and adds the spacing, the Z does not, compare $Z$~ &represents $n = 4k \hphantom{{}+1}, {}$ that is \ldots, with the X row. You can enclose the X comma in braces: {,}. For the second table: Every comma no has an following Ord to include its spacing. – Qrrbrbirlbel Feb 20 '13 at 3:52
@Qrrbrbirlbel: Not sure I understand fully. And how to explain that adding an additional phantom comma at the end of all three rows aligns things – Peter Grill Feb 20 '13 at 3:57

Apart from the fact that the third line is unacceptable, the error is in considering the comma part of the formula, which it isn't.

Text punctuation belongs in the formula only in displayed math, otherwise it should be typed outside the formula markers.

$X$ represents $4k$, that is ...


as

Mumble represents mumble, that is ...

In other words, change all math contents into a mumble. If the comma is inside, it would be included in the corresponding mumble, so it wouldn't have any effect on the reading (and the grammar).

In your example you want to have a space as wide as "+1", with proper spacing, because the formula is wider in one line than in the others. The comma is not under discussion, because it's present in all three instances.

\documentclass{article}
\usepackage{tabularx}

\begin{document}
\noindent
\begin{tabularx}{\linewidth}{r@{ }X}
$X$ & represents $n = 4k$,\hphantom{${}+1$} that is \ldots,\\
$Y$ & represents $n = 4k+1$,                that is \ldots,
\end{tabularx}
\end{document}


Notice also how to avoid the awkward $X$~ in the first column.

-
Totally agree with last line as being unacceptable, and also with punctuation needing to be outside of math mode - it never occurred to me to go back into math mode after the comma to fix the spacing issue. – Peter Grill Feb 20 '13 at 18:05
@PeterGrill This is however not a fix if the line wraps in the X column, because the space inside the \hphantom is frozen, while the one in 4k+1 is not. In this case you have to freeze also the formulas with ${n=4k}$ and ${n=4k+1}$ – egreg Feb 20 '13 at 18:40

First, let us agree that in all rows n = 4k + 1 and n = 4k \hphantom{{} + 1} produces the same horizontal space.

LaTeX organizes every math symbol in categories (examples, see reference 2):

• Ordinary,
• Operator,
• Binary,
• Relation,
• Open,
• Close,
• Punctuation, and
• Inner.

These can be forced by \mathop, \mathord, \mathbin and so on.

There are rules which horizontal space should be placed between two objects.

According to these rules a small space (\,/\thinmuskip) shall be placed between a symbol of category Punctuation and a symbol of category Ordinary and not between one of Ordinary and one of Punctuation (the order matters).

This rule makes the spacing in $1,2, 3$ to look like “1, 2, 3” and not “1,2,3” (spaces in math-mode have no effect). This is also the reason why using a comma to separate integer and decimal in numbers needs to be written as 123{,}456. (The comma “doesn’t see” the 4 anymore and is at the end of a group: No spacing.)

Now, how does that look in your example? (I assume that the \hphantom space is seen as an ordinary symbol (it has to be something).)

First Table

First row

k, \hphantom

Ord Punct Ord

Spacing

Ord none Punct thin Ord

Second row

1 ,

There is no symbol after the ,!

Ord Punct

Spacing

Ord none Punct

This makes a deficit of one small space.

Third row

k \hphantom ,

There is no symbol after the ,!

Ord Ord Punct

Spacing

Ord none Ord none Punct

Again, we are missing one small space compared to the first row.

Solution

I guess you want natural spacing after the comma when no math content follows (the space after math will be used anyway), so let’s make the comma in the first row unaware of its following content (solution to the question) or as egreg has stated, your comma is actually a text-comma, though I find the the , \hphantom{${}+1$} that solution cumbersome, in this case it is semantically correct (solution to the problem).

Take a look at rows Y' and Z' where I added an ordinary atom {} that effects the , as in your original X row the \hphantom does.

Code

\documentclass{article}
\usepackage{tabularx}
\begin{document}
\begin{tabularx}{\linewidth}{>{$}r<{$}@{ }l}
X   & represents $n = 4k, \hphantom{{}+1}$ that is \ldots,  \\
X'  & represents $n = 4k{,} \hphantom{{}+1}$ that is \ldots,  \\
X'' & represents $n = 4k$,\hphantom{${}+1$} that is \ldots,  \\
Y   & represents $n = 4k+1,$ that is \ldots, \\
Y'  & represents $n = 4k+1, {}$ that is \ldots, \\
Z   & represents $n = 4k \hphantom{{}+1},$ that is \ldots, \\
Z'  & represents $n = 4k \hphantom{{}+1}, {}$ that is \ldots, \\
\end{tabularx}
\end{document}


Second table

In the second table you added an phantom-ed , in every row, but every of these commas are not followed by any symbol. They introduce all the same spacing, but the new \hphantoms (row Y and Z) act as ordinary symbols and activate thin spacing after the commas that were a last symbol in the first table’s row.

Reference

1. American Mathematical Society, Mathematics into Type: Section 3 “Mathematics in Print”, pp. 37.

2. LaTeX Companion, second edition. (I only have the German edition: section 8.9 “Symbole in Formeln”, pp. 540ff. and Tabelle 8.7: Abstände zwischen Symbolen)

-
Most excellent explanation of the issue. – Peter Grill Feb 20 '13 at 18:05