4

I'm getting an "Overfull \hbox" error when setting width=\textwidth in axis when using pgfplots.

Here is a MWE. The exact error shown is Overfull \hbox (6.19328pt too wide) in paragraph at lines 22--23.

\documentclass[11pt]{book} \usepackage{pgfplots} \pgfplotsset{compat=1.9} \begin{document} \centering % interestingly. removing this increase the error to `23.19328pt too wide` \begin{tikzpicture} \begin{axis}[ ylabel={Y label}, xmin=0, xmax=10, xtick={0,1,2,3,4,5,6,7,8,9,10}, width={\textwidth}, ] \addplot[] coordinates { (0,21.70)(1,21.71)(8,20.93)(9,20.47)(10,20.40) % Something. Doesn't really matter }; \addlegendentry{Precision} \end{axis} \end{tikzpicture} \end{document} 

I read a lot of answers here and around the web, but nothing seems to work. And being relatively new, I'm not able to debug further.

Any ideas?

4
  • It's the ylabel sticking out because the plot is already text width wide. It's a just a warning Commented Jun 13, 2018 at 11:35
  • Thanks @percusse. What do you mean by just a warning? Is is not actually sticking out in the margin. If it is, any idea about how should I go about fixing it? Commented Jun 13, 2018 at 11:50
  • 1
    without \centering the picture is starting a justified paragraph so is indented by \parindent which is why the box is overfull by an increased amount. Commented Jun 13, 2018 at 12:17
  • 1
    width=\textwidth uses \textwidth- 45pt for the plot and 45pt for the material outside of the plotting area. The 45pt are a fixed length. But 45pt are not enough horizontal space for the ylabel and the xtick 10 in the lower right corner. Commented Jun 13, 2018 at 12:19

1 Answer 1

5

Add the line

\usepackage[showframe]{geometry} 

to the preamble to see what's going on.

  • The left image is the code as given. You see that the label 10 exceeds the text width.
  • The right image is with the \centering removed. The image protrudes even more since now the image starts a new paragraph that begins with an indentation. If you replace \centering by \noindent the image will be typeset without indentation flushleft.

    enter image description here      enter image description here

A solution is to replace

width={\textwidth} 

by

width={\dimexpr\textwidth-7pt} 

where 7pt corresponds to the amount reported by TeX in the overfull hbox message.

enter image description here

Just to make sure: The line \usepackage[showframe]{geometry} is only for test purposes, remove it in the final version.

Finally, here is the code for the picture (without frame).

\documentclass[11pt]{book} \usepackage{pgfplots} \pgfplotsset{compat=1.9} \begin{document} \noindent \begin{tikzpicture} \begin{axis}[ ylabel={Y label}, xmin=0, xmax=10, xtick={0,1,2,3,4,5,6,7,8,9,10}, width={\dimexpr\textwidth-7pt}, ] \addplot[] coordinates {(0,21.70)(1,21.71)(8,20.93)(9,20.47)(10,20.40)}; \addlegendentry{Precision} \end{axis} \end{tikzpicture} \end{document} 

Automatic adjustment: The adjustment can be automated by using the adjustbox package. The following code will resize "Some LaTeX material" to \textwidth.

\documentclass{...} \usepackage{adjustbox} \begin{document} ... \begin{adjustbox}{width=\textwidth} ... some LaTeX material ... \end{adjustbox} ... \end{document} 

Here is the code for the example above.

\documentclass[11pt]{book} \usepackage{adjustbox} \usepackage{pgfplots} \pgfplotsset{compat=1.9} \begin{document} \begin{adjustbox}{width=\textwidth} \begin{tikzpicture} \begin{axis}[ ylabel={Y label}, xmin=0, xmax=10, xtick={0,1,2,3,4,5,6,7,8,9,10}, width={\textwidth}, ] \addplot[] coordinates { (0,21.70)(1,21.71)(8,20.93)(9,20.47)(10,20.40) }; \addlegendentry{Precision} \end{axis} \end{tikzpicture} \end{adjustbox} \end{document} 
4
  • Thanks. I tried similar solution and it works! :) But I have multiple such diagrams with different overfull-ness. Is there any better way than to fix each of them by the amount they're overfull? Commented Jun 13, 2018 at 14:17
  • @nisargjhaveri I have added another solution that resizes the plot automatically. Commented Jun 13, 2018 at 14:34
  • If I understand correctly, the solution with adjustbox scales the whole diagram, changing the font size and everything, while the earlier solution of setting the width, does not change the font size or line widths. Is there any way we can know the diagram's to-be width and set the width parameter accordingly. As @esdd suggested, it leaves 45pt for everything else, is there a way to fix it there? Btw, this works. Thanks a lot. :D And, sorry for being so demanding! :P Commented Jun 13, 2018 at 15:00
  • 1
    @nisargjhaveri The rescaling will be minimal if you use \textwidth also in the plot. Another solution is to choose an upper bound, say 15pt, and always subtract this amount from \textwidth. I can think of solutions where you typeset the plot, then measure the width, and then typeset it a second time with an adjusted width. But this seems to be an overkill. Commented Jun 13, 2018 at 15:09

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.