339

I was wondering which is the correct way to scale a tikzpicture.

I tried with

\begin{tikzpicture}[scale=0.50]

but what I obtain is that distances between elements are scaled but not their sizes or text size too. It's like if the picture collapses on itself, without correctly scaling (as I would imagine, like having a zoom factor)

Am I missing something? Should I use a different command or what?

1
  • 2
    This is intended behaviour. It is considered good typography because it ensures consistent font size and line width throughout the document. All \resizebox and \scalebox-based approaches inevitably lead to a multitude of font sizes and line widths. For a professional looking document it is better to rework existing graphics. The same applies to tables that don't fit.
    – AlexG
    Commented Oct 15, 2020 at 8:22

7 Answers 7

334

You could try using the command \resizebox. You enclose the tikzpicture environment in one of those and then in is scaled. Like this:

\resizebox{<horizontal size>}{<vertical size>}{%
    \begin{tikzpicture}

    \end{tikzpicture}
}

If you want the image to be scaled proportionally, you can give one of the sizes and put ! in the other. There is also a \scalebox{<factor>}{...} macro which allows scaling by a factor. This works well except when the tikzpicture has a matrix command, and the columns are separated by &, in that case, you can change the column separator using the option ampersand replacement in the matrix options as stated in the pgfmanual.

Alternatively use the adjustbox package which provides an adjustbox environment which also allows resizing and scaling while allowing for special content including catcode changes required for verbatim text and the aforementioned &.

3
  • 10
    \scalebox worked great for me! Commented Jul 5, 2018 at 17:28
  • 5
    \resizebox needs units specified (pt or em or cm, etc), otherwise generates error Illegal units of measure inserted. \scalebox worked for me. Commented Jul 23, 2020 at 13:56
  • 2
    @SanderHeinsalu FYI: You can also use \textwidth or \linewidth in \resizebox{<horizontal size>}{<vertical size>}{%content}.
    – Sunsheep
    Commented Jul 30, 2021 at 21:45
118

Pgf has two different types of transformations: coordinate transformations and canvas transformations. When using \begin{tikzpicture}[scale=0.50], you are applying coordinate transformation. All coordinates will be scaled, whereas individual objects (text, line thickness, rounding of corners etc) will not scale. Most of the time that is what you want, you do not want to scale carefully typeset pieces of text, you do not want to make lines ridiculously thick, or too thin to actually print.

If you want to scale everything, you have to use a canvas transformation. For that, pgf has a command \pgftransformscale{}. You can also use the pgflowlevelscope environment:

\begin{tikzpicture}
  \draw (0,0) rectangle (6,6); %create a bounding box to reserve space
  \begin{pgflowlevelscope}{\pgftransformscale{5}}
     \draw (0,0) -- (1,1) node[right]{$x$};
  \end{pgflowlevelscope}
\end{tikzpicture}

You can also use \pgflowlevelsynccm which synchronizes the canvas transformation matrix with the current coordinate transformation matrix, for example like this:

\begin{tikzpicture}[scale=5]
  \draw (0,0) rectangle (1.2,1.2); %create a bounding box to reserve space
  \pgflowlevelsynccm
  \draw (0,0) -- (1,1) node[right]{$x$};
\end{tikzpicture}

Note that because pgf does not (cannot) keep track of the canvas transformations (they are performed by the backend), you will have to make sure there is enough space in your picture for the scaled objects. In the examples above, I try to reserve space by drawing a bounding box first, before applying the canvas transformation. There are other problems with canvas transformations, and I do not recommend using them without carefully reading appropriate parts of the pgf manual.

I include this answer more as an explanation of what is going on, another, preferable answer using \resizebox has already appeared.

1
  • pgflowlevelsynccm reduces the size nicely, but the figure is shifted toward the text and overlaps with the text. I have tried vspace, newpage and all sorts of things but it is a mess. What could be going on here? Thanks. Commented Jul 11, 2019 at 21:50
101

You can use the option "transform shape" for the whole picture, a scope or a single node. Example:

