diff options
author | Karl Berry <karl@freefriends.org> | 2014-08-09 23:20:30 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2014-08-09 23:20:30 +0000 |
commit | a142175a0227a7c7f1dd8d43294950f3bd068b69 (patch) | |
tree | f67306748ea771bf40834019f5f0d23b0773f2f7 /Master/texmf-dist/tex/latex | |
parent | e0932ba24bca208e30671029132591a6edd0e34d (diff) |
clrscode3e (9aug14)
git-svn-id: svn://tug.org/texlive/trunk@34887 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/latex')
-rw-r--r-- | Master/texmf-dist/tex/latex/clrscode3e/clrscode3e.sty | 343 |
1 files changed, 343 insertions, 0 deletions
diff --git a/Master/texmf-dist/tex/latex/clrscode3e/clrscode3e.sty b/Master/texmf-dist/tex/latex/clrscode3e/clrscode3e.sty new file mode 100644 index 00000000000..8157a009d86 --- /dev/null +++ b/Master/texmf-dist/tex/latex/clrscode3e/clrscode3e.sty @@ -0,0 +1,343 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +% clrscode3e.sty + +% See the document "Using the clrscode3e Package in LaTeX 2e" for +% examples. + +% Package for producing pseudocode in the style of Cormen, Leiserson, +% Rivest, and Stein, Introduction to Algorithms, Third edition. + +% LIMITATION: This package works only if each procedure has at most 99 +% numbered lines of code. + +% Each pseudocode procedure is typeset within a codebox environment, +% \begin{codebox}...\end{codebox}. + +% Normally, the first line within the codebox environment is a \Procname +% command. The argument of the \Procname command is a math-mode +% expression consisting of the procedure name and its parameters. The +% name of the procedure itself goes within a \proc command. Example: +% \Procname{$\proc{Matrix-Multiply}(A,B)$} +% The \Procname command is optional. + +% To typeset the name of a procedure (e.g., Matrix-Multiply) in small +% caps, use the \proc command: +% \proc{Matrix-Multiply} + +% To typeset the name of a constant (e.g., nil) in small caps, use the +% \const command: +% \const{nil} + +% To typeset the name of an identifier (e.g., rank) in regular italics, +% use the \id command: +% \id{rank} + +% To typeset the name of a fixed function (e.g., sin) in roman, use the +% \func command: +% \func{sin} +% (Note that several fixed functions, like sin, are already built into +% LaTeX.) + +% The \proc, \const, \id, and \func commands not only use the correct +% font, they also perform the important service of interpreting a dash +% as a hyphen, rather than as a minus sign. These commands may be used +% either in or out of math mode. + +% For attributes, use the various forms of the \attrib commands. + +% Other than the \Procname line, all lines begin with either \li (for a +% numbered line) or \zi (for an unnumbered line). The following +% commands are provided for typesetting keywords and handling automatic +% indentation: + +% Loops: \For, \To, \By, \Downto, \Do, \While, \Repeat, \Until +% Selection: \If, \Then, \Else, \ElseIf, \ElseNoIf +% Jumps: \Return, \Error, \Goto +% Multithreading: \Spawn, \Sync, \Parfor +% Comments: \Comment, \RComment, \CommentSymbol +% Indentation: \Indentmore, \Startalign, \Stopalign + +% \label commands appearing in or after the first numbered line in a +% codebox resolve to the number of the most recent numbered line. + +% \twodots produces the ".." notation used for subarrays. + +% Written for general distribution by Thomas H. Cormen, March 2009. + +% The author grants permission for anyone to use this macro package and +% to distribute it unchanged without further restriction. If you choose +% to modify this package, you must indicate that you have modified it +% prior to your distributing it. I don't want to get bug reports about +% changes that *you* have made! + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\ProvidesPackage{clrscode3e} +\RequirePackage{graphics} % needed for \scalebox command + +% The \useregularv command is because we used a different lowercase v +% in math mode in the book. Most users of the clrscode3e package will +% use the standard lowercase v in math mode, and so the \useregularv +% command is a no-op for them. +\ifdefined\useregularv\else\newcommand{\useregularv}{}\fi + +% Commands for typesetting constants, procedure names, identifiers, and +% fixed functions. +\newcommand{\const}[1]{\textnormal{\scshape#1}} +\newcommand{\proc}[1]{\textnormal{\scshape#1}} +\newcommand{\text@hyphens}{\mathcode`\-=`\-\relax} +\newcommand{\func}[1]{% + \ensuremath{{\useregularv\mathop{\text@hyphens\operator@font#1}\nolimits}}} +\newcommand{\id}[1]{% + \ensuremath{\mathit{\text@hyphens#1}}} + +% Commands for typesetting object attributes. All of these commands +% may be used either in or out of math mode. + +% Definitions: +% An i-string is a string that you would use in an \id command, +% typically one or more non-Greek letters, numerals, or dashes. +% An x-string is a string that you would not use in an \id command, +% typically because it has a subscript or one or more Greek letters. +% A single non-Greek letter can be either an i-string or an x-string. + +% Most of the time, we use the \attrib command, which assumes that its +% first argument, the object name, is an x-string and its second +% argument, the attribute name, is an i-string. Examples of the +% \attrib command: \attrib{A}{length}, \attrib{x}{next}, +% \attrib{\rho}{next-item}. \attrib is just a direct call to the +% \attribxi macro. + +% The four macros \attribxi, \attribxx, \attribix, and \attribii each +% take two arguments. The two letters at the end of the macro name +% determine, in order, how each argument is treated. x indicates that +% the argument should be an x-string, and i indicates that the +% argument should be an i-string. + +% Because we use a single letter to indicate most objects and a string +% of one or more letters to indicate most attribute names, \attribxi +% is the most common macro we use. That's why \attrib is just a +% direct call to \attribxi. + +% Use \attribxx when the object name is an x-string and the object +% name is also an x-string, for example, when the attribute name has a +% subscript: \attribxx{x}{c_i} or \attrib{x}{\id{key}_i}. Another +% time you would use \attribxx is when the attribute name is a Greek +% letter: \attrib{v}{\pi}. + +% When the object name has more than one letter, it is usually an +% i-string. In this case, use \attribii or \attribix, depending on +% the attribute name: \attribii{item}{key}, \attribii{prev-item}{np}, +% \attribix{item}{\pi}. + +% The \attribb, \attribbb, \attribbbb, and \attribbxxi macros are for +% cascading attributes. They just call the appropriate \attribxi and +% \attribxx commands. For one level of cascading, use \attribb: +% \attribb{x}{left}{size}. For two levels, use \attribbb: +% \attribbb{y}{p}{left}{size}. For three levels, use \attribbbb (but +% no examples in the book use this macro). The \attribbxxi macro is +% for one level of cascading where the first attribute given is an +% x-string: \attribbxxi{x}{c_i}{n}. + +% When the object is an edge of a graph, specified by two vertices, +% use \attribe or \attribex: \attribe{u}{v}{f}, \attribex{u}{v}{c'}. + +\newcommand{\attribxi}[2]{\ensuremath{#1.\hspace*{1pt}\id{#2}}} +\newcommand{\attribxx}[2]{\ensuremath{#1.\hspace*{1pt}#2}} +\newcommand{\attribix}[2]{\ensuremath{\id{#1}\hspace*{1pt}.#2}} +\newcommand{\attribii}[2]{\ensuremath{\id{#1}\hspace*{1pt}.\hspace*{1pt}\id{#2}}} +\newcommand{\attrib}[2]{\attribxi{#1}{#2}} +\newcommand{\attribe}[3]{\attribxi{(#1,#2)}{#3}} +\newcommand{\attribex}[3]{\attribxx{(#1,#2)}{#3}} +\newcommand{\attribb}[3]{\attribxi{\attribxi{#1}{#2}}{#3}} +\newcommand{\attribbb}[4]{\attribxi{\attribb{#1}{#2}{#3}}{#4}} +\newcommand{\attribbbb}[5]{\attribxi{\attribbb{#1}{#2}{#3}{#4}}{#5}} +\newcommand{\attribbxxi}[3]{\attribxi{\attribxx{#1}{#2}}{#3}} + +% Command for typesetting subarray ranges. +\newcommand{\twodots}{\mathinner{\ldotp\ldotp}} + +% The codelinenumber counter counts the current line number. +\newcounter{codelinenumber} + +% The indent counter keeps track of the current indentation level. +\newcounter{indent} + +% The \iffirstcodeline command tells us whether we are about to +% produce the first line other than the procedure declaration. +\newif\iffirstcodeline\firstcodelinetrue + +% The \zeroli command makes it so that we're about to produce the +% first line other than the procedure declaration. +\newcommand{\zeroli}{\setcounter{codelinenumber}{0}% +\setcounter{indent}{0}% +\firstcodelinetrue} + +% \digitwidth gives the width of a single digit. All digits are the +% same width. We'll need this amount to do the right thing for line +% numbers. +\newlength{\digitwidth} +\settowidth{\digitwidth}{0} + +% The \li command bumps the counter, outputs it, and skips some space +% A \label cmd for a given numbered line is allowed to appear after the +% \\, as in +% \li $x\gets y$ \label{li:assign-x} +% But if \li merely set \@currentlabel in the usual way via +% \refstepcounter, the value of \@currentlabel does not persist outside +% the current cell. Solution: use an additional, global variable +% \@lilabel. + +% THC: This next command is magic to me. I didn't write it. +\def\@startline{\global\@curtabmar\@nxttabmar\relax + \global\@curtab\@curtabmar\setbox\@curline\hbox + {}\@startfield\strut} + +% \code@init is run at the beginning of a codebox environment. +\def\code@init{% + \zeroli% producing the first line + \setlength{\tabbingsep}{1em}% distance between numbers and code + % Initialize \@lilabel to allow a pageref \label cmd at the beginning + % of the codebox + \global\let\@lilabel\@currentlabel + \def\@currentlabel{\@lilabel}% +} + +% When we make a codebox, we save the code part into a box before +% printing it. We do not actually print the code until we know how many +% line numbers there are. +\newsavebox{\savecode} + +% The \ifprocname command tells us whether this procedure has been +% given a name yet. +\newif\ifprocname\procnamefalse + +% Assume that the width of the codebox is the width of the text, minus +% the width of 2 digits. We'll correct for that later. +\newlength{\codeboxwidth} +\setlength{\codeboxwidth}{\linewidth} % Thanks, David Etherington! +\addtolength{\codeboxwidth}{-2\digitwidth} + +% The "codebox" environment produces an unbreakable section of code +\newenvironment{codebox}{% +\global\procnamefalse% this proc hasn't been given a name yet +\code@init% set up for first line +\begin{lrbox}{\savecode}% save the code into a box +\begin{minipage}[t]{\codeboxwidth}% it'll be a minipage +% Set up the tab stops +\def\codeindent{\textbf{else} }% +\begin{tabbing}% +99\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=codeindent\=codeindent\=\+\kill% +}{% +% Here's what's run at the end of a codebox environment. Start by +% making sure that we have ended at indent level 0. Otherwise, print a +% warning. +\ifnum\value{indent}=0\else\typeout{Warning: Indentation ends at level \theindent\space in codebox on page \thepage.}\fi% +\end{tabbing}\end{minipage}\end{lrbox}% +\addtolength{\topsep}{0.5ex}% for the following trivlist +\begin{trivlist}\item\parindent=0pt% +% If there was a procedure name given, print it now but with a little +% space below, and disallow a page break after the procedure name. +\@nobreaktrue% +\ifprocname\saveprocname\rule[-1.25ex]{0pt}{0pt}\\ \fi% +% Put in the right amount of space, depending on whether we reached +% double digits in the line numbers. +\ifnum\value{codelinenumber}>9\hspace*{2\digitwidth}\else\hspace*{1\digitwidth}\fi% +% Now print the code +\usebox{\savecode}\end{trivlist}% +\addtolength{\topsep}{-0.5ex}\global\procnamefalse} + +% Use the \Procname macro to give the name of the procedure. +\newcommand{\Procname}[1]{\global\def\saveprocname{#1}\global\procnametrue} + +\newcounter{thisindent} % counter for recursive indenting code +\newcommand{\Indent}{\setcounter{thisindent}{\value{indent}}\putindents} +% \putindents is a recursive macro that indents a number of times given +% by the counter thisindent. +\newcommand{\putindents}{\ifnum\value{thisindent}>0\>\addtocounter{thisindent}{-1}\putindents\fi} + +% For typesetting any keyword in the main text. +\newcommand{\kw}[1]{\textbf{#1}} + +% Override the 'gets' symbol. +\def\gets{\mathrel{\hspace{1pt}=\hspace{1pt}}} +\newcommand{\isequal}{\mathrel{\scalebox{0.8}[1]{=}\hspace*{1pt}\scalebox{0.8}[1]{=}}} + +% All of our favorite keywords. +\newcommand{\For}{\textbf{for} } +\newcommand{\To}{\ifmmode\ \textrm{\textbf{to}}\ \else\textbf{to}\ \fi} +\newcommand{\By}{\ifmmode\ \textrm{\textbf{by}}\ \else\textbf{by}\ \fi} +\newcommand{\Downto}{\ifmmode\ \textrm{\textbf{downto}}\ \else\textbf{downto}\ \fi} +\newcommand{\While}{\textbf{while} } +\newcommand{\Repeat}{\textbf{repeat}\>\addtocounter{indent}{1}} +\newcommand{\Until}{\kill\addtocounter{indent}{-1}\liprint\textbf{until} } +\newcommand{\If}{\textbf{if} } +\newcommand{\Then}{\>\addtocounter{indent}{1}} +\newcommand{\Else}{\kill\addtocounter{indent}{-1}\liprint\textbf{else}\>\addtocounter{indent}{1}} +\newcommand{\End}{\addtocounter{indent}{-1}} +\newcommand{\ElseIf}{\kill\addtocounter{indent}{-1}\liprint\textbf{elseif} } +\newcommand{\ElseNoIf}{\kill\addtocounter{indent}{-1}\liprint\textbf{else} \addtocounter{indent}{1}} +\newcommand{\Do}{\>\addtocounter{indent}{1}} +\newcommand{\Return}{\textbf{return} } +\newcommand{\CommentSymbol}{\texttt{\textbf{/\hspace*{-0.3em}/}}} +\newcommand{\Comment}{\CommentSymbol\ } +\newcommand{\RComment}{\`\CommentSymbol\ } +\newcommand{\Goto}{\textbf{goto} } +\newcommand{\Error}{\textbf{error} } % optionally followed by string argument +\newcommand{\EndTest}{\textbf{:}} +\newcommand{\Spawn}{\ifmmode\textbf{spawn}\ \else\textbf{spawn} \fi} +\newcommand{\Sync}{\textbf{sync}} +\newcommand{\Parfor}{\textbf{parallel for} } + +% Indent the next line one level more +\newcommand{\Indentmore}{\addtocounter{indent}{1}} + +\newif\ifnumberedline +\numberedlinetrue + +% The \li command starts a new numbered line. +\newcommand{\li}{\global\numberedlinetrue% +\iffirstcodeline\global\firstcodelinefalse\else\\ \fi +\stepcounter{codelinenumber}% +\liprint} + +% The \lispace command starts a new numbered line with a little extra +% space above, given by the argument. +\newcommand{\lispace}[1]{\iffirstcodeline\global\firstcodelinefalse\else\\[#1] \fi +\stepcounter{codelinenumber}% +\liprint} + +% \liprint actually prints the line number and sets up the indentation. +\newcommand{\liprint}{\protected@xdef\@lilabel{\thecodelinenumber}% +\ifnumberedline\thecodelinenumber\fi\'\Indent% +} + +\providecommand{\numref}[1]{% + \@ifundefined{r@#1}{000}{% + \expandafter\expandafter\expandafter\@firstoftwo + \csname r@#1\endcsname + }% +} + +% \setlinenumber sets the line number to its argument +\newcommand{\setlinenumber}[1]{\setcounter{codelinenumber}{\numref{#1}}% +\addtocounter{codelinenumber}{-1}} +% \setlinenumberplus sets the line number to its first argument plus its +% second argument. +\newcommand{\setlinenumberplus}[2]{\setcounter{codelinenumber}{\numref{#1}}% +\addtocounter{codelinenumber}{-1}\addtocounter{codelinenumber}{#2}} + +% The \zi command starts a new unnumbered line. +\newcommand{\zi}{\global\numberedlinefalse% +\iffirstcodeline\global\firstcodelinefalse\else\\ \fi +\liprint} + +% Temporarily make all lines indented so that they start at the end of +% a given text. +\newcommand{\Startalign}[1]{\\ \pushtabs\FakeIndent#1\=\kill} +\newcommand{\Stopalign}{\poptabs} +\newcommand{\FakeIndent}{\setcounter{thisindent}{\value{indent}}\putfakeindents} +\newcommand{\putfakeindents}{\ifnum\value{thisindent}>0\textbf{else }\addtocounter{thisindent}{-1}\putfakeindents\fi} + +\endinput |