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.
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'm wondering how to change font and color specifically for figure captions in Rmarkdown for a PDF output. I know how to do this for word output (creating a separate document), but often figures are submitted to journals as PDFs.
Note that this is not whether you can change the color of text in a PDF which is answered here: Changing the font size of figure captions in RMarkdown pdf output. If I try this within the fig.cap = "fig text here.\\label{...}" I get the following error:
! Undefined control sequence.
<recently read> \cellcolor
This probably comes closest: Changing the font size of figure captions in RMarkdown pdf output, but I went to CTAN and downloaded the package and am not sure whether I have to find my R libraries and drag it to there, or what. Perhaps this is the only/easiest solution, if so, can you advise on how I should proceed with the downloaded package. Haven't done a lot in latex, so it would probably be finding how colors are defined in latex as well?
Thanks much!
James
Here is my header:
output:
pdf_document:
keep_tex: true
html_document: default
header-includes:
\usepackage{placeins}
---
You could try this:
---
title: "Untitled"
author: "bttomio"
date: "6/13/2021"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r pressure, echo=FALSE, fig.cap="\\textcolor{red}{This is red} and \\textcolor{blue}{this is blue}"}
plot(pressure)
```
-output
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].
I recently asked
Changing the font size of figure captions in RMarkdown HTML output
and I got a very nice answer which uses this CSS method. I wanted to try the same, but this time with Word output. If you don't want to read my former question, I summarize the issue here: I'd like to make the font size of all figure captions in my R Markdown document smaller. The final output is Word,this time, and I'm working in R Studio. To load the picture, I use the include_graphics function from knitr, because I've been told it's the best way (see here). My .Rmd file is:
---
title: "ppp"
author: "ppp"
date: "`r Sys.Date()`"
output:
word_document:
fig_caption: yes
html_document:
fig_caption: yes
---
<style>
p.caption {
font-size: 0.8em;
}
</style>
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = FALSE)
```
```{r foo, fig.cap="$f_{p}$ as a function of $g$ for various values of $r=\\frac{\\rho_{w}}{\\rho_{a}}$"}
# All defaults
include_graphics("download.jpg")
```
This is regular text.
The corresponding output is:
Clearly, this CSS method doesn't work (I guess it's something related to HTML, so it doesn't render in Word). In Word I can manually change the font size for each caption, but I'd rather set some global R Markdown parameter. Is that possible?
Almost as easy as in the HTML case. The following applies to the workflow using LibreOffice. But in Word it should be almost the same:
Produce your docx output file.
Open it in LibreOffice (or Word, or Pages, ...)
In LibreOffice, right-click the caption and choose Edit Style (in Word you can open the styles pane with Ctrl+Shift+Alt+S)
In the menu that popped up you can modify the style for Image Captions
When you are done editing the style, click Apply and just save the file as a docx called template.docx
Finally, add a style reference in the YAML header of your Rmd document like
title: "ppp"
author: "ppp"
date: "July 4, 2017"
output:
word_document:
reference_docx: template.docx
fig_caption: yes
And the captions should be smaller now according to how you changed the style in your reference document.
I am writing a report in R markdown (using pdflatex to convert it into pdf) and I am trying to set the style of the different headers (color, size, interline before and after).
I am using this formalisation :
#H1
##H2
###H3
I have the basics of Markdown and LaTeX but I'm far to be an expert. Is there a way to do it without creating a LaTeX template ? For exemple with some YAML code in the head of the document ?
Mine for now :
---
title: "Guide"
output:
pdf_document:
fig_caption: yes
highlight: tango
toc: yes
toc_depth: 2
documentclass: report
---
Or by changing the R options ?
```{r set-options, echo=FALSE, cache=FALSE}
options(some stuff)
```
I've found some indications while searching that suggest it may be possible, but I could'nt manage to find any understandable exemple ...
Thanks for any feedback.
If you do not want to do any LaTeX, you could add your own CSS (see documentation) and set it that way, as in this answer.
You would then need to do: How to convert Markdown + CSS -> PDF?
However, your results may not be as faithfully rendered as if you create a LaTeX template.