3

I have examined this question, where the OP wanted to discard the effect of \color when it is used inside an other \textcolor. @Rmano was faster than me and answered the question with the same way that I wanted.

However, "our" solution has a little problem about an extra whitespace:

output

I have tried to fix it many ways like \relax, \gobble, \xspace \ignorespaces, but I was shooting randomly and couldn't find the solution. Do you have any ideas?

\documentclass{article}

\usepackage{color}

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

\begin{document} Some text

Some {\color{blue} text}

Some \textcolor{blue}{text}

\textcolor{red}{Some {\color{blue} text}}

\Textcolor{red}{Some {\color{blue} text}} %should produce the same

\Textcolor{red}{Some {\color{blue}text}} %should produce the same

\textcolor{red}{Some {\color{blue} text}}

\end{document}

Tom Solid
  • 839

1 Answers1

3

The standard working of \color ends with \ignorespaces.

\documentclass{article}

\usepackage{color}

\newcommand{\Textcolor}[2]{% \textcolor{#1}{\renewcommand{\color}[1]{\ignorespaces}#2}% }

\begin{document}

Some text

Some {\color{blue} text}

Some \textcolor{blue}{text}

\textcolor{red}{Some {\color{blue} text}}

\Textcolor{red}{Some {\color{blue} text}} %should produce the same

\Textcolor{red}{Some {\color{blue}text}} %should produce the same

\textcolor{red}{Some {\color{blue} text}}

\end{document}

enter image description here

It should actually be

\renewcommand{\color}[2][]{\ignorespaces}

to cope with cases such as \color[rgb]{0.1,1,0.3}.

egreg
  • 1,121,712
  • Thanks, it works :) You should leave a comment at the original question https://tex.stackexchange.com/a/573099/100231 I don't want to take your merits. – Tom Solid Dec 01 '20 at 09:45