polycode.fmt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. %if False
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. % polycode.fmt
  4. %
  5. % better code environment for poly style in lhs2TeX
  6. %
  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.
  10. %
  11. % Andres Loeh, February 2006, ver 1.9
  12. %
  13. % TODO: use \[ \] in arrayhs (fleqn problem)
  14. % think about penalties and better pagebreaks
  15. % by using \allowdisplaybreaks
  16. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  17. %endif
  18. %if not lhs2tex_polycode_fmt_read
  19. %let lhs2tex_polycode_fmt_read = True
  20. %include lhs2TeX.fmt
  21. %
  22. %if style /= newcode
  23. %
  24. %if False
  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.
  28. %endif
  29. %
  30. %subst code a = "\begin{hscode}\SaveRestoreHook'n" a "\ColumnHook'n\end{hscode}\resethooks'n"
  31. %
  32. %
  33. % This package provides two environments suitable to take the place
  34. % of hscode, called "plainhscode" and "arrayhscode".
  35. %
  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.
  41. %
  42. % Changed 19.08.2004 to reflect changes in colorcode. Should work with
  43. % CodeGroup.sty.
  44. %
  45. \ReadOnlyOnce{polycode.fmt}%
  46. \makeatletter
  47. \newcommand{\hsnewpar}[1]%
  48. {{\parskip=0pt\parindent=0pt\par\vskip #1\noindent}}
  49. % can be used, for instance, to redefine the code size, by setting the
  50. % command to \small or something alike
  51. \newcommand{\hscodestyle}{}
  52. % The command \sethscode can be used to switch the code formatting
  53. % behaviour by mapping the hscode environment in the subst directive
  54. % to a new LaTeX environment.
  55. \newcommand{\sethscode}[1]%
  56. {\expandafter\let\expandafter\hscode\csname #1\endcsname
  57. \expandafter\let\expandafter\endhscode\csname end#1\endcsname}
  58. % "compatibility" mode restores the non-polycode.fmt layout.
  59. \newenvironment{compathscode}%
  60. {\par\noindent
  61. \advance\leftskip\mathindent
  62. \hscodestyle
  63. \let\\=\@@normalcr
  64. \(\pboxed}%
  65. {\endpboxed\)%
  66. \par\noindent
  67. \ignorespacesafterend}
  68. \newcommand{\compaths}{\sethscode{compathscode}}
  69. % "plain" mode is the proposed default.
  70. \newenvironment{plainhscode}%
  71. {\hsnewpar\abovedisplayskip
  72. \advance\leftskip\mathindent
  73. \hscodestyle
  74. \let\\=\@@normalcr
  75. \(\pboxed}%
  76. {\endpboxed\)%
  77. \hsnewpar\belowdisplayskip
  78. \ignorespacesafterend}
  79. % Here, we make plainhscode the default environment.
  80. \newcommand{\plainhs}{\sethscode{plainhscode}}
  81. \plainhs
  82. % The arrayhscode is like plain, but makes use of polytable's
  83. % parray environment which disallows page breaks in code blocks.
  84. \newenvironment{arrayhscode}%
  85. {\hsnewpar\abovedisplayskip
  86. \advance\leftskip\mathindent
  87. \hscodestyle
  88. \let\\=\@@normalcr
  89. \(\parray}%
  90. {\endparray\)%
  91. \hsnewpar\belowdisplayskip
  92. \ignorespacesafterend}
  93. \newcommand{\arrayhs}{\sethscode{arrayhscode}}
  94. % The mathhscode environment also makes use of polytable's parray
  95. % environment. It is supposed to be used only inside math mode
  96. % (I used it to typeset the type rules in my thesis).
  97. \newenvironment{mathhscode}%
  98. {\parray}{\endparray}
  99. \newcommand{\mathhs}{\sethscode{mathhscode}}
  100. % texths is similar to mathhs, but works in text mode.
  101. \newenvironment{texthscode}%
  102. {\(\parray}{\endparray\)}
  103. \newcommand{\texths}{\sethscode{texthscode}}
  104. % The framed environment places code in a framed box.
  105. \def\codeframewidth{\arrayrulewidth}
  106. \RequirePackage{calc}
  107. \newenvironment{framedhscode}%
  108. {\parskip=\abovedisplayskip\par\noindent
  109. \hscodestyle
  110. \arrayrulewidth=\codeframewidth
  111. \tabular{@@{}||p{\linewidth-2\arraycolsep-2\arrayrulewidth-2pt}||@@{}}%
  112. \hline\framedhslinecorrect\\{-1.5ex}%
  113. \let\endoflinesave=\\
  114. \let\\=\@@normalcr
  115. \(\pboxed}%
  116. {\endpboxed\)%
  117. \framedhslinecorrect\endoflinesave{.5ex}\hline
  118. \endtabular
  119. \parskip=\belowdisplayskip\par\noindent
  120. \ignorespacesafterend}
  121. \newcommand{\framedhslinecorrect}[2]%
  122. {#1[#2]}
  123. \newcommand{\framedhs}{\sethscode{framedhscode}}
  124. % The inlinehscode environment is an experimental environment
  125. % that can be used to typeset displayed code inline.
  126. \newenvironment{inlinehscode}%
  127. {\(\def\column##1##2{}%
  128. \let\>\undefined\let\<\undefined\let\\\undefined
  129. \newcommand\>[1][]{}\newcommand\<[1][]{}\newcommand\\[1][]{}%
  130. \def\fromto##1##2##3{##3}%
  131. \def\nextline{}}{\) }%
  132. \newcommand{\inlinehs}{\sethscode{inlinehscode}}
  133. % The joincode environment is a separate environment that
  134. % can be used to surround and thereby connect multiple code
  135. % blocks.
  136. \newenvironment{joincode}%
  137. {\let\orighscode=\hscode
  138. \let\origendhscode=\endhscode
  139. \def\endhscode{\def\hscode{\endgroup\def\@@currenvir{hscode}\\}\begingroup}
  140. %\let\SaveRestoreHook=\empty
  141. %\let\ColumnHook=\empty
  142. %\let\resethooks=\empty
  143. \orighscode\def\hscode{\endgroup\def\@@currenvir{hscode}}}%
  144. {\origendhscode
  145. \global\let\hscode=\orighscode
  146. \global\let\endhscode=\origendhscode}%
  147. \makeatother
  148. \EndFmtInput
  149. %
  150. %endif
  151. %endif