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.

I am trying to draw a pgfplot with to different y-axis with their respective label in a full line width and I can't get an alignment of the whole tikzpicture within the margins. The problem seems to come from the ylabel which, on the left axis shift the plot to the right, and on the right axis is plotted out of the picture, ie in the margin. I try to add the ylabel style={overlay} so I get the plot centered but both ylabel are this time in the margins. For sure, I can put a 0.95\linewidth but I don't find this solution good enough since it isn't generalist.

Does anyone knows how can I fix this small example in an automatically way (meaning that pgfplot or tikz finds the right width alone)?

\documentclass{article}

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

\begin{document}
\centering{}
\hrule{}
I\hfill I\hfill I\\
\begin{tikzpicture}
\begin{axis}[
  width=\linewidth,
  xmin=-5,xmax=5,
  axis y line*=left,
  axis x line*=top,
  ylabel={$y=x^{2}$ label},
  ]
  \addplot {x^2};
\end{axis}
\begin{axis}[
  width=\linewidth,
  xmin=0,xmax=15,
  domain=0:15,
  axis y line*=right,
  axis x line*=bottom,
  ylabel={$y=x$ label},
  ]
  \addplot[red] {x};
\end{axis}
\end{tikzpicture}

\end{document}

which produces this badly aligned plot

enter image description here

share|improve this question
    
It seems your plot is too wide- have you tried changing width=\linewidth to something else? –  cmhughes Feb 5 '12 at 21:10
    
I don't want to solve it this way. I would like the tikzpicture find the good width automatically so the solution will be the same for all my plots. Anyway, thanks for the answer. –  Sigmun Feb 5 '12 at 21:15
    
Care to define what is mean by "good width", or "right width"? If you use something like 0.95\linewidth it seems to work fine. If you want precise calculations you would need to adjust for the axis labels (which may be of different sizes). –  Peter Grill Feb 5 '12 at 22:06
    
I would like to add "the problem seems to come from the ylabel which, on the left axis shift the plot to the right, and on the right axis is plotted out of the picture, ie in the margin" in the description of the problem but due to the picture within the post, I can't edit it anymore since I earn some reputation. –  Sigmun Feb 5 '12 at 22:47
    
@Sigmun: Would you be happy if the graph was just aligned properly, i.e. slightly protruding into both margins? Or do you want the graph to take up exactly the width of the surrounding text? –  Jake Feb 5 '12 at 23:51
show 1 more comment

1 Answer

Of course, if you draw something that is wider than the line width, the object will extend past the right margin. You have two possibilities:

\makebox[\linewidth]{%
  \begin{tikzpicture}
  ...
  \end{tikzpicture}}

that will leave the axis' labels in the left and right margins; or

\resizebox{\linewidth}{!}{%
  \begin{tikzpicture}
  ...
  \end{tikzpicture}}

(with the graphicx package) that will rescale the diagram to fit the line width.

Of course, the best solution is to say

width=.8\linewidth

(or what you prefer) and forget about boxing and rescaling. There is no law stating that diagrams must fill the line width; to the contrary, diagrams shorter than the line width should be preferred, in my opinion, if possible.

share|improve this answer
    
This is not really the problem mentioned above. If I don't specified any ylabel, the tikzpicture is both centered and fitting the linewidth. It is the same if I only have the left yaxis, even with an ylabel. This shows that the problem comes from the computation of the bounding box or something similar in the tikzpicture. –  Sigmun Feb 6 '12 at 13:25
    
@Sigmun Not at all. You're asking that the axis is as wide as the line width, not the whole diagram. That's how pgf interprets the instruction width=\linewidth and it's quite natural. –  egreg Feb 6 '12 at 13:29
    
I am asking that both plots are as wide as the line width. There is two axis environment in my tikzpicture and I don't understand why it can interpret the width of the same plot without ylabel and not with them. –  Sigmun Feb 6 '12 at 13:54
    
Try to remove them, and you will see that the ytick on the right axis aren't overlapping the margin as they do if left ylabel is specified. –  Sigmun Feb 6 '12 at 14:00
    
@Sigmun See it in this way: if pgf interpreted width=\linewidth as you wish, the two diagrams would not be superimposed. It can exactly superimpose them because it doesn't take the label into account. I guess that pgf leaves also some room for the tick labels, but of course not for the ylabel. –  egreg Feb 6 '12 at 14:02
show 1 more 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.