gpt4 book ai didi

r - R 帮助手册中可以包含 latex 数学吗?

转载 作者:行者123 更新时间:2023-12-04 17:32:50 25 4
gpt4 key购买 nike

我正在处理 R包,我正在使用包 Roxygen2为我的 R 编写帮助手册功能。我想知道的是是否可以在手册页中将 latex 用于数学方程式?

例如,如果我有一个名为 add2 的函数做了以下事情:

add2 = function(x,y){
z = x+y
return(z)
}

并使用 Roxygen2文档我有以下内容:
##' @include add2.R
{}

##' Compute the sum of x_1 and x_2
##'
##' Computes the sum of x_1 and x_2
##'
##' @param x_1 number of guys
##' @param x_2 number of girls
##' @return The sum of the two values x_1 and x_2
##'
##' @example examples/adding.R
##' @export
##' @author Name

这对我有用,但这在帮助手册中将 x1 和 x2 显示为 x_1 和 x_2 而我希望它看起来像 latex 数学并且实际上在 x 上有下标,即 latex 中的 $x_1$ 和 $x_2$ .

有没有办法做到这一点或做 R不适应这个?

最佳答案

有点。使用类似 \eqn{x_1} 的东西如果您想在纯文本输出中使用(未处理的)LaTeX 标记版本(或者您可以说类似 \eqn{x_1}{x1} 的内容)。 “某种”部分是我不确定 LaTeX 处理将针对哪些特定输出格式进行 - 例如,我猜测您会在(例如)中看到的 HTML 格式的输出版本RStudio 文档 Pane 不会尊重此标记。 (LaTeX 标记肯定会用在手册的 PDF 版本中,我几乎从不看……)

我将继续引用 Mathematics section of Writing R Extensions ...

Mathematical formulae should be set beautifully for printed documentation yet we still want something useful for text and HTML online help. To this end, the two commands \eqn{latex}{ascii} and \deqn{latex}{ascii} are used. Whereas \eqn is used for “inline” formulae (corresponding to TeX’s $…$), \deqn gives “displayed equations” (as in LaTeX’s displaymath environment, or TeX’s $$…$$). Both arguments are treated as ‘verbatim’ text.

Both commands can also be used as \eqn{latexascii} (only one argument) which then is used for both latex and ascii. No whitespace is allowed between command and the first argument, nor between the first and second arguments.

The following example is from Poisson.Rd:

\deqn{p(x) = \frac{\lambda^x e^{-\lambda}}{x!}}{%

p(x) = \lambda^x exp(-\lambda)/x!} for \eqn{x = 0, 1, 2, \ldots}.



截至 2020-05-08, mathjaxr package ,由 Wolfgang Viechtbauer,在 CRAN 上。它

Provides 'MathJax' and macros to enable its use within Rd files for rendering equations in the HTML help files.



来自 README file :

[After installing the package, editing the package DESCRIPTION file appropriately, and adding \loadmathjax{} to the Rd file ... a]n inline equation can then be added with the \mjeqn{latex}{ascii} macro, with the LaTeX commands for the equation given between the first set of curly brackets (which will be rendered in the HTML and PDF help pages) and the plain-text version of the equation given between the second set of curly brackets (which will be shown in the plain text help). With the \mjdeqn{latex}{ascii} macro, one can add ‘displayed equations’ (as in LaTeX’s displaymath environment).

关于r - R 帮助手册中可以包含 latex 数学吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37397607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com