\begin{tikzpicture}[scale=2, transform shape]
  \draw rectangle (1,1) node {foo};
\end{tikzpicture}

\begin{tikzpicture}[scale=2]
  \draw rectangle (1,1) node {bar};
\end{tikzpicture}

But be careful, this will not solve all your cases with scaling tikz pictures, e.g."rounded corners" will not scale. The tikz authors (general) advice is not to scale graphics.

0
49

With newer versions of TikZ, you can also use the transform canvas option, as detailed in section 25.4 of the TikZ 3.00 manual:

\begin{tikzpicture}[transform canvas={scale=2.0}]
   \node{blubb};
\end{tikzpicture}
4
  • 6
    The manual states: "Tracking of the picture size is (locally) switched off", causing for example the figure to overlap with a caption.
    – leezu
    Commented Apr 7, 2020 at 22:04
  • Same as @leezu, this causes "Tracking of the picture size is (locally) switched off", which means the outer frame of the tikz picture is lost, and space is not properly reserved for the picture. See post: tex.stackexchange.com/q/108380/62285
    – Yvon
    Commented Aug 14, 2021 at 4:06
  • Thanks. worked great.
    – Binu Jasim
    Commented Nov 13, 2022 at 1:32
  • 2
    It seems transform canvas affects the shift={(dx,dy)} on the scope you want to scale
    – juanfal
    Commented May 4, 2023 at 16:28
9

One tends to define a base length and draw everything in terms of it. This seems to scale nicely and avoids some of the draw backs of post-scaling.

\makeatletter
\newlength{\b@sis}
\tikzset{
  basis/.code={\setlength{\b@sis}{#1}}, % TikZ assignment code
  basis/.default=1em,                   % Provide a default (\b@sis is undefined/unassigned)
  basis,                                % Set initial Value (\b@sis is defined/assigned)
 }
\makeatother

Sometimes one needs to control this from their text as well. For this one proposes the following broken code. (I haven't required this functionality yet so never fixed it. One can set the value but retrieving it seems rather tricky.)

\usepackage{xparse}
\makeatletter
\NewDocumentCommand{\basis}{g}{
 \IfValueTF{#1}
  {
   \setlength{\b@sis}{#1}
  }{
   % This part is broken
   %\b@sis     % Ideally one would return the measurement
   %\the\b@sis % One could return a string but this is not much use
  }
}
\makeatother

Finally to test the code use the following in your

\makeatletter
\tikzset{
 test/.pic={
  code={\node[draw] at (0,-\b@sis) {\the\b@sis};
        \node[draw] at (0, \b@sis) {Basis :};}},
}
\makeatother
\begin{document}
 \tikz \pic {test};
\end{document}

Provided \basis works correctly then one could also use the following within their text

\tikz \draw (-\basis,-\basis) -- (\basis,\basis);
3

A late answer and I am not sure if of help, but in my thesis I scaled all my TIKZ figures by this:

\resizebox{\columnwidth}{!}
{
   \input{some_tikz.tex}
}

columnwidth will scale to the width of the column, in a 1 column layout document this is identical with textwidth, but in multi-column layout it will scale to the width of the column and ! will automatically calculate the height. Might be useful if you change from a 1-column layout later to a 2-column layout.

1
2

Warning: use at your own risk, nesting tikzpictures not recommended

You could put the tikzpicture into a node, and scale the node

\tikz \node [scale=0.5, inner sep=0] {
   \begin{tikzpicture}
     \draw [line width=10pt] (0,0) rectangle (1,1);
     \node at (0.5,0.5) {abc};
   \end{tikzpicture}
};

(left: without scaling, right: with scaling)

3
  • 2
    Nesting tikzpictures is not recommended.
    – CarLaTeX
    Commented Oct 29, 2017 at 8:16
  • 3
    More context might be useful why this is not recommended: tex.stackexchange.com/a/46792/16352 Commented Oct 29, 2017 at 9:09
  • 1
    I had an internal node that I was able to scale (rather than scaling the whole picture) - so this answer was helpful to me! Commented Oct 29, 2017 at 22:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .