From murdoch@dunc@n @end|ng |rom gm@||@com  Tue May  5 15:37:30 2020
From: murdoch@dunc@n @end|ng |rom gm@||@com (Duncan Murdoch)
Date: Tue, 5 May 2020 09:37:30 -0400
Subject: [Rd] Use of MathJax (or something similar) in .Rd files
In-Reply-To: <65475fc6dc214df7a48151a5b6388cbb@UM-MAIL3214.unimaas.nl>
References: <8021fa8f9ad046ea9478ffaa4d604cb1@UM-MAIL3214.unimaas.nl>
 <CABtg=Km8CKTAJbp3H3aR7QfOPhg3u57sq6UxFzjbUE2PGmsv9Q@mail.gmail.com>
 <9eed70bdcdd54198992940818b963139@UM-MAIL3214.unimaas.nl>
 <2433a78d-0209-cbc2-71c0-6e4feb590293@gmail.com>
 <ccfcbcdad94a43cbbc3ef4f4cbacaf3c@UM-MAIL3214.unimaas.nl>
 <98017b68d8874f44bef58880a02ac5bb@jhu.edu>
 <65475fc6dc214df7a48151a5b6388cbb@UM-MAIL3214.unimaas.nl>
Message-ID: <8ce10555-68da-1a4a-4869-9d79fd79f76e@gmail.com>

The package is really nice, I recommend it.  Package writers should 
install it, but users of their help pages don't need it unless they're 
working offline.  Even then, because of the way MathJax works, they get 
something that's fairly readable, e.g.

"The probability density function of a normal distribution is given by 
\[f(x) = \frac{1}{\sqrt{2\pi} \sigma} 
e^{-\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^2},\] where \(\mu\) 
denotes the mean of the distribution and \(\sigma\) its standard deviation."

For a user who is online, or who has the mathjaxr package installed, 
this would be displayed with the equation as a display equation, and mu 
and sigma displayed properly inline.  To obtain that output I used this 
input in an Rd file:

"The probability density function of a normal distribution is given by 
\mjdeqn{f(x) = \frac{1}{\sqrt{2\pi} \sigma} 
e^{-\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^2},}{f(x) = 1/(sqrt(2 
pi) sigma) e^-((x - mu)^2/(2 sigma^2)),} where \mjeqn{\mu}{mu} denotes 
the mean of the distribution and \mjeqn{\sigma}{sigma} its standard 
deviation."

A frill that might be possible to add would be a test for a remote 
connection, and a fallback to the ascii display if it's not there.  It 
already has a display-time test for the presence of the MathJax files 
locally.

Maybe once all the kinks are worked out this could be incorporated into 
the R distribution.  However, it's not really needed there, since it 
works so well as a contributed package already.

Duncan Murdoch


