Original post is here: eklausmeier.goip.de
1. Problem statement. You have a Bachelor Thesis in LaTeX. This thesis is converted to Markdown. I had written on a similar topic here: Converting Journal Article from LaTeX to Markdown.
2. Solution. I already knew that a Pandoc approach does not work. For the conversion I modified the two Perl scripts used for the journal article conversion:
The result is in:
Using those two script, creating the Markdown file goes like this:
1blogOnlineDialARide einleitung.tex chapter1.tex chapter2.tex chapter3.tex > .../2020/10-15-online-dial-a-ride.md
2blogbibtex thesis.bib >> .../2020/10-15-online-dial-a-ride.md
The file 10-15-online-dial-a-ride.md
still needs some manual editing:
- move the table-of-content to the top, as this is appended at the end with
blogbibtex
- insert an image for the algorithm
3. blogOnlineDialARide
script. Some notes on this Perl script. The input to this script is the concatenation of all relevant LaTeX files.
First define some variables and use strict mode.
1use strict;
2my ($ignore,$inTable,$inAlgo,
3 $chapterCnt,$sectionCnt,$subSectionCnt,$theoremCnt,$itemCnt,
4 $claimCnt,$eqnCnt,$eqnFlag,$tableCnt,$tabInsert,$caseCnt,
5 $enumerate,$prefix) = (0,0,0, 0,0,0,0,0, 0,0,0,0,0,0, 0,"");
6my (@sections) = ();
7my (%H,%Hphp) = ( (), () ); # hash for key=\label, value=\ref, in our case for lemmatas and theorems
The frontmatter header is a simple here-document. Also, it defines some PHP variables, which are needed as the thesis makes some forward references to tables, and lemmas, and we want a single pass over the document file only.
1print <<'EOF';
2---
3date: "2020-10-15 14:00:00"
4title: "Online Dial-A-Ride"
5description: "We consider the online Dial-a-Ride Problem where objects are to be transported between points in a metric space in the shortest possible completion..."
6MathJax: true
7categories: ["mathematics"]
8tags: ["ABORT-OR-REPLAN", "Dial-A-Ride", "online optimization"]
9author: "Roman Edenhofer"
10---
11
12
13<!-- https://docs.mathjax.org/en/latest/input/tex/eqnumbers.html -->
14<script type="text/javascript">
15 window.MathJax = { tex: { tags: 'ams' } };
16</script>
17
18<?php // forward references in text
19 $tab__ABORT = "1";
20 $tab__AAW = "2";
21 $tab__state_of_the_art = "3";
22 $lemma__new_extreme = "3.11";
23 $lemma__waiting = "3.12";
24 $lemma__aborting = "3.13";
25 $lemma__abc = "3.14";
26 $lemma__unique_tour = "3.15";
27 $lemma__upwards = "!unknown!";
28?>
29
30EOF
The main loop looks at each line in main.tex
. After the loop the literature section is added, then all sections collected so far are printed.
1while (<>) {
2 chomp;
3 if (/\\end\{tabular\}/) { $ignore = 0; next; }
4 next if ($ignore);
5
6 next if (/\\addcontentsline\{toc\}/);
7 (...)
8 print $prefix . $_ . "\n";
9}
10
11
12print "## Literature<a id=Literature></a>\n";
13for (@sections) {
14 print $_ . "\n";
15}
16++$sectionCnt;
17print "- [$sectionCnt. Literature](#Literature)\n";
What follows is the part which is marked as (...)
in above code. First of all, just drop irrelevant space.
1 # Space handling
2 s/\s+$//g; # rtrim
3 s/^\s+//g; # ltrim, i.e., erase leading space
4 s/\~/ /g;
5
6 s/\s+%\s+[^%].+$//; # Drop LaTeX comments
7 s/^%.*//g;
The tables are replaces by manually entered Markdown tables:
1 s/\\normalsize//;
2 if (/\\end\{table\}/) {
3 print $table[$tabInsert++];
4 $inTable = 0;
5 next;
6 }
7 if ($inTable) {
8 s/\\caption\{([^\}]+)\}/\n\n__Table $tableCnt:__ $1\n/;
9 }
10 if (/\\begin\{table\}/) {
11 ($ignore,$inTable) = (1,1);
12 next;
13 }
The @table
array is initialized at the top of the Perl file like this:
1my @table = (
2'
3Case | ABORT | open | closed
4--------|----------------------------|------|---------
5general | uncapacitated ($c=\infty$) | 3 | 2.5
6general | preemptive | 3 | 2.5
7
8',
9'
10Case | ABORT-AND-WAIT | open | closed
11--------|----------------------------|--------|---------
12general | uncapacitated ($c=\infty$) | 2.4142 | 2.5
13general | preemptive | 2.4142 | 2.5
14
15',
16'
17Case | General Bounds | open<br>lower bound | open<br>upper bound | closed<br>lower bound | closed<br>upper bound
18--------|-------------------------------|---------------------|---------------------|-----------------------|----------------------
19general | non-preemptive $(c < \infty)$ | 2.0585 | 2.6180 ([MLipmann][]) | 2 | 2 ([Ascheuer][])
20general | uncapacitated $(c=\infty)$ | 2.0346 | 2.4142 ([BjeldeDisser17][]) | 2 | 2
21general | preemptive | 2.0346 | __2.4142__ | 2 (Thm 3.2 in [Ausiello][]) | 2
22general | TSP | 2.0346 | 2.4142 | 2 | 2
23--- | | | | |
24line | non-preemptive $(c < \infty)$ | 2.0585 (Thm 1 in [Birx19][]) | 2.6180 | 1.75 ([BjeldeDisser17][]) | 2
25line | uncapacitated $(c=\infty)$ | 2.0346 | 2.4142 | 1.6404 | 2
26line | preemptive | 2.0346 | 2.4142 ([BjeldeDisser17][]) | 1.6404 | 2
27line | TSP | 2.0346 ([BjeldeDisser17][]) | 2.4142 ([BjeldeDisser17][]) | 1.6404 (Thm 3.3 in [Ausiello][]) | 1.6404 ([BjeldeDisser17][])
28--- | | | | |
29halfline| non-preemptive $(c < \infty)$ | 1.8968 ([MLipmann][]) | 2.6180 | 1.7071 ([Ascheuer][]) | 2
30halfline| uncapacitated $(c=\infty)$ | 1.6272 | 2.4142 | 1.5 | __1.8536__
31halfline| preemptive | 1.6272 | 2.4142 | 2 | 2
32halfline| TSP | 1.6272 | 2.4142 ([MLipmann][]) | 1.5 ([MRIN][]) | 1.5 ([MRIN][])
33
34
35'
36);
Then these predefined elements are inserted one by one: $table[0]
, $table[1]
, etc.
Many special cases are handled, which are specific to this document.
1 # Special cases:
2 s/ \\AOR-server / AOR-server /g; # \AOR outside of math-mode
3 s/ while \\\\ / while /;
4 # MathJax bug prevention
5 s/^Suppose that \$\(L\^\*/Suppose that\n\$\$\n\(L\^\*/;
6 s/ p_R\$\./ p_R\.\n\$\$/;
7 s/L\^\*/L\^\{\\ast\}/g;
8 s/\{t\^start\}/\{eqn: t\^start\}/g;
9 # forward reference resolution
10 s/\\ref\{lemma: waiting\}/[\<\?=\$lemma__waiting\?>\](#lemma__waiting)/g;
11 # MathJax shortcoming
12 s/\\makebox\[0pt\]\{\\text\{(|\\scriptsize)/\{\{/;
13 # double } resolution in eqn:
14 s/eqn: OPT\(t_\{i-j\}\)/eqn: OPT\(t_Ci-jD\)/g;
15 s/eqn: p\^\{AOR\}/eqn: p^cAORd/g;
16 s/eqn: T\^\{return\}/eqn: T\^CreturnD/g;
17 s/eqn: L\^\{\\ast\}/eqn: L\^C\\astD/g;
18 # Some simple conversions to Markdown
19 s/\\textit\{([^\}]+)\}/_$1_/g;
Display math is enclosed in double dollars keeping the \begin{align}
and \end{align}
stuff:
1 if (/\\begin\{align(|\*)\}/) {
2 print "\$\$\n\\begin{align$1}\n";
3 next;
4 } elsif (/\\end\{align(|\*)\}/) {
5 if ($eqnFlag) { print "\\end{align$1}\n\t\\tag{$eqnCnt}\n\$\$\n"; $eqnFlag = 0; }
6 else { print "\\end{align$1}\n\$\$\n"; }
7 next;
8 }
Most algorithms are replaces by HTML quotations. One algorithm, which are particular "complex" is just replaced by an image (screenshot).
1 if (/\\begin\{algorithm\}/) {
2 ($inAlgo,$prefix) = (1,'> ');
3 next;
4 } elsif (/\\end\{algorithm\}/) {
5 ($inAlgo,$prefix) = (0,'');
6 next;
7 }
8 if ($inAlgo == 1) {
9 next if (/\\SetKwData|\\SetKwFunction|\\SetKwInOut/);
10 s/\\;$/<br>/;
11 s/\\caption\{(.+)\}$/__$1__<br>/;
12 s/\\Input\{(.+)\}$/__input:__ $1/;
13 s/\\Output\{(.+)\}$/__output:__ $1/;
14 }
The most difficult part was to replace numbered theorems, lemmas, definitions, claims with something automatic. I use a combination of Perl numbering, and PHP variables. So forward or back references look like this: [look here: $Perl variable](#$PHPvariable)
.
1 ++$theoremCnt if (/\\begin\{(theorem|lemma|remark)\}/);
2 ++$claimCnt if (/\\begin\{claim\}/);
3 ++$caseCnt if (/\\begin\{case\}/);
4 s/\\begin\{definition\}/<p><\/p>\n\n---\n\n__Definition.__/;
5 s/\\begin\{theorem\}/<p><\/p>\n\n---\n\n__Theorem ${chapterCnt}.${theoremCnt}.__/;
6 s/\\begin\{lemma\}/<p><\/p>\n\n---\n\n__Lemma ${chapterCnt}.${theoremCnt}.__/;
7 s/\\begin\{remark\}/<p><\/p>\n\n---\n\n__Remark ${chapterCnt}.${theoremCnt}.__/;
8 s/\\begin\{claim\}/<p><\/p>\n\n__Claim ${claimCnt}.__/;
9 s/\\begin\{case\}/<p><\/p>\n\n_Case ${caseCnt}._/;
10 s/\\end\{(theorem|lemma|remark|claim|case)\}//;
11 s/\\end\{definition\}/\n---\n<p><\/p>\n/;
12 s/\\begin\{proof\}/<p><\/p>\n\n_Proof._/;
13 s/\\end\{proof\}/ ☐\n\n/;
14
15 if (/^\\label\{(.+)\}$/) {
16 my ($phpvar,$key) = ($1,$1);
17 $phpvar =~ s/( |:|"|\^|\{|\}|<|>|\\|\/|\*)/_/g; # create valid PHP variable out of \label
18 $Hphp{$key} = $phpvar;
19 if ($key =~ /^(th|lemma)/) {
20 $H{$key} = "${chapterCnt}.${theoremCnt}";
21 } elsif ($key =~ /^eqn/) {
22 ++$eqnCnt;
23 $eqnFlag = 1;
24 $H{$key} = "${eqnCnt}";
25 next;
26 } elsif ($key =~ /^claim/) {
27 $H{$key} = "${claimCnt}";
28 } elsif ($key =~ /^chapter/) {
29 $H{$key} = "s${chapterCnt}";
30 } elsif ($key =~ /^tab/) {
31 ++$tableCnt if (!defined($H{$key}));
32 $H{$key} = "${tableCnt}";
33 } else {
34 $H{$key} = "unknown hash H: key=$key";
35 }
36 #$_ = '<a id="'.$phpvar.'"></a>';
37 $_ = '<a id="'.$phpvar.'"></a><?php $'.$phpvar.'="'.$H{$key}.'"; ?>';
38 }
39 #s/\\ref\{(.+?)\}(\)|\.| )/\[$H{$1}\](#s$H{$1})$2/g;
40 #s/\\ref\{(.+?)\}(\.| )/\[$H{$1}\](#"s$1")$2/g;
41 #s/\\ref\{(.+?)\}(\.| )/\[$H{$1}\](#\*<\?=\$$Hphp{$1}\?>\*)$2/g;
42 #s/\\ref\{(.+?)\}(\.| )/\[$H{$1}\](#$Hphp{$1})$2/g;
43 #good (almost): s/\\ref\{(.+?)\}(\.| )/\[<\?=\$$Hphp{$1}\?>\](#$Hphp{$1})$2/g;
44 #while (/\\ref\{(.+?)\}(\.|\)| )/g) {
45 while (/\\ref\{([^\}]+?)\}/g) {
46 my $key = $1;
47 if (!defined($H{$key})) {
48 print STDERR "key=|$key| undefined in H\n";
49 my $phpvar = $1;
50 $phpvar =~ s/( |:|"|\^|\{|\}|<|>|\\|\/|\*)/_/g; # create valid PHP variable out of \label
51 $Hphp{$key} = $phpvar;
52 if ($key =~ /^tab/) { # unfortunately, tables are forward referenced
53 ++$tableCnt;
54 $H{$key} = "tab${tableCnt}";
55 }
56 }
57 if ($key =~ /^eqn/) {
58 s/\\ref\{(.+?)\}/$H{$1}/g;
59 } else {
60 s/\\ref\{(.+?)\}(\.|\)| )/\[<\?=\$$Hphp{$1}\?>\](#$Hphp{$1})$2/g;
61 }
62 }
Again, many thesis specific changes.
1 # Substitute own TeX macros
2 s/\\N([^\w])/\\mathbb\{N\}$1/g;
3 s/\\R([^\w])/\\mathbb\{R\}$1/g;
4 #s/\\Q([^\w])/\\mathbb\{Q\}$1/g;
5 #s/\\M([^\w])/\\mathcal\{M\}$1/g;
6 s/\\ABORT/\\hbox\{ABORT\}/g;
7 s/\\OPT/\\hbox\{OPT\}/g;
8 s/\\ALG/\\hbox\{ALG\}/g;
9 s/\\AAW/\\hbox\{AAW\}/g;
10 s/\\AOR/\\hbox\{AOR\}/g;
11 s/\\DOWN/\\hbox\{DOWN\}/g;
12 s/\\abort/\\hbox\{abort\}/g;
13 s/\\replan/\\hbox\{replan\}/g;
14 s/\\diff/\\hbox\{diff\}/g;
15 s/\\prepared/\\hbox\{prepared\}/g;
16 s/\\start/\\hbox\{start\}/g;
17 s/\\ente/\\hbox\{ente\}/g;
18 s/\\move/\\hbox\{move\}/g;
19 s/\\waituntil/\\hbox\{waituntil\}/g;
20 s/\\return/\\hbox\{return\}/g;
21 s/\\new/\\hbox\{new\}/g;
22 s/\\Return/__return:__/g;
23
24 s/\\Tilde/\\tilde/g;
25
26 # Lines to drop, not relevant
27 next if (/\\DontPrintSemicolon/);
Handling of items in LaTeX.
1 if (/\\begin\{itemize\}/) {
2 ($enumerate,$itemCnt,$_) = (0,1,'');
3 } elsif (/\\begin\{enumerate\}/) {
4 ($enumerate,$itemCnt,$_) = (1,1,'');
5 } elsif (/\\end\{(itemize|enumerate)\}/) {
6 ($enumerate,$itemCnt,$_) = (0,0,'');
7 }
8 if (/^\\item /) {
9 if ($enumerate) {
10 s/\\item /${itemCnt}. /;
11 ++$itemCnt;
12 } else {
13 s/\\item /\* /;
14 }
15 }
16 if (/\\item\[([^\]]+)\]/) {
17 s/\\item\[([^\]]+)\]/${itemCnt}. /;
18 ++$itemCnt;
19 }
Handling of chapters, sections, and subsections. For all three I uses different Perl counters. All chapters, sections, etc. can be jumped to.
They are referenced by #s
, followed by chapter number, section number, etc.
1 # sections + subsections
2 if (/\\chapter\*\{(\w+)\}/) { # unnumbered section, line "Introduction"
3 my $s = $1;
4 push @sections, "- [$s](#s$s)";
5 $_ = "\n## $s<a id=s$s></a>\n";
6 } elsif (/\\chapter\{(.+?)\}\s*$/) {
7 my $s = $1;
8 ++$chapterCnt; $sectionCnt = 0; $subSectionCnt = 0; $theoremCnt = 0;
9 push @sections, "- [$chapterCnt. $s](#s$chapterCnt)";
10 $_ = "\n## $chapterCnt. $s<a id=s$chapterCnt></a>\n";
11 } elsif (/\\section\{(.+?)\}\s*$/) {
12 my $s = $1;
13 ++$sectionCnt; $subSectionCnt = 0;
14 push @sections, "- [$chapterCnt.$sectionCnt $s](#s${chapterCnt}_${sectionCnt})";
15 $_ = "\n### $chapterCnt.$sectionCnt $s<a id=s${chapterCnt}_$sectionCnt></a>\n";
16 } elsif (/\\subsection\{(.+?)\}\s*$/) {
17 my $s = $1;
18 ++$subSectionCnt;
19 push @sections, "\t- [$chapterCnt.$sectionCnt.$subSectionCnt $s](#s${chapterCnt}_${sectionCnt}_$subSectionCnt)";
20 $_ = "\n#### $chapterCnt.$sectionCnt.$subSectionCnt $s<a id=s${chapterCnt}_${sectionCnt}_$subSectionCnt></a>\n";
21 }
Citations are easy. I use a feature in Markdown/Commonmark called Link references.
The link in the text is like this [literature123][]
, the actual definition can be anywhere, for example, at the end of the document.
It looks like this: [literature123]: ...
.
1 # Citations
2 s/\\citeauthor\{([\-\w]+)\} \\cite\{([\-\w]+)\}/\[$1\]\[\]/g;
During development of this Perl script I used Beyond Compare again quite intensively, to compare the original against the changed file.
4. blogbibtex
script.
The input to this script is the Bibtex file with all literature references.
The Bibtex file looks something like this:
1@inproceedings{Ascheuer,
2author = {Ascheuer, Norbert and Krumke, Sven Oliver and Rambau, J\"{o}rg},
3title = {Online Dial-a-Ride Problems: Minimizing the Completion Time},
4year = {2000},
5isbn = {3540671412},
6publisher = {Springer-Verlag},
7address = {Berlin, Heidelberg},
8booktitle = {Proceedings of the 17th Annual Symposium on Theoretical Aspects of Computer Science},
9pages = {639–650},
10numpages = {12},
11series = {STACS '00}
12}
13
14@article{Ausiello,
15author = {Ausiello, Giorgio and Feuerstein, Esteban and Leonardi, S. and Stougie, L. and Talamo, Maurizio},
16year = {2001},
17month = {04},
18pages = {560-581},
19title = {Algorithms for the On-Line Travelling Salesman},
20volume = {29},
21journal = {Algorithmica},
22doi = {10.1007/s004530010071}
23}
The Perl script is just a slightly modified version of the script used in Converting Journal Article from LaTeX to Markdown.