在r markdown tufte讲义的空白处打印代码生成的文本

时间:2019-02-27 13:49:15

标签: 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

有什么想法吗?谢谢。

1 个答案:

答案 0 :(得分:0)

标记为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>