8

I'm trying to get the plus-minus sign (\pm) in different colors.

I started with

\newcommand{\rgpm}{~\overset{\mathbf{\displaystyle\color{green}+}}

and probably could further adjust it with some \vspace commands and \raisebox - but it just doesn't seem right. Is there a simple approach?


MWE

\documentclass{article}
\usepackage{color}
\usepackage{amsmath}

\newcommand{\rgpm}[2]{~\overset{\mathbf{\displaystyle\color{#1}+}}{\mathbf{\color{#2}-}}~}
\begin{document}
\[a \rgpm{green}{red} b \pm c  \] 
\end{document}

enter image description here

2 Answers 2

8
\documentclass{article}
\usepackage{stackengine,xcolor}
\def\cpm{\mathbin{\ensurestackMath{\abovebaseline[-3.4pt]{%
  \stackunder[-3.5pt]{\color{green!70}+}{\color{red}-}}}}}
\begin{document}
$1\pm2\cpm3$
\end{document}

enter image description here

If you need a version that works across math styles (EDITED to add \cmp support):

\documentclass{article}
\usepackage{stackengine,xcolor,scalerel}
\def\cpm{\mathbin{\ThisStyle{\ensurestackMath{\abovebaseline[-\dimexpr1pt+2.4\LMpt]{%
  \stackunder[-\dimexpr1pt+2.5\LMpt]{\color{green!70}\SavedStyle+}{%
  \color{red}\SavedStyle-}}}}}}
\def\cmp{\mathbin{\ThisStyle{\ensurestackMath{\abovebaseline[-\dimexpr1.1pt+0.55\LMpt]{%
  \stackunder[-\dimexpr1pt+2.5\LMpt]{\color{green!70}\SavedStyle-}{%
  \color{red}\SavedStyle+}}}}}}
\begin{document}
$1\pm2\cpm3\mp4\cmp5$\par
$\scriptstyle1\pm2\cpm3\mp4\cmp5$\par
$\scriptscriptstyle1\pm2\cpm3\mp4\cmp5$\par
\end{document}

enter image description here

6
  • I was about to ask for your edit, thank you very much! Nov 13, 2015 at 12:31
  • 1
    @thewaywewalk "The difficult we do immediately. The impossible takes a little longer." Nov 13, 2015 at 12:33
  • @thewaywewalk extending the \cpm macro to take color arguments is trivial, but if you would like it presented that way, let me know and I will edit. Nov 13, 2015 at 12:46
  • No, it's trivial, I can manage. If you're bored you could add the minus+plus version, that does not seem trivial. :D Nov 13, 2015 at 12:50
  • 1
    @thewaywewalk Done. Nov 13, 2015 at 13:09
4

Just for fun with plain TeX

\def\cpm{%
  \mathchoice%
  {\xcpm\displaystyle{.2ex}{.53ex}}% displaystyle
  {\xcpm\textstyle{.2ex}{.53ex}}% textstyle
  {\xcpm\scriptstyle{.16ex}{.43ex}}% scriptstyle
  {\xcpm\scriptscriptstyle{.11ex}{.35ex}}% scriptscriptstyle
}

\def\xcpm#1#2#3{\mathbin{\ooalign{%
      \raise #2\hbox{\pdfliteral{0 1 0 rg}$#1+$\pdfliteral{0 g}}\cr
      \lower #3\hbox{\pdfliteral{1 0 0 rg}$#1-$\pdfliteral{0 g}}%
  }}}

$1 \pm 2 \cpm 3$

$_{1 \pm 2 \cpm 3}$

$_{_{1 \pm 2 \cpm 3}}$
\bye

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .