1

I've been trying to debug a .Rnw file I wrote in RStudio for about a week now. The most frustrating part is that it used to work perfectly in its current form but then seemed to inexplicably stop working.

I've tracked down the source of the error to graphics I'm trying to add to the pdf file. It seems that only certain image files break the script. Every graphic that is added to the pdf is created using another R script. Let me know if you need to see that for any reason and I'll make an edit.

Here's the basic format of my .Rnw file:

\documentclass[
  title
]{article}
\usepackage{graphicx}

\begin{document}

<<Setup, include=FALSE, cache=FALSE>>=
##Sets up variables in paragraphs and creates data frames.
@

\title{Report Title}
\maketitle

\setcounter{secnumdepth}{-1}
\section{Report Summary}
Section Paragraph
\vspace{1cm}

\begin{figure}[h!]
  \includegraphics{Fig1.png}
\end{figure}

\end{document}

When I compile the PDF in RStudio, I get an error Running pdflatex.exe on REPORT.tex...failed

When I look at the log file, I see this:

!pdfTeX error: pdflatex.exe (file C:/Users/myname/Documents/Report/Fig1.png): libpng: 
internal error ==> Fatal error occurred, no output PDF file produced!

Anyone have any ideas what has gone wrong?

I've tried changing the images from .png files to .jpg and .tiff with the same results. I've tried adding a \graphicspath to the preamble and tried adding the full path to the \includegraphics section.

EDIT: Here's how I generated the fig1.png image. This should allow you to recreate an example in RStudio. I had to manually copy this code so typos could exist. Also, it will, of course, look like crap because I just recreated the image format with the example mtcars dataset.

png("C:/Users/UserName/Documents/Report/Fig1.png", height = 7, width = 14, units = 'in', res = 350)
p<-ggplot(mtcars, aes(color = mpg, x = cyl, y disp, group = mpg)) + 
  geom_line() + geom_point(shape = 16, size = 2) + coord_cartesian(ylim = c(0,550)) +
  labs(x = "X Axis", y = "Y Axis") +
  ggtitle(expression(atop("Title", atop(italic("Subtext"), "")))) + 
  stat_summary(fun.y=sum, geom="line") +
  theme(plot.title = element_text(face = "bold", size = 16, vjust = 1.5)) + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) + 
  theme(axis.title.y = element_text(vjust = 1))
p
dev.off()
4
  • Could you add the log file? Also, does C:/Users/myname/Documents/Report/Fig1.png exist, and is it a png file?
    – Boris
    Mar 28, 2017 at 16:00
  • Well, "Fig1.png" is an example file name but the file in the log does exist. And considering I'm doing this from my work computer, I'd rather not post the log file.
    – Neal
    Mar 28, 2017 at 18:24
  • Well, can you make a minimal (non) working example without disclosing your work data?
    – Boris
    Mar 28, 2017 at 19:44
  • @Boris I added a working example for you to test with.
    – Neal
    Mar 29, 2017 at 13:32

1 Answer 1

1

You output fig1.png, while your try to include Fig1.png. TeX is case sensitive.

The following works:

\documentclass[
  titlepage
]{article}
\usepackage{graphicx}

\begin{document}

<<Setup, include=FALSE, cache=FALSE>>=
library(ggplot2)
png("Fig1.png", height = 7, width = 14, units = 'in', res = 350)
p<-ggplot(mtcars, aes(color = mpg, x = cyl, y = disp, group = mpg)) + 
  geom_line() + geom_point(shape = 16, size = 2) + coord_cartesian(ylim = c(0,550)) +
  labs(x = "X Axis", y = "Y Axis") +
  ggtitle(expression(atop("Title", atop(italic("Subtext"), "")))) + 
  stat_summary(fun.y=sum, geom="line") +
  theme(plot.title = element_text(face = "bold", size = 16, vjust = 1.5)) + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) + 
  theme(axis.title.y = element_text(vjust = 1))
p
dev.off()
@

\title{Report Title}
\maketitle

\setcounter{secnumdepth}{-1}
\section{Report Summary}
Section Paragraph
\vspace{1cm}

\begin{figure}[h!]
  \includegraphics{Fig1.png}
\end{figure}

\end{document}

By the way, it is better to use vector graphics and generate the figure automatically. The following is, in my opinion, a better style:

\documentclass[
  titlepage
]{article}
\usepackage{graphicx}

\begin{document}

<<Setup, include=FALSE, cache=FALSE>>=
library(ggplot2)
@

\title{Report Title}
\maketitle

\setcounter{secnumdepth}{-1}
\section{Report Summary}
Section Paragraph
\vspace{1cm}

\begin{figure}[h!]
<<Fig1, echo=FALSE>>=
p<-ggplot(mtcars, aes(color = mpg, x = cyl, y = disp, group = mpg)) + 
  geom_line() + geom_point(shape = 16, size = 2) + 
  coord_cartesian(ylim = c(0,550)) +
  labs(x = "X Axis", y = "Y Axis") +
  ggtitle(expression(atop("Title", atop(italic("Subtext"), "")))) + 
  stat_summary(fun.y=sum, geom="line") +
  theme(plot.title = element_text(face = "bold", size = 16, vjust = 1.5)) + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) + 
  theme(axis.title.y = element_text(vjust = 1))
p
@ 
\end{figure}

\end{document}
5
  • I changed the script to use the vector graphics style you put in. I still get a failed attempt to generate the pdf.
    – Neal
    Mar 29, 2017 at 15:33
  • Well, the file in my answer works for me. Can you run? What does the log say?
    – Boris
    Mar 29, 2017 at 18:12
  • ! Emergency stop. <*> TESTREPORT2.tex *** (job aborted, no legal \end found) There is an \end{document} in my code fyi.
    – Neal
    Mar 29, 2017 at 18:41
  • Did you try knitr first?
    – Boris
    Mar 29, 2017 at 19:11
  • Rscript -e "library('knitr'); knit('tmp.rnw')" and then pdflatex tmp
    – Boris
    Mar 29, 2017 at 19:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .