Skip to content

Commit

Permalink
🐛 Bug: 1. Fix the bug where multi-line mathematical formulas cannot b…
Browse files Browse the repository at this point in the history
…e rendered.

2. Fix the bug where the square root cannot be rendered.
  • Loading branch information
yym68686 committed Nov 11, 2024
1 parent ea09f1b commit 69eb690
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name="md2tgmd",
version="0.3.6",
version="0.3.7",
description="md2tgmd is a Markdown to Telegram-specific-markdown converter.",
long_description=Path("README.md").open(encoding="utf-8").read(),
long_description_content_type="text/markdown",
Expand Down
62 changes: 62 additions & 0 deletions src/latex2unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,10 @@ def __init__(self):
"\\frac": self.make_fraction
}

self.unary_with_option_commands = {
"\\sqrt": self.make_sqrt
}

# 其他样式映射 (如 bb, bf, cal, frak, it, tt) 也需要定义

# self.subscripts = {
Expand Down Expand Up @@ -590,6 +594,27 @@ def make_fraction(self, numerator, denominator):

return f"{self.maybe_parenthesize(n)}/{self.maybe_parenthesize(d)}"

def make_sqrt(self, index, radicand):
"""处理\sqrt命令,支持可选的根次参数"""
if not radicand:
return ""

# 根据根次选择合适的根号符号
radix = {
"": "√", # 默认为平方根
"2": "√",
"3": "∛",
"4": "∜"
}.get(index.strip(), None)

if radix is None:
# 对于其他根次,使用上标
sup = self.try_make_superscript(index)
radix = f"{sup}√" if sup else f"({index})√"

# 为被开方数添加上划线
return radix + self.translate_combining("\\overline", radicand)

def try_make_subscript(self, s):
if not s:
return ""
Expand Down Expand Up @@ -663,6 +688,14 @@ def handle_command(self, command, latex, index):
param1, index = self.parse_block(latex, index)
param2, index = self.parse_block(latex, index)
return self.binary_commands[command](param1, param2), index
elif command in self.unary_with_option_commands:
# 检查是否有可选参数
if index < len(latex) and latex[index] == '[':
option, index = self.parse_option(latex, index)
else:
option = ""
param, index = self.parse_block(latex, index)
return self.unary_with_option_commands[command](option, param), index
elif command in ["\\left", "\\right"]:
# 忽略 \left 和 \right 命令
return "", index
Expand Down Expand Up @@ -709,6 +742,23 @@ def parse_spaces(self, latex, start):
return '\n\n', end
return ' ', end

# 添加parse_option方法来解析方括号中的可选参数
def parse_option(self, latex, start):
"""解析[...]形式的可选参数"""
if latex[start] != '[':
return "", start

level = 1
end = start + 1
while end < len(latex) and level > 0:
if latex[end] == '[':
level += 1
elif latex[end] == ']':
level -= 1
end += 1

return self.parse(latex[start+1:end-1]), end

def convert(self, latex):
try:
return self.parse(latex)
Expand Down Expand Up @@ -757,6 +807,16 @@ def convert(self, latex):
# 因此,这辆车的加速度约为 7.22 m/s²。

# 因此,欧拉函数 \( \varphi(35) \) 的值是 24。

# 要计算 \( z \cdot \overline{z} \),我们需要先找到 \( z \) 的共轭。

# 给定 \( z = \sqrt{2}i \),所以 \( \overline{z} = -\sqrt{2}i \)。

# 然后,计算 \( z \cdot \overline{z} \):

# \[ z \cdot \overline{z} = (\sqrt{2}i) \cdot (-\sqrt{2}i) = -(\sqrt{2})^2 \cdot i^2 = -2 \cdot (-1) = 2 \]

# 所以答案是 2。选项中没有正确答案,可能有误。
if __name__ == "__main__":
latex2unicode = LaTeX2Unicode()
result = latex2unicode.convert("\\varphi(35) = 35 \\left(1 - \\frac{1}{5}\\right) \\left(1 - \\frac{1}{7}\\right)")
Expand Down Expand Up @@ -789,3 +849,5 @@ def convert(self, latex):
print(result) # 预期输出: α + β = γ
result = latex2unicode.convert(r"a = \frac{27.8 \, \text{m/s} - 0 \, \text{m/s}}{3.85 \, \text{s}}")
print(result) # 预期输出: α + β = γ
result = latex2unicode.convert(r"z \cdot \overline{z} = (\sqrt{2}i) \cdot (-\sqrt{2}i) = -(\sqrt{2})^2 \cdot i^2 = -2 \cdot (-1) = 2")
print(result) # 预期输出: α + β = γ
9 changes: 7 additions & 2 deletions src/md2tgmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,25 @@ def find_lines_with_char(s, char, min_count):
return "\n".join(lines)

def latex2unicode(text):
text = text.strip()
blockmath = False
if text.startswith("\\["):
blockmath = True
text = re.sub(r"\\\[", "", text)
text = re.sub(r"\\\]", "", text)
text = re.sub(r"\\\(", "", text)
text = re.sub(r"\\\)", "", text)
result = l2u.convert(text)
# result = l2u.convert(rf"{text}")
if blockmath:
result = "\n\n" + result.strip() + "\n\n"
return result

def escape(text, flag=0, italic=True):
# In all other places characters
# _ * [ ] ( ) ~ ` > # + - = | { } . !
# must be escaped with the preceding character '\'.
text = replace_all(text, r"(\\\(.*?\\\))", latex2unicode)
text = replace_all(text, r"(\\\[.*?\\\])", latex2unicode)
text = replace_all(text, r"(\n*\s*\\\[[\D\d\s]+?\\\]\n*)", latex2unicode)
text = re.sub(r"\\\[", '@->@', text)
text = re.sub(r"\\\]", '@<-@', text)
text = re.sub(r"\\\(", '@-->@', text)
Expand Down

0 comments on commit 69eb690

Please sign in to comment.