# knitr figure options don't render properly

I'm created a PDF document using RMarkdown in RStudio using default options.

I want to use figure options like fig.env and fig.ncol, as specified in https://rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf.

Some options like "fig.cap" work, but in the rendered tex, none of my custom environments created work. What is going wrong here?

My ultimate purpose is to use a latex template that specifies multicols. Using the figure* environment is a recommended way to get an image to render full-page within a multicols environment.

My full document looks like:

---
title: "testing graphics output"
author: "my test author"
date: "20 January 2020"
output:
pdf_document:
keep_tex: yes

---

'{r setup, include=FALSE}
knitr::opts_chunk\$set(echo = TRUE)
'

## Including Plots

You can also embed plots, for example:

'{r pressure, echo=FALSE,fig.cap="Here's a figure caption",fig.env="figure*"}
plot(pressure)
'

'{r pressure2, echo=FALSE,fig.cap="Here's a figure caption",fig.env="custom_env"}
plot(pressure)
'

'{r pressure3, echo=FALSE,fig.cap="Here's a figure caption for a GGplot",fig.env="figure*", fig.ncol=2}
library(ggplot2)
ggplot(pressure,aes(x = temperature,y=pressure))+geom_point()
'

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.



The rendered output just for the plots are:


\begin{figure}
\centering
\includegraphics{rmarkdown-graphics-test_files/figure-latex/pressure-1.pdf}
\caption{Here's a figure caption}
\end{figure}

\begin{figure}
\centering
\includegraphics{rmarkdown-graphics-test_files/figure-latex/pressure2-1.pdf}
\caption{Here's a figure caption}
\end{figure}

\begin{figure}
\centering
\includegraphics{rmarkdown-graphics-test_files/figure-latex/pressure3-1.pdf}
\caption{Here's a figure caption for a GGplot}
\end{figure}



Quick take:

1. Create a custom template like @mattwarkentin provided.

or

1. Set all your customized LaTeX in-line.

Thanks. Unfortunately, inline Latex doesn't seem to work.

I can use inline latex to embed an image already generated, but if I try to wrap a the graph output R block with a latex \begin{figure*} command, then it is escaped into plain text as  \textbackslash{}begin\{figure*\}{[}ht{]}.

Plague of backslashes, dang it.

How would a custom template help?

I have experimented with them.

As far as I know there's not a way to use them to, e.g., tell knitr to output figures in a certain way.

Take a look at the one linked to @mattwarkentin. I'm away from LaTeX for a while, but the idea would be that a custom template exposes your commands for use in the same way that the default operates.

Just trying to get caught up on this thread. @benjsmith can you clarify what you are trying to achieve? You want to render a multi-column document with figures that use the full page width?

Hi @mattwarkentin, yes, thanks, that's exactly what I'm trying to do!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.