centre title in PDF converted from markdown using Pandoc - r

I'm converting a markdown document to a PDF document using Pandoc from within R. I'm trying to centre the title.
So far, I've tried:
<center># This is my title</center>
and
-># This is my title<-
but neither have worked. Is there a way to centre the title when converting from markdown to PDF using Pandoc?

pandoc has its own extended version of markdown. This includes a title block.
If the file begins with a title block
% my title
% Me; Someone else
% May 2013
This will be parsed into LaTeX and the resulting pdf as
\title{my title}
\author{Me \and Someone Else}
\date{May 2013}
and then `
\maketitle
called within the document.
This will create the standard centred title.
If you want to change how the title etc is formatted you could use the titling package.
If you want to change how the section headers are formatted, you could use the titlesec package.
To automagically have pandoc implement these you could define your own template. A simpler option is to have a file with your desired latex preamble to be included in the header. and then use the appropriate arguments when calling pandoc (eg -H FILE or --include-in-header=FILE)

Related

knitr: automate producing multiple versions of PDF for beamer slides

For lectures, I am using knitr to produce LaTeX beamer slides as a PDF. For a given lecture, I want to produce also (a) 1-up handout (using the handout option, and (b) the same handout formatted 4-up.
I find I have to run knitr 3 times to do this as shown below. Is there a way to simplify this work flow?
A lecture stub:
\documentclass[10pt,table]{beamer}
\input{inputs/beamer-setup}
\input{inputs/defs}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
...
\end{document}
And I run knitr as
knit2pdf("Lecture1.Rnw")
To get the 1-up handout (which suppresses the separate pages when you use transitions), I edit the first line to:
\documentclass[10pt,table,handout]{beamer}
and run
knit2pdf("Lecture1.Rnw" output="Lecture1-1up.tex")
Finally, to get the 2 x 2 version, I use the LaTeX pgfpages package,
\documentclass[10pt,table,handout]{beamer}
\input{inputs/beamer-setup}
\input{inputs/defs}
\usepackage{pgfpages}
\pgfpagesuselayout{4 on 1}[letterpaper,landscape]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
And run:
knit2pdf("Lecture1.Rnw" output="Lecture1-4up.tex")
(I found that with beamer, I could not simply print the PDF 4-up using Adobe Acrobat -- it generated a corrupt PDF file. I was forced to use pgfpages)
Then, of course I have to revert my .Rnw file to the original if I need re-do the slides. Very tedious. There must be a better way.

KnitR XeTeX Beamer issue

I am trying to use a Beamer theme that requires \usepackage[no-math]{fontspec} with a KnitR document. Here is a minimal example test.Rnw:
\documentclass[handout]{beamer}
\usepackage[no-math]{fontspec}
\begin{document}
\maketitle
<<include=FALSE>>=
options(tikzDefaultEngine='xetex')
#
\begin{frame}{test}
\begin{figure}[htbp]
<<echo=FALSE, dev="tikz">>=
plot(1:100)
#
\end{figure}
\end{frame}
\end{document}
When I Knit the doucument I get:
Error in getMetricsFromLatex(TeXMetrics) :
TeX was unable to calculate metrics for the following string
or character:
m
Common reasons for failure include:
* The string contains a character which is special to LaTeX unless
escaped properly, such as % or $.
* The string makes use of LaTeX commands provided by a package and
the tikzDevice was not told to load the package.
This is an issue specifically related to the no-math option with fontspec along with dev="tikz". If I remove either of these, the document compiles fine. Any suggestions on how to get this to work? The entire purpose of using a tikz device is to allow the format of my graphics to match up with my theme that requires I am trying to use a Beamer theme that requires \usepackage[no-math]{fontspec}. Thanks!

knitr html to Word docx using pandoc

I have been saving some example R markdown html output to Word using pandoc. I actually only do this so I can add some page breaks for easier printing:
system("pandoc -s Exercise1.html -o Exercise1.docx")
Although the output is acceptable I was wondering if there is a way to keep the original syntax highlighting of the R chunks (just as they are in the original knit HTML document)?
Also, I seem to be loosing all images in the conversion process and have to stick them into Word by hand. Is that normal?
Using the rmarkdown package (baked into RStudio Version 0.98.682, the current preview release) it's very simple to convert Rmd to docx, and code highlighting is included in the docx file.
You just need to include this at the top of your markdown text:
---
title: "Untitled" # obviously you can change this
output: word_document # specifies docx output
---
However, it seems that page breaks are still not supported in this conversion.
Why not convert the markdown directly to Word format?
Anyway, Pandoc does not support syntax highlighting in Word: "Currently, the only output formats that uses this information are HTML and LaTeX."
About the images: the Word file would definitely include those if you'd convert the markdown to Word directly. I am not sure about the HTML source, but I suppose you might have a path issue.

Pandoc insert appendix after bibliography

I'm using the knitr package and pandoc in R to convert a .Rmd file to a PDF. Pandoc is linked to a .bib file and automatically inserts the bibliography at the end of the PDF
The entries in my .bib file look like these, taken from http://johnmacfarlane.net/pandoc/demo/biblio.bib:
#Book{item1,
author="John Doe",
title="First Book",
year="2005",
address="Cambridge",
publisher="Cambridge University Press"
}
#Article{item2,
author="John Doe",
title="Article",
year="2006",
journal="Journal of Generic Studies",
volume="6",
pages="33-34"
}
To build my bibliography, I'm using the following function, taken from: http://quantifyingmemory.blogspot.co.il/2013/02/reproducible-research-with-r-knitr.html
knitsPDF <- function(name) {
library(knitr)
knit(paste0(name, ".Rmd"), encoding = "utf-8")
system(paste0("pandoc -o ", name, ".pdf ", name, ".md --bibliography /Users/.../Desktop/test.bib --csl /Users/.../Desktop/taylor-and-francis-harvard-x.csl"))
}
The contents of my .Rmd file is:
This is some text [#item1]
This is more text [#item2]
# References
And outputted PDF looks like this:
If I try to insert an appendix, the references still print at the end of the document, like this:
How do insert an appendix after the references?
With newer pandoc versions, you can specify the bibliography's position with <div id="refs"></div> source
This is some text [#item1]
This is more text [#item2]
# References
<div id="refs"></div>
# appendix
Eventually reference handling will change to make it possible to put the references wherever you like (https://github.com/jgm/pandoc/issues/771), but right now there's no easy way to do it.
As suggested here, you could put your appendix in a separate file, use pandoc to convert it to a LaTeX fragment, then include that fragment using the --include-after-body flag. It would then come after the bibliography.
When working in an Rmarkdown document, enter the following text where the citations are to be located. It can be placed in any part of the document allowing other materials, like an appendix, to follow as necessary. The method relies on pandoc's fenced divs which will work in Rmarkdown.
::: {#refs}
:::
The aforementioned code should not be in an R code chunk, rather it should be placed on blank lines by themselves. Once processed by pandoc via knitter, this code will produce the same result as <div id="refs"></div> mentioned in the answer by #soca. The two lines of code do consistently allow for exact placement of the references in any section of the document.
In the example below, references are placed first under a heading of the same name while all of the code chunks in the document are placed afterwards in a code appendix. Here is the pandoc fenced div placed in Rmarkdown that can be used to generate the image that follows.
# References
::: {#refs}
:::
# Appendix A: R Code
```{r ref.label=knitr::all_labels(), echo=TRUE, eval=FALSE}
```
Provided there is a .bib file identified in the yaml frontmatter, the preceding Rmarkdown produces output similar to the following:
Helpful links:
Pandoc User’s Guide - Placement of the Bibliography
Pandoc User’s Guide - Divs and Spans
How can the position of the bibliograpy section be set Latex format
9.6 Custom blocks (*) | R Markdown Cookbook

Creating a title and numbering of sections on pdf while using R markdown with pandoc

I am using the markdown document created by R and I am trying to create the pdf file from the markdown using pandoc. Everything else works fine but I want the title of the document to appear and the numbering on the sections as in default Latex document. It seems the title defined on rmarkdown appears as a section title for the pdf.
I was able to create double spacing, enter line numbers etc by using a options.sty file. Below is the code that I used to create a pdf.
For options.sty I used:
\usepackage{setspace}
\doublespacing
\usepackage[vmargin=0.75in,hmargin=1in]{geometry}
\usepackage{lineno}
\usepackage{titlesec}
\titleformat{\section}
{\color{red}\normalfont\Large\bfseries}
{\color{red}\thesection}{1em}{}
\titleformat{\subsection}
{\color{blue}\normalfont\Large\bfseries}
{\color{blue}\thesection}{0.8em}{}
\title{Monitoring Stations}
\author{Jdbaba}
I used knitr to create the R markdown file. In the above options.sty file, it seems the program is not taking title and author part. It seems I am missing something.
The code I used to convert markdown to pdf is as follows:
pandoc -H options.sty mydata.md -o mydata.pdf
In latex document, the pdf would have the automatic numbering as well. But my pdf is missing that. Can anyone suggest how numbering can be enabled on the pdf document created using pandoc ?
Thanks.
Pandoc takes the title from a title block in the Markdown file. This is a Pandoc-specific extension to Markdown. The block should have the following format:
% title
% author(s) (separated by semicolons)
% date
So, in your case:
% Monitoring Stations
% Jdbaba
% March 6, 2013
To have the sections numbered, you'll need to run Pandoc with the --number-sections option.

Resources