# R markdown / Plotly R with LaTeX unable to generate plot and get error on a file name with hyphen

I am using LaTeX for some formatting, and trying to generate a PDF file with a plotly R plot as a png. The following piece of code gives me an error as show below. It seems like a problem with the hyphen in the auto generated file name? Any ideas?

---
output:
pdf_document: default
- \usepackage{graphicx}
- \usepackage[dvipsnames]{xcolor}
- \usepackage[most]{tcolorbox}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \setlength{\textheight}{530pt}
- \renewcommand{\footrulewidth}{0.4pt}
- \fancypagestyle{plain}{\pagestyle{fancy}}
---
\begin{tcolorbox}[colbacktitle=Fuchsia,
fonttitle=\centering\Large\bfseries\color{white},
colback=white,boxsep=1mm,arc=0mm,boxrule=1pt,
title=PLOT INFORMATION]\parskip1em

\centering

{r, results = 'asis', echo = FALSE, eval = TRUE, message = FALSE, warn = FALSE}
library(plotly)
p <- mtcars %>%
plot_ly(x = ~wt, y = ~mpg, type = 'scatter', mode = 'markers')
tmpFile <- tempfile(fileext = '.png')
export(p, file = tmpFile)

\end{tcolorbox}


Error message is as follows:

processing file: myfile.Rmd
output file: myfile.knit.md

! Missing $inserted. <inserted text>$
l.103 ![](myfile_
files/figure-latex/unnamed-chunk-1-1.png)<!-- -->
Here is how much of TeX's memory you used:
34563 strings out of 493029
658943 string characters out of 6136233
733691 words of memory out of 5000000
37639 multiletter control sequences out of 15000+600000
25977 words of font info for 38 fonts, out of 8000000 for 9000
1141 hyphenation exceptions out of 8191
63i,3n,101p,10496b,275s stack positions out of 5000i,500n,10000p,200000b,80000s

Warning: Error in : Failed to compile myfile.tex. See myfile.log for more info.
[No stack trace available]


I did install webshot and phantomJS, and set the path correctly as prescribed in this post: Plotly as png in knitr/rmarkdown

NOTE: I am using a render() call in a Shiny app to knit this thing into PDF document as follows:

render('myfile.Rmd', pdf_document())


This call is what then gives me the above error.

It renders on my computer. Are you also making sure ggplot2 is installed? I am not too familiar with Latex, but I am using Mac OSX and TinyTex. I also am using --pdf-engine pdflatex

Your error message suggests that your Shiny app is trying to include a graphic with ![](myfile_files/figure-latex/unnamed-chunk-1-1.png) but use of the underscore, _, in the folder name, myfile_files is being interpreted as the markup for a subscript, which must be written in math mode surrounded by $. That's why you get Missing$ inserted. I suggest you look for a way that does not involve writing the underscore.

I get two other errors that need to be corrected. The first is an options clash loading xcolor. You load it with \usepackage[dvipsnames]{xcolor} but this clashes with the options set when you do \usepackage[most]{tcolorbox}. This can be fixed by adding classoption: dvipsnames to your YAML header. This was previously answered here: Knitr hook to add code before \documentclass line in tex file to avoid options clash with xcolor

The second problem is the syntax for putting the code chunk in a tcolorbox. The correct syntax in Rmd is \tcolorbox ...code chunk... \endtcolorbox as reported here: How can I get Rchunks to render inside tcolorbox?

---
title: "TeX.SE"
classoption: dvipsnames
output:
pdf_document: default
- \usepackage{xcolor}
- \usepackage{graphicx}
- \usepackage[most]{tcolorbox}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \setlength{\textheight}{530pt}
- \renewcommand{\footrulewidth}{0.4pt}
- \fancypagestyle{plain}{\pagestyle{fancy}}
---
\tcolorbox
\centering
{r, results = 'asis', echo = FALSE, eval = TRUE, message = FALSE, warn = FALSE}
library(plotly)
p <- mtcars %>%
plot_ly(x = ~wt, y = ~mpg, type = 'scatter', mode = 'markers')
tmpFile <- tempfile(fileext = '.png')
export(p, file = tmpFile)

\endtcolorbox


• You are correct that some process is generating that temporary file name with an underscore. It might be the render function or the knit function that gets called underneath. I can't seem to be able to control that file name. Any other possible solutions to set an option to not use _ or such? Aug 3, 2020 at 15:57