Latex Markdown



Unfortunately, I haven’t had as much time to make blog postsin the past year or so.I started taking classes as part ofGeorgia Tech’s Online Master of Science in Analytics (OMSA)program last summer (2018) while continuing to work full-time, so extra timeto code and write hasn’t been abundant for me.

  1. Latex Markdown Matrix
  2. Latex Markdown Jupyter
  3. Latex Markdown Cheatsheet
  4. Latex Markdown Table

Now we can easily use markdown-style formatted tables in LaTeX documents and also do calculations with these tables. Some of you may be thinking that we’ve really just moved the complexity from the LaTeX table to the knitr R script, but I think this is worth the effort as the tables are now much easier to maintain and edit. How to use the LaTeX tables generator? Set the desired size of the table using Table / Set size menu option.; Enter the table data into the table: copy (Ctrl+C) table data from a spreadsheet (e.g. Google Docs, LibreOffice Calc, webpage) and paste it into our editor - click a cell and press Ctrl+V. As Hugo and Wowchemy can attempt to parse YAML, Markdown, and LaTeX content in the front matter, Markdown special characters need to be escaped in any math within the front matter fields by using a backslash to prevent the math being parsed as Markdown. The following tips may help: escape each LaTeX backslash with an extra backslash. Markdown which is a markup language that is a superset of HTML. Latex to render mathematical and scientific writing. LaTex LaTeX is a typesetting system that is used in the publication of technical and scientific documents. There are two modes for inserting mathematical expressions using LaTeX—inline and display. This is similar to the two ways to use markdown for code.

Anyways, I figured I would share one neat thing I learnedas a consequence of taking classes—writing compact“cheat sheets”with {rmarkdown}. 1

Writing with {rmarkdown} is fairly straightforward—mostlythanks to an abundance of freely available learning resources, like theR Markdown: The Definitive Guide—and usingCSS to customize your Rmarkdown output to your likingis not too difficult either.(By the way, huge shout-out to Yihui Xieand everyone else who has contributedto the development of the {rmarkdown} package.)My objective was to make an extremely compact PDFthat minimizes all white space 2.Despite my knowledge of CSS,I had a hard time getting an output that I liked purely from CSS,so I looked online to see if I could find some good LaTex templates.(After all, I would be knitting the Rmarkdown document to PDF,and LaTex would be incorporatedvia the equations on the cheat sheet.)Some templates I found worked fine but weren’t completely to my liking. 3

In my search for an “ideal” template, I stumbled upon a small tidbitin the very last portion of thePDF chapter of the R Markdown bookstating “You can also replace the underlying pandoc template using the template option”. 🤔

At first, I was a bit intimidated by the idea of writing my own template.(“I have to write my own template from scratchusing a framework (LaTeX) that I’ve hardly even touched before now! 😨”)But alas, the task became less intimidating when I realized thatI could use the tried-and-true method of copying-pasting-modifyingfrom Stack Overflow!

The Template

Using the template fromthis Stack Overflow post4 as a basis, I endedup creating a relatively minimal template.For the curious reader, see this GitHub repo,for the latest version of my template. It also includes an example cheat sheet.

The “gist” of my template is shown below.

The key for me was to understand how pandoc variableslike $body$ are used as placeholders for user-supplied content.(I know I haven’t mentioned pandoc up to this point,but suffice it to say thatit—along with the R package {knitr}—are what power the {rmarkdown} package.)

Latex to markdown

The multicols command shown in the snippet above is also noteworthy. ThisLaTex command provides the functionality for I wanted most for mycheat sheet—multiple columns of content!I should point out that there are in_header, before_body, and after_body YAML options for customizing PDF output with {rmarkdown}. 5

These options are probably sufficient for most people’s customization needs(so using a custom template would not be necessary).But for me personally, the appeal of having “complete” controlof my output by using a template convinced me to forego these options. 6

Usage

Latex Markdown Matrix

So, exactly how do you use a custom template with {rmarkdown}?It’s as simple as specifying the path to your template file with the templateoption in the YAML header of your Rmarkdown document. 7

Why This Way?

Before I was using Rstudio and {rmarkdown} to write my cheat sheets,I tried out a couple of LaTex editors 8.First, I tried the very popular Overleaf.It is well known and commonly used becauseit is web-based, allows the user to collaborate in real-time, andprovides real-time previewing 9.However, there was just something that felt “clunky” about the editor, andthe ambiguity over package versions and usage was bothersome to me. 10The other editor I tried for some time was TeXworks(with the pdftex distribution)Using the “Typset” command to generate my PDF output on an ad-hoc basis seemed to meto be a satisfactory workflow, but, among other things, I felt limited by the customizationoffered by TeXworks. 11

