This repository provides a tool to convert literate Agda files written in markdown to LaTeX. A common source can be used to produce both .pdf and .html files. The source can include diagrams written in Tikz, and these will appear in both the produced .pdf and .html. The tool uses LuaTeX (available as part of TeXLive), which has good support for unicode.
Literate Agda may be written either in LaTeX (with extension
.lagda.tex) or markdown (with extension .lagda.md). Including
code blocks in LaTeX is easy while including inline code may be
painful, requiring LaTeX macros for special characters or special
unicode declarations. In contrast, in markdown both code blocks and
inline code are easy.
The repository also provides a monospace font assembled from several open source fonts that provides a wide range of monospaced unicode characters, in particular, all those that are used in the textbook PLFA.
The subdirectory examples includes examples of how to typeset an
academic paper for both
JFP
and PACMPL.
The project depends on the following software:
- Agda: for type checking and highlighting literate Agda files.
- Pandoc: for converting files from markdown format to others.
- TeX Live: for LuaTeX, which has good Unicode support.
- pdf2svg: for generating
images from LaTeX code. The linked document has instructions for
Windows and Linux. On a Mac, use
brew install pdf2svgto install the software. - Python: a programming language needed by pandoc-latex-environment
- pandoc-latex-environment: for parsing div blocks in markdown to latex
Under font there is a font file called DejaVu mononoki Symbola Droid. It is a custom combined monospace font that covers a wide
range of Unicode symbols. Install the font globally on your
system. On Mac, you need to first set Font Book - Preferences - Default Install Location to 'Computer' before installing.
- Clone this repo to your local machine
- Copy
makefile,main.tex,main.md,template.html, andstyle.cssto your project. - Near the top of
makefile, set three variables. i.SRCholds the path to the source file directory. ii.FILTERSholds the path topandoc-filtersin this repository. iii.MAINholds the name of the output files. Its value is set tomainby default. The tool produces<MAIN>.pdf(in the root directory) and (in thehtmlsubdirectory)<MAIN>.html.
Each file <FILE>.lagda.md in subdirectory src will be
processed to generate files latex/<FILE>.tex and
html/<FILE>.html. There are two template files in the
root directory, that must be modified to include the appropriate files.
- for
main.tex, use\inputto include each Latex file.\input{latex/<FILE>.tex}
- for
main.md, use the codeblock below to include each html files.```{.include} html/<FILE>.html ```
In the root directory, execute one of the following.
make pdfto generate<MAIN>.pdfin the root directorymake htmlto generate<MAIN>.htmlwith supporting files inhtml/.maketo execute both of the abovemake cleanto remove all log filesmake clean_htmlto remove all generated html filesmake clean_pdfto remove all generated pdf files
Write markdown as usual. Mix in LaTeX by writing `some latex stuff`{=tex}. A more comprehensive list of what is supported can
be found in Markdown references, including
material on Citations.
The project uses pandoc for transpiling, and supports all features of pandoc markdown. The syntax is further extended by some Lua filters in this project. Here are some features that you might find useful:
There are three ways to mix Agda code with other literate parts.
- Code blocks are enclosed in triple backticks.
```
_+_ : ℕ → ℕ → ℕ
zero + n = n
(suc m) + n = suc (m + n)
```
The Agda processor will type check the code and colour it appropriately.
- Displays start with four whitespaces. (In a markdown list, at least eight whitespaces are required.)
0 + n ≡ n
(1 + m) + n ≡ 1 + (m + n)
Display code is not executed by the Agda processor and is not coloured.
- Inline code fragments are surrounded by single backticks.
The constructors for `ℕ` are `zero` and `suc`.
Inline code is not executed by the Agda processor and is not coloured.
For details, see the Agda documentation.
It is possible to write LaTeX mixed with markdown using pandoc tex blocks, which are ignored when generating html. Dually, markdown blocks are ignored when generating tex.
`This will be ignored by HTML`{=tex}
`and this will be ignored by LaTex`{=markdown}
For details, see the raw_tex section in
pandoc markdown.
You could write LaTeX citation commands \cite{} and \shortcite{}
directly in markdown source files. For example,
Applications of parametricity to functional programming are given by Wadler \shortcite{wadler_1989}will be rendered as
Applications of parametricity to functional programming are given by Wadler \shortcite{wadler_1989}in LaTeX and
Applications of parametricity to functional programming are given by Wadler [-@wadler_1989]in the markdown path to HTML.
For HTML to render the citation, specify the location of bib files in the YAML header of the source file:
---
bibliography: references.bib
---
BodyFor LaTeX, use \bibliography{references} in <MAIN>.tex, after
\input and before \end{document}
For HTML, by default pandoc will use a Chicago author-date format for citations and references. To use another style, you will need to specify a CSL 1.0 style file in the csl metadata field. For example:
---
title: "Sample Document"
bibliography: references.bib
csl: journal-of-the-acm.csl
---A primer on creating and modifying CSL styles is here, a repository of CSL styles is here, and an easily browsed list of styles is here.
For LaTeX, in main.tex, specify the name of a .bst file
with the \bibiographystyle command. For example,
\bibliographystyle{ACM-Reference-Format}
Other options could be used, such as
link-citationsto make citations hyperlinked to their corresponding bibliography entries.reference-section-titleto add a section header title before the bibliography.
---
link-citations: true
reference-section-title: References
bibliography: references.bib
---A list of header options appears on the pandoc citeproc man page.
Parts of this section are adapted from the citation section of the R markdown documentation.
You may define your own LaTeX blocks by writing at least 3 consecutive colons plus some attributes. The attributes will become HTML classes and ids when generating HTML, and LaTeX commands if you define a mapping in your YAML header. This feature is supported by pandoc-latex-environment.
---
pandoc-latex-environment:
center: [center]
---
:::{.center}
some content
:::will be rendered in LaTeX as
\begin{center}
some content
\end{center}and in HTML as
<div class="center">
some content
</div>Draw diagrams using the LaTeX package tikz-cd. For HTML the diagrams
will be rendered as SVG files under html/tikz-diagrams.
```{=latex}
\begin{tikzcd}[column sep=5em,row sep=tiny]
\blue{P~a} \arrow[r, "a≐b P" WildStrawberry] \arrow[dddd, "R a"' WildStrawberry]
& \blue{P~b} \arrow[dddd, "R~b" WildStrawberry]
\\~\\~\\~\\
\blue{P'~a} \arrow[r, "a≐b P'" WildStrawberry]
& \blue{P'~b}
\end{tikzcd}
```If you want to include your own LaTeX commands or packages, see the explanation of Pandoc Lua filters.
The important lines are:
\usepackage{fontspec}
\setmonofont[Scale=0.9]{DejaVu mononoki Symbola Droid}and
\newfontfamily{\AgdaSerifFont}{Dejavu Serif}
\newfontfamily{\AgdaSansSerifFont}{DejaVu Sans}
\newfontfamily{\AgdaTypewriterFont}{DejaVu mononoki Symbola Droid}
\renewcommand{\AgdaFontStyle}[1]{{\AgdaSansSerifFont{}#1}}
\renewcommand{\AgdaKeywordFontStyle}[1]{{\AgdaSansSerifFont{}#1}}
\renewcommand{\AgdaStringFontStyle}[1]{{\AgdaTypewriterFont{}#1}}
\renewcommand{\AgdaCommentFontStyle}[1]{{\AgdaTypewriterFont{}#1}}
\renewcommand{\AgdaBoundFontStyle}[1]{\textit{\AgdaSansSerifFont{}#1}They set up the correct fonts, which are crucial for rendering Unicode symbols.
Include header CSS in main.md header using:
---
header-includes: |
<style>
.center{
text-align:center;
}
</style>
---Use together with fenced div blocks for styling HTML pages.
Pandoc enables most of the document transformation. It parses source files into pandoc abstract syntax trees (ASTs) and writes them to another format. Pandoc filters allow the ASTs to be manipulated between the parsing and the writing phase.
The tool provides a collection of filters under pandoc-filters/. For example:
tikz.luatransforms raw_latex blocks of tikz-cd into svg images. Adapted from pandoc lua-filter, this filter transforms tikz-cd commands in LaTeX to SVG images. It does this by rendering the code to pdf first, then usingpdf2svgfor outputting SVG. Changing the local variabletikz_doc_templatewill affect the look of the images.include-files.luasupports```{.include}blocks.rewrite-html-links.luarewrites links from included files. Pandoc processes file<file>.mdto produce<file>.html. Within the latter, crosslinks have the form<file>.html#12345, and the Lau code rewrites these to<main>.html#12345so they work correctly after the produced html is included into<main>.html.codeblocks.luatranslates lagda markdown with three ticks to LaTeX{code}enviroments.citations-tex.luatransforms LaTeX citations in markdown, so they can be later translated to HTML
- improve
tikz.luato be more flexible (use preambles in markdown?) - better dependency management and incorporation with pandoc and Agda