edit_noteRamblings

Latex PDF from Markdown

arrow_backAll ramblings
Programming

Latex PDF from Markdown

TL;DR A workflow for generating professional LaTeX PDFs from Markdown using R-flavored Markdown (Rmd), pandoc, and custom templates. Supports dynamic R-script content, bibliographies, and multiple output formats. I write a lot of technical documents, and it is important to me that they look nice. In the past I have always resorted to using latex directly. It's an ugly language, hard to maintain, but it has always been the best solution.


TL;DR

A workflow for generating professional LaTeX PDFs from Markdown using R-flavored Markdown (Rmd), pandoc, and custom templates. Supports dynamic R-script content, bibliographies, and multiple output formats.

I write a lot of technical documents, and it is important to me that they look nice. In the past I have always resorted to using latex directly. It's an ugly language, hard to maintain, but it has always been the best solution. Microsoft Word completely fails at complex documents, and is tedious and buggy if you want to do anything technical with it. If I have a simpler document without much math or special formatting I've always used Markdown.

Recently however I was able to get the best of both worlds. Using R-script's flavored Markdown it adds a tremendous amount of power to Markdown. The most important feature is the fact that it uses pandoc in order to compile your Markdown text into a latex template which is then used to compile a PDF. This means I get all the power of latex with all the simplicity of Markdown. That alone made it a winner for me, but it also allows you to compile into HTML and a number of other formats which can make it even more useful.

It also has one other very sweet added bonus if you don't mind coding in R-Script: you can put R-script code directly into your markdown in order to produce charts or other output programmatically. R-script is a heavily math oriented programming language so it can be extremely powerful when writing math papers.

Getting Started

If you want to get started using R-flavored Markdown to compile into latex and PDF I have provided my templates on GitHub. I also have another GitHub repository where I provide a complete example of a markdown document and makefile that will compile it into a PDF. Really simple to figure it out, just clone the repository and start editing it. Make sure, of course, that you have R-script installed and all the prerequisites you might need. Finally I also have a third GitHub repository with even more templates and examples. Some of the examples go in depth on showing how you can use r-script inside the Markdown to generate plots and graphs or other data. Worth a look there are templates for CVs/resumes, articles, powerpoint presentations, syllabus, etc.

R-flavored Markdown files use the extension "Rmd" and use all the conventional Markdown syntax with some additions. All you need to do is add some front-matter to the begining of the Markdown file which provides some hints to the latex template to do some formatting. The front-matter looks like the following, taken directly from the example I linked. The rest of the content of the file is just the Markdown, very simple. You can see the full file with front-matter and Markdown as used in this example by clicking the link.

yaml

output:
  pdf_document:
    citation_package: natbib
    keep_tex: true
    fig_caption: true
    latex_engine: pdflatex
    template: templates/syncleus-white.tex

# Required properties
title: "Conditional Probabilities and Bayes Theorem"
abstract: "A simple explanation of conditional probabilities."
author:
  name: Jeffrey Phillips Freeman
  affiliation: Syncleus
  correspondence: jeffrey.freeman@syncleus.com

# Optional Properties
other-authors:
- name: John Doe
  affiliation: NoTech
tags: Statistics, Math
start-date: "April 6, 2017"
date: "`r format(Sys.time(), '%B %d, %Y')`"
draft: "`r format(Sys.time(), '%B %d, %Y')`"
revision: 1.1
tocdepth: 5
title-color: 0,0,90
# No indentation
indent: 0pt
parskip: 3mm
# Standard Anglo-american indentation
#indent: 15pt
#parskip: 0mm
archive: Pseudoscience Journal of America
geometry: left=2cm, right=2cm, top=2.25cm, bottom=2.25cm, headheight=13.6pt, letterpaper
bibliography: ./probabilities.bib
biblio-style: plainnat
biblio-title: References

Below you will see the output of the compiled pdf using this example, different temples can give different layouts and formatting.

    / [pdf]

In the above examples you can see how the date properties are generated dynamically as the current date using R-script. This gives you just a small taste of how you can use R-script directly inside the Markdown files. Most of the properties above are self explanatory but let me provide a few descriptions for clarity all the same.

| Property | Description | |----------|-------------| | **title** | The title of the document. | | **abstract** | A few sentences describing the document used in the abstract box once rendered. | | **author** | Information about the author. | | **other-authors** | Optional supplemental authors. | | **tags** | Optional list of keywords. | | **start-date** | Date the author began working on the paper; used in copyright information. | | **date** | Date the paper was released; used in copyright information. | | **draft** | Optional text for a draft watermark; omit for no watermark. | | **revision** | Revision version number displayed in the upper right corner. | | **tocdepth** | Maximum depth for nested sections in the table of contents. | | **title-color** | RGB integers (0-255) for the title text color. | | **indent** | Paragraph indentation size. | | **parskip** | Vertical distance between paragraphs. | | **archive** | Journal or archive the paper is to be published in. | | **geometry** | LaTeX geometry overrides for custom margins. | | **bibliography** | Location of the natbib/bibtex bibliography file. | | **biblio-style** | Optional bibliography style override. | | **biblio-title** | Title used for the bibliography section. |

Thats really all there is to it. One last thing, I made sure the properties I picked for my template match those expected in a middleman blog written in markdown. This adds some convenience that the same source file can be compiled by either Middleman or R-script.

check_circleKey takeaways

    - R-flavored Markdown (Rmd) lets you write Markdown that compiles to LaTeX PDFs via pandoc - R-script can be embedded to generate charts, plots, and dynamic content programmatically - Custom templates control layout, colors, margins, bibliographies, and draft watermarks - The same source file can be compiled by Middleman or R-script thanks to compatible front-matter

Filed under#Programming
Jeffrey Phillips Freeman
Jeffrey Phillips Freeman

Data scientist, open-source innovator, and three-time founder who writes about graphs, radios, and the occasional impossibility. Allegedly just another data scientist. Say hello →

Keep reading

article

Writing High Quality, Well Scoped, Commits

TL;DR Atomic commits — small, focused, self-contained changes that build and pass tests — make code easier to review, bisect, and revert. This post covers the conventions used by the Linux kernel maintainers, Google's Angular team, and the Git project itself. In modern software projects, a clean Git history is more than just an aesthetic choice – it is a cornerstone of maintainable, collaborative development. Practitioners across the industry (from Linux kernel maintainers to Google’s Angular team) advocate for atomic commits: each commit being a focused, self-contained change that can stand on its own.

Programming
article

Spacemacs Ultimate Cheatsheet

TL;DR A comprehensive cheatsheet covering all the Spacemacs hotkeys worth knowing, available as a downloadable PDF. A cheatsheet I wrote for all the Spacemacs hotkeys worth knowing. Previous Next     / [pdf] View the PDF file here. Download Cheatsheet check_circleKey takeaways - A complete Spacemacs reference cheatsheet is available as a PDF download - Covers all essential hotkeys for efficient Emacs navigation and editing

Programming
article

Hyperassociative Map Explanation

TL;DR HAM (Hyperassociative Map) is a graph-drawing algorithm I invented in 2009 that draws graphs faster and more reliably than traditional force-directed methods — with no oscillations, no damping schedules, and guaranteed monotonic convergence. It was built for real-time distributed graph processing at massive scale. Introduction Almost 8 years ago, on Aug 15, 2009, I invented a new game-changing algorithm called the Hyperassociative Map algorithm. It was released as part of the dANN v2.

Mathematics