[Tex/LaTex] ragged2e: “newcommands” option produces “underfull \hbox” warnings

horizontal alignmenthyphenationline-breakingragged2ewarnings

The ragged2e package provides the commands \Centering, \RaggedRight and \RaggedLeft for setting ragged text while allowing hyphenation. The package offers a newcommands option that sets the standard LaTeX commands \centering, \raggedright and \raggedleft equal to their ragged2e counterparts. However, when using this option, I've stumbled upon two instances where LaTeX will produce Underfull \hbox (badness 10000) warnings:

  • Creating a title with \maketitle:

    \documentclass{article}
    
    \usepackage[newcommands]{ragged2e}
    
    \begin{document}
    
    \author{Author}
    \title{Title}
    \maketitle
    
    \end{document}
    
  • Creating headers and footers with either the fancyhdr or the scrpage2 package:

    \documentclass{article}
    
    \usepackage[newcommands]{ragged2e}
    
    \usepackage[english]{babel}
    \usepackage{blindtext}
    
    \usepackage{fancyhdr}
    \pagestyle{fancy}
    
    \begin{document}
    
    \blinddocument
    
    \end{document}
    

What causes these warnings, and what are proper ways to remove them?

EDIT: In response to Herbert's answer: I'm looking for a solution that keeps hyphenation while getting rid of warnings.

Best Answer

According to p. 4 of the ragged2e documentation (thanks to Herbert for encouraging me to RTFM), the ragged2e lengths which control \leftskip resp. \rightskip for \Centering & friends "must be set to a finite value, to make hyphenation possible". (By default, ragged2e uses a value of 0pt plus 2em instead of standard LaTeX's 0pt plus 1fil.) But finit glue in turn means that "underfull \hbox" warnings become possible. In practice, such warnings are likely to be issued for one-liner or two-liner paragraphs typeset with \Centering & friends in effect -- i.e., they will turn up in (but are not confined to) title pages and headers.

This is demonstrated in the following MWE, which typesets text within three center environments (which, because ragged2e's newcommands option is enabled, internally use \Centering). The first environment will produce an underfull \hbox with a badness of 10000, the second environment one with a badness of 4096. In the third environment, the sixth word is moved to the second line (instead of being hyphenated), and no warning is issued.

To remove all those "underfull \hbox" warnings, \hbadness may be set to a value of 10000 for \Centering & friends (again thanks to Herbert for the tip). Note that, if the newcommands option is enabled, one must add \hbadness=10000\relax (EDIT: or, even better, \hbadness=\@M) to the new definitions of, e.g., \centering as well as to the start of center environments.

\documentclass[12pt]{article}

\usepackage[newcommands]{ragged2e}

% Uncommenting the following lines will remove "underfull \hbox" warnings
\makeatletter
% \g@addto@macro{\centering}{\hbadness=\@M}
% \g@addto@macro{\raggedright}{\hbadness=\@M}
% \g@addto@macro{\raggedleft}{\hbadness=\@M}
% \g@addto@macro{\center}{\hbadness=\@M}
% \g@addto@macro{\flushleft}{\hbadness=\@M}
% \g@addto@macro{\flushright}{\hbadness=\@M}
\makeatother

\begin{document}

\begin{center}
hyphenation
\end{center}

\begin{center}
hyphenation hyphenation hyphenation hyphenation hyphenation
hyphenation hyphenation hyphenation hyphenation
\end{center}

\begin{center}
hyphenation hyphenation hyphenation hyphenation hyphenation
hyphenation hyphenation hyphenation hyphenation hyphenation
\end{center}

\end{document}
Related Question