# Specifying the width and height of a tikzpicture

Is it possible to specify the width and height of a tikzpicture? To scale a tikzpicture I use the option scale, i.e. for instance

\begin{tikzpicture}[scale = 2]
...
\end{tikzpicture}


I tried to change the width and height as

\begin{tikzpicture}[width = 2in, height = 3in]
...
\end{tikzpicture}


but this gives me an error "I do not know the key /tikz/width".

-
With a tikzpicture, you specify all the coordinates, so the picture is as big as needed. With pgfplots, the size of the plot is computed, so you can specify the width and height options to the axis environment within a tikzpicture. – Peter Grill Oct 5 '12 at 17:52
@PeterGrill Yes. I am not using pgfplots though. I should have not tagged the question with pgfplots. I have removed the tag now. – user1876 Oct 5 '12 at 18:29
Ok, but I was trying to explain why it does not make sense to specify the height/width of a tikzpicture, but does make sense to be able to specify the height/width of a pgfplots graph. With a tikzpicture only the scale option makes sense. – Peter Grill Oct 5 '12 at 18:31
@PeterGrill Ok. Yes I understood. – user1876 Oct 5 '12 at 18:46
– Torbjørn T. Oct 5 '12 at 20:43

Use resizebox outside of tikzpicture.

\resizebox{width}{height}{object}


For example:

\documentclass [tikz] {standalone}

\begin {document}
\begin {figure}
\centering
\resizebox {\columnwidth} {!} {
\begin {tikzpicture}
\node [draw] (my node 1) {my node 1};
\node [draw, anchor = west] (my node 2) at (my node 1.east) {centro};
\node [draw, anchor = west] at (my node 2.east) {my node 3};
\end {tikzpicture}
}
\end {figure}
\end {document}

-
But in this solution also things like line widths and font sizes get resized, right? In this case graphics would no longer stay consistent when they have to be resized by different amounts. – Benedikt Bauer Oct 5 '12 at 18:04
I agree with you, but doing so is the purpose of the question, no? – tecepe Oct 5 '12 at 18:37
Not sure about that. I could imagine that the intention is that you can draw your graphic in any relative coordinates or units of measure such that it is automagically sized to a certain width or height. Of course this should happen in a way that the line widths and fonts keep their sizes. Theoretically you could achieve that by drawing everything in units of a variable \picturewidth or so, but this would be rather unconvenient. – Benedikt Bauer Oct 5 '12 at 18:51
Oh I forgot before: I think the idea is that you don't have to care that much about how large your graphic will get at the end. Instead you can just start drawing and will in most cases end up with a graphic that has the desired width or height. – Benedikt Bauer Oct 5 '12 at 19:12

Converting my comments to an answer as I think question is more about specifying a height, width option rather than how to scale picture to get it to be of the appropriate size.

## Summary:

It does not make sense to specify the height/width of a tikzpicture, but does make sense to be able to specify the height/width of a pgfplots graph. With a tikzpicture only the scale option makes sense.

## Explanation:

Within a tikzpicture, you specify all the coordinates, so the picture is as big as needed. So, if you say draw a line from (0,0) to (4,0) you get a line that is 4cm long (cm being the default unit of measurement). So applying a scale factor of say 0.5 would make this line 2cm long. But what does it mean to say draw a line of 4cm in a box that is 2cm wide?
Perhaps you want to only view the parts of the picture in the first 2.0cm, then you are clipping the picture, and there is a macro to do that called \clip.

Now, by default the units are assumed to be cm, so (4,0) corresponds to 4cm in the x-direction. There is an option to specify to use different units with the x=<length>, but again this is scaling.

However, with graphing packages such as pgfplots, the size of the plot is computed, and one can make a determination as to what physical size corresponds to a unit in each of the axis directions. So, for the case of plotting graphs it makes perfect sense to have options to specify the height, or width of the graph.

So, in my mind it does not make sense to have options to specify the height or width of a tikzpicture but does make sense to be able to apply a scale option. For graphing environments such as pgfplots it makes prefect sense to be able to specify a height, and width, and these are available in the axis environment options.

