使 R Markdown 代码块进入数学模式 [英] Make R Markdown code blocks into math mode

查看:63
本文介绍了使 R Markdown 代码块进入数学模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用 R Markdown 来生成作业和考试解决方案,但我更希望它们对非编码人员更具可读性.

I would love to use R Markdown to generate homework and exam solutions, but I would prefer to have them more readable to non-coders.

有没有办法通过数学模式传递 ECHO 输出?那就是我希望有一个 ECHO 看起来更内联"而不像代码.我可以看到如何隐藏它,但在 R Markdown参考指南 我没有看到删除代码块"并将每一行包裹在 $$ 中(或包裹在任何东西中)的选项.有没有办法做到这一点?

I there a way that I can pass the ECHO output through math mode? That is I would love to have an ECHO that looks more "inline" and less like code. I can see how to hide it, but in the R Markdown Reference Guide I don't see an option to remove the "code block" and wrap each line in $$ (or wrap in anything). Is there a way to do this?

这是一个例子.此解决方案包含所有内容,但对某些学生来说可能有点吓人(这不是 R 课程).

Here is an example. This solution has all the meat, but may be a little intimdating to some students (this is not an R course).

8-22 ...

a. ...

```{r part_a}

D_0 = 2.40
g = 0.06
r = 0.12

V = D_0*(1 + g)/(r - g)
V
```

相反,我希望看到更像下面的内容.^[我很感激我可以通过一些剪切和粘贴以及文本编辑器来生成这个输出,我只是想找到最有效的解决方案,因为这是可能我会做不止一次或两次的事情.]

Instead, I would love to see something more like the following.^[I appreciate that I can generate this output with some cutting and pasting and a text editor, I am just trying to find the most efficient solution, since this is likely something that I will do more than once or twice.]

8.22 ...

a. ...

$$ D_0 = 2.40 $$
$$ g = 0.06 $$
$$ r = 0.12 $$
$$ V = D_0 \times (1 + g)/(r - g) = 2.40 \times (1 + 0.06)/(0.12 - 0.06) = `r V`$$

推荐答案

我有一个部分的答案.我还不知道如何用它的值替换变量 x 以便 a 可以用变量打印公式,用数字替换.但是我可以生成数学代码"块,这样我就可以解决问题并生成漂亮的解决方案,而无需进行大量的剪切和粘贴.

I have a partial answer. I don't yet know how to replace variable x with its value so that a can print the formula with variables, replaced by numbers. But I can generate the "math code" block so that I can solve the problem and generate the pretty solution without a lot of cut and paste.

这是一个示例 .Rmd 文件.

Here is an example .Rmd file.

---
author: Richard Herron
title: Homework Solutions
---

8-22 ...

a. ...

There are three parts to this solution.

1. write the equations to solve the problem in R-readable strings.
2. loop over the list and `eval(parse())` the equation strings
3. wrap strings in `$$ $$` with `cat(paste0())`

Chunks should be set to `echo=FALSE` and `results="asis`. You may need to suppress some function output with `invisible()`.


```{r part_a, echo=FALSE, results="asis"}

# just to make sure my eval below works 
rm(list=ls())

# store solution as a list of character equations
solution <- list(
"D_0 = 2.40", 
"g = 0.06", 
"r = 0.12", 
"V = D_0*(1 + g)/(r - g)"
)

# "solve" problem
for (i in seq_along(solution)) eval(parse(text=solution[[i]]))

# display solution as math
cat(paste0("$$", solution, "$$"), sep="\n")
```

Because of the `eval()` loop in the first chunk I can say that $V = `r V`$ in the text that follows.

这是一个将每个 .Rmd 文件转换为 .pdf 的外部文件.

And here is an outer file that convert each .Rmd file into a .pdf.

# load `render` and set working directory
setwd("C:/Users/Richard/Dropbox/Babson College/SME 2021 for 2015 fall/Homework")

# loop over all Rmd files
require(rmarkdown)
require(tools)
files <- list.files(path=".", pattern="*.Rmd")
roots <- sapply(files, file_path_sans_ext)
namesIn <- paste0("", roots, ".pdf")
namesOut <- paste0("", roots, ".pdf")

# solutions
myPdf <- pdf_document(
    fig_caption=TRUE,
    keep_tex=TRUE,
    pandoc_args=c(
        "--variable=classoption:fleqn", 
        "--variable=classoption:twocolumn", 
        paste0("--metadata=date:", format(Sys.time(), "%B %d, %Y"))
        )
    )
lapply(files, FUN=render, output_format=myPdf)
mapply(file.rename, namesIn, namesOut)

生成此 pdf.

这篇关于使 R Markdown 代码块进入数学模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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