# Change TikZ nodes color to grayscale

Say you have some code like this:

\documentclass{standalone}

\usepackage{tikz}
% \PassOptionsToPackage{monochrome}{xcolor}

\begin{document}

\begin{tikzpicture}
% \selectcolormodel{gray}
\node[fill=red] at (0,1) {text};
\node[fill=green] at (0,0.5) {text};
\node[fill=blue] at (0,0){text};
\node[fill=yellow] at (1,1) {text};
\node[fill=violet] at (1,0.5) {text};
\node[fill=orange] at (1,0) {text};
\end{tikzpicture}

\end{document}


And you want to turn the resulting picture in grayscale colors. Is there a way to do this in LaTeX without using external tools (convert, imagemagick, etc.) and, of course, without having to manually change all the color values?

I already tried with the methods from how to create PDF in grayscale mode or TikZ figure only? and Monochrome in Tikz (see the commented code) but they both do not seem to work when the color is specified as a node option.

It would be also nice to know why the \selectcolormodel{gray} method does not work in this context.

-
\PassOptionsToPackage{gray}{xcolor} works when put before loading TikZ. –  Qrrbrbirlbel May 3 '13 at 22:21
@Qrrbrbirlbel You should make an answer out of it! –  knut May 3 '13 at 22:26
@Qrrbrbirlbel Gosh! I tried with monochrome in every possible position and didn't think about gray! I'll accept your answer if you'll make one, but maybe this may now be considered a duplicate? –  dcmst May 3 '13 at 22:35
Interestingly, while \node[fill=<color>] won’t work, \node[<color>, draw=none, fill, text=black] does. (<color> is internally passed to TikZ key color). I guess TikZ uses on some point his own color definition by-passing xcolor. –  Qrrbrbirlbel May 3 '13 at 22:53

I don't know exactly what you mean with without having to manually change all the color values?.

If you only want to avoid to change your picture, then the following solution may be a solution:

\documentclass{standalone}

\usepackage{tikz}

\begin{document}
\definecolor{red}{gray}{0.9}
\definecolor{green}{gray}{0.8}
\definecolor{blue}{gray}{0.7}
\definecolor{yellow}{gray}{0.6}
\definecolor{violet}{gray}{0.5}
\definecolor{orange}{gray}{0.4}

\begin{tikzpicture}
\node[fill=red] at (0,1) {text};
\node[fill=green] at (0,0.5) {text};
\node[fill=blue] at (0,0){text};
\node[fill=yellow] at (1,1) {text};
\node[fill=violet] at (1,0.5) {text};
\node[fill=orange] at (1,0) {text};
\end{tikzpicture}

\end{document}


You redefine all colors, but only once per document. Your tikzpicture is unchanged.

You also may put the redefinitions to a style 'grey_colors.sty' and reuse it for differnt documents.

Qrrbrbirlbel gave a better solution with \PassOptionsToPackage{gray}{xcolor} when put before loading TikZ.

You may combine the two solutions. The redefinition of the colors gives you a better control on which grey tone a color should get.

Example:

\documentclass{standalone}
\PassOptionsToPackage{gray}{xcolor}
\usepackage{tikz}

\begin{document}

gray-option:
\begin{tikzpicture}
\node[fill=red] at (0,1) {text};
\node[fill=green] at (0,0.5) {text};
\node[fill=blue] at (0,0){text};
\node[fill=yellow] at (1,1) {text};
\node[fill=violet] at (1,0.5) {text};
\node[fill=orange] at (1,0) {text};
\end{tikzpicture}

Color redefinition:
\definecolor{red}{gray}{0.9}
\definecolor{green}{gray}{0.8}
\definecolor{blue}{gray}{0.7}
\definecolor{yellow}{gray}{0.6}
\definecolor{violet}{gray}{0.5}
\definecolor{orange}{gray}{0.4}
\begin{tikzpicture}
\node[fill=red] at (0,1) {text};
\node[fill=green] at (0,0.5) {text};
\node[fill=blue] at (0,0){text};
\node[fill=yellow] at (1,1) {text};
\node[fill=violet] at (1,0.5) {text};
\node[fill=orange] at (1,0) {text};
\end{tikzpicture}

\end{document}


-
Thanks for the reply. This may be a solution if you have only few colors and not very similar. The actual document has lots of not so different colors (that's why I said I would not want to change them manually) so an automated approach is preferred. –  dcmst May 3 '13 at 22:39
I think I would combine the two answers. \PassOptionsToPackage{gray}{xcolor} to set all colors to gray and the specific color redefinition for some selected colors, if a color does not look like I want (e.g. if two colors look very similar when they get a gray value). –  knut May 4 '13 at 18:36