r""" Visitor for (table row) MathML elements. The element represents a row in a table/matrix. It is a helper element that contains (table cell) children. This visitor is primarily used by MtableVisitor to convert table rows. """ from lxml import etree as ET from ..core.interfaces import Visitor, Strategy from ..utils import local_name as _local class MtrVisitor(Visitor): r""" Handles (table row) MathML elements. Converts table rows to LaTeX format with cells separated by & and terminated with \\. Examples: Simple row: 12 → 1 & 2 \\ Row with expressions: xy → x & y \\ Single cell row: a → a \\ Empty row: → \\ """ def __init__(self, strategy: Strategy): """Initialize the Mtr visitor.""" super().__init__(strategy) def visit(self, node: ET.Element) -> str: r""" Convert element to LaTeX row. Args: node: The element to convert Returns: LaTeX string with cells separated by & and terminated with \\ Raises: ValueError: If node is not an element """ if not self.can_handle(node): raise ValueError(f"MtrVisitor cannot handle element '{node.tag}'") # Convert all children to cells cells = [] for child in node: if _local(child.tag) == "mtd": cell_latex = self._convert_cell(child) cells.append(cell_latex) # Join cells with & and add row terminator if not cells: return "\\\\" return " & ".join(cells) + " \\\\" def _convert_cell(self, node: ET.Element) -> str: """ Convert a single (table cell) element. Args: node: The element Returns: LaTeX string for cell content """ # Use MtdVisitor for proper cell conversion from .mtd_visitor import MtdVisitor return MtdVisitor(self.strategy).visit(node) def can_handle(self, node: ET.Element) -> bool: """Check if this visitor can handle the given node.""" return _local(node.tag) == 'mtr' @property def element_name(self) -> str: """Get the element name this visitor handles.""" return 'mtr' def __repr__(self) -> str: """String representation for debugging.""" return f"MtrVisitor(strategy={self.strategy.name})"