TransWikia.com

Extra space when usin (re)newcommand

TeX - LaTeX Asked by Tom Solid on December 7, 2020

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}

One Answer

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

Correct answer by egreg on December 7, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP