I was surprised this wasn't possible out of the box, but then I found in the tikz manual: « Do not scale graphics. This means that when generating graphics using an external program, create them “at the right size.” »
–
Will RobertsonDec 2 '10 at 17:16

@Will: I don't see the point of that advice for graphics generated with TikZ as the scaling is quite intelligent (e.g. it keeps line widths and font sizes).
–
CaramdirDec 2 '10 at 19:17

@Caramdir all lengths in tikz are absolute aren't they? I haven't used it much, but I didn't think you could set a figure size and then work in relative coordinates to that.
–
Will RobertsonDec 3 '10 at 0:40

5 Answers
5

This question was asked on comp.text.tex and received a good answer by Ulrike Fischer. It works by typesetting the {tikzpicture} once, measure its width and then retypeset it to the correct width by automatically computing the required scale.

Here's a more user-friendly interface for this solution using the environ package. It works by using a {scaletikzpicturetowidth} environment with the desired width as first argument in combination with specifying the [scale=\tikzscale] option to the tikzpicture. For example, to scale a tikzpicture to {textwidth}, you would use:

@flying sheep: I don't see why you couldn't do that in ConTeXt, but as I don't know it at all, I can't help. Try asking a new question on the subject or go on a ConTeXt specific forum.
–
Philippe GoutetSep 10 '11 at 20:03

Maybe the most simple way to use relative coordinates is using scale=\textwidth/1cm and then only use values between 0 and 1 for your coordinates (assuming your basic scale size is 1cm). You could also estimate the size of your figure and use the same trick. I had that problem since the figure was already done and I wanted to scale it afterwards.

1)

However, this makes the picture part of a macro argument which e.g. doesn't allow verbatim text inside nodes. TikZ itself goes through some effort to process the node content as box not as macro argument to allow any form of code inside it, including verbatim.

The use of the environ package should be also avoided in the general case because it also makes the environment body a macro argument.

2)

In can simply define your own environment using lrbox like in Philippe's answer, but as normal environment and with \resizebox:

3)

I recently created the package adjustbox to give users the power of \includegraphics options for text or other contents. This can be used here nicely:

% Preamble
\usepackage{adjustbox}
% Document
\begin{adjustbox}{width=\textwidth}% there is also 'max width' to only scale it down if it is larger
\begin{tikzpicture}[<options>]
\draw .... ;
\end{tikzpicture}
\end{adjustbox}

It supports verbatim and other special content and will work for normal text as well as other picture environments.

the problem with all these methods is that they will also change the size of text inside the labels, which is not wanted. If you really need verbatim inside the {tikzpicture}, one way would be to write to an external file the environment's content instead of using the environ package.
–
Philippe GoutetFeb 20 '11 at 0:07

2

@Philippe: Ok, that's what he meant with "the fonts still must be correct". But then it could be done using x=\textwidth, i.e. setting the x-unit to the text width and drawing the picture using relative coordinates. The question isn't very clear. The OP doesn't mentions what kind of picture he has etc.
–
Martin Scharrer♦Feb 20 '11 at 0:13

Your second solution doesn't work due to arguments of newenvironment seem to be not propogated to the definition of the closing statement.
–
Dmitry VolosnykhApr 22 '11 at 5:17

Here a solution based on Philippe Goutet's answer from an idea of Ulrike Fischer.
I created a new environment tikzpicture*. The main idea now it's to avoid to use multiple environments. Only one is necessary tikzpicture*. Normally it's possible to use options like with tikzpicture. I made some tests but It would be interesting to make more tests.

Firstly, I suppose that the user wants to adjust all the figures to the same width. I chose \linewidth by default. The user needs to set up the parameters with \setupscalewithtikz. This macro requieres one argument (a length) to apply to newwidth . It's possible to change the value of \newwidth with \setlength{\newwidth}{length}.