r - 在 r markdown tufte 讲义的边距中打印代码生成的文本
问题描述
我试图在我的 Tufte Latex 文档中添加一个边注,该文档部分由 r 代码生成,但没有成功。设置为打印在页边距的代码块似乎只在其中放置图表,而不是文本或表格。如果其中包含内联代码,则调用的边距注释tufte::marginfigure()
会引发错误。边距图块忽略内联代码。我已经成功地让我的代码生成的文本打印在带有脚注的页边空白处,但是我得到了脚注编号,这是我不想要的。我想关闭那个脚注上的编号,但一直没能。
这是一个例子:
---
title: "Tufte Test"
author: "Neal"
date: "`r Sys.Date()`"
output:
tufte::tufte_handout: default
---
```{r setup, include=FALSE}
library(tufte)
library(tidyverse)
```
Here is some normal text with inline code: 2+3=`r 2+3`
\vspace{10pt}
```{r block, fig.margin=TRUE, echo=FALSE, results='asis'}
cat('Here is a margin code block with code-generated text and a plot.')
mtcars %>% ggplot(aes(mpg, disp)) + geom_point()
cat('The text stays in the main body.')
```
\vspace{10pt}
I can combine text and code in a footnote^[2+3=`r 2+3` \newline\vspace{10pt}], but I get footnote numbering, which I don't want.
```{marginfigure, echo=TRUE}
Here is a margin figure with inline code that doesn't work: 2+3=`r 2+3` \newline\vspace{10pt}
```
`r tufte::margin_note('This is a margin note. If I try to include inline code in it, I get an error because it "failed to tidy R code in chunk"')`
和输出:tufte-test.pdf
有任何想法吗?谢谢。
解决方案
标记的代码块marginfigure
只是包装了 LaTeX 环境中的内容,称为marginfigure
. 您可以自己执行此操作,然后将正确处理内联代码。
也就是说,你替换这个:
```{marginfigure, echo=TRUE}
Here is a margin figure with inline code that doesn't work: 2+3=`r 2+3` \newline\vspace{10pt}
```
有了这个:
\begin{marginfigure}
Here is a margin figure with inline code that *does* work: 2+3=`r 2+3` \newline\vspace{10pt}
\end{marginfigure}
你的第一个例子有点复杂。它需要分成三个部分:
\begin{marginfigure}
`r 'Here is a margin code block with code-generated text.'`
\end{marginfigure}
```{r block, fig.margin=TRUE, echo=FALSE}
mtcars %>% ggplot(aes(mpg, disp)) + geom_point()
```
\begin{marginfigure}
`r 'The text doesn\'t stay in the main body.'`
\end{marginfigure}
您要求 PDF 输出,但只是为了完整起见,如果您使用tufte::tufte_html
的是 HTML 输出,则使用
<span class="marginnote">
Here is a margin figure with inline code that *does* work: 2+3=`r 2+3`
</span>
推荐阅读
- javascript - 访问控制允许凭据错误--nodejs
- python - 如何访问类中的局部变量
- node.js - 是否可以将可变参数传递给过滤器?
- javascript - 我使用 Admob 并收到违反 Android 广告 ID 政策和第 4.8 节的使用规定
- javascript - 如何通过 Google 幻灯片上的链接/按钮调用函数?
- docker - 如何找出我已登录的注册表?
- reactjs - 如何调度多个动作
- javascript - Firefox 开发者工具:JavaScript 文件显示为 HTML
- firefox - SVG 过滤器在 Firefox 上导致意外裁剪
- ios - 从 Swift 5 iOS 应用程序进行 Mailchimp API 调用时“缺少 API 密钥”