3

I often come up with a need of a "negative phantom" command.
A simple way to define such a command (let say \nphantom) may be:

\newcommand{\nhphantom}[1]{\setbox0=\hbox{#1}\hspace{-\the\wd0}}

However, this command does not work as I want.
For example, I tried the following code, with which I want to print two letters 'A' and two summations at the same position in both of text-mode and math-mode:

\[
A\nhphantom{$\displaystyle A$}A\quad
\sum_{n=1}^{N}\nhphantom{$\displaystyle\sum_{n=1}^{N}$}\sum_{n=1}^{N}
\]

\begin{center}
A\nhphantom{A}A\quad
$\sum_{n=1}^{N}\nhphantom{$\displaystyle\sum_{n=1}^{N}$}\sum_{n=1}^{N}$
\end{center}

Then the result is as follows: Trial As we can see, my negative phantom works almost as I want for a single letter 'A' but not for summations. For summations, my negative phantom seems to introduce some excess shifting or to give slightly shorter negative phantom. Also, the "error" for summations in text-mode may be larger than in math-mode.

Thus, I would like to ask:

  1. why does the above phenomenon happen?
  2. how can I define \nhphantom with avoiding the above phenomenon?
  • 1
    Welcome to TeX.SX! TeX adds \thinmuskip after an operator that's followed by an ordinary item. – egreg Jan 28 '18 at 15:45
  • Wow, thanks for very quick answer (comment?), egreg. I solved the math mode case by inserting \null before the summation. But still I cannot solve the in-line math-mode case... – Yuta Suzuki Jan 28 '18 at 15:51
  • also, in the "A" example, the math "A" is not the same as the text "A", italic or not, and the metrics are also not necessarily the same. sort of covered in @egreg's answer, but not explicitly. – barbara beeton Jan 28 '18 at 16:00
3

You're forgetting that TeX adds a thin space between two operators and in front of it if it is preceded by an ordinary symbol (in your case A).

For the inline version, in addition to the issue above, you're using \displaystyle for computing the space, when the style is \textstyle.

\documentclass{article}
\usepackage{amsmath}

\newcommand{\nhphantom}[1]{\sbox0{#1}\hspace{-\the\wd0}}

\begin{document}

\noindent
Bad
\[
A\nhphantom{$\displaystyle A$}A\quad
\sum_{n=1}^{N}\nhphantom{$\displaystyle\sum_{n=1}^{N}$}\sum_{n=1}^{N}
\]
Good
\[
A\nhphantom{$\displaystyle A$}A\quad
{\sum_{n=1}^{N}}\nhphantom{$\displaystyle\sum_{n=1}^{N}$}{\sum_{n=1}^{N}}
\]
Bad
\begin{center}
A\nhphantom{A}A\quad
$\sum_{n=1}^{N}\nhphantom{$\displaystyle\sum_{n=1}^{N}$}\sum_{n=1}^{N}$
\end{center}
Good
\begin{center}
A\nhphantom{A}A\quad
${\sum_{n=1}^{N}}\nhphantom{$\sum_{n=1}^{N}$}{\sum_{n=1}^{N}}$
\end{center}

\end{document}

enter image description here

In order to remove the thin spaces I used braces to make the operators into ordinary atoms. In “real world” situations, you have probably to fix the spaces manually.

  • Thanks very much for giving a very understandable answer!! (Even for my very elementary question...) And I am embarrassing on my oversight on \textstyle :P – Yuta Suzuki Jan 28 '18 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.