Convert a PhD thesis to markdown
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.
How to convert .tex to .md for Hugo
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!
- Replace
^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? - Now because there is no coupling between cite place and the bibliography, the numbering must be kept the same, we just generate the list of entries in the right order and put those on a separate webpage:
- First remove
csl
tag: find and replace\{#ref-.+\}
(regex!) with whitespace. - Find+replace all
{.csl-left-margin}
and{.csl-right-inline}
tags with whitespace. - Fix numbering: Find+replace (regex mode in vscode):
(\[)([0-9]+. )(\])
, replace with$2
, which is the second group in the search pattern. - Remove starting square bracket: find+replace (regex)
([0-9]\. )\[
with$1
. - Remove trailing square bracket: find+replace (regex)
\.\]
with.
. - Remove latex commands, especially the extra linebreak positions manually inserted for names and such:
- find+replace (not regex!):
\-`{=latex}
with whitespace. - find+replace (not regex!):
\enskip`{=latex}
with whitespace - find+replace (regex!):
\[([\S\s]+)\]\{.nocase\}
with$1
- That should fix 99% of problems, other problems are fixed by just manually going through when rendered in Hugo.
- You could make every entry a low-level heading (
#####
) 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}
.
- Now, In all the other files, each ref needs to become a link with anchor. So
{{< 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 like12-15
or12,23,43
. Shit. How to convert that. Ok so these options:- 32: from:
\^([0-9]+)\^
, into[\\\\(^{$1}\\\\)]({{< ref "refs.md#$1" >}})
- 32,33: from:
\^([0-9]+),([0-9]+)\^
into:[\\\\(^{$1}\\\\)]({{< ref "refs.md#$1" >}})[\\\\(^{,$2}\\\\)]({{< ref "refs.md#$2" >}})
- 32,33,34: from:
\^([0-9]+),([0-9]+),([0-9]+)\^
into:[\\\\(^{$1}\\\\)]({{< ref "refs.md#$1" >}})[\\\\(^{,$2}\\\\)]({{< ref "refs.md#$2" >}})[\\\\(^{,$3}\\\\)]({{< ref "refs.md#$3" >}})
- 34–36: from:
\^([0-9]+)--([0-9]+)\^
into:[\\\\(^{$1}\\\\)]({{< ref "refs.md#$1" >}})[\\\\(^{-$2}\\\\)]({{< ref "refs.md#$2" >}})
- And the rest must just be done by hand :(
- Replace math:
\$(.+?)\$
into\\\\($1\\\\)
. But it will need manual intervention for sure. - References are weird, interchapter they def need to be fixed manually, remove the latex stuff (
{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?). - Replace some of my custom latex stuff to do with equations:
- Replace
\celsius
with°C
,\um
withμm
- Look for
SI
,num
, especially math-wise to replace.- Try:
\\\\\(\\SI\{(.+?)\}\{(.+?)\}\\\\\)
into$1 $2
- Also smart, look for
_
, these sometimes need to be escaped, even in equations!
*
basically always needs to be escaped in equations!
- Figures:
- Replace with correct links, very little I can do there I am afraid.
- Also, for figure captions, check out this website: https://sebastiandedeyne.com/captioned-images-with-markdown-render-hooks-in-hugo/
- Note that in the caption Katex, links and html tags are mostly unavailable, which is a massive pain, but ok.
- As a sidonote, a pdf file can be converted into an image using using
imagemagick
, with a command likeconvert -density 300 blag.pdf -resize 100% blag.png
, orghostscript
(easier):gs -dSAFER -r150 -sDEVICE=pngalpha -o new.png original.pdf
- References:
- To Figures must be removed, but the number must stay:
- from
(\[)([0-9]+?)(\.)([0-9]+?)(\])(\(#.+?\))
into$4
- Other references look like this:
{reference-type="ref" reference="fig:emergence"}b).
, and needs manual fixing (to other chapters etc.). Look for them withctrl+f
:{reference-type=
- Inter-chapter references can still be made! Just look for
section
andchapter
and manually fix the references:[Chapter 2]({{< ref "2.metcon.md" >}})
- Tables: should be okay, but also, just check them to be sure.
Comments