无法理解“Rstudio pdf knit fails with 'Environment Shaded undefined' error”的意思。

3
我正在使用rticles包创建报告,并在编写代码块的rticle IEEE paper时出现以下错误:

输出文件:rticles_IEEE.knit.md ! LaTeX Error: Environment Shaded undefined.

我尝试了解如何解决此问题,并测试了这个问题/答案。但我无法确定应该把修复放在由knitting生成的文件中(例如,mydoc.tex, IEEE_trans.cls)。
这是我的最小示例:
---
title: Cool IEEE ArticleS
author:
  - name: Ray

abstract: |
   The abstract of my first IEEE paper with rmarkdown
output: rticles::ieee_article
---
Introduction
=============
This is the introduction. And here comes a cool code chunk.
```{r}
x <- seq(-3,3,0.1)
y <- x^3
plot(x,y)
```

注意:

  • 有人评论说这是另一个问题的潜在重复。该线程中给出的答案的问题是没有解释在哪里进行更改。
  • 我不明白指向Yihui的修正的指针。上述文件,以及编织的.tex文件没有形式为\makeatother或\makeatletter的标签。

或者,您可以通过设置 echo = FALSE 来抑制所有代码部分。 - Michael Harper
谢谢Mike,我的想法是使用“特定”的代码块。因此,我的应用程序(或者说如何修复这个问题)是这个功能的罕见用途之一。我认为随着我们向可重复研究的前进,越来越多的代码块将会出现在学术论文中(或者至少在它们的附录中)。参考资料报告了echo = FALSE作为一个解决方案,代价是不显示任何代码块。我猜解决方案是创建一个格式/布局定义来处理阴影元素,并将其附加到模板中。作为一个LATEX的新手,我在这里完全迷失了。 - Ray
2个回答

3
整体问题在于,在rticles IEEE论文中显示代码块时会出现错误,指向未定义的Shaded环境
如果您将以下内容添加到底层LaTeX template.tex中,则较早的帖子中提供的解决方案有效。我的问题是我尝试编辑工作目录中的tex或cls模板。当我偶然发现以下(相关)stackoverflow问题时,我才恍然大悟。
对于rticles包,template.tex不在您正在处理文章R Markdown文件的目录中。相反,它们存储在包库文件夹中(即.libPaths()使用的文件夹)。该文件可以在rmarkdown/templates/中找到,并搜索您正在使用的rticles文件夹(在我的情况下为:ieee_article)。在resources子文件夹中,您将找到目标template.tex。复制一份以备份。

打开template.tex文件,在LATEX命令行\begin{document}之前的任何位置插入以下内容(注意:即该template.tex文件的导言部分)。例如,我将其插入到\begin{document}行之前:

% code to insert to fix environment Shaded undefined issue with
% showing code chunks in rticle IEEE template.
$if(highlighting-macros)$
$highlighting-macros$
$endif$

\begin{document}

保存更改并返回到R / RStudio和您的IEEE论文R Markdown文件,然后点击knit按钮。现在将显示R代码块(如果您想要的话)。
我尚未进行全面测试,但该方法适用于所有缺少用于显示r-code块(即environment {Shaded} )的环境定义的rticle模板。

值得指出的是,如果您更新了软件包,文件模板将被覆盖! - Michael Harper
2
谢谢你的指引。实际上,需要注意变化或按照你下面注入的 header.tex 进行操作。太棒了,我想这需要我几十年才能想出这样一个 LATEX 头文件。感谢分享。 - Ray
1
修复已经被纳入rticles软件包的开发版本中(下一个CRAN版本应该是0.6)。谢谢! - Yihui Xie
@YihuiXie 目前还没有合并到主分支中,请添加说明以便其他人拉取开发分支并在 R 中使用它。 - Aditya Peshave
我不知道是什么让你相信它不在主分支中,但它确实在主分支中:https://github.com/rstudio/rticles/commit/bc8a23932e02cb4da517606fbc9abafcdc4fe08d 如果你打开我之前回复中的链接,你应该会看到如何安装开发版本的说明。 - Yihui Xie

2

不要复制模板,你可以将允许高亮的必需代码放在一个独立的header.tex 文件中。 当你将其包含在模板中时,这基本上就是取代了 $highlighting-macros$

header.tex

\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}

在您的目录中复制此文件,然后在YAML中引用此头文件:

---
title: Short Paper
output: 
  rticles::ieee_article:
    includes:
      in_header: header.tex
---

# Introduction

This is the introduction. And here comes a cool code chunk.
```{r}
x <- seq(-3,3,0.1)
y <- x^3
plot(x,y)
```

在此输入图片描述

这种方法适用于任何包含$header-includes$标签的template.tex`模板。


太棒了,Mickey。感谢您分享LATEX以优雅地解决这个问题,并将其放在单独的“header.tex”文件中。 - Ray
没问题!如果您对此满意,请点赞答案 :) - Michael Harper
完成了。再次感谢。请容我留在template.tex的微调上。我对LATEX知之甚少,您的解决方案让我进入了另一个宇宙。我注意到您的观点,即包更新将覆盖template.tex中的修复。因此,我会记住可以将补丁作为通过YAML包含的header.tex注入。也许是我的LATEX新手状态使我更倾向于3行代码。 - Ray

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接