And so I turned to Rstudio and {rmarkdown} and didn’t look back.While learning how to create a custom template was a (minor) inconvenience,it has paid off in a number of ways:

  • I can use a familiar editor—Rstudio.

  • I can use a familiar workflow—writing in an Rmarkdown document and knitting to create my desired output.

  • Because I’m using {rmarkdown}, I can use {rmarkdown} functionality that is not available when solely writing in LaTex.

This last point is huge.The whole world of markdown syntax is valid!For example,I can add emphasis to text with markdown’s ** and __ tokens (instead of LaTex’s more “verbose” syntax);I can use # to define section headers (which I just think is super “elegant”);and I can use HTML comments to comments out multiple lines of text.(Note that native LaTex only has a single-line comment token—%. 12)Additionally, beyond just the markdown functionality, I can include R codethanks to added layer of functionality offered by {rmarkdown}.

The one big thing that I feel like I “sacrificed” by moving to Rstudio and {rmarkdown}is the live preview feature that comes with Overleaf (and can be emulatedwith some configuration in other LaTex editors). Nonetheless, I feel like I geta reasonable facsimile of this feature with Rstudio’s functionalityfor inline previews of equations. 13Below are examples of the preview capabilities for both single- andmulti-line equations.

What Works for Me May Not Work For You

Although what I’ve described in this post has been working well for me—andI’d encourage others to try it out—I don’t claim itto be the “best” solution for all of your cheat sheet needs. 14If you’ve got a workflow that works for you, that’s great! Keep using it!Be pragmatic.

  1. For those unfamiliar with the concept of a cheat sheet, there’s no malice in it, despite what the moniker implies. From my experience, it is relatively common for teachers to let students use self-created note sheets (i.e. cheat sheets) for aid with taking exams. ^
  2. in order to maximize the amount of space used for content, of course ^
  3. One of the ones that I really liked was this one. However, it’s a bit more complex than I wanted. (This one implements a “structure” in which one “main” tex file references several others with the input Latex command.) ^
  4. which was super helpful for a LaTex noob like me because it has comments explaining what specific lines/sections are doing ^
  5. See the PDF chapter of the R Markdown book for some guidance with these. ^
  6. I’m sure I could create a perfectly fine cheat sheet using just these options, or, even re-create the output that I have achieved with my template. ^
  7. You can specify other options as well, such as keep_latex: true for an alternative LaTex engine with latex_engine. ^
  8. and there are lots of them out there^
  9. The live preview feature is probably my favorite of all. ^
  10. Others may view the hands-off approach to package management as an advantage of using Overleaf. ^
  11. Perhaps this is the fault of my own. Perhaps all the customization that I would like exists and I just have not discovered how to enable it. ^
  12. I realize that you can define custom commands or use a package to create multi-line comments in LaTex, but that ruins the point that I’m trying to make 😊. ^
  13. See the “Show equation and image previews” option in Tools > Global Options… > R Markdown. ^
  14. I wouldn’t be surprised if I find a better workflow for myself in the future. ^

6.10 Use a custom Pandoc LaTeX template (*)

Latex Markdown Jupyter

Pandoc converts Markdown to LaTeX through a template. The template is a LaTeX file containing Pandoc variables, and Pandoc will replace these variables with their values. Below is a simple template that only contains a single variable $body$:

Latex Markdown Cheatsheet

The value of $body$ is the LaTeX code generated from the body of the Markdown document. For example, if the body text is Hello **world**! in Markdown, the value of $body$ will be Hello textbf{world}!.

Latex Markdown

If the LaTeX customization methods in Sections 6.1, 6.2, and 6.4 are not enough for you, you may try to use a custom template instead. A template allows you to use arbitrary LaTeX code in it, and hence is much more flexible. To use a template, include the path of the template in the template option of pdf_document, e.g.,

The default LaTeX template of Pandoc can be found at https://github.com/jgm/pandoc/tree/master/data/templates (named default.latex). If you want to create your own template, you may want to start with this template.

Latex Markdown Table

For the full list of Pandoc variables and their meanings (such as $body$ and $title$), see Pandoc’s manual at https://pandoc.org/MANUAL.html#templates. You can also use arbitrary custom variables, which are typically passed to the template from the YAML metadata. If you want to learn by examples, you may take a look at the MonashEBSTemplates package (https://github.com/robjhyndman/MonashEBSTemplates), which has provided several custom LaTeX templates. These templates are under the inst/rmarkdown/templates/*/resources/ directories (here * denotes the template names). For example, the template for the output format MonashEBSTemplates::memo allows you to use a variable branding in the YAML metadata to control whether to include the brand logo of Monash University. This is achieved by an if statement in the template that looks like this: