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.

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

\colorbox{gray}{
\begin{minipage}{.33\textwidth}
adfjk adfjk adfjk adfjk adfjk adfjk adfjk adfjk adfjk adfjk adfjk adfjk 
\colorbox{yellow}{
\begin{tikzpicture}
\begin{axis}[width=\textwidth,ymax=3.5, ymin=-3.5]
  \addplot[blue, domain=-5.8:5.8,samples=30] {sin((180/pi)*x)};
% ....
\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

share|improve this question
    
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
add comment

1 Answer

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]
  \addplot[blue, domain=-5.8:5.8,samples=30] {sin((180/pi)*x)};
\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}
adfjk adfjk adfjk adfjk adfjk adfjk adfjk adfjk adfjk adfjk adfjk adfjk 
\colorbox{yellow}{\includegraphics[width=\textwidth]{graph1.tikz}}
\end{minipage}%
}
\end{document}
share|improve this answer
1  
It's copied from the OP but you have a word space at the start and end of the gray colorbox which is probably not desired. –  David Carlisle Jan 8 '13 at 20:08
    
@DavidCarlisle: Cheers! I wish I could somehow give you some TikZ reputation for that, but alas... –  Jake Jan 8 '13 at 21:03
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.