Skip to content

[R-pkg-devel] MathJax for Rd files

21 messages · Wolfgang Viechtbauer, Helmut Schütz, Tiago Olivoto +3 more

#
Dear all,

I'm not sure if this is the correct place to make this question, but here I
am.

I'm trying to use MathJax for html help files in my package metan
(https://cran.r-project.org/package=metan)

I have success with the following approach:

 

Including the following call

 

#'\if{html}{\out{

#'<script id="MathJax-script" async

#'src="https://cdn.jsdelivr.net/npm/mathjax at 3.0.5/es5/tex-chtml-full.js">

#'  </script>

#'}} 

 

And then typing the equations I need. A simple example bellow

 

#' \ifelse{html}{\out{\[\sqrt{a^2}\]}}{\deqn{\sqrt{a^2}}}

 

Is there any way to put "tex-chtml-full.js" locally so that everyone that
has my package installed see the equations rendered properly even offline?

Thanks in advance

Olivoto
#
Dear Olivoto,

There is a package now for this purpose:

https://cran.r-project.org/package=mathjaxr
https://github.com/wviechtb/mathjaxr

Best,
Wolfgang
#
Dear Wolfgang,
Thank you so much for the awesome package
I've installed mathjaxr and it worked.
I have some more questions, with a short example bellow
I created an equation in my mgidi.R file as follows

#' \loadmathjax
#' \mjsdeqn{MGIDI_i = \sum\limits_{j = 1}^f}

And then I ran devtools::document()
The mgidi.Rd contains

\loadmathjax
\mjsdeqn{MGIDI_i = \sum\limits_{j = 1}^f}

But when I try to preview the current document as HTML, the equation is not
rendered and the help page shows
\loadmathjax \mjsdeqnMGIDI_i = \sum\limits_j = 1^f
The equation is only rendered after I install my package locally with
devtools::install(). Is this an expected behavior?

Another question is regarding more complex formulae.
I tried using
#' \loadmathjax
#' \mjsdeqn{MGID{I_i} = {\left[ {\sum\limits_{j = 1}^f {{{\left( {{F_{ij}} -
{F_j}} \right)}^2}} } \right]^{0.5}}}

Even after installing the package locally the equation is not rendered in
the html help page, which shows the tex command. 
I tested the same equation with https://www.mathjax.org/#demo and it seems
to be ok.
Have I missed anything or used any code not supported by mathjaxr?
Thanks in advance
Olivoto

-----Mensagem original-----
De: Viechtbauer, Wolfgang (SP)
<wolfgang.viechtbauer at maastrichtuniversity.nl> 
Enviada em: quarta-feira, 13 de maio de 2020 04:40
Para: tiagoolivoto at gmail.com; r-package-devel at r-project.org
Assunto: RE: [R-pkg-devel] MathJax for Rd files

Dear Olivoto,

There is a package now for this purpose:

https://cran.r-project.org/package=mathjaxr
https://github.com/wviechtb/mathjaxr

Best,
Wolfgang
offline?
#
Seems like you are using roxygen2. I have little experience with that, as all my Rd files are 'handcrafted' (plus 100% organic and biodegradable).

But what specifically do you mean by "when I try to preview the current document as HTML"? Is this some particular functionality of roxygen2 / devtools / RStudio? Or do you simply mean using tools::Rd2HTML()? Then you will have to use permissive=TRUE so that the macros from mathjaxr (which will not be recognized by default by parse_Rd()) are just passed through as text. Or you could try adjusting the 'macros' argument, maybe in combination with loadPkgRdMacros(), to specify that the mathjaxr macros should be loaded. See help(Rd2HTML, package="tools") and help(parse_Rd, package="tools") for details.

But if I want to see the HTML help file, I just install and load the package and call up the help file on some function. That works for sure.

As for the more complex equation - it includes a lot of curly braces. See the note I added recently to the 'devel' version about this:

https://github.com/wviechtb/mathjaxr#issues

Best,
Wolfgang
#
Hi Wolfgang,

Viechtbauer, Wolfgang (SP) wrote on 2020-05-13 16:53:
As are mine. ;-)
In the HTML-preview of RStudio the LaTeX is indeed not parsed. I get it 
only (in the HTML man-page and the PDF) if I build the package.

