5

So I wanted to write down a definite integral from say a to b but I want a to be red and b to be green. I tried using:

\int_{a}^{b} 

and it works great, but when I add color using \usepackage[dvipsnames]{xcolor} like

\int_{color{red}a}^{color{green}b} 

b breaks out of the integral and becomes an exponent

also when I only add color to b like

\int_{a}^{\color{green}b} 

a becomes green too instead of just b

does anyone know how to fix it?

\documentclass{article} \usepackage[dvipsnames]{xcolor} \begin{document} \begin{equation} \int_{\color{red}a}^{\color{green}b}f(x)dx \end{equation} \end{document} 
1
  • 5
    use {{\color...}} the superscript brace does not scope the color (or use \mathcolor{red}{a}) Commented Oct 16, 2023 at 18:56

2 Answers 2

5

Just to follow up on @DavidCarlisle's comment: You need to write {\color{...}...}, not color{...}... for the terms in the lower and upper limits of integration.

Actually, if your TeX distribution is reasonably up to date -- say, mid-2022 or more recent -- you may want to use \mathcolor{...}{...} for math-y material.

enter image description here

\documentclass{article} \usepackage[dvipsnames]{xcolor} % 'dvipsnames' option to access "JungleGreen" \begin{document} \begin{equation} \int_{\mathcolor{red}{a}}^{\mathcolor{JungleGreen}{b}} f(x)\,dx \end{equation} \end{document} 
2
  • 1
    Thanks, I didn't know that \mathcolor or \textcolor even existed, also I specified the package because I am actually using "JungleGreen" instead and didn't want to eliminate the unlikely scenario of the package causing the problem. Commented Oct 17, 2023 at 11:26
  • @GeorgeGkoumas - Thanks. I'll go ahead and update my answer to employ "JungleGreen" instead of "blue". :-) Commented Oct 17, 2023 at 11:45
4

As a general rule, avoid \color preferring \textcolor whenever possible, that is, almost always you just have a small chunk of text to colorize.

With recent LaTeX versions, you can use \mathcolor with the same syntax as \textcolor when in math, but when the object to be colored is a single ordinary atom, \textcolor works flawlessly (when you cannot update your LaTeX).

\documentclass{article} \usepackage{amsmath} \usepackage[dvipsnames]{xcolor} \begin{document} \begin{gather*} \int_{a}^{b}f(x)\,dx \\ \int_{\mathcolor{red}{a}}^{\mathcolor{green}{b}}f(x)\,dx % <--- best \\ \int_{\textcolor{red}{a}}^{\textcolor{green}{b}}f(x)\,dx % <--- also works \end{gather*} \end{document} 

enter image description here

1
  • Thanks, I didn't even know \mathcolor and \textcolor existed Commented Oct 17, 2023 at 11:27

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.