When I knit a PDF using RMarkdown, it automatically creates bookmarks in the PDF file from any headers I use.
For example a "bookmark" in the PDF is created when I do:
this is a header 2
Is there any way to prevent this from happening?
Thanks.
Looks like you have to modify the latex template.
https://github.com/rstudio/rmarkdown/blob/master/inst/rmd/latex/default.tex#L105
Set bookmarks=false at that line above. Then save as mytemplate.tex file to your working directory, and use this option in your front matter:
output:
pdf_document:
template: mytemplate.tex
Related
I am using rmarkdown to generate both HTML and pdf file. In my .Rmd file, I included a GIF like this:
![](www/script.gif)
When I knit the to HTML it works fine.
rmarkdown::render(documentation_file, encoding="UTF-8")
However, when I try to knit to PDF using
rmarkdown::render(documentation_file, rmarkdown::pdf_document(latex_engine = "xelatex"), encoding="UTF-8")
I have the following problem:
! LaTeX Error: Unknown graphics extension: .gif.
I do not mind to lose the animation of the gif, a static version of it is perfectly fine.
Is there any easy way to include/convert on the fly the GIF to my PDF document?
It's not possible to directly include GIFs in a LaTeX document.
In general LaTeX, you can only include GIFs if you use latex to compile your document; when using pdflatex, xelatex and lualatex you need to manually convert your figure to e.g. PNG, JPG or PDF.
RMarkdown by default uses pdflatex; while you may change the LaTeX engine by specifying e.g. latex_engine: xelatex below pdf_document in the YAML header of your document, it is not possible to use latex to compile (latex would first create a DVI file, which is then converted to a PS and then in turn to a PDF).
So the easiest (and only) solution would be to convert all GIF figures to PNGs (or JPGs), and then include them as images in your RMarkdown document.
Try this in your chunk
if (knitr:::is_latex_output()) {
knitr::asis_output('\\url{....}')
} else {
knitr::include_graphics("script.gif")
}
And in the url put the url to your gif
You could create a snapshot image of the first frame of the gif, https://stackoverflow.com/a/12554723/10346727
I don't think PDF supports gif format, or any moving format for that matter.
How do I change an .Rmd file to an .md file, that I can then push to GitHub? I have knit to HTML
Is it possible to do this by something as simple as changing the file extension to ".md" and doing a save as? Any insight into what the differences are amongst these types of files would be so helpful!
Also, how different should an .Rmd and .md file look when opened?
Thank you!
You can add keep_md to your YAML header.
---
output:
html_document:
keep_md: true
---
Just be aware you will need to also push any related assets to Github as well as the .md file
You can use
knitr::knit("my_file.Rmd")
which should output my_file.md (a Markdown file with included code chunks) and generate figures (in a figs/ directory, I think, not sure).
Is that possible at all? At the moment I have to render my .Rmd files twice, once for an html and once for a pdf report. Each of them take about 50 mins. So if I can use the html .md files created after rendering, that would save me 50 mins.
You can keep the markdown output of knitr with keep_md: yes
---
output:
html_document:
keep_md: yes
---
Then, using pandoc, you can produce your pdf from this file; in a terminal (not a R console):
pandoc mydoc.md -o mydoc.pdf
You'll have to install pandoc if you haven't already, to use it without knitr.
You might loose a few things, though, because knitr adapts its md intermediary file to the final output.
It is possible to render multiple outputs, but not with the "knit" Button in RStudio. Write your desired output in the YAML header and then use output_format = "all" as argument in
rmarkdown::render(<your-rmd-file.rmd>, output_format ="all")
So the YAML header looks like:
title: "multiple outputs"
output:
pdf_document: default
html_document: default
Or any option you want to set for the different output formats.
If your .md document is already created, you can simply use:
library("rmarkdown")
render("mydoc.md", output_format = "pdf_document")
If you always need to generate both html and pdf, then render both at the same time using J_F's solution.
Alternatively, you can tell the compiler to keep the intermediate .md file. Then, you can easily compile that into other formats.
Go to:
1. RStudio > Open your .Rmd file
2. Click on the Gear (settings) drop down and choose Output Options
3. Choose Advanced
4. Check Keep markdown source file
5. Click OK
6. Knit to HTML
7. Open .md file in editor
8. In Gear (settings) drop down, select PDF as Output Format, AND select (No Preview)
9. In the Preview dropdown, select Preview PDF and a .pdf file should be created
As an alternative to steps 2-5, edit your file so that the header includes:
---
title: "blah blah"
output:
html_document:
keep_md: yes
---
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 ---.
Using RStudio's "Compile Notebook to html" feature, I noticed that a temporary .md file was created in the process, but deleted automatically. At one point I was lucky enough to see its content, and it is exactly what I need: the code chunks alternate with output chunks, all perfectly formatted.
So my question is: how do I generate such an .md file directly form an R script?
You could also run knitr::spin() directly from the R console.
If you run "Knit to HTML", a markdown file is temporarily created and deleted. To keep the markdown file, add the following to the YAML code at the top of the RMD file.
---
output:
html_document:
keep_md: true
---
Here is a helpful reference: https://bookdown.org/yihui/rmarkdown/html-document.html#keeping-markdown