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 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}}{}%
    }%
    \begin{adjustwidth}{2.5em}{0pt}
}{
    \end{adjustwidth}
    \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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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
   \begin{adjustwidth}{2.5em}{0pt}
  }
  {\end{adjustwidth}\bigskip}

Still, I don't understand how you can manage five final optional arguments. It's not the best way to proceed, in my opinion.

share|improve this answer
    
thanks. so the \wd0\relax\box0 calculates the width used by the contents of \sbox0? –  crater2150 Mar 3 '13 at 18:44
    
regarding the optional arguments: I know, that it is not a good solution, I wanted to have a look at using a comma-separated parameter after I know the rest of the code won't need any bigger changes for solving the hbox problem ;) –  crater2150 Mar 3 '13 at 18:47

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.