6

My use case is like this: I use a "symbol" font like fontawesome, where each letter/glyph is used as an icon, and is colorized in the Latex document using \textcolor. However, some symbols I need are not in fontawesome, so I need to supply them as vector art - that is, .pdf; and in that case, I'd like to colorize the thusly included PDF in the same way as the rest of the fontawesome glyphs.

So far, I've found this:

Can \includegraphics be used to change an image color?

Interestingly, for the vector graphics that I've tried it on, \color command actually changes all the black into whatever color was given.
This is supported by pdfTeX/LuaTeX only and only if the graphics does not contain color settings. This behavior can be overwritten by the option resetcolor for \includegrahics. Then pdftex.def calls \normalcolor before the image is included.

Change color in finished pdf file

Easy case with uncolored PDF and pdfTeX (unhappily very seldom):
The PDF file contains some drawings without explicit color settings and the image will be included by the pdftex driver of the graphics package.

I was puzzled by this at first, but I think I finally came to an example on how to create such a PDF "without explicit color settings", which I'll post here - however, I'd like to know if there's a better/more generic approach that will allow me to "uncolorize" PDFs for inclusion with coloring in Latex (maybe by using imagemagick or something similar?).

Ok, so first, let's generate the vector image to be included using Latex - for simplicity, using picture environment (without Tikz); this is include1.tex:

% include1.tex; build with:
% pdflatex include1.tex
\documentclass[border=0.5mm,varwidth]{standalone}
\begin{document}
  \linethickness{0.5mm}
  \setlength{\unitlength}{10mm}
  \begin{picture}(1,1)(0,0)
    \put(0,0){\line(0,1){1}}
    \put(0,0){\line(1,0){1}}
    \put(1,0){\line(0,1){1}}
    \put(0,1){\line(1,0){1}}
    \put(0.3,0.2){\rule{3mm}{6mm}}
    \put(0.4,0.35){\rule{4mm}{3mm}}
  \end{picture}
\end{document}

So, after running pdflatex include1.tex, I get include1.pdf, which looks like this in evince (it appears colored with black):

include1.png

Then, here is the Latex document I'll be including that into, testinclude.tex:

% testinclude.tex; build with:
% pdflatex testinclude.tex
\documentclass{article}
\usepackage{color}
\usepackage{graphicx}

\begin{document}
Testing here 1: \textcolor{blue}{\includegraphics[height=1em]{include1.pdf}}

Testing here 2: \textcolor{blue}{\includegraphics[height=1em]{include1.pdf}}
\end{document}

After compiling with pdflatex testinclude.tex, I can confirm all is as expected - that is, the included PDF is colorized:

testinclude1.png

All good so far; now, I open include1.pdf in Inkscape, save it first as SVG include2.svg; then reopen the include2.svg in Inkscape, and Save As include2.pdf.

Now, if I change the "Testing here 2" line in testinclude.tex to Testing here 2: \textcolor{blue}{\includegraphics[height=1em]{include2.pdf}}, and recompile with pdflatex testinclude.tex, then I get this:

testinclude2.png

So, clearly the PDF exported from Inkscape is not uncolorized any more, and so Latex cannot colorize it blue, so it appears black.

We can inspect the include1.pdf and include2.pdf by uncompressing them with the pdftk program:

pdftk include1.pdf output include1-unc.pdf uncompress
pdftk include2.pdf output include2-unc.pdf uncompress

Under Ubuntu GNU/Linux, we can view these files with less -L include1-unc.pdf (or by opening them in a text editor). The relevant portion of the uncolorized include1-unc.pdf is:

...
endobj 
4 0 obj 
<<
/Length 298
>>
stream
q
1 0 0 1 0.709 1.417 cm
0 0 1.417 28.346 re f
Q
q
1 0 0 1 1.417 0.709 cm
0 0 28.346 1.417 re f
Q
q
1 0 0 1 29.055 1.417 cm
0 0 1.417 28.346 re f
Q
q
1 0 0 1 1.417 29.055 cm
0 0 28.346 1.417 re f
Q
q
1 0 0 1 9.921 7.087 cm
0 0 8.504 17.008 re f
Q
q
1 0 0 1 12.756 11.339 cm
0 0 11.339 8.504 re f
Q

endstream 
endobj 
3 0 obj 
...

... where we can see a bunch of PDF graphics operators like q and Q.

For the colorized, Inkscape-exported include2-unc.pdf, the relevant portion is:

...
endobj 
4 0 obj 
<<
/Length 212
>>
stream
q
0 0 0 rg /a0 gs
0.711 29.763 1.414 -28.348 re f
1.418 2.126 28.344 -1.418 re f
29.055 29.763 1.418 -28.348 re f
1.418 30.47 28.344 -1.414 re f
9.922 24.095 8.504 -17.008 re f
12.758 19.841 11.336 -8.504 re f
Q

endstream 
endobj 
2 0 obj 
...

... where the rg should be the PDF operator that sets the RGB color; so 0 0 0 rg would set the color to RGB 0,0,0 - that is, black.

So, we can simply "erase" this RGB setting by replacing 0 0 0 rg (which is 8 characters long) with a string of spaces with same length (so we don't mess up the PDF XREF dictionary), either in a text editor like nano, or directly with sed:

sed -i 's/0 0 0 rg/        /' include2-unc.pdf

... then replace the "Testing here 2" line in testinclude.tex with Testing here 2: \textcolor{blue}{\includegraphics[height=1em]{include2-unc.pdf}}, and recompile with pdflatex testinclude.tex, -- and then we're back to a colorized PDF inclusion, as in the first image in this post.

So, my questions are:

  • Is there a tool that would help me automatically "uncolorize" PDFs that I want to include in this manner (so I don't have to uncompress PDFs and manually search for, and replace, PDF color operators)?
  • Is there a technique/package in Latex that would allow me to refer to colorized PDFs on disk, but will "uncolorize" them before they are included with \includegraphics?
2

based on https://tex.stackexchange.com/a/74107/36296

Instead of replacing text in the pdf itself, you could use tikz to colour the shape in the pdf:

\documentclass{article}
\usepackage{color}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{fadings}

\begin{tikzfadingfrompicture}[name=include2,inner sep=0]
  \node [fill=transparent!0]   {\includegraphics[height=1em]{include2.pdf}};
\end{tikzfadingfrompicture}

\newcommand{\mysymbol}{%
    \begin{tikzpicture}[inner sep=0]
      \node[fill=.,minimum width=1em,minimum height=1em](a){};
      \path[scope fading=include2,fit fading=false];
      \node[fill=white,minimum width=1em,minimum height=1em]{};
    \end{tikzpicture}%
}

\begin{document}
Testing here 1: \textcolor{blue}{\includegraphics[height=1em]{include1.pdf}}

Testing here 2: \textcolor{blue}{\includegraphics[height=1em]{include2.pdf}}

Testing here 2: \textcolor{blue}{\mysymbol}

Testing here 2: \textcolor{red}{\mysymbol}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.