[Tex/LaTex] XeLaTeX and TikZ – how to ensure the PDFs are using the CMYK color model

colorpdfpgfplotstikz-pgfxetex

The place I want to print my document at needs the PDF to be provided in CMYK. The document is mostly text, except for a few graphs and plots which are done in TikZ or pgfplots.

As suggested in this forum, calling \usepackage[cmyk]{xcolor} should do the trick, but I have no way of verifying that my resulting document is indeed set using CMYK. Using imageMagick, the PDF is reported as:

$ identify -verbose test.pdf | grep Colorspace
   Colorspace: Gray
   Colorspace: sRGB
   [... lots of Colorspace: sRGB lines here ...]

I tried the command provided in the accepted answer of this thread on stackoverflow:

$ gs        -o test-cmyk.pdf        -sDEVICE=pdfwrite        -sProcessColorModel=DeviceCMYK        -sColorConversionStrategy=CMYK        -sColorConversionStrategyForImages=CMYK         test.pdf 

But the result when calling identify is the same: every single colorspace is reported as Colorspace: sRGB. The same happens when I use imageMagick directly to try and convert the PDF:

$ convert in.pdf -profile "/usr/share/ghostscript/9.07/iccprofiles/default_cmyk.icc" out.pdf

I found a few discussions in this channel, in particular the two below. However, neither give me a final answer as to how to check if my PDF is indeed using the CMYK color model.

PDF colour model and LaTeX

Option cmyk for xcolor package does not produce a CMYK PDF

In particular, the latter sugests the use of the command \pdfcompresslevel=0. The problem with that is, that apparently XeLaTeX does not like that command:

! Undefined control sequence.
l.4 \pdfcompresslevel
                     =0

Best Answer

Here's a quick bash script to check for instances of RGB colors:

#!/bin/sh
pdftops $1.pdf -eps $1.eps
status=$(grep -o "RGB" $1.eps | wc -l)
echo "$status instance(s) of RGB colorspaces found in file $1.eps"
exit $status

And a .bat version for Windows:

@echo off
pdftops %1.pdf -eps %1.eps
type %1.eps | find "RGB" /c > __my%1.tmp
set /p status=<__my%1.tmp
del __my%1.tmp
echo %status%
exit /b %status%

TeX Live includes a copy of pdftops; MikTeX/other users will need to download and place on their paths a Windows port of pdftops. One such port is provided as a part of xpdf.

This solution is probably not perfect, but it's the best thing I've come up with that doesn't require Adobe Acrobat Pro.

Usage:

Unix/Linux{-based}:

$ sh checkcolorspace test

Windows:

> checkcolorspace.bat test

Explanation:

Converting to EPS gives a plaintext file that can be readily parsed. All color information is listed with one of /DeviceGray, /DeviceCMYK, or /DeviceRGB. By looking for RGB entries, we can determine if RGB is used anywhere in the PDF.

Test File:

\documentclass{article}
\usepackage[
  cmyk, % uncomment _one_ of these only, 
%  rgb, % for testing purposes
]{xcolor}

\begin{document}
\textcolor{red}{test}
\textcolor{green}{test}
\textcolor{blue}{test}
\textcolor{cyan}{test}
\textcolor{magenta}{test}
\textcolor{yellow}{test}
\textcolor{black}{test}
\end{document}