0

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.

  • it doesn't make any difference but why use the low level \fontfamily{cmtt}\selectfont istead of simply \ttfamily ? – David Carlisle Jan 26 '21 at 00:42
  • I'm brand new to this so I didn't know it was a thing to be honest – Please Help Jan 26 '21 at 00:43
  • 1
    you are using a monospace font (why???) so tex has no flexibility to adjust the spacing so there is a fixed known number of characters per line, you need to put less characters on a line or use a smaller font by adding \small or 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:45
  • I'm using a monospace font in order to represent code. – Please Help Jan 26 '21 at 00:48
  • It is hard to help as it isn't clear what output you want, the style being used with monospace text with inline proportional italic font mathematics is rather unusual (to say the least) – David Carlisle Jan 26 '21 at 00:48
  • I definitely agree with you there, but that's how it's done in my textbook – Please Help Jan 26 '21 at 00:50
  • I do not understand your comment, the code like parts eg p=p*x are set in variable width italic, it is the non-code descriptions Output: The solution to the polynomial expression determined by... that you are setting monospace. – David Carlisle Jan 26 '21 at 00:50
  • Basically any mathematical operation is in proportional font, but anything that is pseudo code (if statements, for loop headers, etc.) are in monospace – Please Help Jan 26 '21 at 00:52
  • adding \raggedright to 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:52
  • What would you suggest? – Please Help Jan 26 '21 at 00:54
  • I'm open to suggestions. Right now I'm just following the way it's done in my textbook. – Please Help Jan 26 '21 at 00:54
  • If you just had the keywords if for etc 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
  • Fortunately or unfortunately, that's also how it is in my textbook. I'd be willing to change to something similar if you have any ideas, but I don't, so the textbook template is what I'm going with for now. – Please Help Jan 26 '21 at 00:57
  • well as I say \raggedright is an answer then – David Carlisle Jan 26 '21 at 00:58
  • Ok. Thanks so much! – Please Help Jan 26 '21 at 00:58
  • but you post a self answer I wouldn't want to post an answer suggesting that output. Look at anything in [tag:algorithmicx] eg https://tex.stackexchange.com/questions/144840/vertical-loop-block-lines-in-algorithmicx-with-noend-option on this site for examples of pseudocode laid out in a more reasonable way – David Carlisle Jan 26 '21 at 01:00

0 Answers0