set a tikzpicture to a specific width

12582 views
Skip to first unread message

John S.

unread,
Oct 24, 2010, 11:45:14 PM10/24/10
to
How would you resize a tikzpicture so that the width matches exactly,
say, the width of the textblock? I feel like this is a very common and
important issue, but I haven't found anything on it. One shouldn't
have to set scaling and clipping just to do this.

Thanks.

GL

unread,
Oct 25, 2010, 3:20:36 AM10/25/10
to
Le 25/10/2010 05:45, John S. a �crit :

\begin{tikzpicture}[x=2cm,y=2ex]

no ??

Ignasi Furio

unread,
Oct 25, 2010, 3:24:16 AM10/25/10
to

Probably is not what you want but you can draw your tikzpicture on an
external pdf and the include with \includegraphics[width=\linewidth]
{external_tikzpicture}.

Ignasi

Marc van Dongen

unread,
Oct 25, 2010, 12:40:54 PM10/25/10
to

Hi,

This is something I've tried to figure out myself. Unfortunately, I've
never managed to figure out how to do this (with tikz). The best thing
I can suggest is that you just put your tikzpicture in a tex or latex
box.

Regards,


Marc van Dongen

Herbert Voss

unread,
Oct 25, 2010, 12:49:00 PM10/25/10
to

\resizebox{\linewidth}{!}{%
... your tikz stuff ...
}

needs package graphicx

Herbert

John S.

unread,
Oct 25, 2010, 10:22:06 PM10/25/10
to
Thanks everyone, but I should have been clearer. The solutions using
other graphics packages scale a tikzpicture all together; I'm
interested in keeping the nodes at the right size so that the text
(labels, etc.) isn't distorted. Is this possible?

GL

unread,
Oct 26, 2010, 4:00:31 AM10/26/10
to
Le 26/10/2010 04:22, John S. a �crit :


Here you have a box containing a TikZpicture of exactly 200pt x 150pt

\begin{document}\makeatletter

\setbox0=\hbox{%
\begin{tikzpicture}[x=1pt,y=1pt]
\path [use as bounding box,red] (0,0) rectangle (200pt,150pt);
\draw (0,20) -- ++(20,0) node [blue] {Hello}
[->,very thick,orange] -- ++(0,40) node [green,anchor=south]
{\Large Wolrd};
\end{tikzpicture}%
}

box0:\quad wd=\the\wd0\quad ht=\the\ht0\quad dp=\the\dp0

\fbox{\box0}

\end{document}\endinput

GL

unread,
Oct 26, 2010, 4:36:36 AM10/26/10
to
Le 26/10/2010 10:00, GL a �crit :

> Le 26/10/2010 04:22, John S. a �crit :
>> Thanks everyone, but I should have been clearer. The solutions using
>> other graphics packages scale a tikzpicture all together; I'm
>> interested in keeping the nodes at the right size so that the text
>> (labels, etc.) isn't distorted. Is this possible?
>

Would you 100pt x 75pt ? Then add [scale=.5] in the {tikzpicture} envir.
options.

Ulrike Fischer

unread,
Oct 26, 2010, 4:50:47 AM10/26/10
to

\documentclass{book}
\usepackage{tikz}
\newsavebox\mybox
\begin{document}
\newcommand\myscalefactor{1}
\newcommand\tikzcontent{%
\begin{tikzpicture}
\pgftransformscale{\myscalefactor}
\draw (-4,0)--(4,0);
\node {root} child {node {left}} child {node {right} child {node
{child}}
child {node {child}} };
\end{tikzpicture}}

\savebox\mybox{%
\fbox{\tikzcontent}}

\usebox{\mybox}

\pgfmathparse{\textwidth/\wd\mybox}
\edef\myscalefactor{\pgfmathresult}
\noindent\fbox{\tikzcontent}

\noindent \rule{\textwidth}{2pt}
\end{document}


the nodes should not define the natural size of the picture or the
result will be wrong (remove the \draw to see the effect).


--
Ulrike Fischer

GL

unread,
Oct 26, 2010, 4:58:17 AM10/26/10
to
Le 26/10/2010 10:50, Ulrike Fischer a �crit :

> Am Mon, 25 Oct 2010 19:22:06 -0700 (PDT) schrieb John S.:
>
>
> the nodes should not define the natural size of the picture or the
> result will be wrong (remove the \draw to see the effect).

What do you think about [use as bounding box] ?

Martin Heller

unread,
Oct 26, 2010, 8:12:29 PM10/26/10
to
On 2010-10-26 10:58, GL wrote:

>
> What do you think about [use as bounding box] ?

[use as bounding box] will override the automatic bounding box
calculation done by pgf/tikz. This will give you a box of a known size
that you can scale to a given size. This will scale everything including
text in nodes. This is not what the OP was asking for.

GL

unread,
Oct 27, 2010, 12:56:17 PM10/27/10
to
Le 27/10/2010 02:12, Martin Heller a �crit :
Hmmm.. \begin{tikzpicture}[scale=.5]
\path [use as bounding box] ... rectangle ...

does not scale the text (I mean the fonts...)

But the OP is not very clear on his intentions may be.

Itay

unread,
Nov 1, 2010, 9:18:12 AM11/1/10
to

Just use a dummy object of the required width.
Try the code below with the two offered versions of \documentclass.
See comments inside the code below.

HTH
Itay


<code>
\documentclass{article} % Full width text
%\documentclass[twocolumn]{article} % Narrow width text
\usepackage{tikz}
\begin{document}
\fbox{% % Depict the actual tikzpicture width.
\begin{tikzpicture}
% The next line forces a picture of width ~ \linewidth.
% \linewidth is determined by the class option.
% May replace with \textwidth, or any other tex length.
\path (0cm,0cm) -- (\linewidth, 0cm);
\draw[line width=4pt] (1,-3) -- ++ (0,6)
node[anchor=south] {This is a 4pt line};
\end{tikzpicture}%
}
% The next line demonstrates that we didn't scale
% the objects inside tikzpicture.
This rule has width of 4pt: \rule{4em}{4pt}
\end{document}
</code>

--
Itay Furman it...@fastmail.fm
--------------------------------------
http://longitude.weizmann.ac.il/~itayf
--

Reply all
Reply to author
Forward
0 new messages