2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5
% better code environment for poly style in lhs2TeX
7
% Permission is granted to include this file (or parts of this file)
8
% literally into other documents, regardless of the conditions or
9
% license applying to these documents.
11
% Andres Loeh, February 2006, ver 1.9
13
% TODO: use \[ \] in arrayhs (fleqn problem)
14
% think about penalties and better pagebreaks
15
% by using \allowdisplaybreaks
16
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
18
%if not lhs2tex_polycode_fmt_read
19
%let lhs2tex_polycode_fmt_read = True
25
% The follwing subst replaces the bad default from lhs2TeX.fmt.
26
% The idea is to just provide the basic structure in the subst, and
27
% let the rest be handled by a LaTeX environment.
30
%subst code a = "\begin{hscode}\SaveRestoreHook'n" a "\ColumnHook'n\end{hscode}\resethooks'n"
33
% This package provides two environments suitable to take the place
34
% of hscode, called "plainhscode" and "arrayhscode".
36
% The plain environment surrounds each code block by vertical space,
37
% and it uses \abovedisplayskip and \belowdisplayskip to get spacing
38
% similar to formulas. Note that if these dimensions are changed,
39
% the spacing around displayed math formulas changes as well.
40
% All code is indented using \leftskip.
42
% Changed 19.08.2004 to reflect changes in colorcode. Should work with
45
\ReadOnlyOnce{polycode.fmt}%
48
\newcommand{\hsnewpar}[1]%
49
{{\parskip=0pt\parindent=0pt\par\vskip #1\noindent}}
51
% can be used, for instance, to redefine the code size, by setting the
52
% command to \small or something alike
53
\newcommand{\hscodestyle}{}
55
% The command \sethscode can be used to switch the code formatting
56
% behaviour by mapping the hscode environment in the subst directive
57
% to a new LaTeX environment.
59
\newcommand{\sethscode}[1]%
60
{\expandafter\let\expandafter\hscode\csname #1\endcsname
61
\expandafter\let\expandafter\endhscode\csname end#1\endcsname}
63
% "compatibility" mode restores the non-polycode.fmt layout.
65
\newenvironment{compathscode}%
67
\advance\leftskip\mathindent
73
\ignorespacesafterend}
75
\newcommand{\compaths}{\sethscode{compathscode}}
77
% "plain" mode is the proposed default.
79
\newenvironment{plainhscode}%
80
{\hsnewpar\abovedisplayskip
81
\advance\leftskip\mathindent
86
\hsnewpar\belowdisplayskip
87
\ignorespacesafterend}
89
% Here, we make plainhscode the default environment.
91
\newcommand{\plainhs}{\sethscode{plainhscode}}
94
% The arrayhscode is like plain, but makes use of polytable's
95
% parray environment which disallows page breaks in code blocks.
97
\newenvironment{arrayhscode}%
98
{\hsnewpar\abovedisplayskip
99
\advance\leftskip\mathindent
104
\hsnewpar\belowdisplayskip
105
\ignorespacesafterend}
107
\newcommand{\arrayhs}{\sethscode{arrayhscode}}
109
% The mathhscode environment also makes use of polytable's parray
110
% environment. It is supposed to be used only inside math mode
111
% (I used it to typeset the type rules in my thesis).
113
\newenvironment{mathhscode}%
114
{\parray}{\endparray}
116
\newcommand{\mathhs}{\sethscode{mathhscode}}
118
% texths is similar to mathhs, but works in text mode.
120
\newenvironment{texthscode}%
121
{\(\parray}{\endparray\)}
123
\newcommand{\texths}{\sethscode{texthscode}}
125
% The framed environment places code in a framed box.
127
\def\codeframewidth{\arrayrulewidth}
128
\RequirePackage{calc}
130
\newenvironment{framedhscode}%
131
{\parskip=\abovedisplayskip\par\noindent
133
\arrayrulewidth=\codeframewidth
134
\tabular{@@{}||p{\linewidth-2\arraycolsep-2\arrayrulewidth-2pt}||@@{}}%
135
\hline\framedhslinecorrect\\{-1.5ex}%
136
\let\endoflinesave=\\
140
\framedhslinecorrect\endoflinesave{.5ex}\hline
142
\parskip=\belowdisplayskip\par\noindent
143
\ignorespacesafterend}
145
\newcommand{\framedhslinecorrect}[2]%
148
\newcommand{\framedhs}{\sethscode{framedhscode}}
150
% The inlinehscode environment is an experimental environment
151
% that can be used to typeset displayed code inline.
153
\newenvironment{inlinehscode}%
154
{\(\def\column##1##2{}%
155
\let\>\undefined\let\<\undefined\let\\\undefined
156
\newcommand\>[1][]{}\newcommand\<[1][]{}\newcommand\\[1][]{}%
157
\def\fromto##1##2##3{##3}%
158
\def\nextline{}}{\) }%
160
\newcommand{\inlinehs}{\sethscode{inlinehscode}}
162
% The joincode environment is a separate environment that
163
% can be used to surround and thereby connect multiple code
166
\newenvironment{joincode}%
167
{\let\orighscode=\hscode
168
\let\origendhscode=\endhscode
169
\def\endhscode{\def\hscode{\endgroup\def\@@currenvir{hscode}\\}\begingroup}
170
%\let\SaveRestoreHook=\empty
171
%\let\ColumnHook=\empty
172
%\let\resethooks=\empty
173
\orighscode\def\hscode{\endgroup\def\@@currenvir{hscode}}}%
175
\global\let\hscode=\orighscode
176
\global\let\endhscode=\origendhscode}%