%First Steps with Markdown %Marco Malva %2015/12/22 - 2021/05/18
The format traditionally Markdown is now rebranded as commonmark. One can read more on commonmark here http://commonmark.org/
The commonly used suffix for markdown files is .md
Markdown works great for smaller documents, the available extensions are great,
and the documents can be written with a simple text editor (e.g. vim
) or a WYSIWYG editor such as typora
.
However,
- not all extensions work for all target formats, which is a bummer
- not all features are supported by the editors
- lack of standard across the tools and target formats is an issue
- not all editors are supported on all platforms, e.g. lack on Raspberry Pie.
- the CSS Style makes a huge difference in the resulting HTML
- PDF and HTML output do not support the same set of extensions
If you are looking into writing longer technical documents markdown might not be the best solution:
In this case, Sphinx and AsciiDoctor will most likely serve you better:
- Sphinx: https://www.sphinx-doc.org/en/master/
- Sphinx - Getting Started, see https://docs.readthedocs.io/en/stable/intro/getting-started-with-sphinx.html
- AsciiDoctor: https://asciidoctor.org/
As for scientific writing with Markdown:
Other options for this purpose are:
- R Markdown: R Markdown: The Definitive Guide
- Jupyter Notebooks: Project Jupyter | Home
- LaTeX: LaTeX - A document preparation system
- Introduction to LaTeX - Writing papers the right way
- useful_latex.dvi
This document started out to cover the basics of Markdown but was gradually extended to included advanced topics such as:
- pretty format tables
- convert CSV files to tables
- math expressions
- embed UML and other charts
- embed plots
- filter to embed CSV files
- various UI editors
The motivation is to enable the reader to unlock some of the power of Markdown while still keeping the scope contained.
Some side aspects are tested and illustrated in small sample markdown files in the current folder.
I added a lot of stuff, mostly related to filter, on 2019/01/13.
I added some details on editors on 2021/05/08.
I added PDF generation on 2021/05/08.
I added various CSS templates on 2021/05/09.
Polished writing on 2021/05/18.
The tool to convert Markdown documents to HTML, PDF, etc. is called pandoc
.
Its documentation is a worthwhile read, see http://pandoc.org/
To convert as markdown files into HTML with pandoc
I use a command line similar to the one shown below:
# works for standard stuff
pandoc markdown-first-steps.md -o markdown-first-steps.html -s --self-contained --highlight-style tango -c ~/tools/pandoc/1882525/buttondown.css --toc
# works w/ math equations, CSV inline, CSV import, etc
pandoc markdown-first-steps.md -o markdown-first-steps.html -s --self-contained --highlight-style tango -c ~/tools/pandoc/1882525/buttondown.css --toc --webtex -F mermaid-filter -F R-pandoc -F pandoc-placetable -F pandoc-pyplot
Some of the
pandoc
extensions (e.g. embed CSV files) do not work w/ some/most/all (?) editors.
A CSS file resulting in nicer HTML is provided here: pandoc.css. It lacks the highlight of the current line in a table but is otherwise superior.
Other CSS files:
- GitHub-like CSS for pandoc standalone HTML files (perfect for HTML5 output). Based on Marked.app's GitHub CSS. Added normalize.css (v2.1.3) in the prior to GitHub css. · GitHub
- sakura: a minimal classless css framework / theme
- MVP.css - Minimalist stylesheet for HTML elements
- Pandoc: Convert Markdown to HTML Using a Custom Template
More CSS related links are curated here: css-links.md
The command to convert this file to a nice looking PDF:
pandoc markdown-first-steps.md -o markdown-first-steps.pdf --from markdown --template eisvogel --listings -F mermaid-filter -F R-pandoc -F pandoc-placetable -F pandoc-pyplot -F pandoc-csv2table
This requires the Eisvogel Latex Template for
pandoc
, see GitHub - Wandmalfarbe/pandoc-latex-template: A pandoc LaTeX template to convert markdown files to PDF or LaTeX.
In any case, the creation of PDF from Markdown is a very vast subject that is only covered rudimentary in this document.
One can copy & paste the HTML output of pandoc
into Google sites, though only without the --toc
option.
I add the TOC via "Insert | Table of contents" in Google sites and choose a width of 1250 points instead of the default 250.
The pandoc
command is:
pandoc markdown-first-steps.md -o markdown-first-steps.html -s --self-contained --highlight-style tango -c ~/tools/pandoc/1882525/buttondown.css
If you have images in your document, those images do not copy&paste properly, and one must add them manually to the Google site. Maybe we can find a better way to import the HTML page.
Bitbucket renders markdown documents correctly, though it does not seem to understand the optional title and author.
In a Markdown document the title, the author, and the date may be added in the first three lines if those start with a percent (%
) character.
There are various tools to convert a CSV or Excel table into a Markdown table.
I prefer to embed CSV files directly, but the required extensions do not work in every setup, see section [](# Embed CSV Files - pandoc-csv2table
)
One such online conversion tool is: http://www.tablesgenerator.com/markdown_tables
There is also a tool one can install and run locally, but it is limited to CSV files.
I like it for confidential data and for automation.
The tool is called csvtomd
and is python3
based.
One can download it here: https://github.com/mplewis/csvtomd.git.
mkdir -p
git clone https://github.com/mplewis/csvtomd.git
You can test it with python3 ${HOME}/dev/public/csv/csvtomd/csvtomd/csvtomd.py test.csv
I created a shell script (${HOME}/bin/csvtomd
) to make it work:
#!/bin/bash
# python3 /usr/bin/csvtomd $@
python3 ${HOME}/dev/public/csv/csvtomd/csvtomd/csvtomd.py $@
This project is no longer under development as the developer moved on to a Java Script based solution:
mkdir -p ~/dev/public/csv
cd ~/dev/public/csv
git clone https://github.com/mplewis/csvtomd-web.git
cd csvtomd-web
npm install
npm run unit
npm run dev
The command above runs a local web-server that converts the data on http://localhost:8080, but it seems to have no CLI version right now. Anyway, the python
based version is still working fine.
For nicer formatting of tables I used a CSS that I downloaded from here: https://gist.github.com/1882525.git.
I have that public git repository downloaded to my local folder ${HOME}/tools/pandoc/
.
It renders tables more nicely, such as alternating colors with rows.
A CSS file resulting in overall nicer HTML is provided here: pandoc.css. It lacks the highlight of the current line in a table but is otherwise superior to the other CSS file.
mkdir -p ${HOME}/tools/pandoc/
cd ${HOME}/tools/pandoc/
git clone https://gist.github.com/1882525.git
Tables | Are | Cool |
---|---|---|
col 3 is | right-aligned | $1600 |
col 2 is | centered | $12 |
zebra stripes | are neat | $1 |
The sample from the test.csv
:
Name | Position | Wanted |
---|---|---|
Andromedus, Darrow au | Leader | Yes |
Augustus, Victoria au | Accomplice | Yes |
Here are some sample equations copied from the pandoc
math sample (math.text
): https://pandoc.org/demo/math.text.
They can be rendered by adding any of the following switches, --mathml
, --webtex
, --mathjax
, --katex
, i.e. with any of the following commands:
pandoc markdown-first-steps.md -o markdown-first-steps.html -s --self-contained --highlight-style tango -c ~/tools/pandoc/1882525/buttondown.css --toc --mathml
pandoc markdown-first-steps.md -o markdown-first-steps.html -s --self-contained --highlight-style tango -c ~/tools/pandoc/1882525/buttondown.css --toc --webtex
pandoc markdown-first-steps.md -o markdown-first-steps.html -s --self-contained --highlight-style tango -c ~/tools/pandoc/1882525/buttondown.css --toc --katex
The rendering result differs, as does the output file size, which varies between 18 KiB, 21 KiB, and 2000 KiB.
It seems that those work also w/o --self-contained
, in which case the file sizes are 16 KiB, 15 KiB, and 15 KiB.
The
--katex
did only work after I upgradedpandoc
to version 2.5 from https://github.com/jgm/pandoc/releases/tag/2.5 (before the upgrade the version installed from the default repository was version 2.2.1) and after I installedKatex
https://katex.org/docs/node.html:
sudo npm -g install katex
The
--mathjax
did not render the math correctly, nor did it help to omit the flag.
Please note that the above does not render work correctly in ghostwriter
.
This sample is borrowed from https://stackedit.io/app# and has inline math expressions, explicit expressions, and note blocks, all of which also work with ghostwriter
but the inline math expression.
The Gamma function satisfying
You can find more information about LaTeX mathematical expressions here.
Here is two equations to be rendered in Pandoc in ghostwriter
which does requires to include a java script from mathjax (idea seen here: https://github.com/wereturtle/ghostwriter/issues/81):
<script src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
And the second one, which only works properly in $$
block and not [[ ... ]]
block:
That is all I have for math right now.
The Markdown editors are an ever-expanding list of applications, each with its strong suit and draw back.
Today (2021/05/08), I came across haroopad
, and it may yet displace typora
:
- it does support
vim
mode - it renders the Math and Mermaid fine
On the negative side:
- it does not support
~~~{.bash}
and similar notations for code blocks, so one must use~~~bash
instead, but that seems to the case for most editors - some of the advanced features, like embedding CSV files, do not work
Since I discovered typora
, it has replaced ghostwriter
as my favorite WYSIWYG Markdown editor.
I like:
- single view
- build-in spell check is decent, not as good as Gramarly in
code
but decent - it renders Math and Mermaid fine
- some of the advanced features, like embedding CSV files, seems to work
Initially, I mostly used ghostwriter
for its real-time preview of the HTML. It is fair but lacks syntax highlighting in source code blocks and requires some special considerations w.r.t. math expressions.
The Github page of ghostwriter
is: https://github.com/wereturtle/ghostwriter.
Most Markdown documents start their life in vim
.
I used code
because of the unofficial Grammarly plugin for advanced spell-check.
There is also an online editor under https://stackedit.io/editor, which seems to be more powerful than ghostwriter
but works less well with the local file system as one has to go through import/export to read/write files from/to the local file system.
It can also be installed as a Google Chrome extension <> and run from a local node:
mkdir -p ~/tools/markdown/
cd ~/tools/markdown/
git clone https://github.com/benweet/stackedit.git
cd stackedit
npm install
npm start
The above makes the stackedit
available under localhost:8080.
Another online editor which supports graph is markvis
: https://github.com/geekplux/markvis.
It can run locally via node
after installing it as follows:
git clone https://github.com/geekplux/markvis
cd markvis
npm install
There is derivate work such as markvis-bar
that allows to create SVG/PNG charts such as https://github.com/geekplux/markvis-bar:
# installation
git clone https://github.com/geekplux/markvis-bar.git
cd markvis-bar
npm install
# exection
npm run example
The last command creates SVG/PNG/HTML bar-chart files under the examples
folder.
The markdown-plus
https://mdp.tylingsoft.com/ seems to be only available for Windows and Mac.
It has additional features such as charts (line charts, bar charts, etc.) via dagre-d3 (https://github.com/dagrejs/dagre-d3).
The ability to add UML and other charts in a Markdown document is something I just now (2019/01/12) discovered while looking into stackedit
.
In the past, I used apps like dot
, plantuml
to create UML and other diagrams, but I might switch to embed them directly in Markdown documents.
One can use mermaid
https://mermaidjs.github.io/ for Markdown documents.
It also has a CLI interface https://github.com/mermaidjs/mermaid.cli.
You need to install a recent version of mermaid
https://mermaidjs.github.io/, https://github.com/raghur/mermaid-filter and https://www.npmjs.com/package/mermaid-filter.
I ran into some permission problems, but --unsafe-perm=true
solved it for me:
sudo npm install --global mermaid-filter --unsafe-perm=true
Now add -F mermaid-filter
to the pandoc
command line:
pandoc markdown-first-steps.md -o markdown-first-steps.html -s --self-contained --highlight-style tango -c ~/tools/pandoc/1882525/buttondown.css --toc --webtex -F mermaid-filter;
So do not install the three year old version (https://www.npmjs.com/package/pandoc-mermaid-filter):
sudo npm install --global git+https://github.com/nishimaki10/pandoc-mermaid-filter.git
And the following command line:
pandoc markdown-first-steps.md -o markdown-first-steps.html -s --self-contained --highlight-style tango -c ~/tools/pandoc/1882525/buttondown.css --toc --webtex -F pandoc-mermaid;
This example is borrowed from https://www.npmjs.com/package/pandoc-mermaid-filter. Originally, it did not render properly with pandoc
but it works with:
pandoc markdown-first-steps.md -o markdown-first-steps.html -s --self-contained --highlight-style tango -c ~/tools/pandoc/1882525/buttondown.css --toc --webtex -F mermaid-filter -F R-pandoc
It also shows nicely in typora
and haroopad
:
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;
This example borrowed from the slackedit
welcome page and, of course, works fine with this editor, i.e. it even worked before I installed mermaid
.
However, they did not work right off the bat with ghostwriter
or pandoc
.
You can render UML diagrams using Mermaid. For example, this will produce a sequence diagram:
sequenceDiagram
Alice ->> Bob: Hello Bob, how are you?
Bob-->>John: How about you John?
Bob--x Alice: I am good thanks!
Bob-x John: I am good thanks!
Note right of John: Bob thinks a long<br/>long time, so long<br/>that the text does<br/>not fit on a row.
Bob-->Alice: Checking with John...
Alice->John: Yes... John, how are you?
And this will produce a flow chart:
graph LR
A[Square Rect] -- Link text --> B((Circle))
A --> C(Round Rect)
B --> D{Rhombus}
C --> D
Using python plot
: https://github.com/LaurentRDC/pandoc-pyplot
cabal install pandoc-pyplot
Just add filter -F pandoc-pyplot
to the pandoc
command line:
pandoc markdown-first-steps.md -o markdown-first-steps.html -s --self-contained --highlight-style tango -c ~/tools/pandoc/1882525/buttondown.css --toc --webtex -F mermaid-filter -F R-pandoc -F pandoc-placetable -F pandoc-pyplot
A simple figure with:
import matplotlib.pyplot as plt
plt.figure()
plt.plot([0,1,2,3,4], [1,2,3,4,5])
plt.title('This is an example figure')
The above does not display correctly with
typora
orharoopad
. Inharoopad
the remainder is screwed up b/c of it, which is why I had to modify the line from{plot_target=my_figure.jpg plot_alt="This is a simple figure"}
The R-plot
requires the R-pandoc
filter, i.e. the command line switch -F R-pandoc
:
pandoc markdown-first-steps.md -o markdown-first-steps.html -s --self-contained --highlight-style tango -c ~/tools/pandoc/1882525/buttondown.css --toc --webtex -F mermaid-filter -F R-pandoc
Here is a the R-plot
output:
require(stats)
D = 150
T = 10
t = seq(0, 80, 0.01)
x = -D*exp(-(t/T))+D
v = (D/T)*exp(-(t/T))
plot(t, x, type="l", main="position through time", xlab="time (s)", ylab="position (m)", xlim=c(0,80), ylim=c(0, D+10), xaxs = "i", yaxs = "i")
It was created with the following R
script:
require(stats)
D = 150
T = 10
t = seq(0, 80, 0.01)
x = -D*exp(-(t/T))+D
v = (D/T)*exp(-(t/T))
plot(t, x, type="l", main="position through time", xlab="time (s)", ylab="position (m)", xlim=c(0,80), ylim=c(0, D+10), xaxs = "i", yaxs = "i")
The above does not render correctly in
typora
orharoopad
.
References:
- The home page: https://hackage.haskell.org/package/R-pandoc
- The github page: https://github.com/cdupont/R-pandoc.
Getting R-pandoc
to work was a little bit convoluted as it required to
- install cabal
- upgrade cabal
- change environment variable
PATH
to use upgradedcabal
- install
R-pandoc
- optional install of Haskell tool
stack
The R-pandoc
did not work out-of-the-box, I had to modify the dependency file to get it to work.
As far as the installation and update are concerned I followed the instructions as laid out here: https://www.haskell.org/cabal/index.html#troubleshooting. An alternative way to install the latest version of cabal
directly can be found here: https://github.com/haskell/ghcup.
The installation of cabal
:
sudo apt-get install cabal
sudo apt-get install cabal-install
The upgrade of cabal
:
cabal update
cabal install Cabal
cabal install cabal-install
The change to PATH
which are persisted in ~/.bashrc
and ~/.zshrc
:
export PATH=${HOME}/.cabal/bin:${PATH}
export PATH=${HOME}/.local/bin:${PATH}
The installation of the R-pandoc
filter from the source:
mkdir -p cd ~/tools/pandoc/
cd ~/tools/pandoc/
git clone https://github.com/cdupont/R-pandoc.git
cd R-pandoc
~/.cabal/bin/cabal install
I also installed Haskell tool stack
as per https://docs.haskellstack.org/en/stable/README/:
curl -sSL https://get.haskellstack.org/ | sh
The pandoc
CLI can be extended with filters, see:
- https://pandoc.org/filters.html
- https://github.com/jgm/pandoc/wiki/Pandoc-Filters
- https://github.com/mvhenderson/pandoc-filter-node
There are many more filters available for pandoc
.
The section about
pandoc-csv2table
was removed as it did not work and created failures when creating PDF documents.
The pandoc-placetable
is a possible alternative.
The pandoc-placetable
filter allows embedding CSV files as tables: https://github.com/mb21/pandoc-placetable
The installation:
cabal install pandoc-placetable
This filter can be used by adding the filter -F pandoc-placetable
:
pandoc markdown-first-steps.md -o markdown-first-steps.html -s --self-contained --highlight-style tango -c ~/tools/pandoc/1882525/buttondown.css --toc --webtex -F mermaid-filter -F R-pandoc -F pandoc-placetable
A CSV file reference example
An inline example:
some,values
even,"with spaces"
- Pandoc filter to execute python code blocks from a markdown and place print output and figures to a converted markdown file: https://github.com/caiofcm/filter_pandoc_run_py
- Pandoc filter for variable substitution using Mustache syntax: https://github.com/michaelstepner/pandoc-mustache
- Pandoc filter for generating figures with Matplotlib from code directly in documents: https://github.com/LaurentRDC/pandoc-pyplot
- Diagrams: http://hackage.haskell.org/package/diagrams-pandoc, https://archives.haskell.org/projects.haskell.org/diagrams/doc/quickstart.html
- Pandoc Extras: https://github.com/jgm/pandoc/wiki/Pandoc-Extras
- Pandoc Filter: https://github.com/jgm/pandoc/wiki/Pandoc-Filters
- Using knitr and pandoc to create reproducible scientific reports: http://galahad.well.ox.ac.uk/repro/
- Best Python/Jupyter/PyCharm experience + report generation with Pandoc filters: https://www.reddit.com/r/Python/comments/9picgq/best_pythonjupyterpycharm_experience_report/
- Padoc Filters in R: https://cran.r-project.org/web/packages/pandocfilters/README.html
- Quick R - Graph Section: https://www.statmethods.net/graphs/bar.html
- https://libraries.io/search?keywords=pandoc&languages=JavaScript
- learnbyexample - Customizing pandoc to generate beautiful pdf and epub from markdown
- xetex - How to set a font family with pandoc? - TeX - LaTeX Stack Exchange
- pandoc doesn't text-wrap code blocks when converting to pdf - Stack Overflow
- CTAN: Package fvextra
- CTAN: Package awesomebox
- https://raw.githubusercontent.com/Wandmalfarbe/pandoc-latex-template/master/examples/code-blocks-listings/document.md
- Pandoc: How can I get numbered LaTeX equations to show up in both PDF and HTML output - TeX - LaTeX Stack Exchange
- GitHub - tomduck/pandoc-eqnos: A pandoc filter for numbering equations and equation references.
- https://raw.githubusercontent.com/tomduck/pandoc-eqnos/master/demos/demo2.md
- Documentation
- GitHub - iandol/dotpandoc: Pandoc Data directory, including customised filters and templates for producing multiple outputs for academic content.
- pandoc-latex-template/examples/boxes-with-pandoc-latex-environment-and-awesomebox at master · Wandmalfarbe/pandoc-latex-template · GitHub
- 6.10 Use a custom Pandoc LaTeX template (*) | R Markdown Cookbook
- Release Eisvogel (Version 2.0.0) · Wandmalfarbe/pandoc-latex-template · GitHub
- How to change the background color and border of a Pandoc-generated blockquote? - TeX - LaTeX Stack Exchange
- pandoc - How can I create a text box for a note in markdown? - Stack Overflow
- How to create a good-looking notification or warning box in Github Flavoured Markdown? - Stack Overflow
- Emoji support in markdown_github · Issue #2523 · jgm/pandoc · GitHub
- Emoji preview if GitHub rendering is selected · Issue #66 · mike-ward/Markdown-Edit · GitHub
- markdown-it-container - npm
- markdown-it-container - npm
- GitHub - markdown-it/markdown-it-container: Fenced container plugin for markdown-it markdown parser
- GitHub - markdown-it/markdown-it: Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed
- GitHub - executablebooks/markdown-it-py: Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed. Now in Python!
- Notebooks written entirely in Markdown
- The MyST Syntax Guide
- Wealth Distribution Dynamics in MyST
- Getting Started
- MyST-Parser/docs/examples at master · executablebooks/MyST-Parser · GitHub
- Getting Started with Sphinx — Read the Docs 5.14.3 documentation
- Demo Paragraph Level Markup — Sphinx Book Theme
- Overview — Sphinx documentation