[Tex/LaTex] Same font embedded twice when including graphics created with standalone

embeddingfontsgraphicsxetex

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}
\pgfplotsset{compat=newest}
%% ===================================================
\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:

XITS embedded 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.

Best Answer

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.

Related Question