简体   繁体   English

RStudio presentation / slidify / pandoc中的两列布局

[英]two-column layouts in RStudio presentations/slidify/pandoc

I'm trying to come up with a good system for generating slides and accompanying handouts. 我正在努力想出一个很好的系统来生成幻灯片和随附的讲义。 The ideal system would have the following properties: 理想的系统具有以下属性:

  • beautiful in both presentation (PDF/HTML) and handout (PDF) layouts (handouts should have room for taking notes) 演示文稿(PDF / HTML)和讲义(PDF)布局都很漂亮(讲义应该有笔记的余地)
  • embedded R chunks, figures, other JPG/PNG pictures, etc. 嵌入式R块,数字,其他JPG / PNG图片等
  • easy to compose 容易撰写
  • build using command-line tools 使用命令行工具构建
  • bibliography support 参考书目支持
  • pandoc slide separator format (automatically generate a new slide after headers of a specified level) is preferred pandoc幻灯片分隔符格式(在指定级别的标题之后自动生成新幻灯片)是首选
  • I can live with a little bit of additional processing (eg via sed ), but would prefer not to write a huge infrastructure 我可以接受一些额外的处理(例如通过sed ),但不想写一个庞大的基础设施
  • two-column layouts : there is a SO post on how to get multi-column slides from pandoc , but it is LaTeX- rather than HTML-oriented. 两列布局有关如何从pandoc获取多列幻灯片SO帖子 ,但它是LaTeX-而不是面向HTML。
  • ability to adjust sizes of embedded images (other than R-generated figures) and column widths on the fly 能够在运行中调整嵌入图像(除了R生成的图形)和列宽之外的大小

Here's what I've discovered so far about the various options: 以下是我迄今为止发现的各种选项:

  • Slidify : 滑动
    • doesn't do pandoc slide separator format, although there is a workaround 没有pandoc幻灯片分隔符格式,虽然有一种解决方法
    • the suggestion for creating handouts is to print to PDF; 创建讲义的建议是打印到PDF; I'd like to leave room for notes etc. (I could probably figure out a way to do that using something like PDFtk or psnup ...) 我想为笔记等留出空间(我可能想办法用像PDFtkpsnup这样的东西......)
  • RStudio presentations ( .Rpres files): RStudio演示文稿( .Rpres文件):
    • does lots of things nicely, including multi-columns with specified widths 做很多事情,包括具有指定宽度的多列
    • doesn't support pandoc slide separator format 不支持pandoc幻灯片分隔符格式
    • I can't figure out what's going on under the hood. 我无法弄清楚引擎盖下发生了什么。 There is RStudio documentation that describes the translation process for regular HTML, but it doesn't seem to cover the R presentation format (which isn't quite the same). 有一个RStudio文档描述了常规HTML的翻译过程,但它似乎没有涵盖R表示格式(这不完全相同)。 (I have previously invested some effort in figuring out how to get RStudio-like output via pandoc ...), which means I can't generate slides etc. from the command line. (我之前已经投入了一些努力来弄清楚如何通过pandoc获得类似RStudio的输出 ......),这意味着我无法从命令行生成幻灯片等。
  • RStudio's Development Version (as of March 2014) comes bundled with Pandoc and version 2 of rmarkdown . RStudio的开发版本 (截至2014年3月)与Pandoc和rmarkdown第2版​​捆绑在一起。 It addresses many of the above issues with the .Rpres format. 它以.Rpres格式解决了上述许多问题。
  • pandoc : may be the only markdown-translator that has features such as footnotes, bibliography support, etc.. I can also use pandoc to generate LaTeX using the tufte-handout class , which meets my criteria of beauty. pandoc :可能是唯一具有脚注,参考书目支持等功能的降价转换器。我也可以使用pandoc来生成使用tufte-handout类的 LaTeX,这符合我的美容标准。
    • Unfortunately, it seems not to have built-in two-column format support. 不幸的是,似乎没有内置的双列格式支持。 Yihui Xie's HTML5 example doesn't show any two-column examples, and it claims (on slide 5) that clicking the "Knit HTML" button in RStudio is equivalent to pandoc -s -S -i -t dzslides --mathjax knitr-slides.md -o knitr-slides.html , but it doesn't seem to be ... 谢毅辉的HTML5示例没有显示任何两列示例,它声称(在幻灯片5中)单击RStudio中的“Knit HTML”按钮等同于pandoc -s -S -i -t dzslides --mathjax knitr-slides.md -o knitr-slides.html ,但它似乎不是......
  • LaTeX/beamer: I could simply compose in Rnw (knitr-dialect Sweave) rather than R markdown to begin with. LaTeX / beamer:我可以简单地用Rnw (knitr-dialect Sweave)而不是R markdown开始。 This would give me ultimate flexibility ... 这会给我最大的灵活性......
    • despite many years of LaTeX use I do find LaTeX composition more of a pain than markdown composition. 尽管使用了多年的乳胶,但我确实发现乳液成分比降低成分更令人痛苦。

After all that, my specific question is: what's the best (easiest) way to generate a two-column layout for HTML output ? 毕竟,我的具体问题是: 为HTML输出生成两列布局的最佳(最简单)方法什么

