4

I have the following in my document and it works as I expect, scaling the listing to fit the page, but it gives me a spurious Overfull \hbox warning that I would like to get rid of:

\begin{adjustbox}{max width=\linewidth}
\begin{lstlisting}
A long line of code which I want to be scaled to fit the page.
\end{lstlisting}
\end{adjustbox}

Here is the warning:

Overfull \hbox (27.00378pt too wide) in paragraph at lines 8--9
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][] 
 []

where line 8 is the long line inside the listing.

Is there a clean way to get rid of the warning?

  • Maybe like this? (I did not test it.) – user121799 Nov 27 '18 at 0:42
  • @marmot I don't want to remove overfull warnings for the rest of the document though. Can I put hfuzz in a group? – user11171 Nov 27 '18 at 2:29
  • I don't know, can't you just try out (e.g. by doing the listing twice, once in a group with this and once without)? – user121799 Nov 27 '18 at 2:31
3

The Overfull \hbox warning is from the lstlisting environment which creates a paragraph box \linewidth long and the content is wider than that. Scaling it down afterwards makes it fit then, but does not remove the previous warning.

One easy way to avoid this is to make the listings paragraph wider, which can be done by wrapping it into a varwidth environment with a wide enough width. adjustbox can do this for you be adding the varwidth=<width> key. Here you can take \maxdimen to ensure that is as wide as possible.

\documentclass{article}

\usepackage{listings}
\usepackage{adjustbox}
\usepackage{lipsum}

\begin{document}

\begin{adjustbox}{varwidth=\maxdimen,max width=\linewidth}
\begin{lstlisting}
A long line of code which I want to be scaled to fit the page.
\end{lstlisting}
\end{adjustbox}

\lipsum[1]

\end{document}
| improve this answer | |
3

The process of inserting hfuzz=<dimen> specifically when using the adjustbox environment can be automated using etoolbox:

\documentclass{article}

\usepackage{listings,adjustbox,etoolbox,lipsum}

\AtBeginEnvironment{adjustbox}{\hfuzz=100pt}% Choose your poison

\begin{document}

\begin{adjustbox}{max width=\linewidth}
\begin{lstlisting}
A long line of code which I want to be scaled to fit the page.
\end{lstlisting}
\end{adjustbox}

\lipsum[1-50]

\end{document}
| improve this answer | |
1

As suggested by marmot, using \hfuzz to remove the warning works. I don't think this solution is elegant, though. It seem to me like an error that the listing inside the adjustbox gives the warning to begin with.

\documentclass[a4paper, 10pt]{article}
\usepackage{listings}
\usepackage{adjustbox}
\begin{document}

\begin{adjustbox}{max width=0.5\linewidth}
\begin{lstlisting}
A long line of code which I want to be scaled to fit the page.
\end{lstlisting}
\end{adjustbox}

% No warning:
{\hfuzz=10000pt
\begin{adjustbox}{max width=0.5\linewidth}
\begin{lstlisting}
A long line of code which I want to be scaled to fit the page.
\end{lstlisting}
\end{adjustbox}
}

% Warning:
\begin{adjustbox}{max width=0.5\linewidth}
\begin{lstlisting}
A long line of code which I want to be scaled to fit the page.
\end{lstlisting}
\end{adjustbox}

\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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