\documentclass[11pt]{article} \usepackage[mtbold]{mathtime} \usepackage{clrscode} \input{page} \newcommand{\defn}[1]{\textbf{\textit{#1}}} \newcommand{\subheading}[1]{\subsubsection*{#1}} \begin{document} \title{CS 188 Lecture Notes} \author{Lecturer: Tom Cormen \\ Scribe: Lin Zhong} \date{October 8, 2003} \maketitle We will finish the \LaTeX{} part of the course this Friday. The first assignment will go out this Friday too. \section{The \texttt{clrscode} Package in \LaTeX{}2e} The \texttt{clrscode} package is used to typeset pseudocode in the style of \textit{Introduction to Algorithms}. There are many other packages for typesetting pseudocode in \LaTeX{}. You can use Google to search by keywords such as ``tex algorithm.'' For detailed information on the \texttt{clrscode} package, please refer to the document \textit{Using the \texttt{clrscode} Package in \LaTeX{}2e}. \subsection{Typesetting names} \subheading{Four types of names} Pseudocode in CLRS uses four types of names: identifiers, procedures, constants, and fixed functions. \texttt{clrscode} provides the commands \verb`\id`, \verb`\proc`, \verb`\const`, and \verb`\func` for these names. Each of these commands takes one argument, which is the name being typeset. These commands work both in and out of math mode. When used in math mode, and when the name given as an argument contains a dash, the dash is typeset as a hyphen rather than as a minus sign. \subheading{The $\backslash$\texttt{twodots} command} Besides these four commands for names, \texttt{clrscode} has another command \verb`\twodots` denoting subarrays. The source text \verb`$A[1 \twodots j-1]$` will produce $A[1 \twodots j-1]$. The \verb`\twodots` command must be used in math mode. \subsection{The \texttt{codebox} environment} Use the \texttt{codebox} environment to make procedures. Each procedure should go in a separate codebox, even if you have multiple procedures appearing consecutively. The space in the source code is ignored. We put it in only for convenience when we read the source code. \subheading{The $\backslash$\texttt{Procname} command} The \verb`\Procname` command specifies the name of the procedure. It takes as a parameter the procedure name and parameters, typically all in math mode. \verb`\Procname` makes its argument flush left against the margin, and it leaves a little bit of extra space below the line. \subheading{The $\backslash$\texttt{li} and $\backslash$\texttt{zi} commands} \verb`\li` starts a numbered line, and \verb`\zi` starts an unnumbered line. \subheading{Tabs} A \texttt{codebox} environment has a \texttt{tabbing} environment within it. Note that the \texttt{tabbing} environment within a \texttt{codebox} has nothing to do with tabs that you enter in your source code; when you press the TAB key, that is the same as pressing the space bar in the eyes of \LaTeX{}2e. To go to the next tab stop, type \verb`\>`. \subheading{Keywords} Commands \verb`\For`, \verb`\Do`, and \verb`\While` produce the keywords \kw{for}, \kw{do}, and \kw{while}. \verb`\Do` and some other commands also affect indentation. To include a keyword in the main text, use the \verb`\kw` command. Note that \LaTeX{}2e is case sensitive. Thus, \verb`\for` is not a legal command here. Commands such as \verb`\For`, \verb`\To`, \verb`\Downto`, \verb`\By`, \verb`\While`, \verb`\If`, \verb`\Return`, \verb`\Goto`, and \verb`\Error` simply produce their corresponding keywords, typeset in boldface. \verb`\Comment` produces the comment symbol \Comment. None of the above commands affects indentation. \verb`\Do` not only produces the keyword \kw{do}, but also increments the indentation level. \verb`\End` not only produces the keyword \kw{end}, but also decrements the indentation level. The \verb`\If`, \verb`\Then`, and \verb`\Else` commands format an \kw{if}-\kw{then} or \kw{if}-\kw{then}-\kw{else} statement. The keywords \kw{then} and \kw{else} are only one tab stop in, and the bodies of the \kw{then} and \kw{else} clauses are another tab stop in. Thus, each level of indentation is actually two tab stops. An \verb`\End` command ends an \kw{if}-\kw{then} or \kw{if}-\kw{then}-\kw{else} statement. For more complicated ``\kw{if}-ladders,'' you can use the \verb`\ElseIf` and \verb`\ElseNoIf` commands; the keyword \kw{elsenoif} is not indented. The commands \verb`\Repeat` and \verb`\Until` produce a \kw{repeat}-\kw{until} loop. The keyword \kw{repeat} is flush left against the tab stop, and \kw{until} is flush right. \verb`\Comment` is used to produce the comment symbol. Use the tab command \verb`\>` to explicitly tab to where you want the comment to begin. Make the comments align vertically by trial-and-error. Alternatively, you can also use command \verb`\RComment` to make the comment flush against the right-hand margin. \subheading{Indentation} \sloppy If any line is wider than the page, you can break it into multiple lines and use command \verb`\Indentmore` to indent the second and subsequent lines. An \verb`\End` command cancels the effect of a previous \verb`\Indentmore`. \fussy \subheading{Referencing line numbers} The \texttt{codebox} environment is set up to allow you to place \verb`\label` commands on lines of pseudocode and then reference these labels. The references will resolve to the line numbers. \section{Final Formating} \subsection{Line breaks} \subheading{Avoid bad line breaks} There are three rules to follow: \begin{itemize} \item Do not type \verb`x and y`. Type \verb`x and~y` instead, unless the items are not short. \item Do not type \verb`vertex $v$`. Type \verb`vertex~$v$` instead, unless the two-or-more-item rule supersedes this rule, in which case you should type \verb`vertices $u$ and~$v$`. \item Try not to let the line break before an ellipsis. Rewrite the text or put the math expression in an \verb`\mbox`, like \verb`\mbox{$x_1 + x_2 + \cdots + x_n$}`. If the whole expression cannot fit onto one line, you may break the line after an $=$. \end{itemize} \subheading{Avoid overfull lines} The cause of an overfull line could be a hyphenated word, a \verb`\verb` command, or an \verb`\mbox` command. The reason why hyphenated word could cause an overfull line is that \LaTeX{} will break the line only after the hyphens in a hyphenated word. Line breaks are not allowed within the argument to \verb`\verb` and~\verb`\mbox`. When you have an overfull box, you'll get an error message in the log file. The easiest way to avoid overfull lines is to put \verb`\sloppy` and \verb`\fussy` around the paragraph with the overfull line. For text, if \verb`\sloppy` and \verb`\fussy` don't fix your problem, then you may have to rewrite. If you're using the verbatim environment, you can shrink it in both dimensions by using a smaller font. There are several size-changing command like \verb`\small`, \verb`\footnotesize`, \verb`\tiny`, or even \verb`\large`, \verb`\Large`, or \verb`\LARGE`. You should put these commands within an environment when using them. Never leave overfull lines on the document. If you do, it tells the reader that you forget to read your paper. \subheading{Math displays} If a displayed equation is too wide to fit onto one line, break it into multiple lines and turn it into an eqnarray. If an eqnarrary is just a touch too wide, use the \verb`\narrowarray` (before the eqnarray) and \verb`\normalarray` (after the eqnarray) commands to reduce the space between all columns. \subheading{Page breaks} A \defn{widow line} is a single line from a multiline paragraph that appears at the top of a page. An \defn{orphan line} is the opposite of a widow line: a single line from a multiline paragraph that appears at the bottom of a page. \TeX{} inserts page breaks so as to avoid widow and orphan lines. \LaTeX{} will also avoid having a heading appear at the bottom of a page without at least one line of text below it. It is because \TeX{} and \LaTeX{} avoid widow lines, orphan lines, and headings at the bottom of a page that you might see a page break occur earlier than you think is necessary. To force a page break, you can use the commands \verb`\newpage`, \verb`\clearpage`, and \verb`\pagebreak`. The \verb`\newpage` command will not cause any remaining figures or tables to print. The \verb`\clearpage` command will force a new page and spit out any remaining figures or tables. And the \verb`\pagebreak` command may give you the vertical equivalent of an underfull line. \TeX{} normally tries to make each page the same height, and so you might get an abnormal amount of space between paragraphs by using command \verb`\pagebreak`. If that happens, consider inserting the \verb`\raggedbottom` declaration (rather than the default \verb`\flushbottom`). To inhibit a page break, you can use the \verb`\enlargethispage` command to squeeze in more text, e.g., \verb`\enlargethispage{2ex}`. And the starred form of this command, \verb`\enlargethispage*{2ex}`, also tells \LaTeX{} to shrink the open vertical space on the page as much as possible. \end{document}