You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RStudio中RSweave文件加载tidyverse库后fancyhdr包编译错误排查求助

RSweave + TinyTeX: Fixing Fancyhdr Error After Loading Tidyverse

Hey there, let's break down what's happening with your RSweave document and how to fix it. You mentioned that everything works fine until you load tidyverse—then fancyhdr throws an undefined control sequence error, but removing the header code or skipping tidyverse makes it compile again. Here's why, and how to fix it for good.

Why This Happens

First, look at how you're defining your title and author in the LaTeX preamble:

\\makeatletter 
\\title{\\textbf{\\underline{Assignment 2}}}\\let\\thetitle\\@title 
\\author{Devansh Kamra} 
\\makeatother 

You correctly map LaTeX's internal \\@title macro (which stores your title text) to your custom \\thetitle variable for the header. But you forgot to do the same for the author! Your fancyhdr setup uses \\rhead{\\theauthor}, but \\theauthor isn't actually defined anywhere.

So why didn't this error pop up before loading tidyverse? Two key reasons:

  1. Without tidyverse, your ggplot(data = mpg) code fails, so the generated TeX document is super short. The \\maketitle command automatically sets the first page's style to plain, which overrides your fancy header setup—LaTeX never tries to render the header, so it never notices \\theauthor is missing.
  2. When you load tidyverse, ggplot2 triggers knitr to include the pgf package (for vector graphics support). The pgf package changes how LaTeX handles page styles, so even the first page checks the header macros. That's when it catches the undefined \\theauthor and throws the error.

The Overfull \\hbox warnings are just side effects from pgf's helper code being slightly too wide—they're not fatal, but we can ignore them once the core error is fixed.

The Fix

All you need to do is add one line to map LaTeX's internal \\@author macro to your \\theauthor variable, right where you define the author:

\\makeatletter 
\\title{\\textbf{\\underline{Assignment 2}}}\\let\\thetitle\\@title 
\\author{Devansh Kamra}\\let\\theauthor\\@author  % Add this line
\\makeatother 

After making this change, recompile your document:

  • The fancyhdr header will correctly display your title and author
  • tidyverse will load without issues
  • The PDF will generate successfully

Optional: Fixing Overfull Hbox Warnings

If you want to get rid of those Overfull \\hbox messages, you can tweak your page margins slightly (e.g., increase them to 1.3in instead of 1.25in) in the geometry package setup. But this is totally optional—your PDF will work perfectly even with those warnings.

内容的提问来源于stack exchange,提问作者Devansh Kamra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 11:23:11