Rstudio rmarkdown:在单个PDF中同时提供纵向和横向布局
我想知道如何使用Rstudio rmarkdown:在单个PDF中同时提供纵向和横向布局,r,pdf,rstudio,knitr,r-markdown,R,Pdf,Rstudio,Knitr,R Markdown,我想知道如何使用rmarkdown生成一个在同一文档中同时具有纵向和横向布局的pdf。如果有一个纯r标记选项,它甚至比使用乳胶更好 这里有一个小的,可重复的例子。首先,在RStudio中呈现此.Rmd(按Knit PDF按钮)将生成一个PDF,其中所有页面均为横向布局: --- title: "All pages landscape" output: pdf_document classoption: landscape --- ```{r} summary(cars) ``` \newpa
rmarkdown
生成一个在同一文档中同时具有纵向和横向布局的pdf。如果有一个纯r标记选项,它甚至比使用乳胶更好
这里有一个小的,可重复的例子。首先,在RStudio中呈现此.Rmd
(按Knit PDF按钮)将生成一个PDF,其中所有页面均为横向布局:
---
title: "All pages landscape"
output: pdf_document
classoption: landscape
---
```{r}
summary(cars)
```
\newpage
```{r}
summary(cars)
```
然后尝试创建一个混合纵向和横向布局的文档。YAML
中的基本设置是根据“包含”部分完成的。_header
文件“header.tex”中的仅包含\usepackage{lscape}
,这是一个建议用于knitr
横向布局的包。.tex
文件与.Rmd
文件位于同一目录中
---
title: "Mixing portrait and landscape"
output:
pdf_document:
includes:
in_header: header.tex
---
Portrait:
```{r}
summary(cars)
```
\newpage
\begin{landscape}
Landscape:
```{r}
summary(cars)
```
\end{landscape}
\newpage
More portrait:
```{r}
summary(cars)
```
但是,此代码会导致错误:
# ! You can't use `macro parameter character #' in horizontal mode.
# l.116 #
# pandoc.exe: Error producing PDF from TeX source
# Error: pandoc document conversion failed with error 43
非常感谢您的帮助。因此,pandoc
解析latex环境的内容,但您可以在header.tex文件中愚弄它:
\usepackage{lscape}
\newcommand{\blandscape}{\begin{landscape}}
\newcommand{\elandscape}{\end{landscape}}
因此,这里的\begin{scape}
被重新定义为\blandscape
,而\end{scape}
被重新定义为\elandscape
。使用.Rmd
文件中新定义的命令似乎可以:
---
title: "Mixing portrait and landscape"
output:
pdf_document:
includes:
in_header: header.tex
---
Portrait
```{r}
summary(cars)
```
\newpage
\blandscape
Landscape
```{r}
summary(cars)
```
\elandscape
\newpage
More portrait
```{r}
summary(cars)
```
正如baptiste提到的,如果在LaTeX环境中封装R命令,pandoc将不会解析它们,而是将它们按原样放入生成的LaTeX中:这就是导致错误的原因。除了baptiste漂亮而简单的修复之外,您还可以使用xtable
R包,它提供了从R输出创建更性感的LaTeX表的可能性。要使以下示例正常工作,您需要在header.tex
文件中添加\usepackage{rotation}
:
---
title: "Mixing portrait and landscape"
output:
pdf_document:
keep_tex: true
includes:
in_header: header.tex
---
```{r, echo=FALSE}
library(xtable)
```
Portrait
```{r, results='asis', echo=FALSE}
print(xtable(summary(cars), caption="Landscape table"), comment=FALSE)
```
Landscape:
```{r, results='asis', echo=FALSE}
print(xtable(summary(cars), caption="Landscape table"),
floating.environment="sidewaystable", comment=FALSE)
```
第二个表格将在横向稳定
环境中打印,而不是通常的表格
:因此它将以横向模式在单独的页面中打印。请注意,lscape
软件包以横向模式放置的表格和图形或横向环境中的表格和图形将始终放置在单独的页面中,请参阅本非常重要的文档第91页:
由于我觉得这有点烦人,我设法找到了一种方法,将纵向和横向表格保持在同一页面内(在这个过程中浪费了我整个下午):
对于景观表,我使用了这里提供的\rotatebox
建议:
为了实现这一点,我只需使用打印(xtable(…
部分)生成表的制表部分,然后我必须捕获输出并“手动”使用表格
和旋转框
命令将其包围,将所有内容转换为字符串R输出,以便pandoc不会将其视为LaTeX环境。对于纯rmarkdown解决方案,祝您好运!对于最常见的情况
有三个条件
一切都在肖像模式
一切都在风景模式
纵向和横向模式的混合
让我们把范围缩小到每种情况
第一个,假设我们有一个降价文档,从下面的代码开始。这是创建rmd文件时Rstudio中的默认设置。当您编织它时。毫无疑问,它会自动假定它是纵向模式
title: "Landscape and Portrait"
author: "Jung-Han Wang"
date: "Thursday, March 19, 2015"
output: pdf_document
当您想要将PDF文件编织到横向模式时,您只需要添加classoption:横向
title: "Landscape and Portrait"
author: "Jung-Han Wang"
date: "Thursday, March 19, 2015"
output: pdf_document
classoption: landscape
如果您想要两者的混合,您需要在YAML中添加.tex文件。通过引用我上面提到的链接。您可以在此处下载.tex代码。或者只需复制代码并将其保存为header.tex即可。为了简化操作,请将此.tex文件与要编织的rmd文件放在一起。请确保您做了以下两件事:
复制tex文件并将其与rmd文件一起移动。
将rmd的开头更改为:
title: "Landscape and Portrait"
author: "Jung-Han Wang"
date: "Thursday, March 19, 2015"
output:
pdf_document:
includes:
in_header: header.tex
这是我处理这个问题后的总结,主要得益于巴普蒂斯特的回答
我在我的博客中加入了一些快照和例子
希望这有帮助。
祝您好运。在先前解决方案的基础上,以下解决方案不需要辅助头.tex
文件。所有内容都包含在.Rmd
文件中。相反,LaTeX命令是在YAML头中的头includes
块中定义的。可以找到更多信息
另外,我注意到使用lscape
LaTeX包可以旋转页面的内容,但不能旋转PDF页面本身。这可以通过pdflscape
包解决
---
title: "Mixing portrait and landscape WITHOUT a header.tex file"
header-includes:
- \usepackage{pdflscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
output: pdf_document
---
Portrait
```{r}
summary(cars)
```
\newpage
\blandscape
Landscape
```{r}
summary(cars)
```
\elandscape
\newpage
More portrait
```{r}
summary(cars)
```
感谢您的研究和回答(+1)。从中“不解析latex环境的内容”并不完全清楚。但我认为我的latex
无知也是罪魁祸首。这是一个复杂的工具链,有三/四个不同的参与者(knitr rmarkdown/pandoc latex)我发现在文档之外,很难找出东西在哪里断裂。最好的方法似乎是独立运行它们:首先,查看结果.md
(好,这里),然后是md->tex转换(这就是它出错的地方)。错误消息没有帮助,因为它已经是下一步(latex)。使用此解决方案,在pdf中,#不是由“#简介”创建的结构化标题,而是作为一个符号问题出现:where do(或should)header.tex文件处于活动状态,因此可以读取?我大量使用RMarkdown,但相对较新,尚未了解所有互锁软件包以及它们是如何协同工作的。header.tex文件应该与您的方法工作的目录相同。为了更容易理解,我认为pandoc的问题在于它变得怪异当您使用环境而不是宏时。这就是为什么我像您建议的那样包括\newcommand{\blandscape}{\begin{scape}}和\ne
---
title: "Mixing portrait and landscape WITHOUT a header.tex file"
header-includes:
- \usepackage{pdflscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
output: pdf_document
---
Portrait
```{r}
summary(cars)
```
\newpage
\blandscape
Landscape
```{r}
summary(cars)
```
\elandscape
\newpage
More portrait
```{r}
summary(cars)
```