Skip to content

Instantly share code, notes, and snippets.

@FSharp4
Created November 5, 2022 06:00
Show Gist options
  • Save FSharp4/20bf0db8baf02499a398a8cb15f9cb6a to your computer and use it in GitHub Desktop.
Save FSharp4/20bf0db8baf02499a398a8cb15f9cb6a to your computer and use it in GitHub Desktop.

Pretty Printing Markdown

The goal of this gist is to create a workflow for parsing Markdown (github-flavored + KaTeX) to pdfs.

Dependencies

  • pandoc
  • latex
  • python: Specifically, Python 3.11.
  • grid-table-py: Install via pip install grid-table-py. This is why Python 3.11 is needed; I will try to adjust compatibility to suit a wider range of python when I have free time.

Why is this needed?

Pandoc is capable of generating very nice files from Markdown. The main issue is that the tables it generates from the gfm (Github-Flavored Markdown) Pipe Table syntax are not ideal; it has no way of knowing where to insert line breaks in table entries, so unwieldy tables are rendered poorly.

Adapted from https://en.wikipedia.org/wiki/Chalcogen

Element Number Valence Electrons/shell Melting Point (°C) Boling Point (°C) Density at STP (g/cm3)
Oxygen 8 2, 6 -219 -183 0.00143
Sulfur 16 2, 8, 6 120 445 2.07
Selenium 34 2, 8, 18, 6 221 685 4.3
Tellurium 52 2, 8, 18, 18, 6 450 988 6.24
Polonium 84 2, 8, 18, 32, 18, 6 254 962 9.2
Livermorium 116 2, 8, 18, 32, 32, 18, 6 (predicted) 220 (predicted) 800 (predicted) 14 (predicted)
| Element     | Number | Valence Electrons/shell               | Melting Point (°C) | Boling Point (°C) | Density at STP (g/cm<sup>3</sup>) |
| ----------- | ------ | ------------------------------------- | ------------------ | ----------------- | --------------------------------- |
| Oxygen      | 8      | 2, 6                                  | -219               | -183              | 0.00143                           |
| Sulfur      | 16     | 2, 8, 6                               | 120                | 445               | 2.07                              |
| Selenium    | 34     | 2, 8, 18, 6                           | 221                | 685               | 4.3                               |
| Tellurium   | 52     | 2, 8, 18, 18, 6                       | 450                | 988               | 6.24                              |
| Polonium    | 84     | 2, 8, 18, 32, 18, 6                   | 254                | 962               | 9.2                               |
| Livermorium | 116    | 2, 8, 18, 32, 32, 18, 6 _(predicted)_ | 220 _(predicted)_  | 800 _(predicted)_ | 14 _(predicted)_                  |

Table with column overlap

The issue here is that the table columns overlap with each other. Another issue with even more unwieldy tables and alternate templates is that the table will continue printing off the visible page.

Steps

  1. Use grid_table_py in.md out.md to replace pipe table syntax with grid table syntax
  2. Use pandoc to generate the pdf. I prefer using the Eisvogel template. For example: pandoc out.md -o out.pdf --tempalte eisvogel

Example bash file:

grid_table_py in.md out.md
pandoc out.md -o out.pdf --template eisvogel
rm out.md

Example of corrected tables:

The above table becomes

+------------+----------+----------------+-----------+-----------+-------------+
| Element    | Number   | Valence Electr | Melting   | Boling    | Density at  |
|            |          | ons/shell      | Point     | Point     | STP (g/cm<s |
|            |          |                | (°C)      | (°C)      | up>3</sup>  |
+============+==========+================+===========+===========+=============+
| Oxygen     | 8        | 2, 6           | -219      | -183      | 0.00143     |
+------------+----------+----------------+-----------+-----------+-------------+
| Sulfur     | 16       | 2, 8, 6        | 120       | 445       | 2.07        |
+------------+----------+----------------+-----------+-----------+-------------+
| Selenium   | 34       | 2, 8, 18, 6    | 221       | 685       | 4.3         |
+------------+----------+----------------+-----------+-----------+-------------+
| Tellurium  | 52       | 2, 8, 18, 18,  | 450       | 988       | 6.24        |
|            |          | 6              |           |           |             |
+------------+----------+----------------+-----------+-----------+-------------+
| Polonium   | 84       | 2, 8, 18, 32,  | 254       | 962       | 9.2         |
|            |          | 18, 6          |           |           |             |
+------------+----------+----------------+-----------+-----------+-------------+
| Livermoriu | 116      | 2, 8, 18, 32,  | 220 _(pre | 800 _(pre | 14 _(predic |
| m          |          | 32, 18, 6      | dicted)_  | dicted)_  | ted)_       |
|            |          | _(predicted)_  |           |           |             |
+------------+----------+----------------+-----------+-----------+-------------+

Corrected Image

Remaining issues:

  • The python textwrap doesn't know how to word wrap well where columns are too thin (notice that sometimes "(predicted)" is printed choppy.
  • grid_table_py requires more testing for edge cases; it may not work for all compliant markdown files yet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment