# Silencing overfull hbox warning for listing in adjustbox

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}


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? 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

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{lipsum}

\begin{document}

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

\lipsum[1]

\end{document}


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

\documentclass{article}

\begin{document}

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

\lipsum[1-50]

\end{document}


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}
\begin{document}

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

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