ramnathv / slidify

Generate reproducible html5 slides from R markdown
http://www.slidify.org
844 stars 338 forks source link

difficulties with equations in slidify #214

Open cboettig opened 11 years ago

cboettig commented 11 years ago

The default markdown parser clobbers my equations (usually because it mistakes underscores for markdown emphasis). I usually use pandoc as my markdown parser since it avoids this difficulty. Perhaps there is an easy fix in slidify? The following slide doesn't render correctly using the default setup+mathjax enabled:


---
title       : 
subtitle    : 
author      : 
job         : 
framework   : io2012        # {io2012, html5slides, shower, dzslides, ...}
highlighter : highlight.js  # {highlight.js, prettify, highlight}
hitheme     : tomorrow      # 
widgets     : mathjax       # {mathjax, quiz, bootstrap}
mode        : selfcontained # {standalone, draft}

---

Solve: 

$$V_t(S_t) = \max_{x_t \in \chi_t} \left(C(S_t, x_t) + 
            \gamma \sum_{s^{\prime} \in \mathcal{S}} \mathbb{P}(s^{\prime} | S_t^n, x_t) V_{t+1}^{n-1} s^{\prime} \right)$$

(parses fine in pandoc/mathjax) Thanks for a great tool and for the help!

ramnathv commented 11 years ago

I am having no trouble with it. Here is how it is rendered for me. Is that what you would expect?

screen shot 2013-05-28 at 3 01 21 pm

What version of Slidify are you using? I would recommend updating the latest version on the dev branch on github. You can install both slidify and slidifyLibraries using.

require(devtools)
pkgs = c('slidify', 'slidifyLibraries')
install_github(pkgs, 'ramnathv', ref = 'dev')
cboettig commented 11 years ago

Interesting ... looks good on your version. I installed the dev branch, now I get an error (before I just had unparsed equations)

 slidify('index.Rmd')

processing file: index.Rmd
  |.................................................................| 100%
  ordinary text without R code

output file: index.md

Error in render(value, context) : could not find function "tmpl"
In addition: Warning message:
In partial(key$key[i], partials, indent) :
  No partial 'head' or file 'head.mustache' found

