Sign up ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.


share|improve this question

3 Answers 3

up vote 13 down vote accepted

How about


and if you want it a bit more automated


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

enter image description here

share|improve this answer
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:




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.

enter image description here

share|improve this answer
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 this case, I want to have the arrow 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:

colored accent problem

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:


\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 }
    \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}%
   \tmpdim=\wd0 \advance\tmpdim by-\wd1
   \tmpmudim=\expandafter\ignorept\the\tmpdim mu \tmpmudim=288\tmpmudim
   \tmpdim=16em \divide\tmpmudim
{\lccode`\?=`\p \lccode`\!=`\t  \lowercase{\gdef\ignorept#1?!{#1}}}


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


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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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