Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance LaTeX rendering in Markdown with support for environments #269

Merged
merged 5 commits into from
Aug 12, 2024
Merged

Enhance LaTeX rendering in Markdown with support for environments #269

merged 5 commits into from
Aug 12, 2024

Conversation

rian-dolphin
Copy link
Contributor

@rian-dolphin rian-dolphin commented Aug 12, 2024

This PR improves the KatexMarkdownJS function to provide better LaTeX rendering capabilities within Markdown content. Key enhancements include:

  1. Support for LaTeX environments (e.g., equation, align, gather, multline)
  2. Separate handling for inline and display math
  3. Customizable delimiters for inline and display math
  4. Configurable list of LaTeX environments to be treated as display math

Changes:

  • Updated KatexMarkdownJS function in fasthtml/js.py
  • Added support for processing LaTeX environments
  • Improved regex patterns for more accurate inline and display math detection
  • Made math delimiters and environment list customizable

Breaking Change:

The katex_tags parameter has been replaced with separate inline_delim and display_delim parameters. Users who previously explicitly specified katex_tags will need to update their code.

Migration guide:

  • Old: KatexMarkdownJS(katex_tags='$')
  • New: KatexMarkdownJS(inline_delim='$', display_delim='$$')

Minimum working example:

This example demonstrates some environments which failed before this PR and now render properly.

from fasthtml.common import *

hdrs = (KatexMarkdownJS(),)
app, rt = fast_app(hdrs=hdrs)


@app.get("/")
def test():
    content = """
Here is some math $x+y=3$.

Examples of environments:
\\begin{align}
x &= y + z \\\\
a &= b + c + d + e
\\end{align}
and the equation environment:
\\begin{equation}
e = mc^2
\\end{equation}
Some more math using the display style deliminators:
$$
1 \\times 2 = 2
$$
final example
$$1 \\times 2 = 2$$
"""
    return Div(content, cls="marked")

serve()

Edit: attaching a before and after screenshot of the example app above.
bef_aft

@rian-dolphin
Copy link
Contributor Author

cc: yym68686 as this is related to a previous PR

Copy link
Contributor

@jph00 jph00 left a comment

Choose a reason for hiding this comment

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

Great! Some minor requests inline. Also, can you please change the code to use named format params and passing a format dict, e.g. like

print('%(Company)s is a %(Department)s Portal.' 
          %{'Company': "GFG", 'Department': "CS"})

fasthtml/js.py Outdated Show resolved Hide resolved
fasthtml/js.py Outdated Show resolved Hide resolved
fasthtml/js.py Outdated Show resolved Hide resolved
fasthtml/js.py Outdated Show resolved Hide resolved
@rian-dolphin
Copy link
Contributor Author

@jph00 I think those are all addressed!

@jph00
Copy link
Contributor

jph00 commented Aug 12, 2024

speedy!

@jph00 jph00 merged commit 36c476f into AnswerDotAI:main Aug 12, 2024
1 check failed
@jph00 jph00 added the enhancement New feature or request label Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants