latex_converter

LaTeX to Discord Formatting Converter.

Converts LaTeX/math formatting to Discord-friendly text with Unicode symbols.

Strategy: - Preserve fenced and inline code as-is. - Convert display math to plain text (no code fences). - Convert inline math to inline code with lightweight symbol mapping. - Replace common LaTeX commands with Unicode equivalents. - Handle nested \frac{}{}, \sqrt{}, superscript/subscript to Unicode. - Support matrix environments and special math fonts.

latex_converter.convert_latex_to_discord(text)[source]

Convert common LaTeX/math formatting to Discord-friendly text.

The module’s sole public entry point. Preserves fenced code blocks and inline code spans untouched; everything else is scanned for LaTeX delimiters and rewritten into Unicode symbols so the bot’s replies render cleanly in Discord, which has no native math support. Cheaply bails out when the text contains no LaTeX delimiters at all, and on any conversion error returns the original text unchanged (logging a warning) so a malformed expression can never blank out a reply. Delegates the real work to _do_convert().

Called by postprocess_response in response_postprocessor.py as one stage of the outbound message-formatting pipeline (after raw-LaTeX wrapping, before table conversion).

Parameters:

text (str) – The candidate reply text, possibly containing LaTeX math and formatting commands.

Returns:

The converted text, or the input unchanged when it is empty, not a string, contains no LaTeX, or conversion raised.

Return type:

str