Skip to content

Commit 596dbe2

Browse files
new section about table of contents
1 parent 26c5a69 commit 596dbe2

File tree

7 files changed

+175
-0
lines changed

7 files changed

+175
-0
lines changed

_posts/2019-03-11-customizing-pandoc.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ date: 2019-03-11T21:05:25
2020
* [Syntax highlighting](#syntax-highlighting)
2121
* [Bullet styling](#bullet-styling)
2222
* [PDF properties](#pdf-properties)
23+
* [Adding table of contents](#adding-table-of-contents)
2324
* [Resource links](#resource-links)
2425

2526
<br>
@@ -147,6 +148,8 @@ $ ./md2pdf.sh sample_1.md sample_1_settings.pdf
147148

148149
Do compare the pdf generated side by side with previous output before proceeding.
149150

151+
![warning](/images/warning.svg) On my system, `DejaVu Serif` did not have *italic* variation installed, so I had to use `sudo apt install ttf-dejavu-extra` to get it.
152+
150153
<br>
151154

152155
## <a name="syntax-highlighting"></a>Syntax highlighting
@@ -251,6 +254,58 @@ This [tex.stackexchange Q&A](https://tex.stackexchange.com/questions/23235/elimi
251254

252255
<br>
253256

257+
## <a name="adding-table-of-contents"></a>Adding table of contents
258+
259+
There's a handy option `--toc` to automatically include table of contents at top of the generated `pdf`. You can control number of levels using `--toc-depth` option, the default is 3 levels. You can also change the default string `Contents` to something else using `-V toc-title` option.
260+
261+
`./md2pdf_syn_bullet_prop_toc.sh sample_1.md sample_1_toc.pdf` gives:
262+
263+
![table of contents]({{ '/images/pandoc_pdf/table_of_contents.png' | absolute_url }}){: .align-center}
264+
265+
However, if you want to add something prior to table of contents (cover image for example), you need to hack stuff. As far as I know, there's no inbuilt method. The `-B` option only allows verbatim inclusion, whereas we need markdown here. The only cover image option I see is `--epub-cover-image` but we need `pdf` here. I arrived at a workaround after about a day of trying things - the solution looks simple, but I just didn't know what is possible until I went through multiple options and this one worked.
266+
267+
For this example, `![cover image](cover.png)` is added to `sample_1.md` at the top to create input markdown file. The modified script now looks like:
268+
269+
```bash
270+
#!/bin/bash
271+
272+
pandoc "$1" \
273+
-f gfm \
274+
--toc \
275+
--include-in-header chapter_break.tex \
276+
--include-in-header inline_code.tex \
277+
--include-in-header bullet_style.tex \
278+
--include-in-header pdf_properties.tex \
279+
--highlight-style pygments.theme \
280+
-V toc-title='Table of contents' \
281+
-V linkcolor:blue \
282+
-V geometry:a4paper \
283+
-V geometry:margin=2cm \
284+
-V mainfont="DejaVu Serif" \
285+
-V monofont="DejaVu Sans Mono" \
286+
--pdf-engine=xelatex \
287+
-o temp.tex
288+
289+
fn="${2%.*}"
290+
291+
perl -0777 -pe \
292+
's/begin\{document\}\n\n\K(.*?^\}$)(.+?)\n/$2\n\\thispagestyle{empty}\n\n$1\n/ms' \
293+
temp.tex > "$fn".tex
294+
295+
xelatex "$fn".tex &> /dev/null
296+
xelatex "$fn".tex &> /dev/null
297+
298+
rm temp.tex "$fn".{tex,toc,aux,log}
299+
```
300+
301+
The `pandoc` command is changed to produce `tex` output instead of `pdf`. The `perl` script will switch the positions of cover image and table of contents. Also, `\thispagestyle{empty}` is added to remove page number from showing up with cover image, see also [tex.stackexchange: clear page](https://tex.stackexchange.com/questions/360739/what-is-the-use-of-clearpage-thispagestyleempty-cleardoublepage). See my tutorial on [perl one-liners](https://github.com/learnbyexample/Command-line-text-processing/blob/master/perl_the_swiss_knife.md) if you are interested in learning how to write such powerful commands.
302+
303+
After modifying the `tex` file, `xelatex` command is directly used to get the `pdf` output. For some reason, the table of contents goes awry but gives correct output if the command is called twice! The output file is named same as input, but with extension changed from `tex` to `pdf`. Finally, the temporary files are removed.
304+
305+
The `bash` script invocation is now `./md2pdf_syn_bullet_prop_toc_cover.sh sample_5.md sample_5.pdf`.
306+
307+
<br>
308+
254309
## <a name="resource-links"></a>Resource links
255310

256311
* [pandoc: manual](https://pandoc.org/MANUAL.html)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
pandoc "$1" \
4+
-f gfm \
5+
--toc \
6+
--include-in-header chapter_break.tex \
7+
--include-in-header inline_code.tex \
8+
--include-in-header bullet_style.tex \
9+
--include-in-header pdf_properties.tex \
10+
--highlight-style pygments.theme \
11+
-V toc-title='Table of contents' \
12+
-V linkcolor:blue \
13+
-V geometry:a4paper \
14+
-V geometry:margin=2cm \
15+
-V mainfont="DejaVu Serif" \
16+
-V monofont="DejaVu Sans Mono" \
17+
--pdf-engine=xelatex \
18+
-o "$2"
19+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
pandoc "$1" \
4+
-f gfm \
5+
--toc \
6+
--include-in-header chapter_break.tex \
7+
--include-in-header inline_code.tex \
8+
--include-in-header bullet_style.tex \
9+
--include-in-header pdf_properties.tex \
10+
--highlight-style pygments.theme \
11+
-V toc-title='Table of contents' \
12+
-V linkcolor:blue \
13+
-V geometry:a4paper \
14+
-V geometry:margin=2cm \
15+
-V mainfont="DejaVu Serif" \
16+
-V monofont="DejaVu Sans Mono" \
17+
--pdf-engine=xelatex \
18+
-o temp.tex
19+
20+
fn="${2%.*}"
21+
22+
perl -0777 -pe \
23+
's/begin\{document\}\n\n\K(.*?^\}$)(.+?)\n/$2\n\\thispagestyle{empty}\n\n$1\n/ms' \
24+
temp.tex > "$fn".tex
25+
26+
xelatex "$fn".tex &> /dev/null
27+
xelatex "$fn".tex &> /dev/null
28+
29+
rm temp.tex "$fn".{tex,toc,aux,log}
30+
31+

files/pandoc_pdf/sample_1_toc.pdf

47.9 KB
Binary file not shown.

files/pandoc_pdf/sample_5.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
![cover image](cover.png)
2+
3+
# Introduction
4+
5+
This is a sample [GitHub style markdown](https://github.github.com/gfm/) file.
6+
Top level headers are chapters and other headings are for sub-sections.
7+
8+
# Python
9+
10+
* Lists are declared within `[]` and elements are separated by `,`
11+
* Each element can be of any data type, including list data type
12+
13+
## Example
14+
15+
Use `for` loop to iterate over a list.
16+
17+
```python
18+
numbers = [2, 12, 3, 25, 624, 21, 5, 9, 12]
19+
odd_numbers = []
20+
even_numbers = []
21+
22+
for num in numbers:
23+
odd_numbers.append(num) if(num % 2) else even_numbers.append(num)
24+
25+
print(f'numbers: {numbers}')
26+
print(f'odd_numbers: {odd_numbers}')
27+
print(f'even_numbers: {even_numbers}')
28+
```
29+
30+
# Ruby
31+
32+
* Arrays are declared within `[]` and elements are separated by `,`
33+
* Each element can be of any data type, including array data type
34+
35+
## Example
36+
37+
Use `each` method to iterate over an array.
38+
39+
```ruby
40+
numbers = [2, 12, 3, 25, 624, 21, 5, 9, 12]
41+
odd_numbers = []
42+
even_numbers = []
43+
44+
numbers.each { |n| n.even? ? even_numbers.append(n) : odd_numbers.append(n) }
45+
46+
puts "numbers: #{numbers}"
47+
puts "odd_numbers: #{odd_numbers}"
48+
puts "even_numbers: #{even_numbers}"
49+
```
50+
51+
# CLI
52+
53+
Executing the Python and Ruby programs mentioned in previous chapters:
54+
55+
```bash
56+
$ python3.7 list_looping.py
57+
numbers: [2, 12, 3, 25, 624, 21, 5, 9, 12]
58+
odd_numbers: [3, 25, 21, 5, 9]
59+
even_numbers: [2, 12, 624, 12]
60+
61+
$ ruby array_looping.rb
62+
numbers: [2, 12, 3, 25, 624, 21, 5, 9, 12]
63+
odd_numbers: [3, 25, 21, 5, 9]
64+
even_numbers: [2, 12, 624, 12]
65+
```
66+
67+
# Conclusion
68+
69+
This sample file helps you see a demo for `markdown` to `pdf` conversion using `pandoc`.
70+

files/pandoc_pdf/sample_5.pdf

59.2 KB
Binary file not shown.
7.92 KB
Loading

0 commit comments

Comments
 (0)