Say, you have your own little website, which you made as a hobby. And say you wrote a PhD thesis in LaTeX. Would it not be nice to also publish it on LaTeX? How hard can it be?
As it turns out, not that hard actually. Because of the wonderful tool called pandoc. Pandoc converts many document types into other document types. And since this website is made using Hugo, which uses markdown files, I can simply convert the LaTeX files to markdown using pandoc, and paste them into my website.
There are some bumps on the road though. And I thought I would share them, just for myself, and who knows who else this might be useful for. Do note that some of the instructions below are quite Hugo-specific, so just skip those if you don't want them.
Before you start, install pandoc!
Start by running:
pandoc -t markdown-citations -s main.tex -o ./md/thesis.md --bibliography thesislib-manual.bib --citeproc --csl https://raw.githubusercontent.com/citation-style-language/styles/master/nature.csl
The .csl
file I put in there determines what citations will look like. You can pick any if them here.
The -t
argument makes sure there are no markdown-style citations, because Hugo does not support those .
This is a good start, and really does the vast majority of the heavy lifting.
Now there is quite a bit of stuff that still needs (semi-)manual fixing. You will need a text editor with regex find-and-replace support, like vscode or Notepad++. The rest of the steps I took. It is messy, but should be clear I think (?) - so good luck!
^X^
(where x is a number) to the following format: \\(^X\\)
. This is inline KaTex math. Maybe a link to the separate ref page later on?csl
tag: find and replace \{#ref-.+\}
(regex!) with whitespace.{.csl-left-margin}
and {.csl-right-inline}
tags with whitespace.(\[)([0-9]+. )(\])
, replace with $2
, which is the second group in the search pattern.([0-9]\. )\[
with $1
.\.\]
with .
.\-`{=latex}
with whitespace.\enskip`{=latex}
with whitespace\[([\S\s]+)\]\{.nocase\}
with $1
#####
) on the reference page with a named anchor ( {#name}
after entry). Then you can refer in text to the correct ref. To do that, you first need to meake sure the entire reference is on 1 line (not split over multiple). To enforce that: run find+replace (with regex): ([0-9]+\. .+)\n(.+)
into $1 $2
, run it a few times until no more matches are found and done! To add the heading and anchor: find+replace (with regex): ([0-9]+)(\.) (.+)
into $1$2 ##### $3 {#$1}
.{{< ref "document.md#anchor" >}}
needs to be added. So a link will look like: Blahblah[\\(^1\\)]({{< ref "refs.md#1" >}})
. So, the find and replace will be: \^([0-9]+)\^
, into [\\\\(^{$1}\\\\)]({{< ref "refs.md#$1" >}})
. However, this does NOT take into account references like 12-15
or 12,23,43
. Shit. How to convert that. Ok so these options:\^([0-9]+)\^
, into [\\\\(^{$1}\\\\)]({{< ref "refs.md#$1" >}})
\^([0-9]+),([0-9]+)\^
into: [\\\\(^{$1}\\\\)]({{< ref "refs.md#$1" >}})[\\\\(^{,$2}\\\\)]({{< ref "refs.md#$2" >}})
\^([0-9]+),([0-9]+),([0-9]+)\^
into: [\\\\(^{$1}\\\\)]({{< ref "refs.md#$1" >}})[\\\\(^{,$2}\\\\)]({{< ref "refs.md#$2" >}})[\\\\(^{,$3}\\\\)]({{< ref "refs.md#$3" >}})
\^([0-9]+)--([0-9]+)\^
into: [\\\\(^{$1}\\\\)]({{< ref "refs.md#$1" >}})[\\\\(^{-$2}\\\\)]({{< ref "refs.md#$2" >}})
\$(.+?)\$
into \\\\($1\\\\)
. But it will need manual intervention for sure.{reference-type=...
) with find+replace (regex): \{(reference-type=)(.+\nreference=)(.+?)\}
, replace with nothing, and \{reference-type="ref" reference=(.+?)\}
with nothing (sometimes broken over line, sometimes not?).\celsius
with °C
, \um
with μm
SI
, num
, especially math-wise to replace.\\\\\(\\SI\{(.+?)\}\{(.+?)\}\\\\\)
into $1 $2
_
, these sometimes need to be escaped, even in equations!*
basically always needs to be escaped in equations!imagemagick
, with a command like convert -density 300 blag.pdf -resize 100% blag.png
, or ghostscript
(easier): gs -dSAFER -r150 -sDEVICE=pngalpha -o new.png original.pdf
(\[)([0-9]+?)(\.)([0-9]+?)(\])(\(#.+?\))
into $4
{reference-type="ref" reference="fig:emergence"}b).
, and needs manual fixing (to other chapters etc.). Look for them with ctrl+f
: {reference-type=
section
and chapter
and manually fix the references: [Chapter 2]({{< ref "2.metcon.md" >}})