Advertisement
Guest User

agda.sty

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