在 r markdown tufte handout 的边缘打印代码生成的文本

printing code-generated text in margin of r markdown tufte handout

我试图在我的 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 的代码块只是将内容包装在名为 marginfigure 的 LaTeX 环境中。您可以自己做,然后内联代码将被正确处理。

也就是你替换这个:

```{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>