2

When exporting a tikzpicture to its own pdf file for publications, I want to be able to specify the exact size and resolution of the exported pdf file. Specifically 8.5 cm wide and 600 dpi images.

MWE:

\documentclass[12pt]{standalone}

\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,arrows.meta,external}

\tikzexternalize

\begin{document}

\resizebox{8.5cm}{!}{
     \begin{tikzpicture}
            %some drawing here
     \end{tikzpicture}
    }

\end{document}

Then use pdflatex -shell-escape TikzFigure.pdf to create the figure.

  • A tikzpicture is vectorial, why do you want to define some dpi? – Ignasi Jan 25 '17 at 20:36
  • Okay when I first asked the question I didn't understand the difference between vector and raster pdfs. So tikz is pure vector and thus theres no dpi? Alright, but what if I use tikz to annotate/modify a pre-existing picture/pdf? Is there a way to set the dpi in that case? Because the image might not be a pure vector format. Or do you have to do this to the image itself before including it in a tikz drawing? – Neil Jan 25 '17 at 21:46
  • That would be the simplest solution for sure to do this externally in advance ... – Stefan Pinnow Jan 26 '17 at 12:01
2

Resolution

TikZ is by nature a vector graphics system and the generated PDF file keeps this property. Of course, bitmap images can be included and used in the TikZ graphics. But TeX is not an program with capabilities for image processing. It only can include bitmap images and can do some transformations (e.g. scale, rotate). But it cannot change the image data. Therefore, the image resolution cannot be changed.

Scale to final width

The following hacks into internals of pgf to access the picture box, before it is typeset. At the beginning of \pgfsys@typesetpicturebox, the picture box \pgfpic has size zero and the origin of the picture is the reference point of the box. The dimensions of the picture are provided by other registers \pgf@picminx, \pgfpicminy, ... They can be used to calculate the actual with and then the scale factor. The box is then resized by the scale factor and the registers are updated according to the scale factor.

Example:

\documentclass[12pt]{standalone}

\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,arrows.meta,external}
\usepackage{etoolbox}

\tikzexternalize

\makeatletter
\newcommand*{\set@picwidth}[1]{%
  \edef\RequestedPicWidth{\the\dimexpr(#1)}%
  \edef\ActualPicWidth{\the\dimexpr\pgf@picmaxx-\pgf@picminx}%
  \ifx\RequestedPicWidth\ActualPicWidth
  \else
    \ifdim\ActualPicWidth>\z@
      \pgfmathsetmacro\ScaleFactor{\RequestedPicWidth/\ActualPicWidth}%
      \typeout{* Scale factor: \ScaleFactor}%
      \setbox\pgfpic=\hbox{%
        \scalebox{\ScaleFactor}{\box\pgfpic}%
      }%
      \pgf@picminx=\ScaleFactor\pgf@picminx
      \pgf@picmaxx=\ScaleFactor\pgf@picmaxx
      \pgf@picminy=\ScaleFactor\pgf@picminy
      \pgf@picmaxy=\ScaleFactor\pgf@picmaxy
      \pgf@shift@baseline=\ScaleFactor\pgf@shift@baseline
      \pgf@trimleft@final=\ScaleFactor\pgf@trimleft@final
      \pgf@trimright@final=\ScaleFactor\pgf@trimright@final
    \else
      \errmessage{The actual picture width (\ActualPicWidth) is not positive.}%
    \fi
  \fi
}
\newenvironment{picwidth}[1]{%
  \pretocmd\pgfsys@typesetpicturebox{\set@picwidth{#1}}{}{%
    \errmessage{Patching \noexpand\pgfsys@typesetpicturebox failed!}%
  }%
  \ignorespaces
}{%
  \ignorespacesafterend
}
\makeatother

\begin{document}
  \begin{picwidth}{8.5cm}
    \begin{tikzpicture}
      \draw (0, 0) circle[radius=1] node{Hello};
   \end{tikzpicture}%
  \end{picwidth}
\end{document}

Result

The .log file contains the calculated scale factor: 4.22034

| improve this answer | |

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.