# Same font embedded twice when including graphics created with standalone

I use XeLaTeX, TikZ and standalone to create a figure, e.g. a flowchart. When I use includegraphics{} to include that figure into another document which uses the same font, then that font will be embedded into the pdf once for the main document and then again for each figure.

To reproduce the problem, here is the code for a simple flowchart and another document that includes the flow chart pdf using \includegraphics.
The font XITS will be included twice.

% !TEX program = XeLaTeX
\documentclass[tikz=true]{standalone}
\usepackage{fontspec}
\setmainfont{XITS}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usepackage{pgfplots}
%% ===================================================
\begin{document}
\tikzstyle{block} = [draw, rectangle, fill=blue!20, text width=9.5em, node distance=14em, text centered, minimum height=4em]
\tikzstyle{line} = [draw, -latex']
%
\begin{tikzpicture}[node distance = 2cm, auto]
\node [block] (Create) {create flowchart with TikZ and standalone};
\node [block, right of=Create] (Include) {include flowchart into main document};
\node [block, right of=Include] (CheckFont) {check which fonts have been embedded};
\path [line] (Create) -- (Include);
\path [line] (Include) -- (CheckFont);
\end{tikzpicture}
\end{document}


and the main document

% !TEX program = XeLaTeX
\documentclass[]{article}
\usepackage{graphicx}
\usepackage{fontspec}
\setmainfont{XITS}
% ===============
\begin{document}
Text before the figure.
\begin{figure}
\centering
\includegraphics{tikz_flowchart.pdf}
\caption{Some figure}
\label{fig:flowchart}
\end{figure}
Some more text.
\end{document}


In the document properties Adobe Reader shows XITS twice:

If I include the Tikz flowchart directly into the main document, the font is emnbedded only once, but I don't want to compile it every time and sometimes I use other tools (e.g. Inkscape) to create figures (but they all use the same font).

In order to have a smaller pdf file (and because I think this behavior is strange) I would like to have each font included only once. I am aware of the tool pdfsizeopt, as described in the question How to create small PDF files for the Internet, but while it does decrease the file size, it does not help with the issue of fonts being included multiple times.

-
This is not specific to XeLaTeX, TikZ, or standalone; I’ve seen the same thing in LuaLaTeX when using the article class and a tabular environment to produce a letterhead, which I then include in letters with the wallpaper package. IIRC the same thing happened in pdfLaTeX too. In the case of letterhead it’s not surprising, since that’s where one tends to use titling caps and ligatures which don’t appear in the body of a letter, so the subsets aren’t the same. –  Thérèse Jul 4 '13 at 22:32

I found no LaTeX-internal way to optimize that.You can use pdfsizeopt as an external tool to optimize the embedded fonts. It works great as long as the font names of the instances of the font are the same. It also uses subsetting, so that only the glyphs that you use are embedded and it converts fonts to a smaller format.

I suggest to disable the conversion of black-white-images to JBIG2 format for compatibility reasons and maybe the PNGOUT optimization for speed reasons. In every other aspect I always had full compatibility and good performance when optimizing PDF from PdfLaTeX and LuaTeX/ConTeXt.

-
Ver similar to tex.stackexchange.com/questions/50829/…? Does LuaTeX have an equivalent to \pdfinclusioncopyfonts=1 (See) page 32 of the [pdftex manual][1] [1]: mirror.ctan.org/systems/pdftex/manual/pdftex-a.pdf –  WillAdams Jul 5 '13 at 19:31
@WillAdams it has all the pdftex primitives I think. It has that one certainly (have not checked if it works in this case) –  David Carlisle Jul 8 '13 at 16:32
@DavidCarlisle @WillAdams Does XeLaTeX also support this command? Original question was about XeLaTeX and whenever I try using \pdfinclusioncopyfont I get the error message Undefined control sequence. –  matth Jul 8 '13 at 20:21
@matth probably not. Luatex is based on the pdftex sources and is a pdf engine generating pdf so incorporates all the \pdf... primitives. xetex is based on classic dvi tex and pdf is generated by the dvipdfmx dvi driver, so an analogous question would be is there a \special that could control font loading in dvipdfmx (I don't know the answer). –  David Carlisle Jul 8 '13 at 20:28
I'm not sure when, but pdfsizeopt has been dropped from Google Code "due to a complaint received under the U.S. Digital Millennium Copyright Act." :( pdfsizeopt.py is still available, and directions of use seem to now be separate/scattered on the Internet. –  Werner Sep 20 '13 at 5:35

Unfortunately, XeTeX doesn't have a facility for parsing an imported graphic and remapping the fonts --- pdfTeX does afford this (and lualatex inherits it) and includes a command to enable doing so:

Try \pdfinclusioncopyfonts=0

From page 32 of the pdftex manual:

\pdfinclusioncopyfonts (integer)

If positive, this parameter forces pdfTEX to include fonts from a pdf file loaded with \pdfximage, even if those fonts are available on disk. Bigger files might be created, but included pdf files are sure to be embedded with the adequate fonts; indeed, the fonts on disk might be different from the embedded ones, and glyphs might be missing.

-
Where should I put that command? No matter what I tried, I get an Undefined control sequence error. Would you mind creating a fully compilable MWE? –  matth Jul 8 '13 at 20:16
use it w/ lualatex, not xelatex, per David Carlisle above. –  WillAdams Jul 8 '13 at 22:07