I want to change the default template of the Rmarkdown document. I edited the file named document.Rmd in the templates in the RStudio application folder so that the YAML is:
---
title: 'document'
output:
html_document:
self_contained: true
toc: true
toc_float: true
bookdown::pdf_document2:
extra_dependencies: ["float"]
toc: true
---
However, when I open a new Rmarkdown document from RStudio, I get a prompt that asks what type of Rmarkdown document I want. I choose HTML, click OK, and then I get the file opened with these two YAMLs!
---
title: "Untitled"
output: html_document
---
---
title: 'document'
output:
html_document:
self_contained: true
toc: true
toc_float: true
bookdown::pdf_document2:
extra_dependencies: ["float"]
toc: true
---
I do not want to go through the hustle of creating a package or a template. This simple solution should work, I think.
My question: Does anybody know how to work around this? Thank you in advance!
After reading your question, I found this source, which indicated that you could add a template. I also read that document.RMD does not include the YAML. That's why you are getting a double-YAML.
Since I had never added a template for R Markdown, I went through the steps to see how it all came together. That led me to create a repository on Github, and well, here we are. Essentially, I made a Github repository that you can install with the exact template you wanted.
You can also look at the code, as well. Since it was created just to answer this question, if you wanted it to be different, you can change it, as well.
You'll find the repository here: fraupflaume/customYAML
If you want to install it:
devtools::install_github("fraupflaume/customYAML")
To use it, you don't have to call the library (there's no R code). Start an R Markdown script, select "From Template," then choose "Custom YAML."
Related
I am writing my thesis using the bookdown package and the memoir latex class. Everything works relatively fine when I am exporting to pdf or to html but I am unable to export the thesis to a word document...
I get the following mysterious error:
Error in files2[[format]] :
attempt to select less than one element in get1index
It is difficult to provide a reproducible example though, as I am working from my messy dissertation repository.
But here is (a part of) my _output.yml file:
bookdown::pdf_book:
includes:
in_header: latex/preamble.tex # defines style and latex options
before_body: latex/before_body.tex # defines cover page
latex_engine: xelatex # lualatex or xelatex
citation_package: none # needs to be "none" in order to use the csl file
keep_tex: true # keeps the .tex file
dev: "cairo_pdf"
toc: false # deactivates default table of contents
highlight: pygments # code highlighting
pandoc_args: [ "--csl", "./csl/apa6.csl" ] # specifies the csl file to be used
bookdown::word_document2:
pandoc_args: [
"--csl", "./csl/apa6.csl",
"--bibliography", "./bib/packages.bib",
"--bibliography", "./bib/thesis.bib",
#"--reference-doc", "./assets/2018-05-17-aim1-draft.docx",
"--filter", "pandoc-citeproc"
#"--filter", "./assets/fix-apa-ampersands.py"
]
Any idea ?
This is a bug of the bookdown package, which I just fixed on Github. Please try the development version there:
remotes::install_github('rstudio/bookdown')
I have recently learned that modifying the default (Pandoc?) template for a Rmd pdf report gives access to some neat customisation.
So in order to modify the default template I first move it to my working directory using:
file.copy(system.file("rmd/latex/default-1.17.0.2.tex", package ="rmarkdown"), "template.tex")
Next I type up my Rmd file:
---
output:
pdf_document:
template: template.tex
keep_tex: true
latex_engine: xelatex
includes:
in_header: in_header.tex
number_sections: true
---
This is a rmd kind of document.
Where in_header.tex includes only one line
\geometry{a4paper, top=38mm, left=45mm, right=45mm}
I first just want to run the Rmd file with the template as is (expecting no change) but the report fails to compile if and only if I include the template - otherwise it runs without an issue.
The error is
! Undefined control sequence.
l.58 \geometry
[...]
Question
How can I use the template argument to build on an editable template in my working folder without causing any other change to my current setup.
Info
> R.Version()[["version.string"]]
[1] "R version 3.5.1 (2018-07-02)"
> packageVersion("rmarkdown")
[1] ‘1.10’
> packageVersion("knitr")
[1] ‘1.20’
As you are already driving this from R, you may as well use the (excellent) example set up by the rticles package which provides a larged number of LaTeX customizations for (academic) papers.
And you can then run with that them. For example, over the last two years I added these packages:
tint for a modern 'Tufte-alike' writeup
pinp for very nice two-column pdf vignettes
link for LaTeX letters and some extra
binb for variants of beaner packages
This allows you to
set a custom template.tex
include whichever LaTeX class files / style files you need
programmatically set options
I find this preferable to copying stanzas around which I did previously (eg for slides).
The rmarkdown::pdf_output function seems to do a few things differently when the default template is used. For example, it sets the variable graphics=yes. I suspect that it also sets geometry, but I have not seen where it does that. Anyway, if you want to use a custom template based on the default template you are "fighting" with this block from the template:
$if(geometry)$
\usepackage[$for(geometry)$$geometry$$sep$,$endfor$]{geometry}
$endif$
The geometry package is only loaded, when the geometry header is defined. One simple solution to your problem is to move the geometry definition from the header include to the YAML header:
---
geometry:
- a4paper
- top=38mm
- left=45mm
- right=45mm
output:
pdf_document:
template: template.tex
keep_tex: true
latex_engine: xelatex
includes:
in_header: in_header.tex
number_sections: true
---
This is a rmd kind of document.
With this you can remove \geometry{...} from in_header.tex.
I'm using R-markdown for a couple of reports. I do it with self contained graphics because I send it via email.
This worked till recently. But now the images are not shown anymore (with the error of a cross in a box). It might be that I updated the Version of RStudio. I used this a the top of the script:
---
title: "blablablab"
author: "blablablab"
output:
html_document:
fig_caption: yes
toc: no
toc_depth: 2
toc_float: no
number_sections: false
self_contained: yes
---
I know that this question is not very specific. I just wanted to ask if there is anybody which also had self contained graphics which suddenly didn't work anymore.
Thanks for the help in advance!
Greetings Dani
I just had same problem. In RStudio i had previously:
output:
html_document:
self_contained: yes
and it always worked properly producing self-contained HTML. TODAY it stopped working properly. I did update Rstudio a few weeks ago and might not have noticed problem, not sure. SO now i changed to:
output:
html_document:
self_contained: yes
mode: selfcontained
Which seems strange and redundant with two separate lines trying to make things self-contained BUT it worked.
As a sanity check, make sure that you do not have the option include=FALSE in your global chunk options opts_chunk$set
When that is the case, the images are created and stored in the figure-html directory, but not embedded in the html file.
EDIT: After some investigation, this question is really about the following option in the output yml:
citation-package: biblatex
Without this option, bookdown is using the default citeproc and it's not clear how to modify the number of authors. However, when this option is used, the referencing is not working anymore and my document contains only the refnames in bold instead of inline citations. So I really need to know why the citation-package: biblatex is not working
===== original question below
I can't get bookdown to honour my maxcitename=2 settings. I have tried using this output yml
output:
bookdown::pdf_book:
includes:
in_header: preamble.tex
keep_tex: yes
toc_depth: 3
toc_appendix: yes
with this line in the preamble.tex file:
\usepackage[backend=bibtex, maxcitenames=2, style=authoryear]{biblatex}
I have also tried using this output yml:
bibliography: [likertimputebiblio.bib, packages.bib]
biblatexoptions: [maxcitenames=2]
csl: harvard-university-of-wolverhampton.csl
link-citations: true
nocite: |
#R-bookdown
and I have also tried this output yml:
site: bookdown::bookdown_site
documentclass: book
header-includes:
- \usepackage[backend=bibtex, maxcitenames=2, style=authoryear]{biblatex}
but nothing seems to work.
Please help. Thanks.
The solution to this problem was found after much perseverence!
When setting the output yml, indented under output: etc...
citation_package: biblatex
... the in line references were failing to link to the .bib file and so the refnames were just showing up in bold and failing to make any inline citations.
The expected solution should be to use the additional option:
biblatexoptions: [backend=bibtex, maxcitenames=2]
(maxcitenames=2 is the main reason I want to use biblatex) but this was failing with the error "option backend not recognized."
Finally the solution was to modify the default template in the directory
C:\Program Files\R-3.4.0\library\rmarkdown\rmd\latex
at line 100, from
\usepackage$if(biblio-style)$[style=$biblio-style$]$endif${biblatex}
to
\usepackage$if(biblio-style)$[backend=bibtex, style=$biblio-style$]$endif${biblatex}
I would like to suggest to the package author that this is a bug that needs fixing, because backend=bibtex is a valid option and should have been passed through
I would like to be able to specify multiple output formats at the same time, for instance html_document and a pdf_document. I know that this can be done very simply with something like
---
output: [html_document, pdf_document]
---
I might have some of that syntax off, but I can not seem to find the documentation anywhere. I have recently discovered knitr-bootstrap and love it. It is what I have been looking for to be able to dynamically hide my code and output blocks.
Unfortunately, by default, the YAML block for the knitr-bootstrap invocation is quite complex and I do not know how to specify multiple outputs for this.
I have looked at the YAML spec and tried a few different things but I am at a loss. Below is my current YAML frontmatter.
---
title: "Beta Regression Comparison"
opset: bootstrap
output:
knitrBootstrap::bootstrap_document:
title: "Beta Regression Comparison"
theme: Simplex
highlight: Solarized - Light
theme.chooser: FALSE
highlight.chooser: FALSE
menu: FALSE
pdf_document
---
The solution is to change pdf_document to pdf_document: default. I can't unfortunately find a reference for this syntax in the official documentation. If however you open a RMarkdown document in a recent version of RStudio, click Knit HTML and then Knit PDF, it uses this : default syntax.
The syntax is:
---
output:
html_document:
keep_md: yes
pdf_document: default
---
In my case, I tried to knit multiple output-documents using bookdown and found this post which allowed me to get the desired result.
You can write the output-definition in your YAML header as follows:
---
output:
bookdown::pdf_document2:
template: "path-to-my-template"
bookdown::word_document2:
default
knit: (function(inputFile, encoding){
rmarkdown::render(inputFile, encoding = encoding,
output_dir = "my-output-path", output_format = "all") })
---