markshea / app.py
markshea2022's picture
Upload app.py
97e67e7 verified
import gradio as gr
import re
def show_latex(latex_input):
# Use regex to find all occurrences of \begin{...} and \end{...} and wrap them with $$
latex_input = wrap_begin_end_with_dollars(latex_input)
# Convert inline LaTeX ($...$) to block LaTeX ($$...$$)
latex_input = re.sub(r'(?<!\$)\$(?!\$)(.*?)\$', r'$$\1$$', latex_input) # replace single $...$ with $$...$$ while keeping existing $$...$$ unchanged
# Ensure **bold** text formatting
latex_input = re.sub(r"\*\*(.*?)\*\*", r"**\1**", latex_input)
return f"{latex_input}"
def clear():
return "", ""
def wrap_begin_end_with_dollars(text):
# Use regex to find all occurrences of \begin{...} and \end{...} and wrap them with $$
text = re.sub(r'(\\begin{[^}]+})', r'$$\1', text) # Wrap \begin{}
text = re.sub(r'(\\end{[^}]+})', r'\1$$', text) # Wrap \end{}
return text
with gr.Blocks() as iface:
with gr.Row():
with gr.Column():
latex_input = gr.Textbox(label="Enter LaTeX", lines=5, placeholder="Type your LaTeX here...")
show_button = gr.Button("Show")
clear_button = gr.Button("Clear")
with gr.Column():
latex_output = gr.Markdown(label="Rendered LaTeX")
show_button.click(show_latex, inputs=latex_input, outputs=latex_output)
clear_button.click(clear, inputs=[], outputs=[latex_input, latex_output])
iface.launch(share=True)