I want to insert an image in a R Markdown report, so i used this:
![Uniform Crossover](C:\Users\Miguel Prada\OneDrive\Documentos\Estudio\Invest UPM\ProjectANN\reports\crossover.png)
This works, but the image is located in the top of the page, not in the place where i wrote the code.
Does anyone know why this is happening?
Thanks
As of 2019, perhaps the best solution is to make a Latex preamble for your Rmd file as explained here. In other words:
Save following preamble.tex file in your working directory:
\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
\expandafter\origfigure\expandafter[H]
} {
\endorigfigure
}
Note that the "H" tells the image to go where you insert it. Then call for the preamble.tex in the YAML header of your Rmd file:
---
title: "example"
author: "you"
date: "`r format(Sys.time(), '%d %B %Y')`"
output:
rmarkdown::pdf_document:
fig_caption: yes
includes:
in_header: preamble.tex
---
![Uniform Crossover](C:\Users\Miguel Prada\OneDrive\Documentos\Estudio\Invest UPM\ProjectANN\reports\crossover.png)
Reply from comments
From #tung
Try knitr::include_graphics + use fig.xxx chunk option and see more in options/#plots
From #r2evans
If you look up other discussions on how LaTeX deals with figure/picture/table placement, you'll see that it has its idea of what is best, defaulting to the top of the page following the paragraph to which it is "anchored".
There are ways to override this, and perhaps knitr's can help, where you encourage LaTeX's optimization engine to bias in different directions. So, since you're producing PDFs, expand your learning to understand its use of \begin{figure}[fig.pos].
Related
I am preparing a manuscript for publication and need a way to change (remove actually) the background color of code chunks in my PDF file (generated through Latex).
Reproducible example:
---
title: TestFile
output:
pdf_document: null
---
```{r,echo=TRUE}
test=c(1,2,3)
print(test)
```
```{r,echo=TRUE}
test=c(1,2,3)
print(test)
Another option, is to use Pandoc Syntax Highlighting styles, which is very simple and has very nice options. Compared to the LaTeX option you used, this is perhaps less flexible, but has the advantage of changing not only the background of code chunks, but also the font colour.
To do this, you only have to add the highlight style you want to the YAML. For example, adding the zenburn highlight:
output:
pdf_document:
highlight: zenburn
Will produce this:
Instead of this:
I hope this helps, but let me know if not.
I think I've found a solution. This is a purely latex solution, but it does the trick for what I need (which is outputting to PDF via latex) using the package xcolor. [PDF output] https://i.stack.imgur.com/yEADg.png
Reproducible Example:
---
title: TestFile
output:
pdf_document: null
header-includes: \usepackage{xcolor}
---
\definecolor{shadecolor}{RGB}{255,255,255}
```{r,echo=TRUE}
test=c(1,2,3)
print(test)
```
I use Bookdown with a pdf output.
In my document, I include images generally using the method
\![\label{imagelabel}image title](image_path.png).
I would like to know if it is possible, in addition to a title, to add comments to the image. I would like to see "Figure #: Image Title. My comments (e.g. this figure shows that...)", but that the comments are not displayed in the List of Figures.
Is this possible and if so, how?
Thank you in advance!
I don't use bookdown, but it's a close relative of pdf_document in rmarkdown. This works there:
---
title: "image.Rmd"
output:
pdf_document:
keep_tex: true
toc: true
---
```{r}
knitr::opts_chunk$set(dev='pdf')
```
\newcommand{\comment}[1]{}
\listoffigures
```{r theplot,fig.show="hide"}
plot(rnorm(1000))
```
![\label{thefig}This is the caption\comment{this is the comment}](image_files/figure-latex/theplot-1.pdf)
Interestingly, the comment doesn't show up in the .tex file, it was removed by Pandoc. If you actually do want to see the comment in the output, you can turn it on using something like
\newcommand{\comment}[1]{\textit{#1}}
in place of the definition above.
I have defined certain pandoc options in _output.yml:
```
author: "abc"
date: "`r format(Sys.time(), '%d %B, %Y')`"
```
But, these get ignored, unless these are defined in the YAML header inside the R Markdown file.
Is this prohibited from including in the separate YAML file? Is so, could you please mention what other parameters should be defined inside the Markdown file.
An _output.yml file can only be used for setting the output formats, as explained within the bookdown book. So you can specify anything which relates to the output format (HTML/PDF/Word) such as:
html_document:
toc: TRUE
theme: flatly
pdf_document:
toc: FALSE
In your examples, the parameters you provide are about document content. So anything like author, title, date, fontsize can't be specified.
This issue has also been addressed within the GitHub issues of R Markdown for further reading.
I am using Rstudio, to create a pdf / html document from an Rmd file. The header looks sth like this:
title: "Title"
author: "Me"
date: "`r format(Sys.time(), '%B %d, %Y')`"
bibliography: bibliography.bib
output:
html_document:
toc: true
number_sections: true
Now, I have some sections, and then include the references. After that, an appendix should follow, but I encounter the exact same problem as described here: Pandoc insert appendix after bibliography
There is a fixed solution in this thread, but I have no idea how I can do that within RStudio directly. To get the document, I just press the "Knit html" button, and do not run any pandoc commands myself. So where should I put the
--include-after-body
part, and how should the appendix rmd file look like?
As noted in the rmarkdown manual, you could use this syntax:
---
output:
html_document:
includes:
after_body: appendix.md
---
This is equivalent to the general way to add arbitrary pandoc arguments to a Rmd file:
---
output:
html_document:
pandoc_args: ["--include-after-body=appendix.md"]
---
The following might be easier; works if you knit to PDF, Word or HTML:
Everything I wanted to say in the main document.
# References
<div id="refs"></div>
\newpage
# Appendix
Some details that will bore the readers of the main document.
In the original post, this was also posted as an answer (few years after current question was asked): see https://stackoverflow.com/a/44294306/8234333 and https://stackoverflow.com/a/16428699/8234333
By default the PDF documents created by the Knit PDF are US Letter size. Instead I would like to create A4 size documents. I have a feeling this should simple to change, either in the RStudio GUI or by adding an option to the metadata at the top of the Rmd file. Unfortunately I can't find any instructions how to do this. Is there a way to specify paper size, preferably within the Rmd file itself? I am still using RStudio version 0.98.953 but can upgrade if it would help.
I'd be grateful if someone could point me in the right direction.
OK, so I figured it out. In the .Rmd file's header, options documentclass and classoption get written into the preamble of the resulting .tex file. The article document class accepts a number of paper size options including a4paper. The header in the .Rmd file will then look something like this:
---
title: "Title"
author: "Name"
date: "Date"
output:
pdf_document
documentclass: article
classoption: a4paper
---
For more information see: http://rmarkdown.rstudio.com/pdf_document_format.html
At least in newer versions of the rmarkdown R package (and Pandoc) you can just set:
---
output: pdf_document
papersize: a4
---