R 图的编译错误

R 图的编译错误

我提前感谢你的建议。以下代码大约在两年前有效,但在我将 TexLive 和 R 更新到最新版本后,它停止工作。错误消息是

段落在 \FV@BeginScanning 完成之前结束。

下面是代码。当我运行 R chunk 时,我没有遇到任何问题。

\documentclass[10pt,xcolor=svgnames, compress]{beamer}
\usepackage{multirow}
\usepackage{Sweave}
\usepackage{fontspec}
\usepackage{mathtools}
\usepackage[MathematicalAlphanumericSymbols]{ucharclasses}
\usepackage[math-style=TeX]{unicode-math}


\usetheme{Luebeck}
\usefonttheme{professionalfonts}
\usefonttheme[stillsansseriflarge]{serif}
\renewcommand{\emph}[1]{\textbf{#1}}


\setmainfont[Ligatures=TeX,Scale=.95]{Palatino}
\setsansfont{Trebuchet MS}
% \setmonofont{Inconsolata}
% \setmathfont{Asana-Math.otf}

\AtBeginDocument{
  \DeclareMathOperator{\var}{var}
  \DeclareMathOperator{\sd}{SD}
  \DeclareMathOperator{\SE}{SE}
  \DeclareMathOperator{\cov}{cov}
  \DeclareMathOperator{\corr}{corr}
}

\newcommand{\phat}{\hat{p}}
\newcommand{\xbar}{\bar{x}}
\newcommand{\ybar}{\bar{y}}
\newcommand{\Cond}[2]{\left.#1\,\middle|\,#2\right.}
\newcommand{\EV}[1]{\mathbf{E}\!\left(#1\right)}
\newcommand{\Var}[1]{\mathbf{var}\!\left(#1\right)}
\newcommand{\SD}[1]{\mathbf{SD}\!\left(#1\right)}
\newcommand{\dist}[2]{#1\!\left(#2\right)}
\newcommand{\Prob}[1]{\mathbf{P}\!\left(#1\right)}
\newcommand{\Cov}[2]{\mathbf{cov}\!\left(#1,\,#2\right)}
\newcommand{\Corr}[2]{\mathbf{corr}\!\left(#1,\,#2\right)}

\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{parskip}
\usepackage{csquotes}

\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}

\setbeamerfont{headline}{family=\sffamily}
\setbeamerfont{footline}{family=\sffamily}

\definecolor{Sred}{RGB}{204,0,0}
\definecolor{Sblue}{RGB}{53,76,151}
\definecolor{SWarmGrey}{cmyk}{.16,.23,.23,.44}
\definecolor{SMedium}{cmyk}{.06,.09,.39,.16}

\def\mycolorA{Sblue}
\def\mycolorB{Sred}
\def\mycolorC{white}
\def\mycolorD{SMedium}


 \setbeamercolor{title}{fg=\mycolorA, bg=\mycolorC}
\setbeamercolor{frametitle}{fg=\mycolorA, bg=\mycolorC}
\setbeamercolor{section in head/foot}{fg=white,bg=\mycolorB}
\setbeamercolor{subsection in head/foot}{fg=white,bg=\mycolorB}
\setbeamercolor{title in head/foot}{fg=white,bg=\mycolorB}
\setbeamercolor{subtitle in head/foot}{fg=white,bg=\mycolorB}
\setbeamercolor{structure}{fg=\mycolorA}
\setbeamercolor{page number in head/foot}{fg=white,bg=\mycolorB}

\setbeamertemplate{frametitle continuation}{}

\setbeamertemplate{footline}
{
  \hbox{%
  \begin{beamercolorbox}[wd=.45\paperwidth,ht=2.25ex,dp=1ex,left]{title
    in head/foot}%
    \usebeamerfont{title in head/foot}~\inserttitle
  \end{beamercolorbox}%
   \begin{beamercolorbox}[wd=.10\paperwidth,ht=2.25ex,dp=1ex,center]{page
      number in head/foot}%
    \usebeamerfont{page number in head/foot}\insertpagenumber
  \end{beamercolorbox}%
  \begin{beamercolorbox}[wd=.45\paperwidth,ht=2.25ex,dp=1ex,left]{subtitle in head/foot}%
    \usebeamerfont{title in head/foot}\insertsubtitle
  \end{beamercolorbox}%
}%
  \vskip0pt%
}

\setbeamertemplate{headline}
{
   \hbox{%
   \begin{beamercolorbox}[wd=\paperwidth,ht=2.25ex,dp=1ex,center]{title
       in head/foot}%
     \usebeamerfont{title in head/foot}\insertnavigation{\paperwidth}
   \end{beamercolorbox}%
 }%
 \vskip0pt%
}


\usepackage{tikz}
\usetikzlibrary{positioning}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}

\begin{document}
\SweaveOpts{concordance=TRUE}


<<include=FALSE, echo=FALSE>>=
library(knitr)
opts_chunk$set(
concordance=TRUE
)
@



<<setup, echo=FALSE, message=FALSE, warning=FALSE>>=
library(grid)
library(grDevices)
library(plyr)
library(xtable)
library(tidyverse)
library(knitr)
library(readxl)
library(scales)
library(reshape2)
library(readxl)
library(showtext)
library(extrafont)
loadfonts(device = "win")


Sred <- rgb(204,0,0,max=255)
Sblue <- rgb(53,76,151,max=255)
get_S_mix <- colorRamp(c(Sred,Sblue)) ## a function on (0,1) for amount of red
Spurple <- rgb(get_S_mix(.5),max=255)

font_add("Trebuchet",
         regular="c:/Windows/Fonts/trebuc.ttf")
font_add("Palatino",
         regular="c:/Windows/Fonts/pala.ttf",
         bold="c:/Windows/Fonts/pala.ttf",
         italic="c:/Windows/Fonts/pala.ttf")
plot.text.family <- "Trebuchet"
slide.text.family <- "Palatino"
plot.text.family <- "Trebuchet"
slide.text.family <- "Palatino"
showtext_auto()

options(digits=3)

opts_chunk$set(echo=FALSE, cache=TRUE,
               scipen=999, dev='cairo_pdf',
               fig.height=2.8, fig.width=5,
               fig.showtext=TRUE,
               xtable.booktabs=TRUE, xtable.include.rownames=FALSE,
               message=FALSE, warning=FALSE, error=TRUE)

plot.text.family <- "Trebuchet"
slide.text.family <- "Palatino"
theme_set(theme_bw())
theme_update(text=element_text(face="plain",colour="black",size=14,
                               hjust=.5,vjust=.5,angle=0,lineheight=.9,
                               family=plot.text.family),
             plot.margin=grid::unit(rep(.04,4),"in"))
set.seed(1234)
sanitize <- function(x) x
@

                 
<<NPR>>=
options(scipen=999)
start.yr <- 2017
d <- 2000
M <- 9854
n <- 5
p <- .483
x <- 0:n
px <- dbinom(x, n, p)
@                  
\begin{frame}{fragile}

The probability function for the binomial model is
\begin{align*}
  \Prob{X=x}=\binom{n}{x}p^{x}\left(1-p\right)^{n-x}\text{, where }\binom{n}{x}=\frac{n!}{x!\left(n-x\right)!}
\end{align*}
              
For $n=\Sexpr{n}$ and $p=\Sexpr{p}$,
                  
<<binomialBars, fig.width=3.5, fig.height=2>>=
PB <- data.frame(x=x, Prob_X=px) %>%
  ggplot(aes(x=x, y=Prob_X)) %>%
  + geom_bar(stat="identity", color="black",fill="white") %>%
  + geom_text(aes(y=Prob_X+.015, label=format(Prob_X, nsmall=3)), size=2) %>%
  + scale_y_continuous("P(X)") %>%
  + scale_x_continuous("Season Ticket Purchases (X)",breaks=x) %>%
  + theme(text=element_text(size=9))
PB
@
\end{frame}                  
\end{document}

相关内容