9

The line

\[ \textcolor{green}{\sum}_{k=0}^n \]

makes the k=0 and n parts go to the right of Sigma instead of at the top and bottom, as it should be. Can we retain k=0 and n at the top and bottom?

1
  • 2
    If you often use color in math-mode you may be interested in \mathcolor (avoids problems with spacing): Colored symbols Commented Mar 2, 2013 at 21:50

4 Answers 4

14

The \textcolor macro will undo the operator status of \sum so that both subscript and superscript are placed like they would at an ordinary symbol.

The macro \mathop makes the whole sequence (the green sum symbol) an operator again.

When you often use colored operator symbols, define a new macro, say

\newcommand*{\opcolor}[2]{\mathop{\textcolor{#1}{#2}}}

and use it as \opcolor{green}{\sum}.

I have also defined a \csum macro based on \opcolor.

Code

\documentclass{article}
\usepackage{xcolor}
\newcommand*{\opcolor}[2]{\mathop{\textcolor{#1}{#2}}}
\newcommand*{\csum}[1]{\opcolor{#1}\sum}

\begin{document}
\[ \mathop{\textcolor{green}{\sum}}_{k=0}^n \]

\[ \csum{green}_{k=0}^n \]

\[ \opcolor{green}{\sum}_{k=0}^n \]
\end{document}

Output

enter image description here

2
  • Isn’t \operatorname generally recommended over \mathop? Commented Mar 18, 2018 at 13:40
  • \mathop isn't a macro, it is TeX primitive.
    – wipet
    Commented Aug 16, 2022 at 18:38
8

Declare the green symbol as an operator via \mathop.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\begin{document}
\[ \mathop{\textcolor{green}{\sum}}_{k=0}^n \]
\end{document}
4

Color support in LaTeX is implemented by setting the color and resetting it after the end of the current group. \textcolor uses the curly braces as group. This has the side effect in math, that it also makes a subformula with horizontal spacing of a normal math atom.

The following example defines \mathcolor that uses \begingroup and \endgroup for the group without the side effect of a subformula in math.

If the integral is colored only, then the color reset inserts a special that prevents the proper working for the sub- and superscripts. \mathop helps for \sum, but will fail for \int, because TeX will no longer now, what's inside \mathop{...} if it sets the sub- and superscripts.

Therefore, the example uses green for the whole term including sub- and superscripts and sets black for the latter explicitly.

\documentclass{article}
\usepackage{xcolor}
\colorlet{mygreen}{green!80!black}% a littler darker

\makeatletter
\newcommand*{\mathcolor}{}
% Catch an optional argument in #1
\def\mathcolor#1#{\math@color{#1}}%
\newcommand*{\math@color}[3]{%
  % #1: empty or optional argument including brackets
  % #2: mandatory color argument
  % #3: stuff to be colored
  \begingroup\color#1{#2}#3\endgroup
}
\makeatother

\begin{document}
\[
  \mathcolor{mygreen}{
    \int_{\mathcolor{black}{k=0}}^{\mathcolor{black}{n}}
  }
  \mathcolor{red}{=}
  \mathcolor{mygreen}{
    \int\limits_{\mathcolor{black}{k=0}}^{\mathcolor{black}{n}}
  }
\]
\end{document}

Result

2
  • Two questions: Why do you define \mathcolor twice? In the second definition, what is the effect of the second #? I can't make sense of the comments in your code.
    – schtandard
    Commented Mar 18, 2018 at 13:32
  • 2
    @schtandard The first definition uses \newcommand that would throw an error message, if \mathcolor is already defined. \def does not do such checking. The # at the end of the parameter text catches right before a opening curly brace (the mandatory color argument here). Thus, #1 gets all up to the {. Examples: \color[HTML]{FF0000} with #1 as [HTML]; \color{mygreen} with empty #1. Commented Mar 18, 2018 at 13:35
3

With a recent version of LaTeX (2022-06-01) you can use \mathcolor: you just need to activate color support.

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

\begin{document}

\[
\mathcolor{green!80!blue}{\sum}_{k=1}^n a_k
\]
\begin{center}
$\mathcolor{green!80!blue}{\sum}_{k=1}^n a_k$
\end{center}

\end{document}

The example shows that the colored operator works in all math styles.

enter image description here

You must log in to answer this question.

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