Any other advice will also be appreciated. 任何其他建议也将不胜感激。

This is an old Q, but I was recently plagued by a similar question, here's what I found: 这是一个古老的Q,但最近我被一个类似的问题困扰,这是我发现的:

Using the RPres format, two columns can be specified like so ( details ). 使用RPres格式,可以像这样指定两列( 详细信息 )。 Note that RPres can only be converted to HTML by clicking a button in RStudio, there doesn't seem to be any command line method, which is a bit annoying. 请注意,RPres只能通过单击RStudio中的按钮转换为HTML,似乎没有任何命令行方法,这有点烦人。 Despite, that I'd say it is currently the simplest and most flexible method for getting slide columns with markdown: 尽管如此,我认为它是目前使用markdown获取幻灯片列的最简单,最灵活的方法:

=== 

Two Column Layout  
===

This slide has two columns

***

```{r, echo=FALSE}
plot(cars)
```

在此输入图像描述

Some flexibility is afforded by adjusting the column proportions: 通过调整色谱柱比例可以提供一些灵活性:

===

Two Column Layout  
===
left: 30%
This slide has two columns

***

```{r, echo=FALSE}
plot(cars)
```

在此输入图像描述

With rmarkdown we can get two columns, but with no control over where the break is, which is a bit of a problem: 使用rmarkdown我们可以获得两列,但无法控制中断的位置,这有点问题:

---
output: ioslides_presentation
---


## Two Column Layout  {.columns-2}

This slide has two columns


```{r, echo=FALSE}
plot(cars)
```

在此输入图像描述

We can also mix markdown and LaTeX in an Rmd file using the beamer_presentation format in RStudio to get two columns like this, but can't run any code in either column, which is a limitation: 我们还可以使用RStudio中的beamer_presentation格式在一个Rmd文件中混合markdown和LaTeX来获得这样的两列,但不能在任一列中运行任何代码,这是一个限制:

---
output: beamer_presentation
---

Two Column Layout 
-------

\begin{columns}
\begin{column}{0.48\textwidth}
This slide has two columns
\end{column}
\begin{column}{0.48\textwidth}
If I put any code in here I get an error, see
https://support.rstudio.com/hc/communities/public/questions/202717656-Can-we-have-columns-in-rmarkdown-beamer-presentations-
\end{column}
\end{columns}

在此输入图像描述

Seems like a regular Rnw LaTeX doc is the best way to get columns if you want to use LaTex, not this markdown hybrid (cf. two column beamer/sweave slide with grid graphic ) 看起来像常规的Rnw LaTeX doc是获取列的最佳方式,如果你想使用LaTex,而不是这个降价混合(参见两列beamer / sweave slide with grid graphic

In all of the above an image can be placed in an column. 在上述所有情况中,图像可以放在一列中。

The slidify website has instructions on making two columns here: http://slidify.org/customize.html but it's not clear what has to go into the assets/layouts folder to make it work slidify网站有关于在这里列出两列的说明: http ://slidify.org/customize.html但是不清楚要进入assets / layouts文件夹以使其工作

I now have what I think is a reasonable solution that should apply at least to ioslides -based solutions, and maybe (?) to other HTML5-based formats. 我现在拥有的是一个合理的解决方案,至少应该适用于基于ioslides的解决方案,也许(?)适用于其他基于HTML5的格式。 Starting here , I added 这里开始,我补充道

<style>
div#before-column p.forceBreak {
    break-before: column;
}
div#after-column p.forceBreak {
    break-after: column;
}
</style>

to the beginning of my document; 到我的文件的开头; then putting <p class="forceBreak"></p> within a slide with {.columns-2} breaks the column at that point, eg 然后在{.columns-2}的幻灯片中放置<p class="forceBreak"></p>在该点打破列,例如

## Latin hypercube sampling {.columns-2}

- sample evenly, randomly across (potentially many) uncertain parameters

<p class="forceBreak"></p>

