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
Related
I can't seem to hack my way through all the possibilities and have a full bibliography inserted in the PDF output of RStudio, knitr, an .Rnw script, and the "Compile PDF" button. The desired text in the PDF would be the details of the cited works.
Here is a Lilliputian bibtex file, called jabrefbibtest.bib, saved in the working directory.
#Book{GreentargetEngagement2012,
Title = {"2012 - In - House Counsel New Media Engagement Survey"},
Author = {"Inside Counsel "},
Publisher = {"Greentarget"},
Year = {"2012"},
Pages = {"20"},
Plots = {"9"},
Tables = {"0"},
Url = {"http://www.greentarget.com/wp-content/uploads/2012/01/2012GTZGICSurveyReportFinal-WebsiteVersion.pdf"}
}
#Book{CitiprivateBank,
Title = {"Intellectual Leadership with Law Watch"},
Author = {""},
Publisher = {""},
Year = {"2008"},
Pages = {"2"},
Plots = {"1"},
Tables = {"4"},
Url = {"http://www.citigroup.com/privatebank/lawassociates/pdfs/lawwatch/slipsheet.pdf"}
}
The .Rnw script, stripped down, is
\documentclass[11pt]{article}
\usepackage[backend=bibtex]{biblatex}
% \addbibresource{} # not sure if this is needed
\begin{document}
<<bibbackground, echo=FALSE, include=FALSE>>=
setwd("~/R/knitr docs/")
Sys.setenv(TEXINPUTS=getwd(),
BIBINPUTS=getwd(),
BSTINPUTS=getwd())
#
\bibliographystyle{plain}
\bibliography{jabrefbibtest}
Here is one citation \cite{GreentargetEngagement2012} and here is a second \cite{CitiprivateBank}.
Now do full References show below?
\printbibliography
\end{document}
The log:
! Package biblatex Error: '\bibliographystyle' invalid.
See the biblatex package documentation for explanation.
Type H <return> for immediate help.
...
l.59 \bibliographystyle{plain}
Use the package option 'style' instead.
I'm ignoring this command.
! LaTeX Error: Can be used only in preamble.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.60 \bibliography
{jabrefbibtest}
Your command was ignored.
Type I <command> <return> to replace it with another command,
or <return> to continue without it.
LaTeX Warning: Citation 'GreentargetEngagement2012' on page 1 undefined on inpu
t line 62.
[more omitted]
Along with The Latex Companion, Dynamic Documents with R and knitr, by Yihui Xie, two LaTeX primers and the 262-page manual for biblatex I have struggled through the complicated advice of these sites. Fruitlessly.
https://tex.stackexchange.com/questions/71565/knitr-and-biblatex
https://tex.stackexchange.com/questions/63852/question-mark-instead-of-citation-number
http://texblog.org/2013/08/20/rknitr-automatic-bibliography-generation-with-biblatex-in-rstudio/
http://www.inside-r.org/packages/cran/knitcitations/docs/bibliography
EDIT after COMMENTS
All the PDF file has is this:
References
Here is one citation [?] and here is a second [?].
Now do full References show below?
As the error messages told you:
Don't use \bibliographystyle{plain} (this does not work for biblatex); use the style option in \usepackage[]{biblatex} instead;
\bibliography{jabrefbibtest} must be put in the preamble instead of the body.
After you correct these issues, it should work:
\documentclass[11pt]{article}
\usepackage[backend=bibtex]{biblatex}
\bibliography{jabrefbibtest}
% or use \addbibresource{jabrefbibtest.bib}
\begin{document}
Here is one citation \cite{ABFWomenFirstChairs2015} and
here is a second \cite{ACCGCSkills2013}.
Now do full References show below?
\printbibliography
\end{document}
BTW, RStudio probably does not support the default backend biber of biblatex, so the backend=bibtex option was used.
I use exactly this setup below to get (note I'm not a fan of changing wd in knitr/rmarkdown and removed this; also your keys in the Rnw didn't match the key in the mwe):
\documentclass[11pt]{article}
\usepackage[american]{babel}
\usepackage[style=apa,backend=biber,bibencoding=latin1]{biblatex}
\DeclareLanguageMapping{american}{american-apa}
\addbibresource{jabrefbibtest.bib}
\begin{document}
<<bibbackground, echo=FALSE, include=FALSE>>=
#setwd("~/R/knitr docs/")
Sys.setenv(TEXINPUTS=getwd(),
BIBINPUTS=getwd(),
BSTINPUTS=getwd())
#
%\bibliographystyle{plain}
Here is one citation \cite{GreentargetEngagement2012} and here is a second \cite{CitiprivateBank}.
Now do full References show below?
\printbibliography
\end{document}
Also after the Rnw knits I have to run the tex file though a LaTeX compiler to render the references the first time.
I always just place \bibliography{jabrefbibtest} at the end of the script where I want the references to occur.
To include all references from your .bib file in your bibliography, i.e. even ones that you didn't end up citing, include the line
\nocite{*} right before the line \printbibliography
I writing a Word document with R markdown in R Studio. I can get many things, but at the moment I am not figuring out how can I get a page break. I have found solutions but only for rendered latex / pdf document that it is not my case.
Added: To insert a page break, please use \newpage for formats including LaTeX, HTML, Word, and ODT.
https://bookdown.org/yihui/rmarkdown-cookbook/pagebreaks.html
Paragraph before page break.
\newpage
First paragraph on a new page.
Previously: There is a way by using a fifth-level header block (#####) and a docx template defined in YAML.
After creating headingfive.docx in Microsoft Word, you select Modify Style of the Heading 5, and then select Page break before in the Line and Page Breaks tab and save the headingfive.docx file.
---
title: 'Making page break using fifth-level header block'
output:
word_document:
reference_docx: headingfive.docx
---
In your Rmd document, you define reference_docx in the YAML header, and now you can use the page-breaking #####.
Please see below.
https://www.r-bloggers.com/r-markdown-how-to-insert-page-breaks-in-a-ms-word-document/
With the help of John MacFarlane and others on the pandoc google group, I put together a filter that does this. Please see:
https://groups.google.com/forum/#!topic/pandoc-discuss/FzLrhk0vVbU
In short, the filter needs to look for something to replace with the openxml for pagebreak. In this case
\newpage
is being replaced with
<w:p><w:r><w:br w:type=\"page\"/></w:r></w:p>
This allows for a single latex markup to be interpreted for both pdf and word output.
Joel
What you are trying to do is force a "page break" or "new page" in a word document generated with Pandoc. I have found a way to do this in my environment but I'm not sure it will work in every environment.
My environment:
* R-studio / Pandoc / MS-WORD starting with an "*.Rmd" file and generating a DOCX file.
In my RMD file the key idea is that i've created what acts like a TEMPLATE document (MyFormattingDocument.docx) and in that word document I tweak the STYLES for things like "Heading 1" and/or "Heading 2" and or "footnote" or whatever other predefined styles I want to tweak.
(SEE THIS: http://rmarkdown.rstudio.com/word_document_format.html#style-reference ) for explanation of style reference and how to set the header information in your RMD file to specify a reference document.
SOOOO in my case... i tweak the "Heading 1" style in WORD to include a forced "Page Break Before" in the Paragraph formatting for "Heading 1". Exactly how you force every "Heading 1" to always "Page Break" is different in different versions of Microsoft WORD but if you follow the WORD documentation and modify the "Heading 1" style THEN every "Heading 1" will always have a pagebreak before it.
THEN... you save this template file in the some directory you're working from with the RMD file... and it is USED AS a template. THE CONTENTS of the file are ignored.... so don't worry... you can put sample text in this file and test that the formatting all works.... THE CONTENTS ARE IGNORED but the STYLES are USED in the new word document which will be built by the RMD file so.... then every "Heading 1" will have a break before it.
NOTE: You could obviously do the same with ANY style that has a one-to-one mapping from PANDOC MARKUP so you could instead just make all "Heading 3" or whatever.... just look at see in your RMD created DOCX what "STYLE" is being applied and then tweak that style even if you need to insert some "fake" lines with essentially blank content just for the purpose of forcing a style to appear in the DOCX
Here is an R script that can be used as a pandoc filter to replace LaTeX breaks (\pagebreak) with word breaks, per #JAllen's answer above. With this you don't need to compile a pandoc script. Since you are working in R Markdown I assume one has R available in the system.
#!/usr/bin/env Rscript
json_in <- file('stdin', 'r')
lat_newp <- '{"t":"RawBlock","c":["latex","\\\\newpage"]}'
doc_newp <- '{"t":"RawBlock","c":["openxml","<w:p><w:r><w:br w:type=\\"page\\"/></w:r></w:p>"]}'
ast <- paste(readLines(json_in, warn=FALSE), collapse="\n")
ast <- gsub(lat_newp, doc_newp, ast, fixed=TRUE)
write(ast, "")
Save this as page-break-filter.R or something like that and make it executable by running chmod +x page-break-filter.R in the terminal.
Then include this filter the R Markdown YAML like so:
---
title: "Title
author: "Author"
output:
word_document:
pandoc_args: [
"--filter", "/path/to/page-break-filter.R"
]
---
You can use the R package worded. This avoids the need for a template word file. See https://github.com/davidgohel/worded.
The output parameter needs to be set to worded::rdocx_document and you need to call library(worded).
---
date: "2018-03-27"
author: "David Gohel"
title: "Document title"
output:
worded::rdocx_document
---
```{r setup, include=FALSE}
library(worded)
```
You can then add <!---CHUNK_PAGEBREAK---> to your document whenever you want a page break.
The package allows various word formatting options using a similar mechanism.
When updating to R 4.0.0, the <!---CHUNK_PAGEBREAK---> solution was not working any more for me.
Instead I could use the run_pagebreak() function from the officer package, still in combination with the officedown package:
---
output: word_document
---
```{r settings}
library(officedown)
library(officer)
```
Hello world on page 1
`r run_pagebreak()`
Hello world on page 2
R Markdown 1.16 introduced a new feature which allows to insert a page break by adding a paragraph that contains only the commands \pagebreak or \newpage:
Paragraph before page break.
\pagebreak
First paragraph on a new page.
See also the pagebreaks section in the R Markdown cookbook.
It is not an automated solution. But I have been adding the text '#####page break' to my markdown document. Then in MS Word using find-replace to replace the text "page break" with "^m" (manual page break).
Sungpil's article was close, but didn't quite work. This was the best solution I found for this:
https://scriptsandstatistics.wordpress.com/2015/12/18/rmarkdown-how-to-inserts-page-breaks-in-a-ms-word-document/
Even better, the author included the Word template to make this work. The R-blogger's link to his template is broken, and the header is formatted wrong. Some notes I took:
1) You might need to include the whole path to the word template in your Rmd header, like so:
output:
word_document:
reference_docx: C:/workspace/myproject/mystyles.docx
2) The template at the link above changed some of the default style settings so you'll need to change them back
My solution is not very robust but can work for some of us.
Assuming you need a page break before each level 1 title in your word document, I defined this in the format template used in the yaml field reference_docx: .
In this document you modify the Heading 1 format (or equivalent) to insert a page break before the Title. Do not forget to start your template with the first docx rendered with knitr (pandoc) in RStudio.
Ok, I found this in the markdown docs.
Horizontal Rule / Page Break
Three or more asterisks *** or dashes ---.
I am working with markdonw v2, the rmarkdown package. Throughout the .Rmd file, I create links to websites or images
[Link1][pathLink1]
![Image1][pathImage1]
then, at the end of the document I give the references
[pathLink1]:http://website.com/linkes/Link1.md
![pathImage1]:./images_rmd/
There are other reports that talk about the same citation and use same images in different contexts. I would like to create a separate file containing all the links and path difinitions, so that I could simply source it at the end of each .Rmd file, like I would call in an R environment
source(/Rcode1.R)
Question: How do I "source" another file in .Rmd, so that the sourced code prints needed text strings into the .Rmd file?
This would offer some help with citations and scientific paper composition in HTML and PDF.
http://yihui.name/knitr/demo/child/
```{r child, child = '~/path/to/child.Rmd'}
```
and similarly for .Rnw files:
<<child, child = '~/path/to/child.Rnw'>>=
#
And a full example: https://github.com/yihui/knitr-examples/blob/master/087-child-example.Rnw
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)
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.