# tikzpicture in minipage

Can I set the exact width to a tikzpicture? I thought so, with "width", but I tried this:

\colorbox{gray}{
\begin{minipage}{.33\textwidth}
\colorbox{yellow}{
\begin{tikzpicture}
\begin{axis}[width=\textwidth,ymax=3.5, ymin=-3.5]
% ....
\end{axis}
\end{tikzpicture}
}
\end{minipage}
}


(-- sorry: I cannot post images--) but the yellow rectangle is smaller than \textwidth. I set it to \textwidth+1.3cm (seems to be a fixed margin) and does more or less what I expect, but is there a cleaner solution? Thanks

-
Welcome to TeX.sx!.As new user without image posting privileges simply include the image as normal and remove the ! in front of it to turn it into a link. A moderator or another user with edit privileges can then reinsert the ! to turn it into an image again. – texenthusiast Jan 8 '13 at 17:51
Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – Qrrbrbirlbel Jan 8 '13 at 17:58

The width option in PGFPlots only results in approximately the correct size, because it works with static assumptions about the space taken up by the labels. To scale a plot exactly to some specified width, you have a couple of options: See pgfplots: how can I scale to text width?.

One approach that seems particularly well suited here is to use Patrick Häckler's excellent package tikzscale. To use it, you need to keep the code for the tikzpicture in a separate file (which is a good idea in real documents anyway, because it keeps everything much more maintainable), which you then include using \includepicture[width=...]{<filename>}. tikzscale then does some magic in the background, and you end up with a perfectly sized plot.

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.7}

\usepackage{tikzscale}

\usepackage{filecontents}
\begin{filecontents}{graph1.tikz}
\begin{tikzpicture}
\begin{axis}[width=\textwidth,ymax=3.5, ymin=-3.5]
\end{axis}
\end{tikzpicture}
\end{filecontents}

\setlength{\fboxsep}{0pt} % Otherwise you introduce padding in the yellow box

\begin{document}
\colorbox{gray}{%
\begin{minipage}{.33\textwidth}