Enter a frame number, or 0 to exit   

 1: slidify("index.Rmd")
 2: slidify.R#7: render_page(page, payload = list(site = site), return_page)
 3: render.R#32: in_dir(dirname(page$file), {
    if (page$mode == "selfcontained") {
        page$url[["lib"]] <- page$url[["lib"]] %||% "libraries"
        with(p
 4: utils.R#171: force(expr)
 5: render.R#64: cat(whisker.render(layout, payload, partials = partials), file = outputFile)
 6: whisker.render(layout, payload, partials = partials)
 7: tmpl(data)
 8: renderTemplate(values = values, context = context, texts = texts, renders = renders, debug = debug)
 9: mapply(values, renders, FUN = function(value, render) {
    render(value, context)
})
10: (function (value, render) 
{
    render(value, context)
})(dots[[1]][[1]], dots[[2]][[1]])
11: render(value, context)

hmm....

ramnathv commented 11 years ago

Did you update slidifyLibraries too? You need the dev version of slidifyLibraries as well. Can you post your sessionInfo?

Also, delete your libraries folder, since it will not be regenerated if already present. I am assuming, you didnt make any customizations in the libraries folder, as the recommended way to add custom files is to use the assets folder.

cboettig commented 11 years ago

aha, deleting the libraries folder let me successfully run slidify. Same as before though, isn't showing the equation...

sessionInfo()

 sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=C                 LC_NUMERIC=C               LC_TIME=C                  LC_COLLATE=C               LC_MONETARY=C             
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=C                 LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
 [1] slidifyLibraries_0.2.11 slidify_0.3.51          pdgControl_0.0-2        data.table_1.8.8        ggplot2_0.9.3.1         reshape2_1.2.2         
 [7] knitcitations_0.4-4     bibtex_0.3-5            knitr_1.2               devtools_1.2           

loaded via a namespace (and not attached):
 [1] Hmisc_3.10-1.1     MASS_7.3-26        Matrix_1.0-12      RColorBrewer_1.0-5 RCurl_1.95-4.1     RWordPress_0.2-3   Rflickr_0.2-1      XML_3.96-1.1      
 [9] XMLRPC_0.3-0       cluster_1.14.4     colorspace_1.2-2   cubature_1.1-2     dichromat_2.0-0    digest_0.6.3       evaluate_0.4.3     expm_0.99-0       
[17] formatR_0.7        grid_3.0.1         gtable_0.1.2       httr_0.2           labeling_0.1       lattice_0.20-15    markdown_0.5.4     memoise_0.1       
[25] munsell_0.4        parallel_3.0.1     plyr_1.8           proto_0.3-10       scales_0.2.3       snowfall_1.84-4    socialR_0.0-1      stringr_0.6.2     
[33] tools_3.0.1        whisker_0.3-2      xtable_1.7-1       yaml_2.1.7   
ramnathv commented 11 years ago

Can you post the .md and .html files as a gist? I want to inspect what exactly is happening there. What version of the markdown package do you have installed?

cboettig commented 11 years ago

.md and .html here:

https://github.com/cboettig/nonparametric-bayes/commit/3799a32fa068544cab11c79c8cb2bcd00d009e73

as my session info shows, I have markdown_0.5.4 installed.

Thanks for the help troubleshooting!

ramnathv commented 11 years ago

For some reason, the markdown package seems to be parsing the content between the $$ signs. This could be because you have different settings for markdown. One quick workaround is to place the equation within div tags to ensure that markdown does not parse the equation. Can you check if that works?

I will continue to investigate what might be problem.

ramnathv commented 11 years ago

I just pushed a small patch that might work. Install it from here https://github.com/ramnathv/slidify/tree/patch-md2html. (ref = 'patch-md2html')

The key is this function. I think that renderMarkdown does not use the same defaults as markdown::markdownToHTML which is what was causing the problem. I would appreciate if you can check this with different equations that you have. I will add it to a visual test-suite so that I can check how changes break my builds in the future.

md2html <- function(md){
  renderMarkdown(text = md, 
    renderer.options = c('hard_wrap'),
    extensions = markdownExtensions()
  )
}
cboettig commented 11 years ago

installed the patch (via install_github(.. ref='patch-md2html') like you suggested), but am getting the same html with the equations mangled by the markdown parser...

On Tue, May 28, 2013 at 1:08 PM, Ramnath Vaidyanathan < notifications@github.com> wrote:

I just pushed a small patch that might work. Install it from here https://github.com/ramnathv/slidify/tree/patch-md2html. (ref = 'patch-md2html')

The key is this function. I think that renderMarkdown does not use the same defaults as markdown::markdownToHTML which is what was causing the problem. I would appreciate if you can check this with different equations that you have. I will add it to a visual test-suite so that I can check how changes break my builds in the future.

md2html <- function(md){ renderMarkdown(text = md, renderer.options = c('hard_wrap'), extensions = markdownExtensions() )}

— Reply to this email directly or view it on GitHubhttps://github.com/ramnathv/slidify/issues/214#issuecomment-18576464 .

Carl Boettiger UC Santa Cruz http://carlboettiger.info/

ramnathv commented 11 years ago

Did wrapping the equation in a div tag help? That should force the markdown parser to skip the entire block.

ramnathv commented 11 years ago

Actually, I did something stupid. I never committed the change I made before I pushed. I will experiment with different options tonight to see what works best with equations.

cboettig commented 11 years ago

Yup, div wrapping works. It's not a good solution for inline equations (span-wrapping doesn't), as both these wrapping rules are part of the original markdown specification and work with any parser.

It looks like things might work if markdownExtensions were to use the "latex_math" option? (maybe with the no_intra_emphasis option too?)

Not too familiar with the sundown library. I use pandoc to avoid these problems...

On Tue, May 28, 2013 at 4:01 PM, Ramnath Vaidyanathan < notifications@github.com> wrote:

Did wrapping the equation in a div tag help? That should force the markdown parser to skip the entire block.

— Reply to this email directly or view it on GitHubhttps://github.com/ramnathv/slidify/issues/214#issuecomment-18585982 .

Carl Boettiger UC Santa Cruz http://carlboettiger.info/

ramnathv commented 11 years ago

This has been a thorn in my flesh for a really long time. I will set aside some time to resolve this ASAP. Does the problem occur with both inline and display equations?

cboettig commented 11 years ago

Yes, the problem occurs with display equations unless they are wrapped in a div. The problem always occurs with inline equations since it doesn't make sense to wrap them in divs. Of course the problem only occurs in equations that have markdown-like markup, such as multiple underscores, but that's pretty common.

Rstudio's mathjax handling does okay on this (e.g. handles most display equations fine without div wrapping) though occasionally I have had trouble. Have never had trouble in pandoc's rendering...

On Tue, May 28, 2013 at 4:55 PM, Ramnath Vaidyanathan < notifications@github.com> wrote:

This has been a thorn in my flesh for a really long time. I will set aside some time to resolve this ASAP. Does the problem occur with both inline and display equations?

— Reply to this email directly or view it on GitHubhttps://github.com/ramnathv/slidify/issues/214#issuecomment-18587931 .

Carl Boettiger UC Santa Cruz http://carlboettiger.info/

ramnathv commented 11 years ago

I agree. The part that I am unable to understand is, why it works perfectly for me. What OS are you on?

cboettig commented 11 years ago

Yeah, that's a real mystery to me too. I'm on Ubuntu 13.04 (or as the sessionInfo puts it, Platform: x86_64-pc-linux-gnu (64-bit))

Do you have the same version of the markdown package as me?

On Tue, May 28, 2013 at 5:07 PM, Ramnath Vaidyanathan < notifications@github.com> wrote:

I agree. The part that I am unable to understand is, why it works perfectly for me. What OS are you on?

— Reply to this email directly or view it on GitHubhttps://github.com/ramnathv/slidify/issues/214#issuecomment-18588310 .

Carl Boettiger UC Santa Cruz http://carlboettiger.info/

cboettig commented 11 years ago

The markdown package may be reading options in from an environment, my guess is that you have some markdownExtensions environmental variables set, e.g. "latex_math" that I mentioned earlier. Because your code (e.g. what you sent, below) does not explicitly set markdownExtensions, the configuration can differ between users...

md2html <- function(md){ renderMarkdown(text = md, renderer.options = c('hard_wrap'), extensions = markdownExtensions() ) }

On Tue, May 28, 2013 at 5:20 PM, Carl Boettiger cboettig@gmail.com wrote:

Yeah, that's a real mystery to me too. I'm on Ubuntu 13.04 (or as the sessionInfo puts it, Platform: x86_64-pc-linux-gnu (64-bit))

Do you have the same version of the markdown package as me?

On Tue, May 28, 2013 at 5:07 PM, Ramnath Vaidyanathan < notifications@github.com> wrote:

I agree. The part that I am unable to understand is, why it works perfectly for me. What OS are you on?

— Reply to this email directly or view it on GitHubhttps://github.com/ramnathv/slidify/issues/214#issuecomment-18588310 .

Carl Boettiger UC Santa Cruz http://carlboettiger.info/

Carl Boettiger UC Santa Cruz http://carlboettiger.info/

ramnathv commented 11 years ago

In my original version, I set nothing. I suspect renderMarkdown has a different default behavior as compared with that used by knit2html.