![](LHScrop.png)
[User:Saittam, Wikipedia](https://commons.wikimedia.org/wiki/File:LHSsampling.png#/media/File:LHSsampling.png)

There may be an even better way, but this isn't too painful. 可能有更好的方法,但这不是太痛苦。

@ChrisMerkord points out in comments that @ChrisMerkord在评论中指出

.forceBreak { -webkit-column-break-after: always; break-after: column; }

worked instead (I haven't tested ...) 工作(我还没有测试......)

I got an idea from HERE , the basic solutions was: 我从这里得到了一个想法,基本的解决方案是:


### Function *inner_join*
. . .

`<div style="float: left; width: 50%;">`
``` {r, echo = FALSE, results = 'markup', eval = TRUE}
kable(cbind(A,B))                                    
```
`</div>`
`<div style="float: right; width: 50%;">`
```{r, echo = TRUE, results = 'markup', eval = TRUE}
inner_join(A,B, by="C")
```
`</div>`

There is a workaround for beamer error. 有关投影仪错误的解决方法

In short: Error is related to pandoc conversion engine, which treats everything between \\begin{...} and \\end{...} as TeX . 简而言之:错误与pandoc转换引擎有关,它将\\begin{...}\\end{...}视为TeX It can be avoided by giving new definition for begin{column} and end{column} in yaml header. 通过为yaml标头中的begin{column}end{column}提供新定义,可以避免这种情况。

Create mystyle.tex and write there: 创建mystyle.tex并在那里写:

\def\begincols{\begin{columns}}
\def\begincol{\begin{column}}
\def\endcol{\end{column}}
\def\endcols{\end{columns}}

In the Rmd file use these new definitions 在Rmd文件中使用这些新定义

---
output:
  beamer_presentation:
    includes:
      in_header: mystyle.tex
---


Two Column Layout 
-------

\begincols
  \begincol{.48\textwidth}

This slide has two columns.

  \endcol
\begincol{.48\textwidth}

```{r}
#No error here i can run any r code
plot(cars)
```

  \endcol
\endcols

And you get: 你得到: 在此输入图像描述

So far I haven't been able to do better than hacking my own little bit of markup on top of the rmd format: I call my source file rmd0 and run a script including this sed tidbit to translate it to rmd before calling knit : 到目前为止,我还没有能够做的比黑客的标记我自己的一点在上面更好的rmd格式:我打电话给我的源文件rmd0并运行,包括这样的脚本sed珍闻要翻译成rmd之前调用knit

sed -e 's/BEGIN2COLS\(.*\)/<table><tr><td style="vertical-align:top; width=50%" \1>/' \
    -e 's/SWITCH2COLS/<\/td><td style="vertical-align:top">/' \
    -e 's/END2COLS/<\/td><\/tr><\/table>/' ...

There are a few reasons I don't like this. 有几个原因我不喜欢这个。 (1) It's ugly and special-purpose, and I don't have a particularly good way to allow optional arguments (eg relative widths of columns, alignment, etc.). (1)它是丑陋和特殊目的,我没有一个特别好的方法来允许可选参数(例如列的相对宽度,对齐等)。 (2) It has to be tweaked for each output format (eg if I wanted LaTeX/beamer output I would need to substitute \\begin{columns}\\begin{column}{5cm} ... \\end{column}\\begin{column}{5cm} ... \\end{column}\\end{columns} instead (as it turns out I want to ignore the two-column formatting when I make LaTeX-format handouts, so it's a little easier, but it's still ugly). (2)必须针对每种输出格式进行调整(例如,如果我想要LaTeX / beamer输出,我需要替换\\begin{columns}\\begin{column}{5cm} ... \\end{column}\\begin{column}{5cm} ... \\end{column}\\end{columns} (因为事实证明我想在制作LaTeX格式的讲义时忽略两列格式,所以它更容易一点,但它仍然是丑陋)。

Slidify may yet be the answer. Slidify可能是答案。

You can use fenced_divs notation or ::: to create columns or `Two Content layout'. 您可以使用fenced_divs表示法或:::来创建列或“两个内容布局”。 See also this page to know more about the notation. 另请参阅此页面以了解有关表示法的更多信息。

## Slide With Image Left

::: columns

:::: column
left
::::

:::: column
right

```{r your-chunk-name, echo=FALSE, fig.cap="your-caption-name"}
knitr::include_graphics("your/figure/path/to/the-image.pdf")

#The figure will appear on the right side of the slide...
```
::::

:::

Since pandoc 2+ , which supports the notation, was implemented in RStudio v1.2+ , you may need to install RStudio v1.2+ first. 由于支持符号的pandoc 2+是在RStudio v1.2+ ,因此您可能需要首先安装RStudio v1.2+ The installation is easy enough (at least in my case); 安装很简单(至少在我的情况下); just download and install RStudio v1.2+ . 只需下载并安装RStudio v1.2+ In the way of installation, the former version of RStudio on your computer will be replaced with the new one without uninstalling it manually. 在安装过程中,计算机上的旧版RStudio将被替换为新版本,而无需手动卸载。

The ::: notation can be used even when you knit .Rmd files with beamer_presentation option, as well as when you create HTML slides. 即使您使用beamer_presentation选项编织.Rmd文件,以及创建HTML幻灯片时,也可以使用:::表示法。 So we don't have to neither mix markdown and LaTeX notation in one file, nor add additional codes any longer: just knit the file as you knit other .Rmd with other options. 因此,我们不必在一个文件中混合使用markdown和LaTeX表示法,也不必再添加其他代码:只需在编织其他.Rmd使用其他选项编织文件。

Not a direct solution, but Yihui's Xaringan package https://github.com/yihui/xaringan/ works for me. 不是直接的解决方案,但是Yihui的Xaringan软件包https://github.com/yihui/xaringan/适合我。 It's based on remark.js . 它基于remark.js In the default template, you can use .pull-left[] and .pull-right[] . 在默认模板中,您可以使用.pull-left[].pull-right[] Example: https://slides.yihui.name/xaringan/#15 . 示例: https//slides.yihui.name/xaringan/#15 You only need a minimum tweak on the existing .rmd files. 您只需要对现有的.rmd文件进行最小调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM