Skip to content

FEAT: add LaTeX rendering support for Mermaid flowcharts - #353

Draft
Flomber wants to merge 6 commits into
mainfrom
mermaid_latex
Draft

FEAT: add LaTeX rendering support for Mermaid flowcharts#353
Flomber wants to merge 6 commits into
mainfrom
mermaid_latex

Conversation

@Flomber

@Flomber Flomber commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Closes #349

@Flomber Flomber self-assigned this Aug 24, 2026
@Flomber Flomber added the ⚙️ Enhancement Improvements and optimizations of existing features label Aug 24, 2026
@ComPWA ComPWA deleted a comment from review-notebook-app Bot Aug 24, 2026
Base automatically changed from mermaid_latex_label to main August 24, 2026 13:34
Comment thread docs/usage/visualize.ipynb Outdated
Comment thread src/qrules/io/_labels.py
Comment on lines +25 to +36
_LATEX_TEXT_ESCAPES = str.maketrans({
"\\": R"\textbackslash{}",
"{": R"\{",
"}": R"\}",
"$": R"\$",
"&": R"\&",
"#": R"\#",
"_": R"\_",
"%": R"\%",
"~": R"\textasciitilde{}",
"^": R"\textasciicircum{}",
})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Something feels fishy to me here. Why does the rendered not directly generate the correct string? Generating a syntax string and then doing translations on it is something that should be avoided because (1) it's a maintenance load (you may have to keep extending this list) and (2) what if parts of the string should not be replaced?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is a bit complicated...

With the new formatting class, only plain text, that would be inserted into "\text{}" is escaped due to special symbols in the names would be missinterpreted.
Already set latex code in e.g. "\frac{...}{...}" will not be escaped. ⛔ 🚪

Regarding maintenance: This escape dictionary represents a fixed LaTeX text-escaping tabel, that (hopefully) does not need maintenance... 🙃

Comment thread src/qrules/io/_labels.py Outdated
Comment on lines +183 to +187
def __render_latex_key_and_value(key: str, value: Any) -> str:
if isinstance(value, (Fraction, int)):
no_pm = key.endswith("magnitude") or key == "pid"
return __render_latex_fraction(Fraction(value), plusminus=not no_pm)
return as_latex(value)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

All these functions here are near copies of their as_string() equivalents. We have to identify some generic procedures that are taking place in each of these functions (such as "render for each line, then merge the lines and wrap them in some block"), so that their core remains the same and then only the (in this case) per-line renderer is changed when selecting a different output format.

That may also solve the problem with those translation tables in the top.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Most of the copying is removed/minimalized due to introducing a formatter class that handles some repetitive definitions.
Other definitions minimalistic clones due to using the formatters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚙️ Enhancement Improvements and optimizations of existing features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LaTeX in Mermaid labels

2 participants