agda.sty 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. % ----------------------------------------------------------------------
  2. % Some useful commands when doing highlighting of Agda code in LaTeX.
  3. % ----------------------------------------------------------------------
  4. \ProvidesPackage{agda}
  5. \RequirePackage{ifxetex, ifluatex, xifthen, xcolor, polytable, etoolbox,
  6. calc, environ}
  7. % https://tex.stackexchange.com/questions/47576/combining-ifxetex-and-ifluatex-with-the-logical-or-operation
  8. \newif\ifxetexorluatex
  9. \ifxetex
  10. \xetexorluatextrue
  11. \else
  12. \ifluatex
  13. \xetexorluatextrue
  14. \else
  15. \xetexorluatexfalse
  16. \fi
  17. \fi
  18. % ----------------------------------------------------------------------
  19. % Options
  20. \DeclareOption{bw} {\newcommand{\AgdaColourScheme}{bw}}
  21. \DeclareOption{conor}{\newcommand{\AgdaColourScheme}{conor}}
  22. \newif\if@AgdaEnableReferences\@AgdaEnableReferencesfalse
  23. \DeclareOption{references}{
  24. \@AgdaEnableReferencestrue
  25. }
  26. \newif\if@AgdaEnableLinks\@AgdaEnableLinksfalse
  27. \DeclareOption{links}{
  28. \@AgdaEnableLinkstrue
  29. }
  30. % If the "nofontsetup" option is used, then the package does not
  31. % select any fonts, and it does not change the font encoding.
  32. \newif\if@AgdaSetupFonts\@AgdaSetupFontstrue
  33. \DeclareOption{nofontsetup}{
  34. \@AgdaSetupFontsfalse
  35. }
  36. % If the "noinputencodingsetup" option is used, then the package does
  37. % not change the input encoding, and it does not load the `ucs`
  38. % package.
  39. \newif\if@AgdaSetupInputEncoding\@AgdaSetupInputEncodingtrue
  40. \DeclareOption{noinputencodingsetup}{
  41. \@AgdaSetupInputEncodingfalse
  42. }
  43. \ProcessOptions\relax
  44. % ----------------------------------------------------------------------
  45. % Input encoding setup
  46. \if@AgdaSetupInputEncoding
  47. \ifxetexorluatex
  48. \providecommand{\DeclareUnicodeCharacter}[2]{\relax}
  49. \else
  50. \RequirePackage{ucs}
  51. \RequirePackage[utf8x]{inputenc}
  52. \fi
  53. \fi
  54. % ----------------------------------------------------------------------
  55. % Font setup
  56. \tracinglostchars=2 % If the font is missing some symbol, then say
  57. % so in the compilation output.
  58. \if@AgdaSetupFonts
  59. % XeLaTeX or LuaLaTeX
  60. \ifxetexorluatex
  61. % Hack to get the amsthm package working.
  62. % https://tex.stackexchange.com/questions/130491/xelatex-error-paragraph-ended-before-tempa-was-complete
  63. \let\AgdaOpenBracket\[\let\AgdaCloseBracket\]
  64. \RequirePackage{fontspec}
  65. \let\[\AgdaOpenBracket\let\]\AgdaCloseBracket
  66. \RequirePackage{unicode-math}
  67. \setmainfont
  68. [ Ligatures = TeX
  69. , BoldItalicFont = xits-bolditalic.otf
  70. , BoldFont = xits-bold.otf
  71. , ItalicFont = xits-italic.otf
  72. ]
  73. {xits-regular.otf}
  74. \setmathfont{xits-math.otf}
  75. \setmonofont[Mapping=tex-text]{FreeMono.otf}
  76. % Make mathcal and mathscr appear as different.
  77. % https://tex.stackexchange.com/questions/120065/xetex-what-happened-to-mathcal-and-mathscr
  78. \setmathfont[range={\mathcal,\mathbfcal},StylisticSet=1]{xits-math.otf}
  79. \setmathfont[range=\mathscr]{xits-math.otf}
  80. % pdfLaTeX
  81. \else
  82. % https://tex.stackexchange.com/questions/1774/how-to-insert-pipe-symbol-in-latex
  83. \RequirePackage[T1]{fontenc}
  84. \RequirePackage{amsfonts, amssymb}
  85. \RequirePackage[safe]{tipa} % See page 12 of the tipa manual for what
  86. % safe does.
  87. \fi
  88. \fi
  89. % ----------------------------------------------------------------------
  90. % Colour schemes.
  91. \providecommand{\AgdaColourScheme}{standard}
  92. % ----------------------------------------------------------------------
  93. % References to code (needs additional post-processing of tex files to
  94. % work, see wiki for details).
  95. \if@AgdaEnableReferences
  96. \RequirePackage{catchfilebetweentags, xstring}
  97. \newcommand{\AgdaRef}[2][]{%
  98. \StrSubstitute{#2}{\_}{AgdaUnderscore}[\tmp]%
  99. \ifthenelse{\isempty{#1}}%
  100. {\ExecuteMetaData{AgdaTag-\tmp}}%
  101. {\ExecuteMetaData{#1}{AgdaTag-\tmp}}
  102. }
  103. \fi
  104. \providecommand{\AgdaRef}[2][]{#2}
  105. % ----------------------------------------------------------------------
  106. % Links (only done if the option is passed and the user has loaded the
  107. % hyperref package).
  108. \if@AgdaEnableLinks
  109. \@ifpackageloaded{hyperref}{
  110. % List that holds added targets.
  111. \newcommand{\AgdaList}[0]{}
  112. \newtoggle{AgdaIsElem}
  113. \newcounter{AgdaIndex}
  114. \newcommand{\AgdaLookup}[3]{%
  115. \togglefalse{AgdaIsElem}%
  116. \setcounter{AgdaIndex}{0}%
  117. \renewcommand*{\do}[1]{%
  118. \ifstrequal{#1}{##1}%
  119. {\toggletrue{AgdaIsElem}\listbreak}%
  120. {\stepcounter{AgdaIndex}}}%
  121. \dolistloop{\AgdaList}%
  122. \iftoggle{AgdaIsElem}{#2}{#3}%
  123. }
  124. \newcommand*{\AgdaTargetHelper}[1]{%
  125. \AgdaLookup{#1}%
  126. {\PackageError{agda}{``#1'' used as target more than once}%
  127. {Overloaded identifiers and links do not%
  128. work well, consider using unique%
  129. \MessageBreak identifiers instead.}%
  130. }%
  131. {\listadd{\AgdaList}{#1}%
  132. \hypertarget{Agda\theAgdaIndex}{}%
  133. }%
  134. }
  135. \newcommand{\AgdaTarget}[1]{\forcsvlist{\AgdaTargetHelper}{#1}}
  136. \newcommand{\AgdaLink}[1]{%
  137. \AgdaLookup{#1}%
  138. {\hyperlink{Agda\theAgdaIndex}{#1}}%
  139. {#1}%
  140. }
  141. }{\PackageError{agda}{Load the hyperref package before the agda package}{}}
  142. \fi
  143. \providecommand{\AgdaTarget}[1]{}
  144. \providecommand{\AgdaLink}[1]{#1}
  145. % ----------------------------------------------------------------------
  146. % Font styles.
  147. \ifxetexorluatex
  148. \newcommand{\AgdaFontStyle}[1]{\ensuremath{\mathsf{#1}}}
  149. \ifthenelse{\equal{\AgdaColourScheme}{bw}}{
  150. \newcommand{\AgdaKeywordFontStyle}[1]{\underline{#1}}
  151. }{
  152. \newcommand{\AgdaKeywordFontStyle}[1]{\ensuremath{\mathsf{#1}}}
  153. }
  154. \newcommand{\AgdaStringFontStyle}[1]{\ensuremath{\texttt{#1}}}
  155. \newcommand{\AgdaCommentFontStyle}[1]{\ensuremath{\texttt{#1}}}
  156. \newcommand{\AgdaBoundFontStyle}[1]{\ensuremath{\mathit{#1}}}
  157. \else
  158. \newcommand{\AgdaFontStyle}[1]{\textsf{#1}}
  159. \ifthenelse{\equal{\AgdaColourScheme}{bw}}{
  160. \newcommand{\AgdaKeywordFontStyle}[1]{\underline{#1}}
  161. }{
  162. \newcommand{\AgdaKeywordFontStyle}[1]{\textsf{#1}}
  163. }
  164. \newcommand{\AgdaStringFontStyle}[1]{\texttt{#1}}
  165. \newcommand{\AgdaCommentFontStyle}[1]{\texttt{#1}}
  166. \newcommand{\AgdaBoundFontStyle}[1]{\textit{#1}}
  167. \fi
  168. % ----------------------------------------------------------------------
  169. % Colours.
  170. % ----------------------------------
  171. % The black and white colour scheme.
  172. \ifthenelse{\equal{\AgdaColourScheme}{bw}}{
  173. % Aspect colours.
  174. \definecolor{AgdaComment} {HTML}{000000}
  175. \definecolor{AgdaOption} {HTML}{000000}
  176. \definecolor{AgdaKeyword} {HTML}{000000}
  177. \definecolor{AgdaString} {HTML}{000000}
  178. \definecolor{AgdaNumber} {HTML}{000000}
  179. \definecolor{AgdaSymbol} {HTML}{000000}
  180. \definecolor{AgdaPrimitiveType}{HTML}{000000}
  181. % NameKind colours.
  182. \definecolor{AgdaBound} {HTML}{000000}
  183. \definecolor{AgdaInductiveConstructor} {HTML}{000000}
  184. \definecolor{AgdaCoinductiveConstructor}{HTML}{000000}
  185. \definecolor{AgdaDatatype} {HTML}{000000}
  186. \definecolor{AgdaField} {HTML}{000000}
  187. \definecolor{AgdaFunction} {HTML}{000000}
  188. \definecolor{AgdaMacro} {HTML}{000000}
  189. \definecolor{AgdaModule} {HTML}{000000}
  190. \definecolor{AgdaPostulate} {HTML}{000000}
  191. \definecolor{AgdaPrimitive} {HTML}{000000}
  192. \definecolor{AgdaRecord} {HTML}{000000}
  193. \definecolor{AgdaArgument} {HTML}{000000}
  194. % Other aspect colours.
  195. \definecolor{AgdaDottedPattern} {HTML}{000000}
  196. \definecolor{AgdaUnsolvedMeta} {HTML}{D3D3D3}
  197. \definecolor{AgdaTerminationProblem}{HTML}{BEBEBE}
  198. \definecolor{AgdaIncompletePattern} {HTML}{D3D3D3}
  199. \definecolor{AgdaError} {HTML}{696969}
  200. % Misc.
  201. \definecolor{AgdaHole} {HTML}{BEBEBE}
  202. % ----------------------------------
  203. % Conor McBride's colour scheme.
  204. }{ \ifthenelse{\equal{\AgdaColourScheme}{conor}}{
  205. % Aspect colours.
  206. \definecolor{AgdaComment} {HTML}{B22222}
  207. \definecolor{AgdaOption} {HTML}{000000}
  208. \definecolor{AgdaKeyword} {HTML}{000000}
  209. \definecolor{AgdaString} {HTML}{000000}
  210. \definecolor{AgdaNumber} {HTML}{000000}
  211. \definecolor{AgdaSymbol} {HTML}{000000}
  212. \definecolor{AgdaPrimitiveType}{HTML}{0000CD}
  213. % NameKind colours.
  214. \definecolor{AgdaBound} {HTML}{A020F0}
  215. \definecolor{AgdaInductiveConstructor} {HTML}{8B0000}
  216. \definecolor{AgdaCoinductiveConstructor}{HTML}{8B0000}
  217. \definecolor{AgdaDatatype} {HTML}{0000CD}
  218. \definecolor{AgdaField} {HTML}{8B0000}
  219. \definecolor{AgdaFunction} {HTML}{006400}
  220. \definecolor{AgdaMacro} {HTML}{006400}
  221. \definecolor{AgdaModule} {HTML}{006400}
  222. \definecolor{AgdaPostulate} {HTML}{006400}
  223. \definecolor{AgdaPrimitive} {HTML}{006400}
  224. \definecolor{AgdaRecord} {HTML}{0000CD}
  225. \definecolor{AgdaArgument} {HTML}{404040}
  226. % Other aspect colours.
  227. \definecolor{AgdaDottedPattern} {HTML}{000000}
  228. \definecolor{AgdaUnsolvedMeta} {HTML}{FFD700}
  229. \definecolor{AgdaTerminationProblem}{HTML}{FF0000}
  230. \definecolor{AgdaIncompletePattern} {HTML}{A020F0}
  231. \definecolor{AgdaError} {HTML}{F4A460}
  232. % Misc.
  233. \definecolor{AgdaHole} {HTML}{9DFF9D}
  234. % ----------------------------------
  235. % The standard colour scheme.
  236. }{
  237. % Aspect colours.
  238. \definecolor{AgdaComment} {HTML}{B22222}
  239. \definecolor{AgdaOption} {HTML}{000000}
  240. \definecolor{AgdaKeyword} {HTML}{CD6600}
  241. \definecolor{AgdaString} {HTML}{B22222}
  242. \definecolor{AgdaNumber} {HTML}{A020F0}
  243. \definecolor{AgdaSymbol} {HTML}{404040}
  244. \definecolor{AgdaPrimitiveType}{HTML}{0000CD}
  245. % NameKind colours.
  246. \definecolor{AgdaBound} {HTML}{000000}
  247. \definecolor{AgdaInductiveConstructor} {HTML}{008B00}
  248. \definecolor{AgdaCoinductiveConstructor}{HTML}{8B7500}
  249. \definecolor{AgdaDatatype} {HTML}{0000CD}
  250. \definecolor{AgdaField} {HTML}{EE1289}
  251. \definecolor{AgdaFunction} {HTML}{0000CD}
  252. \definecolor{AgdaMacro} {HTML}{458B74}
  253. \definecolor{AgdaModule} {HTML}{A020F0}
  254. \definecolor{AgdaPostulate} {HTML}{0000CD}
  255. \definecolor{AgdaPrimitive} {HTML}{0000CD}
  256. \definecolor{AgdaRecord} {HTML}{0000CD}
  257. \definecolor{AgdaArgument} {HTML}{404040}
  258. % Other aspect colours.
  259. \definecolor{AgdaDottedPattern} {HTML}{000000}
  260. \definecolor{AgdaUnsolvedMeta} {HTML}{FFFF00}
  261. \definecolor{AgdaTerminationProblem}{HTML}{FFA07A}
  262. \definecolor{AgdaIncompletePattern} {HTML}{F5DEB3}
  263. \definecolor{AgdaError} {HTML}{FF0000}
  264. % Misc.
  265. \definecolor{AgdaHole} {HTML}{9DFF9D}
  266. }}
  267. % ----------------------------------------------------------------------
  268. % Commands.
  269. \newcommand{\AgdaNoSpaceMath}[1]
  270. {\begingroup\thickmuskip=0mu\medmuskip=0mu#1\endgroup}
  271. % Aspect commands.
  272. \newcommand{\AgdaComment} [1]
  273. {\AgdaNoSpaceMath{\AgdaCommentFontStyle{\textcolor{AgdaComment}{#1}}}}
  274. \newcommand{\AgdaOption} [1]
  275. {\AgdaNoSpaceMath{\AgdaCommentFontStyle{\textcolor{AgdaOption}{#1}}}}
  276. \newcommand{\AgdaKeyword} [1]
  277. {\AgdaNoSpaceMath{\AgdaKeywordFontStyle{\textcolor{AgdaKeyword}{#1}}}}
  278. \newcommand{\AgdaString} [1]
  279. {\AgdaNoSpaceMath{\AgdaStringFontStyle{\textcolor{AgdaString}{#1}}}}
  280. \newcommand{\AgdaNumber} [1]
  281. {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaNumber}{#1}}}}
  282. \newcommand{\AgdaSymbol} [1]
  283. {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaSymbol}{#1}}}}
  284. \newcommand{\AgdaPrimitiveType}[1]
  285. {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaPrimitiveType}{#1}}}}
  286. % Note that, in code generated by the LaTeX backend, \AgdaOperator is
  287. % always applied to a NameKind command.
  288. \newcommand{\AgdaOperator} [1]{#1}
  289. % NameKind commands.
  290. % The user can control the typesetting of (certain) individual tokens
  291. % by redefining the following command. The first argument is the token
  292. % and the second argument the thing to be typeset (sometimes just the
  293. % token, sometimes \AgdaLink{<the token>}). Example:
  294. %
  295. % \usepackage{ifthen}
  296. %
  297. % % Insert extra space before some tokens.
  298. % \DeclareRobustCommand{\AgdaFormat}[2]{%
  299. % \ifthenelse{
  300. % \equal{#1}{≡⟨} \OR
  301. % \equal{#1}{≡⟨⟩} \OR
  302. % \equal{#1}{∎}
  303. % }{\ }{}#2}
  304. %
  305. % Note the use of \DeclareRobustCommand.
  306. \newcommand{\AgdaFormat}[2]{#2}
  307. \newcommand{\AgdaBound}[1]
  308. {\AgdaNoSpaceMath{\AgdaBoundFontStyle{\textcolor{AgdaBound}{\AgdaFormat{#1}{#1}}}}}
  309. \newcommand{\AgdaInductiveConstructor}[1]
  310. {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaInductiveConstructor}{\AgdaFormat{#1}{\AgdaLink{#1}}}}}}
  311. \newcommand{\AgdaCoinductiveConstructor}[1]
  312. {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaCoinductiveConstructor}{\AgdaFormat{#1}{\AgdaLink{#1}}}}}}
  313. \newcommand{\AgdaDatatype}[1]
  314. {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaDatatype}{\AgdaFormat{#1}{\AgdaLink{#1}}}}}}
  315. \newcommand{\AgdaField}[1]
  316. {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaField}{\AgdaFormat{#1}{\AgdaLink{#1}}}}}}
  317. \newcommand{\AgdaFunction}[1]
  318. {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaFunction}{\AgdaFormat{#1}{\AgdaLink{#1}}}}}}
  319. \newcommand{\AgdaMacro}[1]
  320. {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaMacro}{\AgdaFormat{#1}{\AgdaLink{#1}}}}}}
  321. \newcommand{\AgdaModule}[1]
  322. {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaModule}{\AgdaFormat{#1}{\AgdaLink{#1}}}}}}
  323. \newcommand{\AgdaPostulate}[1]
  324. {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaPostulate}{\AgdaFormat{#1}{\AgdaLink{#1}}}}}}
  325. \newcommand{\AgdaPrimitive}[1]
  326. {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaPrimitive}{\AgdaFormat{#1}{#1}}}}}
  327. \newcommand{\AgdaRecord}[1]
  328. {\AgdaNoSpaceMath{\AgdaFontStyle{\textcolor{AgdaRecord}{\AgdaFormat{#1}{\AgdaLink{#1}}}}}}
  329. \newcommand{\AgdaArgument}[1]
  330. {\AgdaNoSpaceMath{\AgdaBoundFontStyle{\textcolor{AgdaArgument}{\AgdaFormat{#1}{#1}}}}}
  331. % Other aspect commands.
  332. \newcommand{\AgdaFixityOp} [1]{\AgdaNoSpaceMath{$#1$}}
  333. \newcommand{\AgdaDottedPattern} [1]{\textcolor{AgdaDottedPattern}{#1}}
  334. \newcommand{\AgdaUnsolvedMeta} [1]
  335. {\AgdaFontStyle{\colorbox{AgdaUnsolvedMeta}{#1}}}
  336. \newcommand{\AgdaTerminationProblem}[1]
  337. {\AgdaFontStyle{\colorbox{AgdaTerminationProblem}{#1}}}
  338. \newcommand{\AgdaIncompletePattern} [1]{\colorbox{AgdaIncompletePattern}{#1}}
  339. \newcommand{\AgdaError} [1]
  340. {\AgdaFontStyle{\textcolor{AgdaError}{\underline{#1}}}}
  341. \newcommand{\AgdaCatchallClause} [1]{#1} % feel free to change this
  342. % Used to hide code from LaTeX.
  343. %
  344. % Note that this macro has been deprecated in favour of giving the
  345. % hide argument to the code environment.
  346. \long\def\AgdaHide#1{\ignorespaces}
  347. % Misc.
  348. \newcommand{\AgdaHole}[1]{\colorbox{AgdaHole}{#1}}
  349. % ----------------------------------------------------------------------
  350. % The code environment.
  351. \newcommand{\AgdaCodeStyle}{}
  352. % \newcommand{\AgdaCodeStyle}{\tiny}
  353. \ifdefined\mathindent
  354. {}
  355. \else
  356. \newdimen\mathindent\mathindent\leftmargini
  357. \fi
  358. % Adds the given amount of vertical space and starts a new line.
  359. %
  360. % The implementation comes from lhs2TeX's polycode.fmt, written by
  361. % Andres Löh.
  362. \newcommand{\Agda@NewlineWithVerticalSpace}[1]{%
  363. {\parskip=0pt\parindent=0pt\par\vskip #1\noindent}}
  364. % Should there be space around code?
  365. \newboolean{Agda@SpaceAroundCode}
  366. % Use this command to avoid extra space around code blocks.
  367. \newcommand{\AgdaNoSpaceAroundCode}{%
  368. \setboolean{Agda@SpaceAroundCode}{false}}
  369. % Use this command to include extra space around code blocks.
  370. \newcommand{\AgdaSpaceAroundCode}{%
  371. \setboolean{Agda@SpaceAroundCode}{true}}
  372. % By default space is inserted around code blocks.
  373. \AgdaSpaceAroundCode{}
  374. % Sometimes one might want to break up a code block into multiple
  375. % pieces, but keep code in different blocks aligned with respect to
  376. % each other. Then one can use the AgdaAlign environment. Example
  377. % usage:
  378. %
  379. % \begin{AgdaAlign}
  380. % \begin{code}
  381. % code
  382. % code (more code)
  383. % \end{code}
  384. % Explanation...
  385. % \begin{code}
  386. % aligned with "code"
  387. % code (aligned with (more code))
  388. % \end{code}
  389. % \end{AgdaAlign}
  390. %
  391. % Note that AgdaAlign environments should not be nested.
  392. %
  393. % Sometimes one might also want to hide code in the middle of a code
  394. % block. This can be accomplished in the following way:
  395. %
  396. % \begin{AgdaAlign}
  397. % \begin{code}
  398. % visible
  399. % \end{code}
  400. % \begin{code}[hide]
  401. % hidden
  402. % \end{code}
  403. % \begin{code}
  404. % visible
  405. % \end{code}
  406. % \end{AgdaAlign}
  407. %
  408. % However, the result may be ugly: extra space is perhaps inserted
  409. % around the code blocks.
  410. %
  411. % The AgdaSuppressSpace environment ensures that extra space is only
  412. % inserted before the first code block, and after the last one (but
  413. % not if \AgdaNoSpaceAroundCode{} is used). Example usage:
  414. %
  415. % \begin{AgdaAlign}
  416. % \begin{code}
  417. % code
  418. % more code
  419. % \end{code}
  420. % Explanation...
  421. % \begin{AgdaSuppressSpace}
  422. % \begin{code}
  423. % aligned with "code"
  424. % aligned with "more code"
  425. % \end{code}
  426. % \begin{code}[hide]
  427. % hidden code
  428. % \end{code}
  429. % \begin{code}
  430. % also aligned with "more code"
  431. % \end{code}
  432. % \end{AgdaSuppressSpace}
  433. % \end{AgdaAlign}
  434. %
  435. % Note that AgdaSuppressSpace environments should not be nested.
  436. %
  437. % There is also a combined environment, AgdaMultiCode, that combines
  438. % the effects of AgdaAlign and AgdaSuppressSpace.
  439. % The number of the current/next code block (excluding hidden ones).
  440. \newcounter{Agda@Current}
  441. \setcounter{Agda@Current}{0}
  442. % The number of the previous code block (excluding hidden ones), used
  443. % locally in \Agda@SuppressEnd.
  444. \newcounter{Agda@Previous}
  445. % Is AgdaAlign active?
  446. \newboolean{Agda@Align}
  447. \setboolean{Agda@Align}{false}
  448. % The number of the first code block (if any) in a given AgdaAlign
  449. % environment.
  450. \newcounter{Agda@AlignStart}
  451. \newcommand{\Agda@AlignStart}{%
  452. \ifthenelse{\boolean{Agda@Align}}{%
  453. \PackageError{agda}{Nested AgdaAlign environments}{%
  454. AgdaAlign and AgdaMultiCode environments must not be
  455. nested.}}{%
  456. \setboolean{Agda@Align}{true}%
  457. \setcounter{Agda@AlignStart}{\value{Agda@Current}}}}
  458. \newcommand{\Agda@AlignEnd}{\setboolean{Agda@Align}{false}}
  459. \newenvironment{AgdaAlign}{%
  460. \Agda@AlignStart{}}{%
  461. \Agda@AlignEnd{}%
  462. \ignorespacesafterend}
  463. % Is AgdaSuppressSpace active?
  464. \newboolean{Agda@Suppress}
  465. \setboolean{Agda@Suppress}{false}
  466. % The number of the first code block (if any) in a given
  467. % AgdaSuppressSpace environment.
  468. \newcounter{Agda@SuppressStart}
  469. % Does a "do not suppress space after" label exist for the current
  470. % code block? (This boolean is used locally in the code environment's
  471. % implementation.)
  472. \newboolean{Agda@DoNotSuppressSpaceAfter}
  473. \newcommand{\Agda@SuppressStart}{%
  474. \ifthenelse{\boolean{Agda@Suppress}}{%
  475. \PackageError{agda}{Nested AgdaSuppressSpace environments}{%
  476. AgdaSuppressSpace and AgdaMultiCode environments must not be
  477. nested.}}{%
  478. \setboolean{Agda@Suppress}{true}%
  479. \setcounter{Agda@SuppressStart}{\value{Agda@Current}}}}
  480. % Marks the given code block as one that space should not be
  481. % suppressed after (if AgdaSpaceAroundCode and AgdaSuppressSpace are
  482. % both active).
  483. \newcommand{\Agda@DoNotSuppressSpaceAfter}[1]{%
  484. % The use of labels is intended to ensure that LaTeX will provide a
  485. % warning if the document needs to be recompiled.
  486. \label{Agda@DoNotSuppressSpaceAfter@#1}}
  487. \newcommand{\Agda@SuppressEnd}{%
  488. \ifthenelse{\value{Agda@SuppressStart} = \value{Agda@Current}}{}{%
  489. % Mark the previous code block in the .aux file.
  490. \setcounter{Agda@Previous}{\theAgda@Current-1}%
  491. \immediate\write\@auxout{%
  492. \noexpand\Agda@DoNotSuppressSpaceAfter{\theAgda@Previous}}}%
  493. \setboolean{Agda@Suppress}{false}}
  494. \newenvironment{AgdaSuppressSpace}{%
  495. \Agda@SuppressStart{}}{%
  496. \Agda@SuppressEnd{}%
  497. \ignorespacesafterend}
  498. \newenvironment{AgdaMultiCode}{%
  499. \Agda@AlignStart{}%
  500. \Agda@SuppressStart{}}{%
  501. \Agda@SuppressEnd{}%
  502. \Agda@AlignEnd{}%
  503. \ignorespacesafterend}
  504. % Vertical space used for empty lines. By default \baselineskip.
  505. \newlength{\AgdaEmptySkip}
  506. \setlength{\AgdaEmptySkip}{\baselineskip}
  507. % Extra space to be inserted for empty lines (the difference between
  508. % \AgdaEmptySkip and \baselineskip). Used internally.
  509. \newlength{\AgdaEmptyExtraSkip}
  510. % The code environment.
  511. %
  512. % Code can be hidden by writing \begin{code}[hide].
  513. %
  514. % The implementation is based on plainhscode in lhs2TeX's
  515. % polycode.fmt, written by Andres Löh.
  516. \NewEnviron{code}[1][]{%
  517. % Conditionally hide the code.
  518. \ifthenelse{\equal{#1}{hide}}{}{%
  519. %
  520. % Conditionally emit space before the code block. Unconditionally
  521. % switch to a new line.
  522. \ifthenelse{\boolean{Agda@SpaceAroundCode} \and%
  523. \(\not \boolean{Agda@Suppress} \or%
  524. \value{Agda@SuppressStart} = \value{Agda@Current}\)}{%
  525. \Agda@NewlineWithVerticalSpace{\abovedisplayskip}}{%
  526. \Agda@NewlineWithVerticalSpace{0pt}}%
  527. %
  528. % Indent the entire code block.
  529. \advance\leftskip\mathindent%
  530. %
  531. % The code's style can be customised.
  532. \AgdaCodeStyle%
  533. %
  534. % Used to control the height of empty lines.
  535. \setlength{\AgdaEmptyExtraSkip}{\AgdaEmptySkip - \baselineskip}%
  536. %
  537. % The environment used to handle indentation (of individual lines)
  538. % and alignment.
  539. \begin{pboxed}%
  540. %
  541. % Conditionally preserve alignment between code blocks.
  542. \ifthenelse{\boolean{Agda@Align}}{%
  543. \ifthenelse{\value{Agda@AlignStart} = \value{Agda@Current}}{%
  544. \savecolumns}{%
  545. \restorecolumns}}{}%
  546. %
  547. % The code.
  548. \BODY%
  549. \end{pboxed}%
  550. %
  551. % Does the label Agda@DoNotSuppressAfter@<current code block
  552. % number> exist?
  553. \ifcsdef{r@Agda@DoNotSuppressSpaceAfter@\theAgda@Current}{%
  554. \setboolean{Agda@DoNotSuppressSpaceAfter}{true}}{%
  555. \setboolean{Agda@DoNotSuppressSpaceAfter}{false}}%
  556. %
  557. % Conditionally emit space after the code block. Unconditionally
  558. % switch to a new line.
  559. \ifthenelse{\boolean{Agda@SpaceAroundCode} \and%
  560. \(\not \boolean{Agda@Suppress} \or%
  561. \boolean{Agda@DoNotSuppressSpaceAfter}\)}{%
  562. \Agda@NewlineWithVerticalSpace{\belowdisplayskip}}{%
  563. \Agda@NewlineWithVerticalSpace{0pt}}%
  564. %
  565. % Step the code block counter, but only for non-hidden code.
  566. \stepcounter{Agda@Current}}}
  567. % Space inserted after tokens.
  568. \newcommand{\AgdaSpace}{ }
  569. % Space inserted to indent something.
  570. \newcommand{\AgdaIndentSpace}{\AgdaSpace{}$\;\;$}
  571. % Default column for polytable.
  572. \defaultcolumn{@{}l@{\AgdaSpace{}}}
  573. % \AgdaIndent expects a non-negative integer as its only argument.
  574. % This integer should be the distance, in code blocks, to the thing
  575. % relative to which the text is indented.
  576. %
  577. % The default implementation only indents if the thing that the text
  578. % is indented relative to exists in the same code block or is wrapped
  579. % in the same AgdaAlign or AgdaMultiCode environment.
  580. \newcommand{\AgdaIndent}[1]{%
  581. \ifthenelse{#1 = 0
  582. \or
  583. \( \boolean{Agda@Align}
  584. \and
  585. \cnttest{\value{Agda@Current} - #1}{>=}{
  586. \value{Agda@AlignStart}}
  587. \)}{\AgdaIndentSpace{}}{}}
  588. % Underscores are typeset using \AgdaUnderscore{}.
  589. \newcommand{\AgdaUnderscore}{\_}
  590. \endinput