Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion latex2mathml/walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ def _walk(tokens: Iterator[str], terminator: Optional[str] = None, limit: int =

if token == previous.token == commands.SUBSCRIPT:
raise DoubleSubscriptsError
if token == previous.token == commands.SUPERSCRIPT:
if (token == previous.token == commands.SUPERSCRIPT) and (
previous.children is not None
and len(previous.children) >= 2
and previous.children[1].token != commands.PRIME
):
raise DoubleSuperscriptsError

modifier = None
Expand All @@ -92,6 +96,22 @@ def _walk(tokens: Iterator[str], terminator: Optional[str] = None, limit: int =
):
children = tuple(_walk(tokens, terminator=terminator, limit=1))
node = Node(token=commands.SUBSUP, children=(*previous.children, *children), modifier=previous.modifier)
elif (
token == commands.SUPERSCRIPT
and previous.token == commands.SUPERSCRIPT
and previous.children is not None
and previous.children[1].token == commands.PRIME
):
children = tuple(_walk(tokens, terminator=terminator, limit=1))

node = Node(
token=commands.SUPERSCRIPT,
children=(
previous.children[0],
Node(token=commands.BRACES, children=(previous.children[1], *children)),
),
modifier=previous.modifier,
)
else:
try:
children = tuple(_walk(tokens, terminator=terminator, limit=1))
Expand All @@ -106,13 +126,27 @@ def _walk(tokens: Iterator[str], terminator: Optional[str] = None, limit: int =
except IndexError:
previous = Node(token="") # left operand can be empty if not present

if (
previous.token == commands.SUPERSCRIPT
and previous.children is not None
and len(previous.children) >= 2
and previous.children[1].token != commands.PRIME
):
raise DoubleSuperscriptsError

if (
previous.token == commands.SUPERSCRIPT
and previous.children is not None
and len(previous.children) >= 2
and previous.children[1].token == commands.PRIME
):
node = Node(token=commands.SUPERSCRIPT, children=(previous.children[0], Node(token=commands.DPRIME)))
elif previous.token == commands.SUBSCRIPT and previous.children is not None:
node = Node(
token=commands.SUBSUP,
children=(*previous.children, Node(token=commands.PRIME)),
modifier=previous.modifier,
)
else:
node = Node(token=commands.SUPERSCRIPT, children=(previous, Node(token=commands.PRIME)))
elif token in commands.COMMANDS_WITH_TWO_PARAMETERS:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "latex2mathml"
version = "3.75.4"
version = "3.75.5"
repository = "https://github.com/roniemartinez/latex2mathml"
description = "Pure Python library for LaTeX to MathML conversion"
authors = ["Ronie Martinez <[email protected]>"]
Expand Down
27 changes: 27 additions & 0 deletions tests/test_walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1697,6 +1697,32 @@
],
id="not",
),
pytest.param(
"x_1'",
[
Node(
token="_^",
children=(Node(token="x"), Node(token="1"), Node(token="\\prime")),
),
],
id="issue-392-subscript",
),
pytest.param(
"x'^2",
[
Node(
token="^",
children=(
Node(token="x"),
Node(
token="{}",
children=(Node(token="\\prime"), Node(token="2")),
),
),
)
],
id="issue-392-superscript",
),
],
)
def test_walk(latex: str, expected: list) -> None:
Expand All @@ -1722,6 +1748,7 @@ def test_walk(latex: str, expected: list) -> None:
pytest.param(r"\skew{X}\hat b", InvalidWidthError, id="invalid-width-not-number"),
pytest.param(r"\limits^{\pi}", LimitsMustFollowMathOperatorError, id="limits-must-follow-math-operator-blank"),
pytest.param(r"5\limits^{\pi}", LimitsMustFollowMathOperatorError, id="limits-must-follow-math-operator"),
pytest.param(r"x^2'", DoubleSuperscriptsError, id="issue-392"),
],
)
def test_error(latex: str, exception: Union[Tuple[Any, ...], Any]) -> None:
Expand Down