![在 knitr 中将变量从 TeX 传递到 R](https://linux22.com/image/340776/%E5%9C%A8%20knitr%20%E4%B8%AD%E5%B0%86%E5%8F%98%E9%87%8F%E4%BB%8E%20TeX%20%E4%BC%A0%E9%80%92%E5%88%B0%20R.png)
\newcommand{}
我想在 R 代码中使用我在 TeX 中定义的变量。
假设我定义以下变量:
\newcommand{\CourseCode}{MTH 123}
\newcommand{\CourseSemester}{Fall}
\newcommand{\CourseYear}{2015}
在本文档的后面我想写这样一段话:
Please name your assignments as:
\path{MTH123_FS15_A1_FirstName_LastName}
但我想以编程方式实现这一点:
\path{\CourseCode{}_\CourseSemester{}\CourseYear{}_A1_FirstName_LastName}
但这并不是我想要的。在 R 中,假设我将变量从 TeX 传递到 R,我可以这样做:
\path{\Sexpr{paste0(gsub("[[:blank:]]", "", CourseCodeR), "_",
substr(CourseSemesterR, 1, 1), "S",
CourseYearR %% 100)}_A1_FirstName_LastName}
但是,如何将变量从 TeX 传递到 R CourseYearR <- \CourseYear{}
??
我知道更简单的方法是首先在 R 中定义变量,但我想我不能这样做。