24

How can I change the color of operators like "log", "lim" or the root symbol of \sqrt command?

4
  • 5
    The \textcolor{blue}{stuff to be colored} command works in math mode too ;-) Welcome to TeX.SX. You need \usepackage{xcolor} or \usepackage{color} however. But I don't recommend to use too much colours deviating from the normal (black, I suppose?) colour
    – user31729
    Oct 20, 2015 at 4:58
  • But that would change the color of the operands too. What I want to do is to express only the operator in red so that it stands out from other text leaving operands in black. For example \lim is red but _{n\to\infty} is left unchanged. How can I achieve this? Thanks
    – Ben
    Oct 20, 2015 at 5:40
  • The answer by Croco proved the opposite ;-)
    – user31729
    Oct 20, 2015 at 14:19
  • @ChristianHupfer - Unfortunately, the answer by Croco also introduced a bug, viz., a failure to assure that the newly-colored operators are assigned type mathop.
    – Mico
    Oct 29, 2015 at 16:44

6 Answers 6

20

You could tap into the primitive \mathop:

enter image description here

\documentclass{article}
\usepackage{xcolor,amsmath}
\DeclareMathOperator{\abc}{abc}

\let\oldmathop\mathop
\def\mathop#1{\oldmathop{\textcolor{red}{#1}}}
\let\oldsurd\surd
\def\surd{\textcolor{red}{\oldsurd}}

\begin{document}
\[
  \abc d \quad
  \sin \theta \quad
  \log_e \quad
  \lim_{x \rightarrow 0} \quad
  \int_a^b \quad
  \sqrt[n]{} \quad
  \surd
\]

\end{document}

The above is not thoroughly tested though...

4
  • 1
    Note that \mathop is also used in \stackrel and \overset.
    – egreg
    Oct 20, 2015 at 10:14
  • 3
    In particular, try $\underset{a+b}_{c}$ and the result will probably be unsatisfying.
    – egreg
    Oct 20, 2015 at 10:57
  • Similar comment as I left under egreg's answer: Your code doesn't seem to colorize either \sqrt[3]{2} or \sqrt[\leftroot{-2}\uproot{2}\beta]{k}. (The latter snippet may be found in the user guide of the amsmath package...)
    – Mico
    Oct 29, 2015 at 16:49
  • @Mico: Yes; I made no changes to \sqrt, only to \surd, as it seemed the OP was interested in the "square root symbol".
    – Werner
    Oct 29, 2015 at 17:12
22

Here's a possibility; I wouldn't hack into \mathop, which is a primitive also used in several other situations (in \overset, for instance).

\documentclass{article}
\usepackage{xcolor,amsmath,xpatch,letltxmacro}
\DeclareMathOperator{\abc}{abc}