-
I don't understand. Why wouldn't it make sense to draw the picture, then scale it by whatever factor gives width = \textwidth? – Chris Chudzicki Nov 4 '13 at 1:22
@mrc: Perhaps my answer was not clear: It does make sense to scale a tikzpicture. – Peter Grill Nov 4 '13 at 2:12
I'm sure I misunderstood your answer. I know you said scaling makes sense, but why not scaling by specifying what the width should be after the picture is scaled? – Chris Chudzicki Nov 4 '13 at 2:31
@mrc: Sorry for the delayed response. When you specify to tikz that a picture should consist of a horizontal line that is 1cm long, and a scale=0.5, and that the width of the picture be 3cm, which one is supposed to determine how wide the picture is? Perhaps what you are asking about is How to scale a tikzpicture to \textwidth. – Peter Grill Jan 12 '15 at 20:58
I certainly wouldn't expect to be able to specify scale=0.5 and width=3cm simultaneously. The question you linked to is interesting. Still, I would have expected that tikz would accept keys like width=0.8\textwidth . . . the behavior I would expect is that tikz would draw the unscaled picture, then scale it proportionally so that the width has the desired value. I didn't look too carefully---that seems to be what is described in the question you linked to---I am just confused about why that isn't the default behavior of tikz. – Chris Chudzicki Jan 13 '15 at 23:02

This uses part of my answer to this question: first, the image is drawn virtually to get the scaling factors, then it is drawn using these factors.

## Code

\documentclass{scrartcl}
\usepackage[margin=20mm]{geometry}
\usepackage{tikz}
\usepackage{filecontents}
\usepackage{xifthen}

\newcommand{\getsizes}[2]% width, height
{   \path (current bounding box.south west);
\pgfgetlastxy{\xsw}{\ysw}
\path (current bounding box.north east);
\pgfgetlastxy{\xne}{\yne}
\pgfmathsetmacro{\picwidth}{(\xne-\xsw)/28.453}
\pgfmathsetmacro{\picheight}{(\yne-\ysw)/28.453}
\pgfmathsetmacro{\picxscale}{#1/\picwidth}
\pgfmathsetmacro{\picyscale}{#2/\picheight}
\xdef\xsca{\picxscale}
\xdef\ysca{\picyscale}
}

\newcommand{\xyscaledtikz}[3]% draw commands, width, height
{ \smash{\vphantom{
\begin{tikzpicture}
#1
\getsizes{#2}{#3}
\end{tikzpicture}
}}
\begin{tikzpicture}[xscale=\xsca,yscale=\ysca]
#1
\end{tikzpicture}
}

\begin{filecontents}{sample.tex}
\draw[rotate=30,left color=red,right color=blue] (0,0) rectangle (5,5);
\end{filecontents}

\begin{document}

\xyscaledtikz{\input{picone}}{2}{3}
\xyscaledtikz{\input{picone}}{6}{1}

\xyscaledtikz{\input{picone}}{2}{7}
\xyscaledtikz{\input{picone}}{4}{4}

\end{document}


## Output

-
This is the correct answer. Very good. If you just want to scale to a given height but leave the aspect ratio, replace xscale=\xsca by xscale=\ysca. The same applies for defining width and compute height automatically. – kap Sep 15 '14 at 12:13

width=Xcm,
height=Ycm,


after axis as described in the pgfplots manual, page 232.

Note that the manual describes TikZ commands there.

Also which version are you using? I had problems with an older version of LaTeX where TikZ/PGF did not know half the commands that are in use now.

-
...but this is what the OP suggests doesn't work... – Werner Jan 31 '14 at 21:28
also this is pgfplots syntax and TikZ doesn't know these options. – percusse Jan 31 '14 at 23:01
this works for me :) – LCFactorization Jul 11 '14 at 23:49

Try something like \draw[use as bounding box] (0,0) rectangle (3,2) or(\path) if you don't want to draw) to change the aspect ratio of your image, then use scale to change the size. But I agree with Benedikt and Peter that scaling the dimensions separately will warp the picture you are trying to draw, and that you should probably not be looking for such an option in the first place.

-