I would like to scale my tikzpicture
to exactly (or a proportion of) the \textwidth
Of course I could play with the [scale=0.5]
option until I found the right value, but I assume there must be an easier way.
How to get a properly scaled (the fonts still must be correct) tikzpicture
with an exact width.
Best Answer
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 atikzpicture
to\textwidth
, you would use:Here's a complete compilable code which shows both the unscaled
tikzpicture
and the scaled one:If you need to use the
external
tikzlibrary, here is a workaround:If you need to use
\tikzsetnextfilename{name}
for an automatically scaled picture, use instead the syntax\begin{scaletikzpicturetowidth}[name]{\textwidth}
.