10

I have a pgf picture which I insert with:

\input{m1.pgf}

But the image is too big. Instead of regenerating a smaller image, I'd rather take advantage of vector graphics, and have XeLaTeX scale it. How do I set the width of pgf picture (keeping its aspect ratio)?

MWE

tex:

\documentclass{article}

\usepackage{fontspec} % enagles loading of OpenType fonts
\usepackage{polyglossia} % support for languages

% fonts:
\defaultfontfeatures{Scale=MatchLowercase,Mapping=tex-text} % without this XeLaTeX won't turn "--" into dashes
\setmainfont{DejaVu Sans}
\setsansfont{DejaVu Sans}
\setmonofont{DejaVu Sans Mono}

% pgf:
\usepackage{pgf}

\begin{document}

\input{m1.pgf}

\end{document}

Sample pgf: m1.pgf (it's too big to cite it here, and I can't make much smaller).

11

Maybe simply \scalebox:

\documentclass{article}

\usepackage{fontspec} % enagles loading of OpenType fonts
\usepackage{polyglossia} % support for languages

% fonts:
\defaultfontfeatures{Scale=MatchLowercase,Mapping=tex-text} % without this XeLaTeX won't turn "--" into dashes
\setmainfont{DejaVu Sans}
\setsansfont{DejaVu Sans}
\setmonofont{DejaVu Sans Mono}

% pgf:
\usepackage{pgf}

\begin{document}

\scalebox{0.6}{\input{m1.pgf}}

\end{document}
  • 1
    What is 0.6? Fraction of original size of the image, or fraction of page to be used for an image? I need the second. – Adobe May 31 '13 at 18:25
  • 1
    @Adobe Fraction of original size of the image. The value 0.6 is probably to small for you, but it is only an example, of course. – Przemysław Scherwentke May 31 '13 at 18:27
  • That's already very good. But it will be even better if there were a way to force pgf picture fit in given fraction of the textwidth. – Adobe May 31 '13 at 18:31
  • 8
    @Adobe Then you are rather expecting a solution like this: \resizebox{0.7\textwidth}{!}{\input{m1.pgf}}. – Przemysław Scherwentke May 31 '13 at 18:58
2

You can first draw a phantom to determine the sizes, compute the scaling factor(s) and draw the image again:

Code

\documentclass[a5paper]{scrreprt}
\usepackage{xifthen}
\usepackage{environ}
\usepackage{tikz}

\pgfmathsetmacro{\maximumpicturewidth}{10}% max width in cm
\pgfmathsetmacro{\maximumpictureheight}{5}% max height in cm

\newcommand{\getxyscale}
{   \path (current bounding box.south west);
  \pgfgetlastxy{\xsw}{\ysw}
  \path (current bounding box.north east);
  \pgfgetlastxy{\xne}{\yne}
  \pgfmathsetlengthmacro{\picwidth}{\xne-\xsw}
  \pgfmathsetlengthmacro{\picheight}{\yne-\ysw}
  \pgfmathsetlengthmacro{\maxpicwidth}{\maximumpicturewidth*28.453}
  \pgfmathsetlengthmacro{\maxpicheight}{\maximumpictureheight*28.453}
  \pgfmathsetmacro{\xscale}{\maxpicwidth/\picwidth}
  \pgfmathsetmacro{\yscale}{\maxpicheight/\picheight}
  \xdef\xscalefactor{\xscale}
  \xdef\yscalefactor{\yscale}
}

\NewEnviron{autoscaletikz}%
{\noindent\hphantom{\vphantom{\begin{tikzpicture}\BODY\getxyscale\end{tikzpicture}}}%
\noindent\begin{tikzpicture}[xscale=\xscalefactor,yscale=\yscalefactor]\BODY\end{tikzpicture}}

\begin{document}

\begin{autoscaletikz}
    \fill[red] (0,0) circle (5 and 1);
    \fill[draw=black,left color=blue, right color=orange] (0.5,0) rectangle (4,2);
    \draw (2,-1) rectangle (18,1);
    \draw (4,-0.5)
    \foreach \x in {1,...,12}
    {   arc ({mod(\x,4)*90}:{(mod(\x,4)+1)*90}:{\x/10})
    }   ;
\end{autoscaletikz}

\end{document}

If you want to keep the aspect ratio, simply change the environment:

\NewEnviron{autoscaletikz}%
{\noindent\hphantom{\vphantom{\begin{tikzpicture}\BODY\getxyscale\end{tikzpicture}}}%
\noindent\begin{tikzpicture}[xscale=\xscalefactor,yscale=\xscalefactor]\BODY\end{tikzpicture}}

Result

enter image description here

  • Doesn't work with the sample pgf that I provide in a question. But still - might be useful for coders. – Adobe Jun 5 '13 at 11:35

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.