knitr + LaTeX(Rnw 文件)在 TeXShop 中编译但在 RStudio 中因 UTF-8 错误而失败

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

在同一台机器上,以下 Rnw 文件的 knitr + LaTeX 编译在 RStudio 中失败,出现 Invalid UTF-8 byte "97 LaTeX 错误;但使用 TexShop 编译。这是源代码:

\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}

注意:我不清楚为什么没有这些行就无法编译

\usepackage{authorindex}

\usepackage[nohints]{minitoc}

因为它们似乎与问题无关

这是 TeXShop 的输出图像:

问题似乎是中间 .tex 文件中的 ×

您可以通过选择支持 unicode 的引擎(如 lualatex 或 xelatex)来编译您的文档来避免此问题: