I'm trying to create an environment that takes lets me change fonts to represent an algorithm and I'm getting a Bad Box overfull \hbox error.
MWE:
\documentclass{article}
\usepackage{enumerate, lipsum}
\newenvironment{aparts}{\begin{enumerate}[{(}a{)}]}{\end{enumerate}}
\newenvironment{algo}[3] % function name and parameters, input, output
{
\fontfamily{cmtt}\selectfont
\underline{#1}
Input: #2
Output: #3
\bigskip
}
{\bigskip}
\textheight=9in
\textwidth=6.5in
\topmargin=-.75in
\oddsidemargin=0.25in
\evensidemargin=0.25in
\begin{document}
\begin{aparts}
\item
{\fontfamily{cmtt}\selectfont
\underline{function eval(a[$a_0, a_1, ..., a_n$], x)}
Input: An array of constant coefficients a[$a_0, a_1, ..., a_n$] and a quantity to plug into the polynomial expression determined by the coefficients x
Output: The solution to the polynomial expression determined by the coefficients when x is plugged in as the variable
\bigskip
$p = a_0$
if $i >= 1$:
\quad for $i = n$ to 1:
\qquad $p = p + a_i$
\qquad $p = p * x$
return $p$
}
\bigskip
\item
{\fontfamily{cmtt}\selectfont
\underline{function eval(a[$a_0, a_1, ..., a_n$], x)}
Input: An array of constant coefficients a[$a_0, a_1, ..., a_n$] and a quantity to
\quad plug into the polynomial expression determined by the coefficients x
Output: The solution to the polynomial expression determined by the
\quad coefficients when x is plugged in as the variable
\bigskip
$p = a_0$
if $i >= 1$:
\quad for $i = n$ to 1:
\qquad $p = p + a_i$
\qquad $p = p * x$
return $p$
}
\bigskip
\item
\begin{algo}{function eval(a[$a_0, a_1, ..., a_n$], x)}
{An array of constant coefficients a[$a_0, a_1, ..., a_n$] and a quantity to plug into the polynomial expression determined by the coefficients x}
{The solution to the polynomial expression determined by the coefficients when x is plugged in as the variable}
$p = a_0$
if $i >= 1$:
\quad for $i = n$ to 1:
\qquad $p = p + a_i$
\qquad $p = p * x$
return $p$
\end{algo}
\item
\lipsum[50]
\end{aparts}
\end{document}
I'm getting the following errors:
Overfull \hbox (4.20915pt too wide) in paragraph at lines 35--36 []\OT1/cmtt/m/n/10 Input: An array of constant coefficients a[$\OML/cmm/m/it/10 a[]; a[]; :::; a[]$\OT1/cmtt/m/n/10 ] and a quantity to plug into the []
Overfull \hbox (10.0188pt too wide) in paragraph at lines 37--38 []\OT1/cmtt/m/n/10 Output: The solution to the polynomial expression determined by the coefficients when x []
Overfull \hbox (4.20915pt too wide) in paragraph at lines 88--88 []\OT1/cmtt/m/n/10 Input: An array of constant coefficients a[$\OML/cmm/m/it/10 a[]; a[]; :::; a[]$\OT1/cmtt/m/n/10 ] and a quantity to plug into the []
Overfull \hbox (10.0188pt too wide) in paragraph at lines 88--88 []\OT1/cmtt/m/n/10 Output: The solution to the polynomial expression determined by the coefficients when x []
As you can see, the first one doesn't work, but I fixed it by brute-forcing a line break. Of course, I don't want to have to do that every time, and it's even more annoying if I'm putting it in the parameter for an environment. Also, the reason I think it has to do with fonts is that regular text looks find, as demonstrated in (d).
How can I fix this? Thanks so much.
\fontfamily{cmtt}\selectfontistead of simply\ttfamily? – David Carlisle Jan 26 '21 at 00:42\smallor use a proportional font and let tex adjust the inter-word space to make things fit as it does in normal text. – David Carlisle Jan 26 '21 at 00:45Output: The solution to the polynomial expression determined by...that you are setting monospace. – David Carlisle Jan 26 '21 at 00:50\raggedrightto the preamble makes all the warnings go but I would seriously re-consider if this is the output format that you want to use. It is painful for the reader. – David Carlisle Jan 26 '21 at 00:52ifforetc monospace it would be understandable (look for algorithmicx` and related packages for more structured ways of doing pseudocode) but you have the paragraphs of text describing the input and outputs in monospace as well so they blend in to the pseudocode. it is your document and if that's what you want OK, but it is hard to read. – David Carlisle Jan 26 '21 at 00:55\raggedrightis an answer then – David Carlisle Jan 26 '21 at 00:58