16

I noticed that \cos is somewhat larger then \sin, and I thought of using something like \hphantom{\cos}\neghphantom{\sin} to add the difference after one of my expressions. However it turns out there is no such command. Anyone know of an alternative? Or perhaps a way of defining such a negative phantom myself?

*edit *
Additional information
I was trying to add the space difference at the end of the expression, so after the \alpha. Something like \sin \alpha ABC+additional spacing
Using Hendrik's example I could do something like \mathop{\rlap{$\sin\alpha ABC$}}\hphantom{$\cos\alpha ABC$} but then the line ABC needs to be present twice, so I am hoping for an alternative.

mafp
  • 19,096
Fictional
  • 369
  • Can you show a situation where you'd like to use this? – egreg Jun 25 '13 at 09:10
  • Hm, it is a bit of a complicated story about aligning tikz figures, and there might be a better way for my specific case coming to think of it. But I suppose a function like this would be useful in general. – Fictional Jun 25 '13 at 09:16
  • 1
    I probably wouldn't do it, but in this situation you could use \mathop{\rlap{$\sin$}\hphantom{\cos}} instead of a negative phantom. – Hendrik Vogt Jun 25 '13 at 09:16
  • @HendrikVogt Do I understand correctly this produces a zero width \sin followed by the width of a \cos? That's pretty smart.

    Anyware I can read more about the usage of \rlap ?

    – Fictional Jun 25 '13 at 09:22
  • @Fictional -- there's a nice little example of \rlap in tex by topic (texdoc texbytopic on a tex live system) in section 5.4.5, p.65. – barbara beeton Jun 25 '13 at 13:37
  • @Fictional: Yes, that's correct. And I wrapped the whole thing into a \mathop so that before and after it you also get the same spacing as for \cos. – Hendrik Vogt Jun 30 '13 at 08:56

1 Answers1

13

You can do with mathtools, but I wouldn't recommend it. This leaves a gap on the right of “sin”, but centering it in the available space would be even worse.

\documentclass{article}
\usepackage{mathtools}

\let\sin\relax % remove the previous definition of \sin
\DeclareMathOperator{\sin}{%
  \mathrlap{\operatorname{sin}}\hphantom{\cos}%
}

\begin{document}
$\sin\alpha$

$\cos\alpha$

$e^{\sin x}$

$e^{\cos x}$
\end{document}

enter image description here

A different solution, where you add the space after the \sin<expression> can be

\documentclass{article}
\usepackage{mathtools}

\newcommand{\csin}[1]{%
  \mathop{}\!\mathrlap{\sin#1}\hphantom{\cos#1}%
}

\begin{document}
$3\csin{\alpha}+\cos\beta$

$3\cos\alpha+\cos\beta$

$e^{\csin{x}+i\cos{x}}$

$e^{\cos x+i\cos x}$
\end{document}

Since we need to know what the argument to \csin is, braces delimiting it are necessary.

However, this has limitations because it's difficult to catch the type of the last object in the argument of \csin, so this might break with parenthesized expressions and hand corrections would be needed, for instance

\csin{(x+y)}\mathclose{}

enter image description here

Yet another proposal: a \sincorr space to be added by hand where necessary:

\documentclass{article}

\newcommand{\sincorr}{\mathpalette\dosincorr\relax}
\newcommand{\dosincorr}[2]{%
  \sbox0{$#1\cos$}\sbox2{$#1\sin$}%
  \kern\dimexpr\wd0-\wd2\relax}

\begin{document}
$3\sin\alpha\sincorr+\cos\beta$

$3\cos\alpha+\cos\beta$

$e^{\sin x\sincorr+i\cos x}$

$e^{\cos x+i\cos x}$

\end{document}

The output is identical as with the previous solution.

egreg
  • 1,121,712