Search code examples
rlatexknitrlistingslstlisting

knitr + LaTeX (Rnw file) compiles in TeXShop but fails with UTF-8 error in RStudio


On the same machine the knitr + LaTeX compilation of the Rnw file below fails in RStudio with Invalid UTF-8 byte "97 LaTeX error; but compiles with TexShop. Here is the source code:

\documentclass[nobib,notoc,nofonts]{tufte-book}

\usepackage{authorindex}
\usepackage[nohints]{minitoc}

\renewcommand{\Rcolor}{\color{black}}
\renewcommand{\Rbackground}{\color[RGB]{253, 246 ,228}}
\renewcommand{\Routbackground}{\color[RGB]{228, 246, 253}}
\renewcommand{\Sweavesize}{\footnotesize}
\definecolor{stringColor}{RGB}{237, 121, 83}
\definecolor{commentColor}{RGB}{204, 70, 120}
\definecolor{linkColor}{RGB}{13, 8, 135}
\definecolor{keywordColor}{RGB}{93, 1, 166}
\usepackage{listings}
\lstset{
    language = R,
    breaklines = TRUE,
    frame = single,
    numbers = left,
    numberstyle = \scriptsize,
    numbersep = 5pt,
    framesep = 1pt,
    rulesepcolor=\color{gray},
    rulecolor=\color{black},
        stepnumber = 2,
    firstnumber = 1,
    stringstyle = \color{stringColor},
    commentstyle = \color{commentColor},
    keywordstyle = \color{keywordColor}
}

<<setup, include = FALSE, cache = FALSE>>=
opts_chunk$set(
    fig.path = 'figure/listings-',
    tidy = TRUE,
    tidy.opts = list(
        blank = FALSE,
        collapse = TRUE,
        arrow = TRUE,
        indent = 2,
        width.cutoff = 60,
        tidy_eval = TRUE
    )
)
render_listings()
require(tidyverse)
@

\begin{document}

<<iq>>=
iq <- c(129, 112, 114, 126, 104, 112, 101, 99, 93)
iq
iq <- as_tibble(iq)
iq
@
<<sess>>=
sessionInfo()
@
\end{document}

Note: It's not clear to me why this doesn't compile without the lines

\usepackage{authorindex}

\usepackage[nohints]{minitoc}

since they seem irrelevant to the issue

Here is an image of the output from TeXShop:

output of compilation with TeXShop


Solution

  • The problem seems to be the × in the intermediate .tex file.

    You can avoid the problem by choosing an unicode aware engine, like lualatex or xelatex, to compile your document:

    enter image description here