\xpatchcmd{\qopname}{#3}{\textcolor{red}{#3}}{}{}
\LetLtxMacro\latexsqrt\sqrt
\RenewDocumentCommand{\sqrt}{om}{%
  \colorlet{current}{.}
  \IfNoValueTF{#1}
    {\textcolor{red}{\latexsqrt{\textcolor{current}{#2}}}}%
    {\textcolor{red}{\latexsqrt[#1]{\textcolor{current}{#2}}}}%
}


\begin{document}
\[
  \abc d \quad
  \sin \theta \quad
  \log_e \quad
  \lim_{x \rightarrow 0} \quad
  \int_a^b \quad
  \sqrt{2} \quad
  \sqrt[3]{x+1}
\]

\end{document}

enter image description here

The redefined \sqrt command works also with \leftroot and \uproot.

A variant coloring different the various operators and also for defining new ones to have colors. The commands given as first argument to \colorizeoperator should already been defined and be operators of \lim type.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}

\newcommand{\colorizeoperator}[2]{%
  % #1 = operator, #2 = color
  \begingroup\def\qopname##1##2##3{%
    \xdef#1{%
      \noexpand\qopname
      \unexpanded{##1}%
      ##2%
      {\begingroup\noexpand\color{#2}##3\endgroup}%
    }%
  }%
  #1%
  \endgroup
}
\makeatletter
\newcommand{\DeclareColoredMathOperator}{%
  \@ifstar
    {\def\DCMO@@{\DeclareMathOperator*}\DCMO@}
    {\def\@DCMO{\DeclareMathOperator}\DCMO@}%
}
\newcommand\DCMO@[3]{%
  % #1 = operator, #2 = name, #3 = color
  \DCMO@@{#1}{\begingroup\color{#3}#2\endgroup}%
}
\makeatother

\colorizeoperator{\lim}{blue}
\colorizeoperator{\sin}{red!60}
\DeclareColoredMathOperator*{\argmin}{arg\,min}{green}
\DeclareColoredMathOperator{\tors}{tors}{green!20!blue}

\begin{document}
\[
\lim_{x\to0}x=\sin0-\argmin_x 0+\tors
\]
\end{document}

enter image description here

0
11

FWIW, in ConTeXt you can change the color of operators using

\setupmathematics[functioncolor=red]

and change the color of \sqrt using

\setupmathradical[color=blue]

Here is a minimal example:

\setupmathematics[functioncolor=red]
\setupmathradical[color=blue]

\starttext
\startformula
\sqrt{\log\left( \frac{ \sin x } { \cos x } \right)}
\stopformula
\stoptext

which gives

enter image description here

This only affects operators, so commands like \stackrel, \underset, etc. that use \mathop in the background continue to work as expected.

6

Here's a LuaLaTeX-based solution. The solution doesn't modify the definition of \log, \sin, \cos, etc. Instead, it sets up a Lua function (called color_op) that encases these directives in {\color{<color>}...} wrappers, while ensuring that the now colored objects maintain the spacing properties of objects of type mathop. The Lua function, in turn, is assigned to the process_input_buffer callback, which operates at a very early stage of processing -- before TeX itself does most of its processing.

This setup may, at least at first, seem to be more involved than approaches that modify the low-level macros \mathop and \qopname directly. An upside is that it's possible to assign distinct colors to each "math operator". Another upside is that -- as long as one has at least some familiarity with Lua's pattern matching syntax -- it's straightforward to deal with complicated \sqrt cases, such as $\sqrt[\leftroot{-2}\uproot{2}\beta]{k}$. This code snippet is, by the way, not of my own invention! It comes from the user guide of the amsmath package.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage[dvipsnames,x11names,svgnames]{xcolor} % lots of predefined colors...
\usepackage{amsmath} % for "\leftroot" and "\uproot" macros

\usepackage{luacode}
\begin{luacode}
in_verbatim_env = false
function color_op (line)
  if string.find ( line , "\\begin{[vV]erbatim}" ) then
    in_verbatim_env = true
  elseif string.find ( line , "\\end{[vV]erbatim}" ) then
    in_verbatim_env = false
  else
    if not in_verbatim_env then
      line = string.gsub ( line, "\\log", "\\mathop{\\color{blue}%0}" )
      line = string.gsub ( line, "\\sin", "\\mathop{\\color{magenta}%0}" )
      line = string.gsub ( line, "\\cos", "\\mathop{\\color{SeaGreen}%0}" )
      line = string.gsub ( line, "\\lim", "\\mathop{\\color{cyan}%0}" )

      line = string.gsub ( line, "(\\sqrt)%s-(%b{})", 
                                 "{\\color{brown}%1{\\color{black}%2}}" )
      line = string.gsub ( line, "(\\sqrt%s-%b[])%s-(%b{})", 
                                 "{\\color{red}%1{\\color{black}%2}}" ) 
    end
  end
  return line
end
luatexbase.add_to_callback ( "process_input_buffer", color_op, "color_op" )
\end{luacode}

\begin{document}    
$\log(z)$, 
$\sin\theta$,
$\lim_{x\to\infty} u(x)$

\medskip
$\sqrt{2}$, $\sqrt [\leftroot{-2}\uproot{2}\beta] {k}$, 
$\displaystyle\sqrt[3]{\int_0^1 \frac{\sin x}{\cos x}\,dx}$
\end{document}
3
  • 1
    Note that this will also change the content inside the verbatim environment!
    – Aditya
    Oct 21, 2015 at 2:21
  • 2
    Since many users reach this site after searching on google, I tend to think of the answers to be more general than the specific needs of the OP. My comment was simply to point out that the solution has a limitation that it does substitutions everywhere in the text. Of course, it is possible to disable the substitution function for specific environments, so, in principle, your answer can be extended to handle verbatim etc.
    – Aditya
    Oct 21, 2015 at 4:33
  • 1
    @Aditya - I've taken you up on the suggestion to modify the Lua code so that it doesn't operate on instances of \log, \sin, etc that occur inside verbatim or Verbatim environments.
    – Mico
    Oct 21, 2015 at 11:52
6

The Unicode engines luatex and xetex have the advantage, that colour is taken as a property of the font itself, rather than being added on top of it.

The unicode-math allows to load Unicode math fonts with a certain colour specification. Here we first of all load Latin Modern Math for the entire math mode and proceed to load Latin Modern Math for the glyphs from the \mathop range and \sqrt with the colour specification red. Similar for the operator font.

As you can see in the output, the horizontal bar of the radical is not coloured red. This is due to the fact that the bar is not a math glyph, but a \vrule, which is not a even a glyph.

\documentclass{article}
\usepackage{mathtools,unicode-math,xcolor}

\setmathfont{Latin Modern Math}
\setmathfont{Latin Modern Math}[range={\mathop,\sqrt}, Color=red]
\setmathfontface\mathlm{Latin Modern Math}[Color=red]
\setoperatorfont\mathlm

\DeclareMathOperator{\abc}{abc}
\begin{document}
\[
  \abc d \quad
  \sin \theta \quad
  \log_e \quad
  \lim_{x \rightarrow 0} \quad
  \int_a^b \quad
  \sqrt{2} \quad
  \sqrt[3]{x+1}
\]
\end{document}

enter image description here

2
  • I run MacTeX2015, with all the latest updates, under MacOSX 10.11.1. When I attempt to run your example code, I get the following error message: ! Undefined control sequence. <argument> \g__um_named_ranges_seq l.847 ...{latin,Latin,greek,Greek,num,misc} {up }. What extra code do I need to insert in order to run your MWE?
    – Mico
    Oct 30, 2015 at 17:35
  • @Mico That seems to be a regression of unicode-math. Maybe @WillRobertson can help with that. BTW, for me it works with both, xelatex and lualatex with unicode-math 2015/09/24 v0.8c on Debian GNU/Linux x86_64. Oct 31, 2015 at 14:40
5

As @Christian Hupfer suggested in his comment.

\documentclass{article}
\usepackage{xcolor,amsmath}

\begin{document}

\begin{align*}
\textcolor{black!50!green}{A} &= \color{red}{\sqrt{   \color{blue}{aaa}} }                    \\
\textcolor{black!50!blue}{B} &= \color{black!70}{\lim_{ \color{cyan}{t \rightarrow \infty}} }  \\
\textcolor{green}{C} &= \color{black!10!yellow}{\log_{2}}   \color{blue}{x}
\end{align*}

\end{document}

enter image description here

2
  • 5
    This method doesn't respect the spacing TeX places around objects of type \mathop (which includes \log, \sin, etc).
    – Mico
    Oct 20, 2015 at 11:15
  • 1
    \color{<color spec>} does not take a second argument, as in \color{<color spec>}{…}. You have to use it like {\color{<color spec>} …}. Oct 30, 2015 at 10:12

You must log in to answer this question.

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