I create a PowerPoint with R markdown.
---
title: "Presentation Title"
author: "That is me"
date: "`r format(Sys.time(), '%d.%m.%Y')`"
output:
powerpoint_presentation:
reference_doc: template.pptx
slide_level: 2
---
As you see, I use a custom template file template.pptx.
Now my goal ist to select which layout to be used from that template. I know that officedown allows for a chunk option layout (see https://www.apreshill.com/blog/2021-07-officedown/), but I think it is possible without the additional package as well. Why?
When I put a table on the page via kable, the layout "Content with Caption" from the template is used.
```{r results="asis"}
cat("\n\n## Page Title\n\n")
cat("\n\nSome content\n\n")
data.frame(matrix(c(1))) %>% kable
```
When I use flextable instead, the layout "Title and Content" is used.
```{r results="asis", ft.left=0.9}
cat("\n\n## Page Title\n\n")
cat("\n\nSome content\n\n")
flextable(data.frame(matrix(c(1)))) %>% flextable_to_rmd
```
So it seems that kable() can somehow tell the output to use the other layout. Any ideas why that is the case?
The Pandoc manual says
Content with Caption
This layout is used for any non-two-column slides which contain text followed by non-text (e.g. an image or a table).
So that probably means that the flextable is not recognizes as "non-text" content. Can I add non-text content that won't show up in the presentation? It's not that I wish back times of the clearpixel.gif, but ...
I am not especially proud of this, but the good old clearpixel does the job...
```{r results="asis", ft.left=0.9}
cat("\n\n## Page Title\n\n")
cat("\n\nSome content\n\n")
flextable(data.frame(matrix(c(1)))) %>% flextable_to_rmd
cat("![](clearpixel.png)\n")
```
Whoever finds a cleaner solution (the invisible elemente will still be selectable in the PPT file), please kindly post it here :)
Related
I'd like to output an accessible version of a presentation I'm making with R Markdown ioslides. At this point, I've been struggling to find much documentation on alt-text for dynamically generated images - not pointers to images - there's lots of information available online on that front.
Seems like the easiest thing to do might be to output my document as a word_document instead of a presentation, and ensure that figures have captions, using fig.cap argument in the chunk header.
However, I'd like the captions to be available only as alt-text - so that this document doesn't look strange to folks who aren't using screen reader tech (I'd like to make my alt-text nice and descriptive, but for folks who can see ok, this will seem odd, because the graph will do the talking instead).
I've included a reprex below. How can I still include the fig.cap as alt-text in the output word document, but not have it show up underneath the graph?
Also, if you are aware of any good resources on making RMarkdown docs accessible, I am all ears!!
Thanks for your help.
---
title: 'test doc'
author: "Nova"
date: "April 2020"
output:
word_document:
fig_height: 5
fig_width: 10
height: 500
widescreen: yes
width: 500
---
```{r setup and comments, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
message = F,
warning = F,
fig.height = 5,
fig.width = 10,
fig.cap = NA)
library(ggplot2)
```
## cars
```{r ggplot, include=T, fig.cap = "this is some alternative text about this figure"}
ggplot(cars)
```
I'd go back to some kind of HTML output, because it's simpler.
If you want ioslides output, you can use fig.cap="Some alt text" in the chunk option to set the alt text for the figure. Unfortunately, this also generates a visible caption, but you can suppress that. For one slide, just insert inline CSS:
<style>
.caption {
display:none;
}
</style>
somewhere on the slide before the figure. To do it for the whole presentation, put that text in a .css file and include it with YAML like
output:
ioslides_presentation:
css: style.css
It's probably also possible to change the template for this output style, but that looks like a lot more work.
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.
Is there a way to put my table into one page? I have a table which is broken between two pages. Here is a reproducible example:
---
output: pdf_document
---
`r rep("Text", 400)`
```{r}
# This will create a page break
knitr::kable(mtcars, caption = "A split table")
```
This produces a 2 page pdf with a broken table as follows::
There are two easy ways to do this:
1. Add a page break
If you are only using the PDF output, you can actually integrate LaTeX commands directly into the report. Therefore the \newpage command will force a pagebreak as follows:
---
output: pdf_document
---
`r rep("Text", 400)`
\newpage
```{r}
knitr::kable(mtcars, caption = "A fixed table")
```
2. Use Page Floats:
As RMarkdown uses LaTeX to build the PDF, you can take advantage of the page floats feature. This can take some getting used to, but rather than locking the figure or table in a set position, LaTeX will try and place the figure in a position which it deems "best". With tables, it will try and remove any page breaks.
Here is an example. The table will float to the second page. Make sure to update the YAML to include the header-includes: argument as well:
---
output: pdf_document
header-includes:
- \usepackage{booktabs}
---
`r rep("Text", 400)`
```{r}
knitr::kable(mtcars, format = "latex",
caption = "A caption",
booktabs = TRUE,
longtable = FALSE)
```
`r rep("Text", 200)`
If you look at the output, you will see that the second chunk of text continues on the same page as the first, and that the table is centered on the second page. This is all done automatically by LaTeX and can avoid pain down the road if you add more text before the table which might have previously caused the table to be broken again.
Hope that helps.
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 beamer presentation in rmarkdown and converting it to pdf with knitr. I want to define sections at the header1 level, e.g. # Introduction, and then have a slide titled something else e.g. ## Introducing my brilliant research. Having the header1 level define sections is nice as the names of the sections can be displayed in the slide header in certain beamer themes, and this is why I include it.
But I do not want rmarkdown to insert a slide that simply says the name of the section between sections, which at the moment it is doing. Is there a way to not print a slide with the section name between sections? I thought slide_level would control this behavior but it does not seem to (or perhaps I am using it wrong).
A minimal reproducible example of my problem can be obtained with this code:
---
title: "Test Pres"
author: "Professor Genius Researcher"
date: "24 February 2017"
output:
beamer_presentation:
slide_level: 2
theme: "Singapore"
colortheme: "rose"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
# Markdown Intro
## R Markdown
This is an R Markdown presentation. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.
# Using Bullets
## Slide with Bullets
- Bullet 1
- Bullet 2
- Bullet 3
# Including Chunks
## Slide with R Output
```{r cars, echo = TRUE}
summary(cars)
```
## Slide with Plot
```{r pressure}
plot(pressure)
```
At the moment, this code produces slides that say Markdown Intro, Using Bullets, and Including Chunks. I would like those slides labeling the sections omitted. Is this possible?
Create a new Latex template where you remove this part from the preamble:
\AtBeginSection[]
{
....
}
Place this latex template in the same folder as your .Rmd file and refer to it in the Rmd Yaml front matter using template: mytemplate.tex as explained here.