Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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".

share|improve this question
2  
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
1  
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
    
Related: tex.stackexchange.com/questions/6388/… –  Torbjørn T. Oct 5 '12 at 20:43

5 Answers 5

Just add

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.

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

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.

share|improve this answer
1  
I don't understand. Why wouldn't it make sense to draw the picture, then scale it by whatever factor gives width = \textwidth? –  mrc 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
2  
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? –  mrc Nov 4 '13 at 2:31

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

enter image description here

share|improve this answer

Try something like \draw[use as bounding box] (0,0) rectangle (3,2) 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.

share|improve this answer

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}
share|improve this answer
    
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
1  
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.