Helmut
#
Thanks for the info! Can confirm now that I tried this (I don't usually use RStudio). That's an RStudio issue then. My guess is that 'Preview' uses tools::Rd2HTML() and that indeed won't automatically understand macros coming from mathjaxr (or any add-on package for that matter). This would be an issue to raise with the RStudio folks then.

But once a package is installed, the equations are nicely rendered also in the RStudio help browser, so that part works.

Best,
Wolfgang
#
Thanks, Wolfgang and Helmut for the explanations.
When I was trying to use

#'\if{html}{\out{'<script id="MathJax-script" async
#''src="https://cdn.jsdelivr.net/npm/mathjax at 3.0.5/es5/tex-chtml-full.js
#'</script> }}
#'
#'And then typing
#' \ifelse{html}{\out{\[\sqrt{a^2}\]}}{\deqn{\sqrt{a^2}}}
My mgidi.Rd file was produced with devtools::document();
then, I just used ?mgidi, and the html page was rendered nicely.

If I could do the same with mathjaxr, It would be nice, because I wouldn't need to install the package to see if the equations are as I expect
Anyway, mathjaxr is a very useful package.
Congratulations on the efforts for developing it.

Best
Olivoto

-----Mensagem original-----
De: Viechtbauer, Wolfgang (SP) <wolfgang.viechtbauer at maastrichtuniversity.nl> 
Enviada em: quarta-feira, 13 de maio de 2020 13:14
Para: Helmut Sch?tz <helmut.schuetz at bebac.at>; tiagoolivoto at gmail.com; r-package-devel at r-project.org
Assunto: RE: [R-pkg-devel] MathJax for Rd files

Thanks for the info! Can confirm now that I tried this (I don't usually use RStudio). That's an RStudio issue then. My guess is that 'Preview' uses tools::Rd2HTML() and that indeed won't automatically understand macros coming from mathjaxr (or any add-on package for that matter). This would be an issue to raise with the RStudio folks then.

But once a package is installed, the equations are nicely rendered also in the RStudio help browser, so that part works.

Best,
Wolfgang
#
On 13/05/2020 12:35 p.m., Tiago Olivoto wrote:
Sounds like it's a devtools issue.  I don't know how they are simulating 
the install, but it needs to say to use the mathjaxr macros when 
converting the Rd pages.

Duncan Murdoch
#
Thanks, Duncan. I was about to respond to suggest the same thing.

One way of getting this to work:

Sys.setenv(MATHJAXR.USECDN = "TRUE")
Rd <- file.path("<path>/<file>.Rd") # specify the .Rd file you want to preview
outfile <- tempfile(fileext = ".html")
browseURL(tools::Rd2HTML(Rd, outfile, macros="<path to library where mathjaxr is installed>/mathjaxr/help/macros/mathjax.Rd"))

Best,
Wolfgang
#
Thank you so much! It works!
In my example, I've created a simple helper function, preview_rd()

preview_rd <- function(rdfile){ # without '.rd'
Rd <- file.path(paste("man/", rdfile, ".rd", sep = "")) # specify the .Rd file you want to preview
outfile <- tempfile(fileext = ".html")
browseURL(tools::Rd2HTML(Rd, outfile, macros="D:/Documents/R/win-library/4.0/mathjaxr/help/macros/mathjax.Rd"))
}

I added the macros \loadmathjax and \mjsdeqn in the mgidi.R file, ran devtools::document() and then preview_rd("mgidi")
The html was rendered nicely (in my browser) and shows the equation without installing the package.
That's what I was looking for.
Thanks again!
Olivoto

-----Mensagem original-----
De: Viechtbauer, Wolfgang (SP) <wolfgang.viechtbauer at maastrichtuniversity.nl> 
Enviada em: quarta-feira, 13 de maio de 2020 14:41
Para: Duncan Murdoch <murdoch.duncan at gmail.com>; tiagoolivoto at gmail.com; r-package-devel at r-project.org
Assunto: RE: [R-pkg-devel] RES: MathJax for Rd files

Thanks, Duncan. I was about to respond to suggest the same thing.

One way of getting this to work:

Sys.setenv(MATHJAXR.USECDN = "TRUE")
Rd <- file.path("<path>/<file>.Rd") # specify the .Rd file you want to preview outfile <- tempfile(fileext = ".html") browseURL(tools::Rd2HTML(Rd, outfile, macros="<path to library where mathjaxr is installed>/mathjaxr/help/macros/mathjax.Rd"))

Best,
Wolfgang
#
Glad to hear that. I might consider adding a function along those lines to mathjaxr itself, to make it easier to preview files while working on a help page.

Best,
Wolfgang
#
On 13/05/2020 2:30 p.m., Tiago Olivoto wrote:
Good to hear.  I'd suggest this way to find the macros:

   browseURL(tools::Rd2HTML(Rd, outfile, macros = 
system.file("help/macros/mathjax.Rd", package = "mathjaxr")))

so that it works if you use it on a different system that puts the 
library somewhere else.

Duncan Murdoch
#
Definitively this is a better way to find the macros.
I think a function like 'preview_rd()' could be useful in mathjaxr

Thanks for all support
Olivoto

-----Mensagem original-----
De: Duncan Murdoch <murdoch.duncan at gmail.com> 
Enviada em: quarta-feira, 13 de maio de 2020 16:16
Para: tiagoolivoto at gmail.com; 'Viechtbauer, Wolfgang (SP)' <wolfgang.viechtbauer at maastrichtuniversity.nl>; r-package-devel at r-project.org
Assunto: Re: RES: [R-pkg-devel] RES: MathJax for Rd files
On 13/05/2020 2:30 p.m., Tiago Olivoto wrote:
Good to hear.  I'd suggest this way to find the macros:

   browseURL(tools::Rd2HTML(Rd, outfile, macros = system.file("help/macros/mathjax.Rd", package = "mathjaxr")))

so that it works if you use it on a different system that puts the library somewhere else.

Duncan Murdoch
#
I've added a preview_rd() function to the 'devel' version of mathjaxr:

https://github.com/wviechtb/mathjaxr

If you want to give this a try:

remotes::install_github("wviechtb/mathjaxr")
library(mathjaxr)

Then set the working dir to the root of the package you are working on. Then use:

preview_rd(Rdfile)

with the name of the Rd file given as a character string (either with or without the .Rd/.rd extension).

By default, the browser (or a new tab in your browser) should open up. But if you then make further changes to the Rd file, opening up a new tab each time is inconvenient. Use:

preview_rd(Rdfile, view=FALSE)

to prevent that (then just refresh the page in the open tab in your browser to see the updates).

One limitation: As far as I can tell, it will not be possible to load MathJax from the mathjaxr package when using preview_rd(). So, it will be loaded via the CDN, which means that you need an internet connection for equations to render.

Best,
Wolfgang
#
I installed the 'devel' version of mathjaxr and tested preview_rd()
All seems to be working fine!
Thank you for the quick and useful implementation!
Best,
Olivoto

-----Mensagem original-----
De: Viechtbauer, Wolfgang (SP) <wolfgang.viechtbauer at maastrichtuniversity.nl> 
Enviada em: quarta-feira, 13 de maio de 2020 18:26
Para: tiagoolivoto at gmail.com; 'Duncan Murdoch' <murdoch.duncan at gmail.com>; r-package-devel at r-project.org
Assunto: RE: RES: [R-pkg-devel] RES: MathJax for Rd files

I've added a preview_rd() function to the 'devel' version of mathjaxr:

https://github.com/wviechtb/mathjaxr

If you want to give this a try:

remotes::install_github("wviechtb/mathjaxr")
library(mathjaxr)

Then set the working dir to the root of the package you are working on. Then use:

preview_rd(Rdfile)

with the name of the Rd file given as a character string (either with or without the .Rd/.rd extension).

By default, the browser (or a new tab in your browser) should open up. But if you then make further changes to the Rd file, opening up a new tab each time is inconvenient. Use:

preview_rd(Rdfile, view=FALSE)

to prevent that (then just refresh the page in the open tab in your browser to see the updates).

One limitation: As far as I can tell, it will not be possible to load MathJax from the mathjaxr package when using preview_rd(). So, it will be loaded via the CDN, which means that you need an internet connection for equations to render.

Best,
Wolfgang
#
The issue is not with Rstudio per se but that in devtools' development mode  help() is modified to show the Rd files in the source directory, as one would expect, but doesn't load the RdMacros. So, the issue appears when one is development mode. 

Rdpack::viewRd() can be of some help but it currently loads only the RdMacro's from its package.

Georgi Boshnakov

-----Original Message-----
From: R-package-devel <r-package-devel-bounces at r-project.org> On Behalf Of Viechtbauer, Wolfgang (SP)
Sent: 13 May 2020 17:14
To: Helmut Sch?tz <helmut.schuetz at bebac.at>; tiagoolivoto at gmail.com; r-package-devel at r-project.org
Subject: Re: [R-pkg-devel] MathJax for Rd files

Thanks for the info! Can confirm now that I tried this (I don't usually use RStudio). That's an RStudio issue then. My guess is that 'Preview' uses tools::Rd2HTML() and that indeed won't automatically understand macros coming from mathjaxr (or any add-on package for that matter). This would be an issue to raise with the RStudio folks then.

But once a package is installed, the equations are nicely rendered also in the RStudio help browser, so that part works.

Best,
Wolfgang
______________________________________________
R-package-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel
#
On 14/05/2020 3:30 a.m., Georgi Boshnakov wrote:
Yes, the error appears to be in the pkgload::load_rd_macros() function 
here: 
https://github.com/r-lib/pkgload/blob/1b1adc39628671f2ea4256f0bebcdb76e1f89f45/R/dev-help.r#L47.

It doesn't look at the DESCRIPTION file to see if there's an RdMacros entry.

Duncan Murdoch
#
On Thu, May 14, 2020 at 10:27 AM Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
Isn't that what tools::loadPkgRdMacros() does there? I believe so.

Gabor

[...]
#
On 14/05/2020 5:46 a.m., G?bor Cs?rdi wrote:
Yes, you're right.  So it's not clear why the preview isn't working.

Duncan Murdoch
#
I don't know if this helps but I have the following note in  viewRd() (from 2017!) at https://github.com/GeoBosh/Rdpack/blob/81af94d0e690b9f02275e89d474ce184d63fb044/R/bib.R

...
    ## TODO: (BUG) e is NULL under RStudio
    e <- tools::loadPkgRdMacros(system.file(package = "Rdpack"))
## Rdo <- parse_Rd(infile, macros = e)
...

Maybe I have forgotten to delete the note after fixing that since it works  (I just checked in Rstudio) but maybe that's the reason I restricted it to just macros in Rdpack.

Georgi Boshnakov


-----Original Message-----
From: Duncan Murdoch <murdoch.duncan at gmail.com> 
Sent: 14 May 2020 12:29
To: G?bor Cs?rdi <csardi.gabor at gmail.com>
Cc: Georgi Boshnakov <georgi.boshnakov at manchester.ac.uk>; Viechtbauer, Wolfgang (SP) <wolfgang.viechtbauer at maastrichtuniversity.nl>; Helmut Sch?tz <helmut.schuetz at bebac.at>; tiagoolivoto at gmail.com; r-package-devel at r-project.org
Subject: Re: [R-pkg-devel] MathJax for Rd files
On 14/05/2020 5:46 a.m., G?bor Cs?rdi wrote:
Yes, you're right.  So it's not clear why the preview isn't working.

Duncan Murdoch
#
On 14/05/2020 5:46 a.m., G?bor Cs?rdi wrote:
I think I've tracked it down correctly this time.

The problem appears to be in RStudio code called by 
pkgload::print.dev_Topic. That function correctly loads the macros, but 
has this code:

     if (rstudioapi::hasFun("previewRd")) {
       rstudioapi::callFun("previewRd", x$path)
       return(invisible())
     }

(This is here: 
https://github.com/r-lib/pkgload/blob/1b1adc39628671f2ea4256f0bebcdb76e1f89f45/R/dev-help.r#L68 
.)

This never passes the macros to the preview code built in to RStudio. 
The function in RStudio is here: 
https://github.com/rstudio/rstudio/blob/2596a760603e06251cce829f236f57c5eff048c9/src/cpp/r/R/Api.R#L101 
.  It only accepts one argument, rather than the two that are needed.

Duncan Murdoch