5

In LaTeX, usually I use

default color {\color{blue}changed color.}

enter image description here

or

default color \textcolor{blue}{changed color.}

to change the color of a certain part of the text. Now I want to change the color of the whole sentence to a determined color, say red, as follows

enter image description here

I tried

\textcolor{red}{default color {\color{blue}changed color.}}

but it just has effect on the uncolored text:

enter image description here

For certain reasons, I need to keep "\textcolor{blue}{changed color.}" in the origial text. Is there any way of overwritting the colored text?

David Carlisle
  • 757,742
ZHUANG
  • 97
  • It is not clear, what is your problem. This works: \color{red} default color \textcolor{blue}{changed color}. default color again – Zarko Dec 01 '20 at 07:31

3 Answers3

6

It would be much better not to add the nested color or to add it as a locally defined command such as \mycolor that you could define to be a color, or nothing in different places, but if you really need this you can locally define \color to do nothing.

enter image description here

\documentclass{article}

\usepackage{color} \begin{document}

\textcolor{red}{\renewcommand\color[2][]{}default color {\color{blue}changed color.}} \end{document}

David Carlisle
  • 757,742
  • @campa well as I say, I wouldn't do this at all or better to use a custom command, so hopefully if this form is used you have a pretty good idea what is in that scope and not accepting arbitrary input and need "just in case" safety. But yes it would be safer, I'll update.. – David Carlisle Dec 01 '20 at 08:34
6

Building upon David Carlisle's answer, you can define a macro that will be even nestable (is that a word?) ---

\documentclass{article}

\usepackage{color} \newcommand{\absolutetextcolor}[2]{% \textcolor{#1}{% \renewcommand\color[2][]{}% #2}% }

\begin{document}

\absolutetextcolor{red}{default color {\color{blue}changed color.}}

\absolutetextcolor{red}{default color {\absolutetextcolor{blue}{changed color.}}} \end{document}

enter image description here

Rmano
  • 40,848
  • 3
  • 64
  • 125
-2

enter image description here

\documentclass[10pt,a4paper]{article}

\usepackage{tikz} \begin{document} default color {\color{blue}changed color.}\ \textcolor{red}{default color {\color{blue}changed color.}}\ \textcolor{red}{default color {changed color.}} \end{document}

js bibra
  • 21,280