TransWikia.com

How do I fix underfull hbox when using custom command?

TeX - LaTeX Asked by ChrisMM on October 5, 2021

I’ve written a small command so that I don’t have to keep creating minipages. The command is defined as such:

newcommand{itemimage}[3][]{%
    begin{minipage}{linewidth}
        item #2
        
        centering
        includegraphics[#1]{#3}
    end{minipage}
}%

Any time I use this, it seems to generate a warning for "Underfull hbox (badness 10000) in paragraph"

begin{enumerate}[topsep=0pt]
    %...
    itemimage[width=300px]{This is just some text.}{img001.png}
    %...
end{enumerate}

However, if I don’t use the command, and instead use what the command is, then no warning is generated.

begin{enumerate}[topsep=0pt]
    %...
    begin{minipage}{linewidth}
        item This is just some text.
        
        centering
        includegraphics[width=300px]{img001.png}
    end{minipage}
    %...
end{enumerate}

What am I missing from my itemimage command to prevent these warnings from occurring?

Minimal example:

documentclass[letterpaper,12pt]{extreport}
usepackage{mathtools}
usepackage{enumitem}

edefrestoreparindent{parindent=theparindentrelax}
usepackage{parskip}
restoreparindent

newcommand{itemimage}[3][]{%
    begin{minipage}{linewidth}
        item #2

        centering
        includegraphics[#1]{#3}
    end{minipage}
}%

begin{document}
begin{sloppypar}
begin{enumerate}[topsep=0pt]
    itemimage[width=300px]{This is just some text.}{img001.png}
    
    begin{minipage}{linewidth}
        item This is just some text.
        
        centering
        includegraphics[width=300px]{img001.png}
    end{minipage}
end{enumerate}
end{sloppypar}
end{document}

Code & Warning

One Answer

Don't forget to omit spaces at ends of lines, you were generating a line with just a word space which can not stretch to fill the line hence the underfull box. (note you can use example-image in examples as these images are distributed for such tests)

documentclass[letterpaper,12pt]{extreport}
usepackage{mathtools}
usepackage{enumitem}

edefrestoreparindent{parindent=theparindentrelax}
usepackage{parskip}
restoreparindent

newcommand{itemimage}[3][]{%
    begin{minipage}{linewidth}
        item #2

        centering
        includegraphics[#1]{#3}
    end{minipage}%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}%

begin{document}
begin{sloppypar}
begin{enumerate}[topsep=0pt]
    itemimage[width=300px]{This is just some text.}{example-image.png}
    
    begin{minipage}{linewidth}
        item This is just some text.
        
        centering
        includegraphics[width=300px]{example-image.png}
    end{minipage}
end{enumerate}
end{sloppypar}
end{document}

Correct answer by David Carlisle on October 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP