Skip to content

Commit 3227590

Browse files
committed
feat: convert latex to plain text
Newest GPT models love to provide latex equations. Unfortunately this makes things pretty impossible to read for larger questions. Add a conversion to change latex markup to plain text. Signed-off-by: ldelossa <[email protected]>
1 parent b1985ca commit 3227590

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

chatblade/printer.py

+13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from rich.rule import Rule
1010

1111
from chatblade import utils
12+
from pylatexenc.latex2text import LatexNodes2Text
1213

1314

1415
console = Console()
@@ -85,8 +86,20 @@ def extract_messages(messages, args):
8586
else:
8687
print(message.content.strip())
8788

89+
def format_latex(msg):
90+
converter = LatexNodes2Text()
91+
msg = converter.latex_to_text(msg)
92+
93+
# do no change code blocks to smart quotes, this will break the markdown
94+
# parser.
95+
msg = msg.replace("“", "``")
96+
97+
return msg
8898

8999
def detect_and_format_message(msg, cutoff=None, theme=None):
100+
# convert any latex markup to ASCII.
101+
msg = format_latex(msg)
102+
90103
if cutoff and len(msg) > cutoff:
91104
msg = "... **text shortened** ... " + msg[-cutoff:]
92105
return msg

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ platformdirs==4.2.2
1414
pydantic==2.8.2
1515
pydantic_core==2.20.1
1616
Pygments==2.18.0
17+
pylatexenc==2.10
1718
PyYAML==6.0.1
1819
regex==2024.5.15
1920
requests==2.32.3

0 commit comments

Comments
 (0)