On 05/05/2020 9:11 a.m., Viechtbauer, Wolfgang (SP) wrote:
> Hi All,
> 
> After some tinkering, and with support from Duncan, I put together a package that allows for easy inclusion of MathJax equations in Rd files. The package has been submitted to CRAN, but those who want to try this out already can get it here:
> 
> https://github.com/wviechtb/mathjaxr
> 
> or in other words:
> 
> install.packages("remotes")
> remotes::install_github("wviechtb/mathjaxr")
> 
> Package authors can then use the \loadmathjax macro to enable its use and the \mjeqn{latex}{ascii} and \mjdeqn{latex}{ascii} macros for including inline and displayed equations.
> 
> Feedback and suggestions more than welcome!
> 
> Best,
> Wolfgang
> 
>> -----Original Message-----
>> From: Ravi Varadhan [mailto:ravi.varadhan at jhu.edu]
>> Sent: Thursday, 30 April, 2020 15:58
>> To: Viechtbauer, Wolfgang (SP); r-devel
>> Subject: Re: [Rd] Use of MathJax (or something similar) in .Rd files
>>
>> Thank you.? This is a very useful idea!
>>
>> Best,
>> Ravi
>> ________________________________________
>> From: R-devel <r-devel-bounces at r-project.org> on behalf of Viechtbauer,
>> Wolfgang (SP) <wolfgang.viechtbauer at maastrichtuniversity.nl>
>> Sent: Thursday, April 30, 2020 6:59:02 AM
>> To: r-devel
>> Subject: Re: [Rd] Use of MathJax (or something similar) in .Rd files
>>
>> Thanks Gabor and Duncan! It works. For those interested, I added this to the
>> beginning of the \details{} section:
>>
>> \if{html}{\out{
>> <script id="MathJax-script" async
>>  ?? src="https://cdn.jsdelivr.net/npm/mathjax at 3/es5/tex-mml-chtml.js">
>> </script>
>> }}
>>
>> And then I can use:
>>
>> \if{html}{\out{\(B_{x(a,b)} = \int_0^x t^{a-1} (1-t)^{b-1} dt\)}}
>>
>> or
>>
>> \if{html}{\out{\[B_{x(a,b)} = \int_0^x t^{a-1} (1-t)^{b-1} dt\]}}
>>
>> to render the equation (inline or as 'displaymath'). I'll try to add MathJax
>> locally next.
>>
>> Best,
>> Wolfgang
>>
>>> -----Original Message-----
>>> From: Duncan Murdoch [mailto:murdoch.duncan at gmail.com]
>>> Sent: Thursday, 30 April, 2020 12:22
>>> To: Viechtbauer, Wolfgang (SP); r-devel
>>> Subject: Re: [Rd] Use of MathJax (or something similar) in .Rd files
>>>
>>> On 30/04/2020 6:15 a.m., Viechtbauer, Wolfgang (SP) wrote:
>>>> Interesting. I gave this a try, but couldn't make this work. One would
>>> have to infuse something like
>>>>
>>>> <script id="MathJax-script" async
>>>>  ??? src="https://cdn.jsdelivr.net/npm/mathjax at 3/es5/tex-mml-chtml.js">
>>>> </script>
>>>>
>>>> or
>>>>
>>>> <script id="MathJax-script" async
>>>>  ??? src="<url-to-your-site>/mathjax/tex-chtml.js">
>>>> </script>
>>>>
>>>> into the .html file, but just adding this to the .Rd file (directly or
>>> wrapped with \if{html}{}) doesn't do that (it just gets printed). If
>>> somebody has already successfully done this, I would be interested in
>>> hearing how.
>>>
>>> You'd need to wrap that code in \out (inside the conditional, i.e.
>>> something like
>>>
>>> \if{html}{\out{<script id="MathJax-script" async
>>> src="<url-to-your-site>/mathjax/tex-chtml.js"></script>}}
>>>
>>> Duncan Murdoch
>>>
>>>>
>>>> Best,
>>>> Wolfgang
>>>>
>>>>> -----Original Message-----
>>>>> From: G?bor Cs?rdi [mailto:csardi.gabor at gmail.com]
>>>>> Sent: Thursday, 30 April, 2020 11:51
>>>>> To: Viechtbauer, Wolfgang (SP)
>>>>> Cc: r-devel
>>>>> Subject: Re: [Rd] Use of MathJax (or something similar) in .Rd files
>>>>>
>>>>> You can probably already do this using the \if{html} conditional, and
>>>>> either including the matchjax js files in the package, or getting them
>>>> >from a CDN.
>>>>>
>>>>> Gabor
>>>>>
>>>>> On Thu, Apr 30, 2020 at 9:13 AM Viechtbauer, Wolfgang (SP)
>>>>> <wolfgang.viechtbauer at maastrichtuniversity.nl> wrote:
>>>>>>
>>>>>> Hello All,
>>>>>>
>>>>>> I am wondering if there has ever been any discussion/consideration
>> given
>>>>> to incorporating MathJax (or something similar) into R for rendering
>>>>> equations in .Rd files. I know that equations are rendered beautifully
>> in
>>>>> the pdf manuals, but I suspect the majority of users primarily look at
>>> the
>>>>> html help files when using R. While I am comfortable reading something
>>> like
>>>>> "B_x(a,b) = integral_0^x t^(a-1) (1-t)^(b-1) dt", it would definitely be
>>>>> nicer if this was shown as a 'proper' equation.
>>>>>>
>>>>>> Best,
>>>>>> Wolfgang
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



[Rd] Use of MathJax (or something similar) in .Rd files
Duncan Murdoch