# Prevent underful hbox in custom environment

I created an environment for a thesis about a framework, which formats documentation of some methods. The method(s) are at the top with one line per method, set with \lstinline from the listings package.

Whenever a method signature is too long for one line and it is broken, I get "Underfull \hbox (badness 10000)". I searched for a while, but the recommendations for dealing with that warning are not suitable in this case:

• There are no manual line breaks, that cause the underful \hbox, it occurs only on automatic break.

• adding hyphenation is not suitable for program code, same for changing wording

Here is an example document with my environment:

\documentclass{article}
\usepackage{amsmath,listings,changepage,pbox,ifthen,xparse}
\lstset{basicstyle=\ttfamily,breaklines=true}
\newcommand*{\code}[1]{\lstinline[breaklines]{#1}}

\NewDocumentEnvironment{methods}{oomooooo}{
\smallskip
\noindent\textit{\IfNoValueTF{#1}{Methode\IfValueTF{#4}{n:}{:\phantom{n}}}{#1}}%
\hspace{\IfNoValueTF{#2}{1em}{#2}}%
\pbox[t]{0.8\textwidth}{%
\noindent\code{#3}%
\IfValueTF{#4}{\\\code{#4}}{}%
}%
}{
\bigskip
}

\begin{document}

\begin{methods}{Matrix<T> submatrix()}

This method's signature is short, it does not break and no underful hbox
warning is issued.

\end{methods}

\begin{methods}{Matrix<T> submatrix(int startRow, int startCol, int endRow, int endCol)}

This method's signature is long. An automatic linebreak is inserted,
which results in underful hbox (badness 10000).

\end{methods}
\end{document}


The resulting document looks like I expected, but in the original document which is rather long, all the underful hbox warnings make it difficult to notice real problems.

Is it possible to suppress the underful hbox warnings only when they occur in this specific environment?

Short explanation of the environment: I want to have a variable text "Method", "Methods", "Constructors" or similar on the left, and a left-aligned list of methods right of it. I'm open to suggestions how to acheive that in a different way.

-

I'd suggest to use varwidth instead of \pbox; the problem seems to be in \lstinline, a \raggedright declaration inside varwidth should cure the problem. I'd also measure more precisely the width of your code box.

\documentclass{article}
\usepackage{amsmath,listings,changepage,ifthen,xparse,lipsum,varwidth}
\usepackage[pass,showframe]{geometry}

\lstset{basicstyle=\ttfamily,breaklines=true,columns=fullflexible}
\newcommand*{\code}{\lstinline[breaklines]}

\NewDocumentEnvironment{methods}{oomooooo}
{\smallskip
\noindent
\sbox0{%
\textit{\IfNoValueTF{#1}{Methode\IfValueTF{#4}{n:}{:\phantom{n}}}{#1}}%
\hspace{\IfNoValueTF{#2}{1em}{#2}}%
}%
\dimen0=\dimexpr\textwidth-\wd0\relax\box0
\begin{varwidth}[t]{\dimen0}\raggedright
\code{#3}\IfValueTF{#4}{\\\code{#4}}{}%
\end{varwidth}\par

thanks. so the \wd0\relax\box0 calculates the width used by the contents of \sbox0? –  crater2150 Mar 3 at 18:44