Skip to content

Commit

Permalink
use more explicit delimiters for latex equations in r markdown documents
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed May 25, 2012
1 parent 6d1246b commit c4d7602
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 34 deletions.
45 changes: 33 additions & 12 deletions src/cpp/core/markdown/Markdown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,51 +205,72 @@ class MathFilter : boost::noncopyable
MathFilter(std::string* pInput, std::string* pHTMLOutput)
: pHTMLOutput_(pHTMLOutput)
{
filter(boost::regex("\\${2}[\\s\\S]+?\\${2}"), pInput);
filter(boost::regex("\\$(?!\\s)[^$]*[^$\\s]\\$"), pInput);
filter(boost::regex("\\${2}latex(\\s[\\s\\S]+?)\\${2}"),
pInput,
&displayMathBlocks_);

filter(boost::regex("\\$latex(\\s[\\s\\S]+?)\\$"),
pInput,
&inlineMathBlocks_);
}

~MathFilter()
{
try
{
std::for_each(mathBlocks_.begin(),
mathBlocks_.end(),
boost::bind(&MathFilter::restore, this, _1));
std::for_each(
displayMathBlocks_.begin(),
displayMathBlocks_.end(),
boost::bind(&MathFilter::restore, this, _1, "\\[", "\\]"));

std::for_each(
inlineMathBlocks_.begin(),
inlineMathBlocks_.end(),
boost::bind(&MathFilter::restore, this, _1, "\\(", "\\)"));
}
catch(...)
{
}
}

private:
void filter(const boost::regex& re, std::string* pInput)
void filter(const boost::regex& re,
std::string* pInput,
std::map<std::string,std::string>* pMathBlocks)
{
// explicit function type required because the Formatter functor
// supports 3 distinct signatures
boost::function<std::string(
boost::match_results<std::string::const_iterator>)> formatter =
boost::bind(&MathFilter::substitute, this, _1);
boost::bind(&MathFilter::substitute,
this, _1, pMathBlocks);

*pInput = boost::regex_replace(*pInput, re, formatter);
}

std::string substitute(
boost::match_results<std::string::const_iterator> match)
boost::match_results<std::string::const_iterator> match,
std::map<std::string,std::string>* pMathBlocks)
{
std::string guid = core::system::generateUuid(false);
mathBlocks_.insert(std::make_pair(guid, match[0]));
pMathBlocks->insert(std::make_pair(guid, match[1]));
return guid;
}

void restore(const std::map<std::string,std::string>::value_type& block)
void restore(const std::map<std::string,std::string>::value_type& block,
const std::string& beginDelim,
const std::string& endDelim)
{
boost::algorithm::replace_all(*pHTMLOutput_, block.first, block.second);
boost::algorithm::replace_first(
*pHTMLOutput_,
block.first,
beginDelim + " " + block.second + " " + endDelim);
}

private:
std::string* pHTMLOutput_;
std::map<std::string,std::string> mathBlocks_;
std::map<std::string,std::string> displayMathBlocks_;
std::map<std::string,std::string> inlineMathBlocks_;
};

} // anonymous namespace
Expand Down
14 changes: 9 additions & 5 deletions src/cpp/session/modules/SessionHTMLPreview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,11 +565,11 @@ bool requiresHighlighting(const std::string& htmlOutput)

bool requiresMathjax(const std::string& htmlOutput)
{
boost::regex inlineMathRegex("\\$(?!\\s)[^$]*[^$\\s]\\$");
boost::regex inlineMathRegex("\\\\\\(([\\s\\S]+?)\\\\\\)");
if (boost::regex_search(htmlOutput, inlineMathRegex))
return true;

boost::regex displayMathRegex("\\${2}[\\s\\S]+?\\${2}");
boost::regex displayMathRegex("\\\\\\[([\\s\\S]+?)\\\\\\]");
if (boost::regex_search(htmlOutput, displayMathRegex))
return true;

Expand Down Expand Up @@ -639,12 +639,16 @@ void modifyOutputForPreview(std::string* pOutput)
"config=TeX-AMS-MML_SVG");
#else
// add HTML-CSS options required for correct qtwebkit rendering
std::string target = "tex2jax: {";
std::string target = "<!-- MathJax scripts -->";
boost::algorithm::replace_first(
*pOutput,
target,
"\"HTML-CSS\": { minScaleAdjust: 125, availableFonts: [] }, "
+ target);
target + "\n"
"<script type=\"text/x-mathjax-config\">"
"MathJax.Hub.Config({"
" \"HTML-CSS\": { minScaleAdjust: 125, availableFonts: [] } "
" });"
"</script>");
#endif

// serve mathjax locally
Expand Down
4 changes: 2 additions & 2 deletions src/cpp/session/resources/markdown_help.html
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ <h4>Inline Code</h4>

<h4>LaTeX Equations</h4>
<p>Inline and display LaTeX equations:</p>
<pre><code>$<i>equation</i>$
<pre><code>$latex <i>equation</i> $

$$
$$latex
<i>equation</i>
$$
</code></pre>
Expand Down
11 changes: 1 addition & 10 deletions src/cpp/session/resources/mathjax.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
processEscapes: true,
processEnvironments: false,
inlineMath: [ ['$','$'] ],
displayMath: [ ['$$','$$'] ]
}
});
</script>
<!-- MathJax scripts -->
<script type="text/javascript" src="https://c328740.ssl.cf1.rackcdn.com/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>

20 changes: 15 additions & 5 deletions src/gwt/acesupport/acemode/markdown_highlight_rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ var MarkdownHighlightRules = function() {
regex : "^[ ]{0,2}(?:[ ]?\\_[ ]?){3,}\\s*$"
}, { // MathJax $$
token : "markup.list",
regex : "\\${2}",
next : "mathjax"
regex : "\\${2}latex\\s",
next : "mathjaxdisplay"
}, { // MathJax $
token : ["markup.list","support.function","markup.list"],
regex : "(\\$)" + "((?!\\s)[^$]*[^$\\s])" + "(\\$)"
token : "markup.list",
regex : "\\$latex\\s",
next : "mathjaxinline"
}, { // list
token : "text",
regex : "^\\s{0,3}(?:[*+-]|\\d+\\.)\\s+",
Expand Down Expand Up @@ -134,13 +135,22 @@ var MarkdownHighlightRules = function() {
regex : ".+"
} ],

"mathjax" : [ {
"mathjaxdisplay" : [ {
token : "markup.list",
regex : "\\${2}",
next : "start"
}, {
token : "support.function",
regex : "[^\\$]+"
} ],

"mathjaxinline" : [ {
token : "markup.list",
regex : "\\$",
next : "start"
}, {
token : "support.function",
regex : "[^\\$]+"
} ]
};
};
Expand Down

2 comments on commit c4d7602

@jeffreyhorner
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something to change in the markdown package as well?

@jjallaire
Copy link
Member Author

@jjallaire jjallaire commented on c4d7602 May 25, 2012 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.