Comments (5)
I've encountered the same conversion issues in markdown-pdf that have been mentioned. Specifically, the transformation of &
to &
, \\
to \
, and new lines to <br />
, significantly impacts MathJax rendering, with the double backslashes being a notable concern.
As a workaround, I've added the following code to template.html, which avoids these issues by replacing certain characters within LaTeX blocks. However, this approach feels like a temporary fix rather than a robust solution.
<script>
document.addEventListener("DOMContentLoaded", function () {
var pElements = document.querySelectorAll("p");
pElements.forEach(function (element) {
var html = element.innerHTML;
var updatedHtml = html.replace(
/(\$\$?)([\s\S]+?)\1/g,
function (match, delimiter, code) {
var newCode = code
.replace(/&/g, "&")
.replace(/<br>/g, "")
.replace(/\\(?![a-zA-Z])/g, "\\\\"); // Convert \ at the end of a sentence to \, but exclude \frac etc.
return delimiter + newCode + delimiter;
}
);
element.innerHTML = updatedHtml;
});
});
</script>
from vscode-markdown-pdf.
Thank you @karutt for the workaround script, I am encountering the same issue. I don't quite understand though where I need to put it for the script to be applied. I tried in template.html right before {{{body}}}
(and after some already existing mermaid script), as well as at the beginning of my document. However, I don't see a change in the pdf or html output. For example:
$$
\begin{align}
v_{i+1} &=
\underbrace{M \cdot v_i}_{\text{momentum}} -
\underbrace{\eta \cdot \left \langle \frac{\partial L}{\partial w} |_{w_i} \right \rangle_{D_i}}_{\text{gradient}} -
\underbrace{\mu \cdot \eta \cdot w_i}_\text{weight decay} \\
w_{i+1} &= w_i + v_{i+1}
\end{align}
$$
still becomes:
<p>$$
\begin{align}
v_{i+1} &=
\underbrace{M \cdot v_i}<em>{\text{momentum}} -
\underbrace{\eta \cdot \left \langle \frac{\partial L}{\partial w} |</em>{w_i} \right \rangle_{D_i}}<em>{\text{gradient}} -
\underbrace{\mu \cdot \eta \cdot w_i}</em>\text{weight decay} \
w_{i+1} &= w_i + v_{i+1}
\end{align}
$$</p>
from vscode-markdown-pdf.
The complete code, including the loading of MathJax, is as follows. Please copy this and add it to the bottom of the head tag in your template.js file.
<script>
window.onload = function () {
var pElements = document.querySelectorAll("p");
pElements.forEach(function (element) {
var html = element.innerHTML;
var updatedHtml = html.replace(
/(\$\$?)([\s\S]+?)\1/g,
function (match, delimiter, code) {
var newCode = code
.replace(/&/g, "&")
.replace(/<br>/g, "")
.replace(/<em>/g, "_")
.replace(/<\/em>/g, "_")
.replace(/\\(?![a-zA-Z])/g, "\\\\"); // Convert \ at the end of a sentence to \, but exclude \frac etc.
return delimiter + newCode + delimiter;
}
);
element.innerHTML = updatedHtml;
});
window.MathJax = {
tex: {
inlineMath: [["$", "$"]],
},
svg: {
fontCache: "global",
},
};
(function () {
var script = document.createElement("script");
script.src =
"https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js";
script.async = true;
document.head.appendChild(script);
})();
};
</script>
Additionally, I noticed an issue while running your sample LaTeX code: in certain cases, _
is converted to <em>
among others. I have corrected this issue as well.
from vscode-markdown-pdf.
Alright, that worked. Thanks a bunch!
from vscode-markdown-pdf.
<script>
window.onload = function () {
var pElements = document.querySelectorAll("p");
pElements.forEach(function (element) {
var html = element.innerHTML;
var updatedHtml = html.replace(
/(\$\$?)([\s\S]+?)\1/g,
function (match, delimiter, code) {
var newCode = code
.replace(/&/g, "&")
.replace(/<br>/g, "")
.replace(/<em>/g, "*") // Multiplication sign
.replace(/<\/em>/g, "*")
.replace(/\\(?![a-zA-Z])/g, "\\\\"); // Convert \ at the end of a sentence to \, but exclude \frac etc.
return delimiter + newCode + delimiter;
}
);
element.innerHTML = updatedHtml;
});
window.MathJax = {
tex: {
inlineMath: [["$", "$"]],
},
svg: {
fontCache: "global",
},
};
(function () {
var script = document.createElement("script");
script.src =
"https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js";
script.async = true;
document.head.appendChild(script);
})();
};
</script>
Thanks a lot! There are some modification for my case (just for personally backup):
- remove newline before " (function () { "...
- replace "" by "*"
from vscode-markdown-pdf.
Related Issues (20)
- Enable GitHub Discussion.
- What is the installed location of Chromium, and how can it be deleted? HOT 1
- how to add a underline under the img of the page header HOT 1
- text-underline-offset is not valid
- PDF text selection and search is not working HOT 2
- Add support for markdown note format HOT 1
- Add support for Mermaid invisible links
- The new keywords from metadata to be added to the header/footer template. HOT 1
- [demand]希望增加直接将一个文件夹中所有markdown文件导出为一个pdf的功能 HOT 1
- Blockquote render PDF
- References that skip invalid characters are not created as links
- Style sheet files specified by relative paths are not referenced as is
- Space after capital letter
- Stuck Exporting popups HOT 2
- Bug causes other extension to stop working and crash
- [FR] Increase heading line height
- [FR] Add outline to PDFs
- Option to define URL transformation
- How to solve this error in M series MacBooks? HOT 1
- Single page pdf HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vscode-markdown-pdf.