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}
1

1 Answer 1

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

1

You must log in to answer this question.

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