How to change color of operators (lim, log, etc.)?

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


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...


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.