# How to color just the \vec symbol?

How do I color only the vector symbol in \vec f? I'm using beamer, so xcolor is already loaded.

Extra "points" to an answer that also explains how to fish..and not only gives me the fish.

Thanks!

-

$\color{red}{\vec{\color{black}{f}}}$


and if you want it a bit more automated

\newcommand{\colorvec}[1]{{\color{red}\vec{\color{black}{#1}}}}


which is to be used as $\colorvec{g}$

-
What about $\colorvec{a}+\colorvec{b}$? –  egreg Mar 4 '12 at 0:11
At least correct it. –  egreg Mar 4 '12 at 0:17
@egreg done, by adding extra grouping {} –  cmhughes Mar 4 '12 at 0:24

You can use \textcolor that works also in math mode:

\documentclass{article}
\usepackage{xcolor}
\newcommand{\rvec}[2][black]{%
\textcolor{red}{\vec{\textcolor{#1}{#2}}}
}
\begin{document}
$\vec{x}$

$\rvec{x}$

$\rvec[green]{x}$
\end{document}


In this way you're sure that the color change will be limited to this object. I've added an optional argument to change also the color of the variable.

-
Great idea! love it! I'm surprised it didn't come up in other questions of this type. –  Yossi Farjoun Mar 4 '12 at 0:12
@YossiFarjoun Probably because it's a bad idea to color the arrow. ;-) –  egreg Mar 4 '12 at 0:28
I meant, for example here. I think that for a presentation, color is great when used sparingly...in this case, I want to have the arrow overlaid...so I want it in a different color to bring the attention to it... –  Yossi Farjoun Mar 4 '12 at 14:53

The question How to color only a tilde accent was closed as "Duplicate" improper. There is very interesting problem which isn't solved in mentioned post nor here. When you return back to the black color in the nucleus of mathaccent atom, then this color manipulation needs \special or \pdfliteral and the skewchar correction of accent placement is lost. See the following example:

$\widetilde{E}\ \widetilde{\special{}E}\ \coloredaccent{red}\widetilde{E}$


which gives the result:

Note that the second accent is positioned wrong because here is \special in the nucleus and the skewchar TeX positioning is lost. We need to return to the black color here which expands to such \special or \pdfliteral. How the third accent is created? This surprise follows:

\documentclass{article}
\usepackage{xcolor}

\newmuskip\tmpmudim  \newdimen\tmpdim

\def\coloredaccent#1#2#3{% #1=color, #2=accent, #3=nucleus
{\ifnum\skewchar\textfont1<0 \tmpmudim=0mu
\else \calculatemukern{#3}{\char\skewchar\textfont1 }
\fi
\colorlet{outcolor}{.}
\color{#1}\mkern2\tmpmudim #2{\color{outcolor}\mkern-2\tmpmudim#3}
}
}
\def\calculatemukern #1#2{%
\setbox0=\hbox{\the\textfont1 #1#2}\setbox1=\hbox{\the\textfont1 #1\null#2}%
\tmpmudim=\expandafter\ignorept\the\tmpdim mu \tmpmudim=288\tmpmudim
\tmpdim=16em \divide\tmpmudim
by\expandafter\ignorefracpart\the\tmpdim\relax
}
\def\ignorefracpart#1.#2\relax{#1}
{\lccode\?=\p \lccode\!=\t  \lowercase{\gdef\ignorept#1?!{#1}}}

\begin{document}

$\widetilde{E}\ \widetilde{\special{}E}\ \coloredaccent{red}\widetilde{E}$

\end{document}


We need to emulate the skewchar kerning (implemented in TeX) by macros. First, we do measurement of kern between nulcelus and skewchar. The result is in pt. Second, we convert this result to the mu units in order to the result is applicable in \scriptstyle and \scriptscripstyle too. Finally, the \mkern2\tmpmudim and \mkern-2\tmpmudim is used to the correction of the accent placement.

-