Rendering LaTeX in Hakyll
What is Hakyll?
Hakyll is a Haskell library for generating static sites, mostly aimed at small-to-medium sites and personal blogs. It is written in a very configurable way and uses an xmonad-like DSL for configuration.
When I first started putting together this site, I wanted more flexibility for rendering the Markdown posts in HTML. For my particular needs, LaTeX offered everything I wanted to show here. Now, this site as you see it uses Markdown and Hakyll-specific formatting within LaTeX.
Rendering Markdown in Hakyll
As Hakyll is integrated with pandoc, it is capable of rendering Markdown with proper formatting in HTML. For example, to produce header text (which is used as our table of contents when setting toc to true) we can simply use:
# Header Text ## Subheader Text
Similarly, we can produce code blocks with syntax highlighting:
```haskell main :: IO () main = putStrLn "Hello, world!" ```
which produces:
main :: IO () main = putStrLn "Hello, world!"
Compiling to HTML
Using Markdown files on a Hakyll site is as simple as placing them in a directory, say posts, and then compiling them to HTML like so:
match "posts/*.md" $ do
route $ setExtension "html"
compile $ pandocCompiler
>>= relativizeUrls
Rendering LaTeX in Hakyll
While TeX files are already supported by Hakyll as it uses pandoc, the compilation process is a bit different than the case of Markdown. We can use support files with our LaTeX, like class or style files (we can even use a bib file), however, in its simplest form we can start by using a TeX file like so:
\title{Some Title}
\section{Some Section}
Some text.
Special Characters
As we are using LaTeX, that provides us with a simple way of defining mathematical expressions, among other things. Next, we show some examples of how to use native LaTeX commands in Hakyll:
\newtheorem{theorem}{Theorem}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{lemma}[theorem]{Lemma}
\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\theoremstyle{remark}
\newtheorem{remark}{Remark}
which can be used like so:
\begin{definition}[right-angled triangles]
A \emph{right-angled triangle} is a triangle whose sides of
length~\(a\), \(b\) and~\(c\), in some permutation of order,
satisfies \(a^2+b^2=c^2\).
\end{definition}
Similarly, we can define prime numbers and prove the infinitude of primes:
\begin{definition}[Prime numbers]
A \emph{prime number} is a natural number greater than 1 that
has no positive divisors other than 1 and itself.
\end{definition}
\begin{lemma}[Infinitude of primes]
There are infinitely many prime numbers.
\end{lemma}
Compiling to HTML
This is where we need to handle things a bit differently. Using Hakyll's pandoc integration, we can compile our TeX files to HTML like so:
match "*/*.tex" $ do
route $ constRoute "*/index.html"
compile $ getResourceString
>>= withItemBody (unixFilter "pandoc"
["-f", "latex", "-t", "html5", "--mathjax"])
>>= relativizeUrls
