在 RStudio 中调用库后代码出错
Error in code after calling a library in RStudio
我一直在 RStudio 中编写代码并使用 TinyTex 编译我的 pdf。我在 RStudio 的 RSweave 文件中编写了以下代码:
\documentclass[12 pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amssymb}
\usepackage[margin=1.25in]{geometry}
\usepackage{hyperref}
\usepackage{enumitem}
\usepackage{tcolorbox}
\newtcolorbox{boxy}[1]{colback=green!5!white, colframe=green!75!black, title={#1}}
\usepackage{color}
\usepackage{graphicx}
\definecolor{astral}{RGB}{46,116,181}
\hypersetup{
colorlinks,
citecolor=astral,
filecolor=astral,
linkcolor=black,
urlcolor=astral
}
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{\thetitle}
\rhead{\theauthor}
\cfoot{\thepage}
\setlength{\headheight}{14pt}
\makeatletter
\title{\textbf{\underline{Assignment 2}}}\let\thetitle\@title
\author{Devansh Kamra}
\makeatother
\begin{document}
\maketitle
\begin{boxy}{Question No. 1}
\begin{enumerate}
\item Run \verb|ggplot(data = mpg)|. What do you see?
\item How many rows are in \verb|mpg|? How many columns?
\item What does the \verb|drv| variable describe? Read the help for \verb|?mpg| to find out.
\item Make a scatterplot of \verb|hwy| vs \verb|cyl|.
\item What happens if you make a scatterplot of class vs drv? Why is the plot not useful?
\end{enumerate}
\end{boxy}
\section*{\underline{Solution:}}
\subsection*{\underline{Part 1}}
Running \verb|ggplot(data = mpg)|:
<<>>=
ggplot(data = mpg)
@
We can easily see that \verb|ggplot(data = mpg)| gives us an empty plane to plot the data on.
\end{document}
它给了我这个输出 pdf:
它说它找不到函数 ggplot
,这是完全可以理解的,因为我还没有加载 library("tidyverse")
。但它正在编译 pdf,我认为没有这样的 LaTeX 错误。现在我只需将 library("tidyverse")
添加到之前的代码中:
%%same code before
Running \verb|ggplot(data = mpg)|:
<<>>=
library("tidyverse")
ggplot(data = mpg)
@
%%same code after
但现在我在编译后遇到以下错误,而且它没有生成 pdf。
Loading required package: knitr
processing file: assign2.Rnw
|....................... | 33%
ordinary text without R code
|............................................... | 67%
label: unnamed-chunk-1 (with options)
List of 1
$ indent: chr " "
|......................................................................| 100%
ordinary text without R code
output file: assign2.tex
[1] "assign2.tex"
Compiling document with tinytex ... ! Undefined control sequence.
\f@nch@orh ->\theauthor
\strut
l.132 \end{document}
Error: LaTeX failed to compile D:/hp/Desktop/assign2.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See assign2.log for more info.
Execution halted
failed
Bad Box: pgf.sty:120: Overfull \hbox (32.5757pt too wide) in paragraph at lines 120--121
Bad Box: pgf.sty:124: Overfull \hbox (68.61136pt too wide) in paragraph at lines 124--124
Bad Box: pgf.sty:128: Overfull \hbox (17.62482pt too wide) in paragraph at lines 128--131
Error: pgf.sty:132: Undefined control sequence.
Error: pgf.sty: ==> Fatal error occurred, no output PDF file produced!
现在我可能错了,但是 overfull \hbox 错误通常是 LaTeX 错误,如果是,那么为什么它们在加载之前不显示 library("tidyverse")
?请帮助我纠正这些错误,如果这是微不足道的,我深表歉意,因为我是 R 的新手。
编辑: 删除以下块后,代码为我提供了一个完美编译的 pdf:
%%same code before
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{\thetitle}
\rhead{\theauthor}
\cfoot{\thepage}
\setlength{\headheight}{14pt}
%%same code after
这显然意味着我无法在我的代码中使用包 fancyhdr
。为什么会这样,是否有任何可能的方法可以解决这个问题,以便我能够使用该软件包?
该问题与任何 r 库完全无关。你只有在修复 r 错误时才会看到它的原因是你的文档超过一页长,你实际上会看到你喜欢的 headers (第一页使用不同的页面样式,因此没有错误只有一页)。
如错误消息所述,实际问题是 \theauthor
未定义。标题你自己手动定义,作者也必须自己定义。
\documentclass[12 pt]{article}\usepackage[]{graphicx}\usepackage[]{color}
% maxwidth is the original width if it is less than linewidth
% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
\ifdim\Gin@nat@width>\linewidth
\linewidth
\else
\Gin@nat@width
\fi
}
\makeatother
\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\makeatletter
\@ifundefined{AddToHook}{}{\AddToHook{package/xcolor/after}{\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}}}
\makeatother
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
\let\hlipl\hlkwb
\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
\def\at@end@of@kframe{}%
\ifinner\ifhmode%
\def\at@end@of@kframe{\end{minipage}}%
\begin{minipage}{\columnwidth}%
\fi\fi%
\def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
\colorbox{shadecolor}{##1}\hskip-\fboxsep
% There is no \@totalrightmargin, so:
\hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
\MakeFramed {\advance\hsize-\width
\@totalleftmargin\z@ \linewidth\hsize
\@setminipage}}%
{\par\unskip\endMakeFramed%
\at@end@of@kframe}
\makeatother
\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\makeatletter
\@ifundefined{AddToHook}{}{\AddToHook{package/xcolor/after}{
\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
}}
\makeatother
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX
\usepackage{alltt}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amssymb}
\usepackage[margin=1.25in]{geometry}
\usepackage{hyperref}
\usepackage{enumitem}
\usepackage{tcolorbox}
\newtcolorbox{boxy}[1]{colback=green!5!white, colframe=green!75!black, title={#1}}
\usepackage{color}
\usepackage{graphicx}
\definecolor{astral}{RGB}{46,116,181}
\hypersetup{
colorlinks,
citecolor=astral,
filecolor=astral,
linkcolor=black,
urlcolor=astral
}
\makeatletter
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{\thetitle}
\rhead{\theauthor}
\cfoot{\thepage}
\setlength{\headheight}{14pt}
\makeatother
\makeatletter
\title{\textbf{\underline{Assignment 2}}}\let\thetitle\@title
\author{Devansh Kamra}\let\theauthor\@author
\makeatother
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}
\maketitle
\begin{boxy}{Question No. 1}
\begin{enumerate}
\item Run \verb|ggplot(data = mpg)|. What do you see?
\item How many rows are in \verb|mpg|? How many columns?
\item What does the \verb|drv| variable describe? Read the help for \verb|?mpg| to find out.
\item Make a scatterplot of \verb|hwy| vs \verb|cyl|.
\item What happens if you make a scatterplot of class vs drv? Why is the plot not useful?
\end{enumerate}
\end{boxy}
\section*{\underline{Solution:}}
\subsection*{\underline{Part 1}}
Running \verb|ggplot(data = mpg)|:
\newpage
test
\newpage
test
\end{document}
我一直在 RStudio 中编写代码并使用 TinyTex 编译我的 pdf。我在 RStudio 的 RSweave 文件中编写了以下代码:
\documentclass[12 pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amssymb}
\usepackage[margin=1.25in]{geometry}
\usepackage{hyperref}
\usepackage{enumitem}
\usepackage{tcolorbox}
\newtcolorbox{boxy}[1]{colback=green!5!white, colframe=green!75!black, title={#1}}
\usepackage{color}
\usepackage{graphicx}
\definecolor{astral}{RGB}{46,116,181}
\hypersetup{
colorlinks,
citecolor=astral,
filecolor=astral,
linkcolor=black,
urlcolor=astral
}
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{\thetitle}
\rhead{\theauthor}
\cfoot{\thepage}
\setlength{\headheight}{14pt}
\makeatletter
\title{\textbf{\underline{Assignment 2}}}\let\thetitle\@title
\author{Devansh Kamra}
\makeatother
\begin{document}
\maketitle
\begin{boxy}{Question No. 1}
\begin{enumerate}
\item Run \verb|ggplot(data = mpg)|. What do you see?
\item How many rows are in \verb|mpg|? How many columns?
\item What does the \verb|drv| variable describe? Read the help for \verb|?mpg| to find out.
\item Make a scatterplot of \verb|hwy| vs \verb|cyl|.
\item What happens if you make a scatterplot of class vs drv? Why is the plot not useful?
\end{enumerate}
\end{boxy}
\section*{\underline{Solution:}}
\subsection*{\underline{Part 1}}
Running \verb|ggplot(data = mpg)|:
<<>>=
ggplot(data = mpg)
@
We can easily see that \verb|ggplot(data = mpg)| gives us an empty plane to plot the data on.
\end{document}
它给了我这个输出 pdf:
它说它找不到函数 ggplot
,这是完全可以理解的,因为我还没有加载 library("tidyverse")
。但它正在编译 pdf,我认为没有这样的 LaTeX 错误。现在我只需将 library("tidyverse")
添加到之前的代码中:
%%same code before
Running \verb|ggplot(data = mpg)|:
<<>>=
library("tidyverse")
ggplot(data = mpg)
@
%%same code after
但现在我在编译后遇到以下错误,而且它没有生成 pdf。
Loading required package: knitr
processing file: assign2.Rnw
|....................... | 33%
ordinary text without R code
|............................................... | 67%
label: unnamed-chunk-1 (with options)
List of 1
$ indent: chr " "
|......................................................................| 100%
ordinary text without R code
output file: assign2.tex
[1] "assign2.tex"
Compiling document with tinytex ... ! Undefined control sequence.
\f@nch@orh ->\theauthor
\strut
l.132 \end{document}
Error: LaTeX failed to compile D:/hp/Desktop/assign2.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See assign2.log for more info.
Execution halted
failed
Bad Box: pgf.sty:120: Overfull \hbox (32.5757pt too wide) in paragraph at lines 120--121
Bad Box: pgf.sty:124: Overfull \hbox (68.61136pt too wide) in paragraph at lines 124--124
Bad Box: pgf.sty:128: Overfull \hbox (17.62482pt too wide) in paragraph at lines 128--131
Error: pgf.sty:132: Undefined control sequence.
Error: pgf.sty: ==> Fatal error occurred, no output PDF file produced!
现在我可能错了,但是 overfull \hbox 错误通常是 LaTeX 错误,如果是,那么为什么它们在加载之前不显示 library("tidyverse")
?请帮助我纠正这些错误,如果这是微不足道的,我深表歉意,因为我是 R 的新手。
编辑: 删除以下块后,代码为我提供了一个完美编译的 pdf:
%%same code before
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{\thetitle}
\rhead{\theauthor}
\cfoot{\thepage}
\setlength{\headheight}{14pt}
%%same code after
这显然意味着我无法在我的代码中使用包 fancyhdr
。为什么会这样,是否有任何可能的方法可以解决这个问题,以便我能够使用该软件包?
该问题与任何 r 库完全无关。你只有在修复 r 错误时才会看到它的原因是你的文档超过一页长,你实际上会看到你喜欢的 headers (第一页使用不同的页面样式,因此没有错误只有一页)。
如错误消息所述,实际问题是 \theauthor
未定义。标题你自己手动定义,作者也必须自己定义。
\documentclass[12 pt]{article}\usepackage[]{graphicx}\usepackage[]{color}
% maxwidth is the original width if it is less than linewidth
% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
\ifdim\Gin@nat@width>\linewidth
\linewidth
\else
\Gin@nat@width
\fi
}
\makeatother
\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\makeatletter
\@ifundefined{AddToHook}{}{\AddToHook{package/xcolor/after}{\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}}}
\makeatother
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
\let\hlipl\hlkwb
\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
\def\at@end@of@kframe{}%
\ifinner\ifhmode%
\def\at@end@of@kframe{\end{minipage}}%
\begin{minipage}{\columnwidth}%
\fi\fi%
\def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
\colorbox{shadecolor}{##1}\hskip-\fboxsep
% There is no \@totalrightmargin, so:
\hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
\MakeFramed {\advance\hsize-\width
\@totalleftmargin\z@ \linewidth\hsize
\@setminipage}}%
{\par\unskip\endMakeFramed%
\at@end@of@kframe}
\makeatother
\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\makeatletter
\@ifundefined{AddToHook}{}{\AddToHook{package/xcolor/after}{
\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
}}
\makeatother
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX
\usepackage{alltt}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amssymb}
\usepackage[margin=1.25in]{geometry}
\usepackage{hyperref}
\usepackage{enumitem}
\usepackage{tcolorbox}
\newtcolorbox{boxy}[1]{colback=green!5!white, colframe=green!75!black, title={#1}}
\usepackage{color}
\usepackage{graphicx}
\definecolor{astral}{RGB}{46,116,181}
\hypersetup{
colorlinks,
citecolor=astral,
filecolor=astral,
linkcolor=black,
urlcolor=astral
}
\makeatletter
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{\thetitle}
\rhead{\theauthor}
\cfoot{\thepage}
\setlength{\headheight}{14pt}
\makeatother
\makeatletter
\title{\textbf{\underline{Assignment 2}}}\let\thetitle\@title
\author{Devansh Kamra}\let\theauthor\@author
\makeatother
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}
\maketitle
\begin{boxy}{Question No. 1}
\begin{enumerate}
\item Run \verb|ggplot(data = mpg)|. What do you see?
\item How many rows are in \verb|mpg|? How many columns?
\item What does the \verb|drv| variable describe? Read the help for \verb|?mpg| to find out.
\item Make a scatterplot of \verb|hwy| vs \verb|cyl|.
\item What happens if you make a scatterplot of class vs drv? Why is the plot not useful?
\end{enumerate}
\end{boxy}
\section*{\underline{Solution:}}
\subsection*{\underline{Part 1}}
Running \verb|ggplot(data = mpg)|:
\newpage
test
\newpage
test
\end{document}