mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-06 11:48:41 +00:00
feat: Set Auto-render to recognize AMS environments without $$…$$ delimiters. (#2701)
* feat: Set Auto-render to recognize AMS environments without $$…$$ delimiters. * Replace tab with spaces * Treat AMS environments correctly.
This commit is contained in:
@@ -96,7 +96,14 @@ const renderMathInElement = function(elem, options) {
|
||||
{left: "\\(", right: "\\)", display: false},
|
||||
// LaTeX uses $…$, but it ruins the display of normal `$` in text:
|
||||
// {left: "$", right: "$", display: false},
|
||||
// $ must come after $$
|
||||
// $ must come after $$
|
||||
|
||||
// Render AMS environments even if outside $$…$$ delimiters.
|
||||
{left: "\\begin{equation}", right: "\\end{equation}", display: true},
|
||||
{left: "\\begin{align}", right: "\\end{align}", display: true},
|
||||
{left: "\\begin{alignat}", right: "\\end{alignat}", display: true},
|
||||
{left: "\\begin{gather}", right: "\\end{gather}", display: true},
|
||||
|
||||
{left: "\\[", right: "\\]", display: true},
|
||||
];
|
||||
optionsCopy.ignoredTags = optionsCopy.ignoredTags || [
|
||||
|
@@ -25,13 +25,15 @@
|
||||
<div id="test">
|
||||
This is some text $math \frac12$ other text $\unsupported$
|
||||
<span class="blue">
|
||||
Other node \[ displaymath \frac{1}{2} \] blah $$ \int_2^3 $$
|
||||
Other node \[ \text{displaymath} \frac{1}{2} \] blah $$ \int_2^3 $$
|
||||
</span>
|
||||
and some <!-- comment --> more text \(and math\) blah. And $math with a
|
||||
\$ sign$.
|
||||
<pre>
|
||||
Stuff in a $pre tag$
|
||||
</pre>
|
||||
<p>An AMS environment without <code>$$…$$</code> delimiters.</p>
|
||||
<p>\begin{equation} \begin{split} a &=b+c\\ &=e+f \end{split} \end{equation}</p>
|
||||
</div>
|
||||
<script>
|
||||
renderMathInElement(
|
||||
@@ -39,9 +41,13 @@
|
||||
{
|
||||
delimiters: [
|
||||
{left: "$$", right: "$$", display: true},
|
||||
{left: "\\[", right: "\\]", display: true},
|
||||
{left: "$", right: "$", display: false},
|
||||
{left: "\\(", right: "\\)", display: false}
|
||||
{left: "\\begin{equation}", right: "\\end{equation}", display: true},
|
||||
{left: "\\begin{align}", right: "\\end{align}", display: true},
|
||||
{left: "\\begin{alignat}", right: "\\end{alignat}", display: true},
|
||||
{left: "\\begin{gather}", right: "\\end{gather}", display: true},
|
||||
{left: "\\(", right: "\\)", display: false},
|
||||
{left: "\\[", right: "\\]", display: true}
|
||||
]
|
||||
}
|
||||
);
|
||||
|
@@ -31,6 +31,8 @@ const escapeRegex = function(string) {
|
||||
return string.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
|
||||
};
|
||||
|
||||
const amsRegex = /^\\begin{/;
|
||||
|
||||
const splitAtDelimiters = function(text, delimiters) {
|
||||
let index;
|
||||
const data = [];
|
||||
@@ -57,10 +59,14 @@ const splitAtDelimiters = function(text, delimiters) {
|
||||
if (index === -1) {
|
||||
break;
|
||||
}
|
||||
const rawData = text.slice(0, index + delimiters[i].right.length);
|
||||
const math = amsRegex.test(rawData)
|
||||
? rawData
|
||||
: text.slice(delimiters[i].left.length, index);
|
||||
data.push({
|
||||
type: "math",
|
||||
data: text.slice(delimiters[i].left.length, index),
|
||||
rawData: text.slice(0, index + delimiters[i].right.length),
|
||||
data: math,
|
||||
rawData,
|
||||
display: delimiters[i].display,
|
||||
});
|
||||
text = text.slice(index + delimiters[i].right.length);
|
||||
|
@@ -97,6 +97,15 @@ describe("A delimiter splitter", function() {
|
||||
rawData: "[[ world ]]", display: false},
|
||||
{type: "text", data: " boo"},
|
||||
]);
|
||||
expect("hello \\begin{equation} world \\end{equation} boo").toSplitInto(
|
||||
"\\begin{equation}", "\\end{equation}",
|
||||
[
|
||||
{type: "text", data: "hello "},
|
||||
{type: "math", data: "\\begin{equation} world \\end{equation}",
|
||||
rawData: "\\begin{equation} world \\end{equation}",
|
||||
display: false},
|
||||
{type: "text", data: " boo"},
|
||||
]);
|
||||
});
|
||||
|
||||
it("splits mutliple times", function() {
|
||||
|
@@ -84,6 +84,10 @@ in addition to two auto-render-specific keys:
|
||||
[
|
||||
{left: "$$", right: "$$", display: true},
|
||||
{left: "\\(", right: "\\)", display: false},
|
||||
{left: "\\begin{equation}", right: "\\end{equation}", display: true},
|
||||
{left: "\\begin{align}", right: "\\end{align}", display: true},
|
||||
{left: "\\begin{alignat}", right: "\\end{alignat}", display: true},
|
||||
{left: "\\begin{gather}", right: "\\end{gather}", display: true},
|
||||
{left: "\\[", right: "\\]", display: true}
|
||||
]
|
||||
```
|
||||
|
Reference in New Issue
Block a user