10

I have this command set up: \newcommand\blue[1]{{\color{blue}#1}}

In my document, I use \color{blue}.

Then I'd like to change the colour back to the original. Is there a way I can do this without

  1. scoping the \color{blue} or
  2. hardcoding the colour \color{black}.
8
  • 7
    Only way is \newcommand\blue[1]{\bgroup\color{blue}#1\egroup} or use \newcommand\blue[1]{\textcolor{blue}{#1}}
    – user11232
    Sep 5, 2014 at 14:49
  • 3
    \color is a command to use with care. It will turn the whole text into blue (in your case). It's better, as Harish Kumar proposed, to restrict it's usage to \textcolor{blue}{text}
    – user31729
    Sep 5, 2014 at 14:56
  • @HarishKumar: You should make an answer ;-)
    – user31729
    Sep 5, 2014 at 15:15
  • @HarishKumar but that is explicitly what the question asks that you don't do in (1.) :-) Sep 5, 2014 at 15:55
  • "Changing the color back to the original" is sort of the definition of scoping....
    – Derek
    Sep 5, 2014 at 15:57

2 Answers 2

3
\documentclass{article}

\usepackage{color}
\makeatletter
\def\zz{\edef\zzz{\pdfliteral{\current@color}}}%pdftex version
\makeatother
\begin{document}


\color{green}\zz one two \color{blue} three four \zzz five

\end{document}
1
  • You are one hell of Hacker! ;-) +1
    – user11232
    Sep 5, 2014 at 22:53
2

Store the colours used in your document in a stack. The fifo-stack package provides the necessary functionality.

\documentclass{article}
\usepackage{color}
\usepackage{fifo-stack}
\FSCreate{colors}{black}
\makeatletter
\let\old@color\color
\renewcommand\color[1]{\FSPush{colors}{#1}\old@color{#1}}
\newcommand\colorend{\FSPop{colors}\old@color{\FSTop{colors}}}
\makeatother
\begin{document}
The quick brown fox jumps over the lazy dog.\par
\color{blue}
The quick brown fox jumps over the lazy dog.\par
\color{red}
The quick brown fox jumps over the lazy dog.\par
\colorend
The quick brown fox jumps over the lazy dog.\par
\colorend
The quick brown fox jumps over the lazy dog.
\end{document}

Each time you use a colour, it goes on the stack. The \colorend macro discards the top of the stack (the current colour) and then sets the colour to whatever is now at the top.

colours

You must log in to answer this question.

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