% code.sty: -*- latex -*- % Latex macros for a "weak" verbatim mode. % -- like verbatim, except \, {, and } have their usual meanings. % Environments: code, tightcode, codeaux, codebox, centercode % Commands: \dcd, \cddollar, \cdmath, \cd, \codeallowbreaks, \codeskip, \^ % Already defined in LaTeX, but of some relevance: \#, \$, \%, \&, \_, \{, \} % Changelog at the end of the file. % These commands give you an environment, code, that is like verbatim % except that you can still insert commands in the middle of the environment: % \begin{code} % for(x=1; x] option, then the following newline will % be read *after* ^M is bound to \cr, so we're cool. If there isn't % an option given (i.e., default to [c]), then the @\ifnextchar will % gobble up the newline as it gobbles whitespace. So we insert the % \cr explicitly. Isn't TeX fun? \def\codebox{\leavevmode\@ifnextchar[{\@codebox}{\@codebox[c]\cr}} %] \def\@codebox[#1]% {\hbox\bgroup$\if #1t\vtop \else \if#1b\vbox \else \vcenter \fi\fi\bgroup% \tabskip\z@\setupcode\cd@obeycr% just before cd@obey \halign\bgroup##\hfil\span} \def\endcodebox{\crcr\egroup\egroup\m@th$\egroup} % Center the box on the page: \newenvironment{centercode}% {\begin{center}\begin{codebox}[c]}% {\end{codebox}\end{center}} %% code, codeaux, tightcode %%============================================================================= %% Code environment as described above. Lines are kept on one page. %% This actually works by setting a huge penalty for breaking %% between lines of code. Code is indented same as other displayed paras. %% Note: to increase left margin, use \begin{codeaux}{\leftmargin=1in}. % To allow pagebreaks, say \codeallowbreaks immediately inside the env. % You can allow breaks at specific lines with a \pagebreak form. %% N.B.: The \global\@ignoretrue command must be performed just inside %% the *last* \end{...} before the following text. If not, you will %% get an extra space on the following line. Blech. %% This environment takes two arguments. %% The second, required argument is the \list parameters to override the %% \@listi... defaults. %% - Usefully set by clients: \topsep \leftmargin %% - Possible, but less useful: \partopsep %% The first, optional argument is the extra \parskip glue that you get around %% \list environments. It defaults to the value of \parskip. \def\codeaux{\@ifnextchar[{\@codeaux}{\@codeaux[\parskip]}} %] \def\@codeaux[#1]#2{% \bgroup\parskip#1% \begin{list}{}% {\parsep\z@\rightskip\z@\listparindent\z@\itemindent\z@#2}% \item[]\setupcode\cd@obeylines}% \def\endcodeaux{\end{list}\leavevmode\egroup\ignorespaces\global\@ignoretrue} %% Code env is codeaux with the default margin and spacing \list params: \def\code{\codeaux{}} \let\endcode=\endcodeaux %% Like code, but with no extra vertical space above and below. \def\tightcode{\codeaux[=0pt]{\topsep\z@}}% \let\endtightcode\endcodeaux % {\vspace{-1\parskip}\begin{codeaux}{\partopsep\z@\topsep\z@}}% % {\end{codeaux}\vspace{-1\parskip}} % Reasonable separation between lines of code \newcommand{\codeskip}{\penalty0\vspace{2ex}} % \cd is used to build a code environment in the middle of text. % Note: only difference from display code is that cr's are taken % as unbreakable spaces instead of linebreaks. \def\cd{\leavevmode\begingroup\ifmmode\let\startcode=\startmcode\else% \let\startcode\starttcode\fi% \setupcode\cd@obeycrsp\startcode} \def\starttcode#1{#1\endgroup} \def\startmcode#1{\hbox{#1}\endgroup} % Restore $&#^_~% to their normal catcodes % Define \^ to give the ^ char. % \dcd points to this guy inside a code env. \def\cd@dcd{\catcode`\$=3\catcode`\&=4\catcode`\#=6\catcode`\^=7% \catcode`\_=8\catcode`\~=13\catcode`\%=14\def\^{\char`\^}} % Selectively enable $, and $^_ as special. % \cd@mathspecial also defines \^ give the ^ char. % \cddollar and \cdmath point to these guys inside a code env. \def\cd@dollarspecial{\catcode`\$=3} \def\cd@mathspecial{\catcode`\$=3\catcode`\^=7\catcode`\_=8% \def\^{\char`\^}} % Change log: % Started off as some macros found in C. Rich's library. % Olin 1/90: % Removed \makeatletter, \makeatother's -- they shouldn't be there, % because style option files are read with makeatletter. The terminal % makeatother screwed things up for the following style options. % Olin 3/91: % Rewritten. % - Changed things so blank lines don't get compressed out (the \leavevmove % in \cd@cr and \cd@crwb). % - Changed names to somewhat less horrible choices. % - Added lots of doc, so casual hackers can more easily mess with all this. % - Removed `'"@ from the set of hacked chars, since they are already % non-special. % - Removed the bigcode env, which effect can be had with the \codeallowbreaks % command. % - Removed the \@noligs command, since it's already defined in latex.tex. % - Win big with the new \dcd, \cddollar, and \cdmath commands. % - Now, *only* the chars \{} are special inside the code env. If you need % more, use the \dcd command inside a group. % - \cd now works inside math mode. (But if you use it in a superscript, % it still comes out full size. You must explicitly put a \scriptsize\tt % inside the \cd: $x^{\cd{\scriptsize\tt...}}$. A \leavevmode was added % so that if you begin a paragraph with a \cd{...}, TeX realises you % are starting a paragraph. % - Added the codebox env. Tricky bit involving the first line hacked % with help from David Long. % Olin 8/94 % Changed the font commands for LaTeX2e.