Skip to content

Commit

Permalink
[Tutorials] Improve conversion of math to MDX
Browse files Browse the repository at this point in the history
- Properly convert /begin.../end math blocks by wrapping in $$
- Update our regex for unescaping braces in math to support math wrapped by double dollar signs and containing newlines.
- Make sure $$ symbols are not escaped and include line breaks before and after. I encountered a few examples in our notebooks that didn't match this formatting expected by mdx.

These changes were made when adapting this script to work in the botorch repo:
- CristianLara/botorch@a0b3eaa
- CristianLara/botorch@5118c53
  • Loading branch information
CristianLara committed Dec 2, 2024
1 parent bd70da7 commit 85d32f3
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions scripts/convert_ipynb_to_mdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,20 @@ def sanitize_mdx(mdx: str) -> str:
# Remove some lingering HTML tags that break MDX.
mdx = mdx.replace("<markdowncell>", "")
mdx = mdx.replace("<TODO>", "")
mdx = mdx.replace("<br>", "<br />")
# Remove any HTML comments from the Markdown. They are fine to keep in the
# notebooks, but are not really useful in the MDX.
mdx = re.sub("(<!--.*?-->)", "", mdx, flags=re.DOTALL)
# "\" Escape braces to make the text MDX compatible.
mdx = re.sub("([^\\\\])([{}])", "\\g<1>\\\\\\g<2>", mdx)
# Escaping braces causes issues in math blocks, unescape them.
mdx = re.sub("\\$(.*?)\\$", lambda match: match[0].replace("\\{", "{").replace("\\}", "}"), mdx)

# -- KaTeX --
# Wrap '\begin{}...\end{}' in $$ for KaTeX to work.
mdx = re.sub("(\\\\begin\\\\{(\\w*?)\\\\}(.|\n)*?end\\\\{\\2\\\\})", "$$\\g<1>$$", mdx)
# # make sure $$ symbols are not escaped and include line breaks.
mdx = re.sub("\\\\?\\$\\\\?\\$((?:.|\n)*?)\\\\?\\$\\\\?\\$", "\n$$\n\\g<1>\n$$\n", mdx)
# # Escaping braces causes issues in math blocks, unescape them.
mdx = re.sub("\\$?\\$(.|\n)*?\\$\\$?", lambda match: match[0].replace("\\{", "{").replace("\\}", "}"), mdx)

return mdx

Expand Down

0 comments on commit 85d32f3

Please sign in to comment.