How do I force tikz to use CMYK everywhere (also in shadings)?

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.


Update

tikz now supports this feature since version 3.1.3. Just make sure you have \usepackage[cmyk]{xcolor} before you load tikz.


It's quite late for the OP, but if someone faces this problem, there is now a package (pgf-cmykshadings) that does just that. According to its documentation "The pack­age at­tempts to pro­duce shad­ings con­sis­tent with the cur­rently se­lected xcolor colour model. The rgb, cmyk, and gray colour mod­els from the xcolor pack­age are sup­ported."

MWE

Here's a MWE demonstrating how to use pgf-cmykshadings to produce a CMYK shading as asked in the question. You need to load pgf-cmykshadings before tikz for the shadings to be set up correctly. It's a good idea to load xcolor with the cmyk option before pgf-cmykshadings to ensure that all colours will be output in CMYK and there will be no colour mismatches.

\documentclass{minimal}
\usepackage[cmyk]{xcolor}
\definecolor{mygreen}{cmyk}{1,0,0.57,0.42} 
\usepackage{pgf-cmykshadings}
\usepackage{tikz}
\begin{document}
\color{mygreen}{MY TEXT}
\bigskip

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

\tikz
  \shade [left color=cyan, right color=magenta]
  (1,1) rectangle (5,2);
\end{document}

enter image description here