Documenting equations with deqn and roxygen

前端 未结 2 1419
死守一世寂寞
死守一世寂寞 2021-02-05 05:47

I\'m using \\deqn{}{} with roxygen2 to document equations for a function in a package. The LaTeX (the 1st argument to deqn) renders fine because white

2条回答
  •  逝去的感伤
    2021-02-05 06:32

    This answer doesn't address your ASCII issue, but it currently is my go-to way when rendering latex equations in HTML, so I hope this helps you.

    Take a look at the mathjaxr package (CRAN, GitHub). You can install it either with install.packages("mathjaxr") or remotes::install_github("wviechtb/mathjaxr").

    It introduces a macro \loadmathjax that loads the necessary JavaScript from MathJax to render latex equations. Then use the macro \mjeqn{latex}{ascii} or \mjdeqn{latex}{ascii} instead of \eqn or \deqn and you're good to go.

    In your specific example, we'd have the following:

    #' Example
    #'
    #' Example mathjax function
    #'
    #' \loadmathjax
    #' \mjdeqn{ \sigma = \sqrt{ \frac{Z}{n} \sum
    #'   \textstyle\frac{1}{2}\displaystyle
    #'     \left\[ \left( \log \frac{H_i}{L_i} \right)^2  - (2\log 2-1) \left( \log \frac{C_i}{O_i} \right)^2 \right] }
    #' }{ASCII representation}
    example <- function(a, b, c) {}
    

    (notice the open square bracket is escaped)

    If you use RStudio, you may run into some trouble with the \loadmathjax macro. To preview the content quickly, do the following:

    1. Generate documentation (Ctrl + Shift + D or devtools::document(roclets = c('rd', 'collate', 'namespace')))
    2. Call preview_rd("example.Rd") to preview the documentation

    When you're done, you can inspect the final result with these steps:

    1. Generate documentation (Ctrl + Shift + D or devtools::document(roclets = c('rd', 'collate', 'namespace'))
    2. Install the package (Ctrl + Shift + L or devtools::install())
    3. Restart the R session with .rs.restartR()
    4. Preview documentation with ?example

    Either way should produce the following result:

提交回复
热议问题