11

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.

  • Can you show a situation where you'd like to use this? – egreg Jun 25 '13 at 9: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 9: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 9: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 9: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
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.

  • 1
    Perhaps this wasn't clear: 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' * edit * Using Hendrik's example I could do something like \mathop{\rlap{$\sin\alpha ABC$}\hphantom{$\cos\alpha ABC$} – Fictional Jun 25 '13 at 9:25
  • @Fictional No, it wasn't clear. And I still can't think to situations where this would be useful. – egreg Jun 25 '13 at 9:29
  • I'll add it to the question. – Fictional Jun 25 '13 at 9:35
  • Nice edit, I guess this will do I'll see if anyone else replies. If not I'll accept this answer. – Fictional Jun 25 '13 at 9:48
  • 2
    Is there any way to define a non-math "negative phantom"? – A Feldman Jul 8 '16 at 11:31

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.