[Tex/LaTex] How to force tikz to use CMYK everywhere (also in shadings)

colorshadingtikz-pgf

I'm doing a poster with the baposter which uses tikz and at some point realized that some colors that should be identical are not. It turns out that tikz apparently resorts to RGB color space as soon as there is a shading involved, whereas I'm doing the poster (a I intend to print it…) in CMYK (which is also the default in the baposter class).

Now my question is: How do I force tikz to use CMYK everywhere?

Here's a minimal example that demonstrates the problem:

\documentclass{minimal}

%%% uncomment to use CMYK
\usepackage[cmyk]{xcolor}
\definecolor{mygreen}{cmyk}{1,0,0.57,0.42} 

%%% uncomment to use RGB
% \usepackage[rgb]{xcolor}
% \definecolor{mygreen}{HTML}{009440}

\usepackage{tikz}

\pdfcompresslevel=0

\begin{document}

\color{mygreen}{MY TEXT}

\tikz 
  \shade [left color=mygreen,right color=mygreen] 
  (1,1) rectangle (2,2);
%
\tikz 
  \fill [mygreen] 
  (1,1) rectangle (2,2);

\end{document}

The two boxes, which should have the same color, have not. The shaded box should have the same color as the solid box and the text.

(Note: this question is not so much about the baposter class, however, as baposter uses tikz the underlying problem leads to the fact that the frames of the boxes and the shadings in the header of the boxes have different colors.)

Best Answer

Unfortunately, the requested feature is unsupported.

In general, your approach works fine: if you write

\usepackage[cmyk]{xcolor}

all color definitions result in cmyk colors. But shadings are special: they are not based on xcolor's drivers but on pgf's drivers. And the pgf drivers for shadings supports RGB, nothing else. I believe that pgf calls colorspace conversion routines in order to convert from cmyk back to RGB whenever it generates shadings.

What you need is a feature request for PGF in order to respect the global xcolor configuration and to generate shadings in that color space.


There may be work-arounds. These, however, depend on the urgency - if you say that you will file feature requests and will live with the restriction, that is fine.

If you really need a workaround, you can continue reading.

The work-around that I have in mind is to use pgfplots. It has a couple of plot-related shadings and comes with its own related drivers. These, in turn, support cmyk - and most shadings can be expressed as plot-based shadings. The effort to convert these shadings from tikz pictures which have a super embedding into your pictures to pgfplots plots would be medium.

Here is an attempt in this direction:

\pdfcompresslevel=0
\documentclass{minimal}

%%% uncomment to use CMYK
\usepackage[cmyk]{xcolor}
\definecolor{mygreen}{cmyk}{1,0,0.57,0.42} 

\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

%%% uncomment to use RGB
% \usepackage[rgb]{xcolor}
% \definecolor{mygreen}{HTML}{009440}

\usepackage{tikz}


\begin{document}

\color{mygreen}{MY TEXT}

\tikz 
  \shade [left color=mygreen,right color=mygreen] 
  (1,1) rectangle (2,2);
%
\tikz 
  \fill [mygreen] 
  (1,1) rectangle (2,2);

\begin{tikzpicture}
    % this statement is needed for pgfplots v1.8. 
    % pgfplots 1.9 or newer inherits it from
    % \usepackage[cmyk]{xcolor}:
    \pgfplotsset{mesh/colorspace explicit color output=cmyk}

    \begin{axis}[
        x=1cm,y=1cm,
        hide axis,
        view={0}{90}]
    \addplot[surf,mesh/color input=explicit,shader=interp] 
    table[meta=cdata] {
        x y cdata
        1 1 color=mygreen
        2 1 color=yellow

        1 2 color=mygreen
        2 2 color=yellow
    };
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

It relies on a surface plot with explicit color using the rectangular coordinates (1cm,1cm) (2cm,2cm) and the colors are listed in the table. The verbose syntax color=<name> is necessary to distinguish from something like 0,0,1 or gray=0.5.