R markdown定义用于格式化的宏 [英] R markdown define macros for formatting

查看:38
本文介绍了R markdown定义用于格式化的宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在想是否有一种方法可以用markdown(或更确切地说是R markdown)来定义命令,而不必每次都为频繁的位插入html格式.

I've been wondering if there's a way to define a command in markdown (or R markdown, more specifically) in such a way that you don't have to insert the html formatting each time for frequent bits.

例如,我一直在使用css的"alert alert-info"类在整个文档中插入"Note"块,如下所示:

For example, I've been using the 'alert alert-info' class of css to insert 'Note' blocks throughout documents like so :

<div class="alert alert-info">
  <strong>Note :</strong><br/>
  stuff here
</div>

因为速度越快越好:)我可以自动化吗?(我尝试使用乳胶中的 $ \ newcommand {} $ 来执行此操作,但是整个html代码只是以数学模式打印在页面上...)

Since faster is better :) can I automate this? (I've tried using the $\newcommand{}$ from latex to do this but the whole html code just gets printed in math mode on the page...)

有时我会在注释中添加代码块,这样如果效果很好,那就太好了.

I sometimes add code chunks in the notes so it'd be nice if that worked as well.

不必为LaTeX转义'\'是件好事(尽管如果新命令要快得多,则要付出的代价很小).

Not having to escape '\' for LaTeX would be good (although it'd be a rather small price to pay if a new command is that much faster).

推荐答案

使用 htmltool 包,我们可以构建一个简单的函数来重复该注释,前提是您可以对其进行脚本编写:

Using the htmltool package we can build a simple function to repeat the note, provided that you can script it:

library(htmltools)

alert_note <- function(text) {

  div(class="alert alert-info",
      strong('Note :'), br(),
      text)
}


alert_note('stuff here')
<div class="alert alert-info">
  <strong>Note :</strong>
  <br/>
  stuff here
</div>

要在 .Rmd 文档中使用它,请在某个位置(可能在安装块中)定义该函数,并在未回显块中使用它

To use it in a .Rmd document you define the function somewhere -probably in a setup chunk- and use it in a not echoed chunk

---
title: "Untitled"
date: "5/4/2018"
output: html_document
---

```{r setup, include=FALSE}    
library(htmltools)

strong_alert <- function(text) { 
  div(class="alert alert-info",
      strong('Note :'), br(),
      text)
}
```
## R Markdown    
This is an R Markdown document.

```{r echo=FALSE}
strong_alert('stuff here')
```

这篇关于R markdown定义用于格式化的宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