Create an account Create Markdown Files We're working on giving you the power to create markdown files directly inside Markdown to PDF with quick export functionality. Jan 25, 2019 pagedown: Creating beautiful PDFs with R Markdown and CSS The traditional way to beautiful PDFs is often through LaTeX or Word, but have you ever thought of printing a web page to PDF? Web technologies (HTML/CSS/JavaScript) are becoming more and more. Download the Yzane Markdown PDF extension; Right click inside a Markdown file (md) The content below will appear; Select the Markdown PDF: Export (pdf) option; Note: Emojis are better in Windows than Linux (I don't know why).
3.3 PDF document
To create a PDF document from R Markdown, you specify the pdf_document
output format in the YAML metadata:
Within R Markdown documents that generate PDF output, you can use raw LaTeX, and even define LaTeX macros. See Pandoc’s documentation on the raw_tex extension for details.
Note that PDF output (including Beamer slides) requires an installation of LaTeX (see Chapter 1).
3.3.1 Table of contents
You can add a table of contents using the toc
option and specify the depth of headers that it applies to using the toc_depth
option. For example:
If the TOC depth is not explicitly specified, it defaults to 2 (meaning that all level 1 and 2 headers will be included in the TOC), while it defaults to 3 in html_document
.
You can add section numbering to headers using the number_sections
option:
If you are familiar with LaTeX, number_sections: true
means section{}
, and number_sections: false
means section*{}
for sections in LaTeX (it also applies to other levels of “sections” such as chapter{}
, and subsection{}
).
3.3.2 Figure options
There are a number of options that affect the output of figures within PDF documents:
fig_width
andfig_height
can be used to control the default figure width and height (6.5x4.5 is used by default).fig_crop
controls whether thepdfcrop
utility, if available in your system, is automatically applied to PDF figures (this istrue
by default).If you are using TinyTeX as your LaTeX distribution, we recommend that you run
tinytex::tlmgr_install('pdfcrop')
to install the LaTeX packagepdfcrop
. You also have to make sure the system packageghostscript
is available in your system forpdfcrop
to work. For macOS users who have installed Homebrew,ghostscript
can be installed viabrew install ghostscript
.If your graphics device is
postscript
, we recommend that you disable this feature (see more info in the knitr issue #1365).
fig_caption
controls whether figures are rendered with captions (this istrue
by default).dev
controls the graphics device used to render figures (defaults topdf
).
For example:
3.3.3 Data frame printing
You can enhance the default display of data frames via the df_print
option. Valid values are presented in Table 3.3.
Option | Description |
---|---|
default | Call the print.data.frame generic method |
kable | Use the knitr::kable() function |
tibble | Use the tibble::print.tbl_df() function |
A custom function | Use the function to create the table. See 3.1.6.2 |
For example:
3.3.4 Syntax highlighting
The highlight
option specifies the syntax highlighting style. Its usage in pdf_document
is the same as html_document
(Section 3.1.4). For example:
3.3.5 LaTeX options
Many aspects of the LaTeX template used to create PDF documents can be customized using top-level YAML metadata (note that these options do not appear underneath the output
section, but rather appear at the top level along with title
, author
, and so on). For example:
A few available metadata variables are displayed in Table 3.4 (consult the Pandoc manual for the full list):
Variable | Description |
---|---|
lang | Document language code |
fontsize | Font size (e.g., 10pt , 11pt , or 12pt ) |
documentclass | LaTeX document class (e.g., article ) |
classoption | Options for documentclass (e.g., oneside ) |
geometry | Options for geometry class (e.g., margin=1in ) |
mainfont, sansfont, monofont, mathfont | Document fonts (works only with xelatex and lualatex ) |
linkcolor, urlcolor, citecolor | Color for internal, external, and citation links |
3.3.6 LaTeX packages for citations
By default, citations are processed through pandoc-citeproc
, which works for all output formats. For PDF output, sometimes it is better to use LaTeX packages to process citations, such as natbib
or biblatex
. To use one of these packages, just set the option citation_package
to be natbib
or biblatex
, e.g.
3.3.7 Advanced customization
3.3.7.1 LaTeX engine
By default, PDF documents are rendered using pdflatex
. You can specify an alternate engine using the latex_engine
option. Available engines are pdflatex
, xelatex
, and lualatex
. For example:
The main reasons you may want to use xelatex
or lualatex
are: (1) They support Unicode better; (2) It is easier to make use of system fonts. See some posts on Stack Overflow for more detailed explanations, e.g., https://tex.stackexchange.com/q/3393/9128 and https://tex.stackexchange.com/q/36/9128.
3.3.7.2 Keeping intermediate TeX
R Markdown documents are converted to PDF by first converting to a TeX file and then calling the LaTeX engine to convert to PDF. By default, this TeX file is removed, however if you want to keep it (e.g., for an article submission), you can specify the keep_tex
option. For example:
3.3.7.3 Includes
You can do more advanced customization of PDF output by including additional LaTeX directives and/or content or by replacing the core Pandoc template entirely. To include content in the document header or before/after the document body, you use the includes
option as follows:
3.3.7.4 Custom templates
You can also replace the underlying Pandoc template using the template
option:
Consult the documentation on Pandoc templates for additional details on templates. You can also study the default LaTeX template as an example.
3.3.8 Other features
Similar to HTML documents, you can enable or disable certain Markdown extensions for generating PDF documents. See Section 3.1.10.4 for details. You can also pass more custom Pandoc arguments through the pandoc_args
option (Section 3.1.10.5), and define shared options in _output.yml
(Section 3.1.11).
1.2 Install LaTeX (TinyTeX) for PDF reports. If you would like to create PDF documents from R Markdown, you will need to have a LaTeX distribution installed. Although there are several traditional options including MiKTeX, MacTeX, and TeX Live, we recommend that R Markdown users install TinyTeX. Turn your analyses into high quality documents, reports, presentations and dashboards with R Markdown. Use a productive notebook interface to weave together narrative text and code to produce elegantly formatted output. Use multiple languages including R, Python, and SQL. R Markdown supports a reproducible workflow for dozens of static and dynamic output formats including HTML, PDF, MS.
I often need to write short reports which are not full blown manuscripts, e.g. annual grant progress reports. Though such documents don’t need to adhere to a strict template, I still want them to look nice. I’ve accomplished this for years by writing directly in LaTeX, but I want to align my process with my recent transition to composing most docs in RStudio/Rmd. Ultimately though, I don’t want to abandon the LaTeX look in the compiled document. Thankfully, RStudio will render a LaTeX pdf, but formatting beyond the defaults (which are still nice!) can be a bit mysterious. This repository holds my working template for such purposes.
Here’s a minimal example of what the defaults within a .Rmd
will give you:
Now, two specific things I’d like to change are:
- Left-justify the title/author/date section
- Modify the font specs used in section titles
A solution to these two problems easily generalizes to the broader question of “How do I format the title and H1-H6 specs in the context of LaTeX rendering from .Rmd
files?”
To start, we will borrow the LaTeX template R Markdown is currently using (h/t SO). The relevant remote repo is here, and you can copy the local version you’re using into your working directory with this line:
If you look past the pandoc nastiness in this template file (I at least find it nasty, being that I was mostly unfamiliar with pandoc scripting!), you’ll see familiar LaTeX commands that are often surrounded by $if(X)$
statements that are triggered if X
appears in your .Rmd
YAML. Here’s a straightforward example where, if you have title:
in your .Rmd
YAML header, the maketitle
command will be executed in your LaTeX render:
Now the problem feels more tractable: all we have to do is modify the maketitle
defaults in the usual LaTeX manner within the template.tex
document. Let’s start with the following:
And don’t forget to include template.tex
in your .Rmd
YAML header like so:
Rendering gives the below, nice!
Next, we clearly need to fix the fact that section titles are now larger than the document title! Let’s do this with the LaTeX sectsty
package – you can basically stuff this code anywhere in the preamble, like so:
Let’s also reduce the overall margins a touch via the geometry
argument in the YAML while we’re at it. Here’s the full .Rmd
:
Create Pdf From Markdown Mac
Mission accomplished!
Markdown Vs Latex
Here’s a bonus I learned along the way. You can send custom arguments to your .tex
doc by simply defining new variables in the YAML header (relevant info in the pandoc docs here). Suppose I want an optional subtitle parameter. This is accomplished like so:
With this trick, you can start to do even fancier things (literally), such as include fancyhdr
options. This option is ultimately included in the template.tex
provided in the repository, and here’s the relevant YAML and output:
Happy R Markdowning!
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.
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.)
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
Markdown Export To Pdf
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 template
option in the YAML header of your Rmarkdown document. 7
Python Create Pdf From Markdown
R Markdown To Latex File
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
knit
ting 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.
Create Pdf From Markdown Visual Studio Code
What Works for Me May Not Work For You
R Markdown To Latex
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.
R Markdown To Latex Transfer
Create Pdf From Markdown Linux
- 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. ^
- in order to maximize the amount of space used for content, of course ^
- 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.) ^ - which was super helpful for a LaTex noob like me because it has comments explaining what specific lines/sections are doing ^
- See the PDF chapter of the R Markdown book for some guidance with these. ^
- 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. ^
- You can specify other options as well, such as
keep_latex: true
for an alternative LaTex engine withlatex_engine
. ^ - and there are lots of them out there^
- The live preview feature is probably my favorite of all. ^
- Others may view the hands-off approach to package management as an advantage of using Overleaf. ^
- 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. ^
- 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 😊. ^
- See the “Show equation and image previews” option in Tools > Global Options… > R Markdown. ^
- I wouldn’t be surprised if I find a better workflow for myself in the future. ^