Home > Enterprise >  Error in code after calling a library in RStudio
Error in code after calling a library in RStudio

Time:01-31

I have been writing a code in RStudio and using TinyTex to compile my pdf. I wrote the following code in an RSweave file in RStudio:

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

It gives me this output pdf: enter image description here

It says it couldn't find function ggplot, which is completely understandable as I didn't load library("tidyverse") yet. but it is compiling the pdf, by which I assume there is no LaTeX error as such. Now I simply add library("tidyverse") to the previous code:

%%same code before
Running \verb|ggplot(data = mpg)|:
    <<>>=
    library("tidyverse")
    ggplot(data = mpg)
    @
%%same code after

But now I'm getting following errors after compiling and it doesn't produce a 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!

Now I might be wrong about this, but overfull \hbox errors are generally LaTeX errors, and if they are, then why didn't they show before loading library("tidyverse")? Kindly help me out rectify these errors, and I apologize if this is trivial, for I am new to R.

EDIT: The code gives me a perfectly compiled pdf once I remove the following chunk:

%%same code before
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{\thetitle}
\rhead{\theauthor}
\cfoot{\thepage}
\setlength{\headheight}{14pt}
%%same code after

This apparently means that I am unable to use package fancyhdr in my code. Why is that, and is there any possible way I can resolve the issue so that I am able to use the package?

CodePudding user response:

The problem is completely unrelated to any r libraries. The reason why you only see it if you fix the r errors is that then your document is more than one page long and you actually get to see your fancy headers (the first page uses a different pagestyle, thus no error with only one page).

As the error messages says, the actual problem is that \theauthor is not defined. You manually define it for the title, you must also define it for the author.

\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}
  •  Tags:  
  • Related