📄 code.sty
字号:
% -*- LaTeX -*-
%%% ====================================================================
%%% @LaTeX-style-file{
%%% author = "Dave Mason",
%%% version = "0.15",
%%% date = "2001 April 16",
%%% time = "21:05:41",
%%% filename = "code.sty",
%%% address = "Programming Languages Group
%%% Department of Computer Science
%%% University of Waterloo
%%% Waterloo, Ontario, Canada
%%% N2L 3G1",
%%% telephone = "(519) 885-1211",
%%% FAX = "(519) 885-1208",
%%% checksum = "",
%%% email = "dmason@plg.uwaterloo.ca",
%%% codetable = "ISO/ASCII",
%%% keywords = "",
%%% supported = "yes",
%%% abstract = "Contains the LaTeX style command definitions
%%% for typesetting blocks of code within a
%%% document. Includes numbered lines \label
%%% support and pretty-printing.",
%%% docstring = "The checksum field above contains a CRC-16
%%% checksum as the first value, followed by the
%%% equivalent of the standard UNIX wc (word
%%% count) utility output of lines, words, and
%%% characters. This is produced by Robert
%%% Solovay's checksum utility.",
%%% }
%%% ====================================================================
%
% I will eventually document this properly with the doc style, but in
% the meantime here are the things it provides:
%
% Two environments:
% \begin{verbcode} .... \end{verbcode}
% - verbatim but with \,{,} interpreted.
% \begin{code} .... \end{code}
% - same as verbcode, except leading spaces are
% removed, multiple blanks are reduced
% to a single blank, and indentation is
% controlled by Indent & Exdent
%
% In addition, for both environments the lines
% are numbered and the line number is periodically
% output.
%
% \codelinereset - reset the line number
%
% \codeindentreset - reset the indentation
%
% \meta{a phrase} - typeset as <a\ phrase> in italics mode -- ideal for
% meta variables in grammars, etc.
%
% \math{a formula} - typeset the formula in math mode
%
% \Indent - increase the space at the beginning of the line
% \Exdent - decrease the space at the beginning of the line
% both in increments of \TabIndent. These should
% only be done at the beginning of the line
% (although they can be within macros and \Next
% can be used to help with this). The amount of
% indentation is remembered from one code
% environment to the next. This is useful if
% you want to put some prose between blocks of
% the code, but it implies that you must be
% careful that your \Indents and \Exdents are
% properly paired.
%
% \EncBreak - encourage a page break at this point in the code
% \DisBreak - discourage a page break at this point
% should be used bracketed like Indent/Exdent
% i.e. Dis/Enc increment/decrement a counter
% showing how much you don't want a break.
% Assuming the discouragements don't work too
% much against it, the package will normally try
% to do a page break just before a numbered
% line. (Of course if you have line numbers on
% every line this doesn't have much effect.)
%
% \CancelLine - act as if this line were commented. Usually used
% with an \Exdent.
%
% \BreakPoint - make the start of this line a good breakpoint and
% put in some space.
%
% \Next{fun} - add fun to the queue of things to do at the beginning
% of the next line
% \LineEnd{fun} - add fun to the stack of things to do at the end of
% this line
% for both of these, some commands (particularly font
% changes) may need to be prefixed by \noexpand
%
% \CodeInit - is executed at the start of every code or verbcode
% environment. It may be used to set the font or other
% attributes of the code. You might want to make \\ be an alias for
% end-of-line by saying \let\\=\par, or to make { and }
% be printed by saying \MakeBracketsOther.
%
% \MakeBracketsOther - make { and } be uninterpretted, leaving only \ as
% a special character. Particularly useful for C code.
%
% \CodeNoNumber - turn line numbers off
% \CodeNumber - turn line numbers on - default
%
% \label{name} - works properly. A \ref{name} will give the current
% line number at the point of the \label.
%
% \input{file} - works properly.
%
% \EnableMacro{\macro} - makes \macro{foo} work within the code environment
% this is how \input and \label were enabled.
% Only works for macros with 1 parameter
% (although macros with 0 parameters always work).
%
% The following lengths are defined and can be set with \setlength, etc.
% \MacroTopSep - spacing above a code block
% \MacroIndent - the indentation of the whole block
% \TabIndent - the indentation for each tabbing level
%
% The following counters are defined and can be set with \setcounter, etc.
% CodeLineInc - the increment between displayed line numbers
% default is 5
%
% The following font change macros are used
% \MacroFont - sets the font to use.
% The default font is \small\tt.
%
% as an example of what can be done with \Indent, Exdent, and Next:
% \def\If{{\bf if }\DisBreak\Next\Indent}
% \def\Else{\Exdent{\bf else }\Next\Indent}
% \def\End{\CancelLine\Exdent\EncBreak}
% \def\Comm{\hfill\hbox to 2in\bgroup\em--\,\LineEnd{\hfil\egroup}}
%
% which could be used as:
%
% \If this and that
% do stuff
% more stuff \Comm this is a comment
% \Else
% other stuff
% more other stuff
% \End
% the rest
%
% Bugs:
% This should be the last style in your \documentstyle line, or
% at least should follow any other style that might redefine
% \end or \label, as for them to work properly here, we have to
% set the catcodes of { and } back to normal.
%
% The handling of lines that are too long is not very elegant.
% Making them break at all reasonably seems nigh on impossible,
% so it prevents long lines from being broken. (While this is
% not ideal, it's better than the previous behaviour.)
%
% There seems to be a problem with the first line in a code
% block; particularly in an enumerate environment.
%
\@ifundefined{c@de}{}{\endinput}
\typeout{Style-Option: `code' v0.15 \@spaces\space\space <2001.04.16> (DVM)}%
\newif\ifdo@lineno
\let\CodeNumber=\do@linenotrue
\CodeNumber
\let\CodeNoNumber=\do@linenofalse
\newif\ifcode@rep
\newif\ifcode@num
\newif\ifc@bstart
\newif\ifc@bend
\def\code{
\let\c@delast=\relax\@ifnextchar[{\code@check}{\code@repfalse\c@de}%
}
\def\verbcode{
\let\c@delast=\@vobeyspaces\@ifnextchar[{\code@check}{\code@repfalse\c@de}%
}
\def\codelinereset{\global\c@CodeLineNo\@ne}
\def\codeindentreset{\global\code@indent\z@}
\def\code@check[#1#2]{\code@reptrue
\code@linesave\c@CodeLineNo
\ifcat#1x\@ifundefined{r@#1#2}{\code@numfalse}{\code@numtrue
\expandafter\code@getnum\csname r@#1#2\endcsname!}%
\else\code@numtrue\c@CodeLineNo=#1#2%
\fi
\c@de}
\def\code@getnum#1!{\expandafter\code@getnalt#1}
\def\code@getnalt#1#2{\c@CodeLineNo=#1}
\def\c@de{%
\ifcode@rep\else\codeindentreset\code@numtrue\fi
\topsep \MacrocodeTopsep
\@beginparpenalty \predisplaypenalty
\trivlist \parskip \z@ \item[]%
\rightskip=0pt plus20em minus100em \parindent\z@ \parfillskip\@flushglue
\pretolerance=9999\tolerance=9999\hyphenpenalty=9999\exhyphenpenalty=9999
\leftskip\@totalleftmargin
\advance\leftskip\MacroIndent
\code@width-\leftskip
\advance\code@width\hsize
\advance\leftskip\TabIndent
\parindent\z@%-\TabIndent
\advance\leftskip\code@indent
\global\code@indent\z@
\global\c@CodeLineNext-5
\def\Exdent{\global\advance\code@indent-\TabIndent}%
\def\Indent{\global\advance\code@indent\TabIndent}%
\def\EncBreak{\global\advance\code@penalty-700}%
\def\DisBreak{\global\advance\code@penalty700}%
\def\CancelLine{\global\let\end@@par\relax\global\let\end@@penalty\relax}%
\def\Next##1{\xdef\next@par{\next@par##1}}%
\def\LineEnd##1{\xdef\end@par{##1\end@par}}%
\def\BreakPoint{\penalty-1000\vskip 7pt plus .4pt minus .1pt}%
\let\c@bblock\relax
\let\c@blabel\relax
\let\c@bmid\relax
\def\BBlockOne{\c@bmark\c@bstarttrue\c@bendtrue\BBlockLabel}
\def\BBlockStart{\c@bmark\c@bstarttrue}
\def\BBlockEnd{\c@bmark\c@bendtrue}
\def\BBlockLabel##1{\c@bmark\xdef\c@blabel{##1}}
\let\EnableMacro\code@enablemacro
\let\math\code@@math
\let\meta\code@@meta
\xdef\next@par{\relax}%
\xdef\end@par{\relax}%
\CancelLine
\end@penalty
\ifdo@lineno\global\advance\c@CodeLineNo-1\fi
\def\par{\end@par\xdef\end@par{\relax}%
\advance\leftskip\code@indent
\global\code@indent\z@
\end@@par\global\let\end@@par=\code@end
\@@par\end@@penalty
}%
\obeylines
\everypar{\next@par\xdef\next@par{\relax}%
\global\let\end@@par=\code@end
\global\let\end@@penalty=\end@penalty
\global\xdef\@currentlabel{\number\c@CodeLineNo}}%
\global\@newlistfalse
\global\@minipagefalse
\EnableMacro{\label}%
\EnableMacro{\input}%
\EnableMacro{\end}%
\MacroFont
\@makeother\$%$
\@makeother\&\@makeother\#%
\@makeother\^\@makeother\_\@makeother\~\@makeother\%%
\frenchspacing\c@delast
\CodeInit
}
\def\c@bmark{\ifx\c@bblock\relax\Indent\Indent\Indent\fi\let\c@bblock\c@doblock}
\newbox\c@vbox
\setbox\c@vbox\hbox to 0pt{\hbox to 0.6pt{\hss}\raise -2ex\hbox to 0pt{$\vert$\hss}$\vert$}%
\dp\c@vbox 0pt
\def\c@bvert{\mbox{\copy\c@vbox}}
\def\c@doblock{\hbox to 0pt{\hskip 0pt plus 1fil minus 16383pt\hbox to 15pt{\c@blabel\hss\hbox to 5pt{%
\ifc@bstart
\hbox to 0pt{$\lceil$\hss}%
\ifc@bend
\hbox to 0pt{$\lfloor$\hss}%
\else
\c@bvert
\global\let\c@bmid=\c@bvert
\fi
\else
\ifc@bend
\hbox to 0pt{$\lfloor$\hss}%
\global\let\c@bmid\relax
\else
\c@bmid
\fi
\fi
\global\c@bstartfalse
\global\c@bendfalse
\global\let\c@blabel\relax
\hss}}\hskip\code@width\hskip-20pt}}
\newbox\code@box
\def\code@enablemacro#1{%
\expandafter\code@enablehelp\csname code@save\string#1\endcsname#1
}
\def\code@enablehelp#1#2{%
\let#1=#2
\def#2{\begingroup\catcode`{=1\catcode`}=2 \code@@help#1}%
}
\def\code@@help#1#2{\endgroup#1{#2}}
\def\CodeInit{}
\def\MakeBracketsOther{\@makeother\{\@makeother\}}
\def\code@end{\hskip0pt plus 16383fil minus 16383pt\do@lineno\leavevmode}
\def\end@penalty{\ifdo@lineno\global\advance\c@CodeLineNo\@ne%
\ifnum\c@CodeLineNo<\c@CodeLineNext%
\penalty\code@penalty%
\else%
\c@CodeLineTemp\c@CodeLineInc%
\multiply\c@CodeLineTemp-99%
\advance\c@CodeLineTemp\code@penalty%
\penalty\c@CodeLineTemp%
% \vskip0pt plus 3pt
\fi\fi}
\def\do@lineno{\ifdo@lineno
\ifnum\c@CodeLineNo<\c@CodeLineNext\else
\hbox to 0pt{\hskip 0pt plus 1fil minus 16383pt\theCodeLineNo\ifcode@rep$\succ$\else\ \fi\ \hskip\code@width}%
\ifnum\c@CodeLineNo>\c@CodeLineNext
\global\c@CodeLineNext\c@CodeLineNo
\c@CodeLineTemp\c@CodeLineInc
\divide\c@CodeLineTemp by 2
\global\advance\c@CodeLineNext\c@CodeLineTemp
\global\divide\c@CodeLineNext\c@CodeLineInc
\global\multiply\c@CodeLineNext\c@CodeLineInc
\global\code@last\c@CodeLineNext
\global\advance\code@last-1
\fi
\global\advance\c@CodeLineNext\c@CodeLineInc
\fi\fi
\c@bblock
}
%
\def\endcode{%
\everypar{}%
\next@par%
\global\@inlabelfalse
\global\advance\code@indent\leftskip
\global\advance\code@indent-\@totalleftmargin
\global\advance\code@indent-\TabIndent
\global\advance\code@indent-\MacroIndent
\ifcode@rep\global\c@CodeLineNo\code@linesave\fi
\leftskip\@totalleftmargin
\endtrivlist
\penalty-600
}
\let\endverbcode=\endcode
%\let\endcoder=\endcode
\@ifundefined{MacroFont}{%
\ifx\undefined\selectfont
\def\MacroFont{\small\tt}\else
\def\MacroFont{\math@fontsfalse\reset@font\small\tt}\fi
}{}
\@ifundefined{AltMacroFont}{%
\ifx\undefined\selectfont
\def\AltMacroFont{\small\tt}\else
\def\AltMacroFont{\math@fontsfalse\small\reset@font\it\tt}\fi
}{}
\newskip\MacrocodeTopsep \MacrocodeTopsep = 3pt plus 1.2pt minus 1pt
\newdimen\MacroIndent
\settowidth\MacroIndent{\rm\scriptsize 00\ \ }
\newdimen\TabIndent
\newdimen\code@width
\newdimen\code@indent\code@indent\z@
\settowidth\TabIndent{\rm \ \ \ \ }
%
\begingroup
\obeyspaces%
\catcode`\^^M\active%
\gdef\code@@meta{\begingroup\obeyspaces\catcode`\^^M\active%
\let^^M\do@space\let \do@space%
\catcode`\{=1\catcode`\}=2%
\def\-{\egroup\discretionary{-}{}{}\hbox\bgroup\it}%
\code@meta}%
\gdef\code@@math{\begingroup\catcode`\ =10%
\catcode`\{=1\catcode`\}=2%
\catcode`\^=7\catcode`\_=8%
\leavevmode\hbox\bgroup$%$
\code@math}%
\endgroup%
\def\code@meta#1{\leavevmode\hbox\bgroup$\langle$\it#1\/$\rangle$
\egroup\endgroup}
\def\code@math#1{#1$%$
\egroup\endgroup}
\def\do@space{\egroup\space\hbox\bgroup\it\futurelet\next\sp@ce}
\def\sp@ce{\ifx\next\do@space\expandafter\sp@@ce\fi}
\def\sp@@ce#1{\futurelet\next\sp@ce}
%
\newcount\code@last\code@last\z@
\newcount\code@penalty\code@penalty\z@
\newcount\code@linesave
\newcount\c@CodeLineNo \c@CodeLineNo\@ne
\newcount\c@CodeLineNext \c@CodeLineNext\z@
\newcount\c@CodeLineInc \c@CodeLineInc=5
\newcount\c@CodeLineTemp
\@ifundefined{theCodeLineNo}
{\ifx\selectfont\undefined
\def\theCodeLineNo{\rm\scriptsize\ifcode@num\arabic{CodeLineNo}\else??\fi}%
\else
\def\theCodeLineNo{\reset@font\scriptsize\ifcode@num\arabic{CodeLineNo}\else??\fi}%
\fi}
{}
\endinput
%%
%% End of file `code.sty'.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -