Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否有向LaTeX命令传递R内容的knitr strat?_R_Latex_Knitr - Fatal编程技术网

是否有向LaTeX命令传递R内容的knitr strat?

是否有向LaTeX命令传递R内容的knitr strat?,r,latex,knitr,R,Latex,Knitr,我正在创建一个小的R包,它允许用户使用R代码为表格和图表、多个问题类型、随机排序的问题以及对多项选择项的随机排序回答创建考试。在LaTeX中插入R代码并没有问题,但我遇到的一个问题是需要将通过R接收的文本与LaTeX命令“拍打”在一起。考虑这个例子: 我有一个LaTeX文件: \newcommand{\question}[1]{ \begin{minipage}{\linewidth} \item {#1} \end{minipage} } 我使用readr::read_

我正在创建一个小的R包,它允许用户使用R代码为表格和图表、多个问题类型、随机排序的问题以及对多项选择项的随机排序回答创建考试。在LaTeX中插入R代码并没有问题,但我遇到的一个问题是需要将通过R接收的文本与LaTeX命令“拍打”在一起。考虑这个例子:

我有一个LaTeX文件:

\newcommand{\question}[1]{
  \begin{minipage}{\linewidth}
  \item
    {#1}
  \end{minipage}
}

我使用
readr::read_file
读取内容,并将其存储在变量中。然后,我将问题的内容保存在.json文件中:

...
    {
        "type" : "mc",
        "section_no" : 1,
        "points" : 2,
        "question" : "What is the best beer?",
        "correct_answer" : "Hamm's",
        "lure_1" : "Miller Lite",
        "lure_2" : "PBR",
        "lure_3" : "Naturdays",
        "lure_4" : "Leine's"
    },
...
我使用
jsonlite::fromJSON
(转换为数据帧)读取它,进行一些处理,并将其存储在变量中。让我们调用这些问题及其可用选项
questions
。我一直在做的是用

question.tex <- paste0("\\question{", question[i], "\\\\")
但我认为必须有更好的方法来做到这一点。我正在寻找一个函数,它允许更无缝地将参数传递给我的LaTeX命令,类似于
knitr::magic_func(LaTeX.command,question[I])
,以实现上述结果。这是否存在

也许我要求的是
knitr
没有(或不是设计来拥有)的额外抽象级别?或者有更好的办法?我想在这一点上,我离创建一个能够读取LaTeX命令名、参数数量和适当插入文本的函数已经不远了,但最好不要重新发明轮子!此外,我认为这个问题可以推广到更简单的命令,如
\title
\documentclass
,等等

小型MWE:

##  (more backslashes since we need to escape in R)
tex.command <- "\\newcommand{\\question}[1]{
  \\begin{minipage}{\\linewidth}
  \\item
    {#1}
  \\end{minipage}
}"

q <- "What is the best beer?\\\\A. PBR\\\\B. Naturdays\\\\C. Miller Lite\\\\D. Leine's\\\\E. Hamm's\\\\"

## some magic function here?
magic_func(tex.command, q)

## desired result
"\\question{What is the best beer?\\\\A. PBR\\\\B. Naturdays\\\\C. Miller Lite\\\\D. Leine's\\\\E. Hamm's\\\\\\\\"
###(更多反斜杠,因为我们需要在R中转义)

我不确定我是否理解这个问题。就我所了解的代码而言,
\question
的命令定义是静态的。你不需要把它写在R中并打印到你的TEX文件中;只需将其直接写入TEX文件(这样可以节省双反斜杠)。要生成
\question{…text…}
片段,您的
paste0
方法看起来是合理的(尽管您可能忘记了
}
)。为了获得更具可读性的代码,我更喜欢
sprintf
sprintf(\\question{%s}),question[I])
。另请参见
stringr::str_interp
glue::glue
,用R变量的值替换字符串中的名称。
##  (more backslashes since we need to escape in R)
tex.command <- "\\newcommand{\\question}[1]{
  \\begin{minipage}{\\linewidth}
  \\item
    {#1}
  \\end{minipage}
}"

q <- "What is the best beer?\\\\A. PBR\\\\B. Naturdays\\\\C. Miller Lite\\\\D. Leine's\\\\E. Hamm's\\\\"

## some magic function here?
magic_func(tex.command, q)

## desired result
"\\question{What is the best beer?\\\\A. PBR\\\\B. Naturdays\\\\C. Miller Lite\\\\D. Leine's\\\\E. Hamm's\\\\\\\\"