[Tex/LaTex] Change TikZ nodes color to grayscale

colortikz-pgf

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}

enter image description here
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.

Best Answer

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}

enter image description here

Related Question