Skip to content
Draft
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
2 changes: 1 addition & 1 deletion mathics/builtin/box/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CompiledCodeBox(BoxExpression):

def init(self, *args, **kwargs):
self._elements = args
self.box_options = kwargs
self.box_attributes = kwargs

def boxes_to_text(self, elements=None, **options):
if elements is None:
Expand Down
72 changes: 42 additions & 30 deletions mathics/builtin/box/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class FormBox(BoxExpression):
summary_text = "wrap boxes with an association to a particular form"

def init(self, *elems, **kwargs):
self.box_options = kwargs
self.box_attributes = kwargs
self.form = elems[1]
self.boxes = elems[0]
assert isinstance(self.boxes, BoxElementMixin), f"{type(self.boxes)}"
Expand All @@ -136,7 +136,7 @@ def elements(self):
self.boxes,
self.form,
),
self.box_options,
self.box_attributes,
)
return self._elements

Expand Down Expand Up @@ -178,7 +178,7 @@ def elements(self):
self.num,
self.den,
),
self.box_options,
self.box_attributes,
)
return self._elements

Expand All @@ -193,7 +193,7 @@ def eval(self, num, den, evaluation: Evaluation, options: dict):
def init(self, num, den, **options):
self.num = num
self.den = den
self.box_options = options
self.box_attributes = options


class GridBox(BoxExpression):
Expand Down Expand Up @@ -228,19 +228,21 @@ class GridBox(BoxExpression):
@property
def elements(self):
if self._elements is None:
self._elements = elements_to_expressions(self, self.items, self.box_options)
self._elements = elements_to_expressions(
self, self.items, self.box_attributes
)
return self._elements

def init(self, *elems, **kwargs):
self.box_options = kwargs
self.box_attributes = kwargs
self.items = elems
self._elements = elems

def get_array(self, elements, evaluation):
if not elements:
raise BoxConstructError

options = self.box_options
options = self.box_attributes

expr = elements[0]
if not expr.has_form("List", None):
Expand Down Expand Up @@ -293,13 +295,13 @@ class InterpretationBox(BoxExpression):

def __repr__(self):
result = "InterpretationBox\n " + repr(self.boxes)
result += f"\n {self.box_options}"
result += f"\n {self.box_attributes}"
return result

def init(self, *expr, **options):
self.boxes = expr[0]
self.expr = expr[1]
self.box_options = options
self.box_attributes = options

@property
def elements(self):
Expand All @@ -310,7 +312,7 @@ def elements(self):
self.boxes,
self.expr,
),
self.box_options,
self.box_attributes,
)
return self._elements

Expand Down Expand Up @@ -365,13 +367,13 @@ class PaneBox(BoxExpression):
def elements(self):
if self._elements is None:
self._elements = elements_to_expressions(
self, (self.boxes,), self.box_options
self, (self.boxes,), self.box_attributes
)
return self._elements

def init(self, expr, **options):
self.boxes = expr
self.box_options = options
self.box_attributes = options

def eval_panebox1(self, expr, evaluation, options):
"PaneBox[expr_String, OptionsPattern[]]"
Expand Down Expand Up @@ -409,6 +411,16 @@ class RowBox(BoxExpression):
def __repr__(self):
return f"RowBox[{self.elements[0].__repr__()}]"

def __init__(self, *args, **kwargs):
# indent level nesting level used in rendering MathML boxes
self.indent_level: int = 0

# Consider adding.
# self.width: int = 0

# Pass all arguments to the parent class.
super().__init__(*args, **kwargs)

@property
def elements(self):
if self._elements is None:
Expand All @@ -433,7 +445,7 @@ def eval_list(self, boxes, evaluation):

def init(self, *items, **kwargs):
# TODO: check that each element is an string or a BoxElementMixin
self.box_options = {}
self.box_attributes = {}
if len(items) == 0:
self.items = tuple()
return
Expand Down Expand Up @@ -509,13 +521,13 @@ def elements(self):
if self._elements is None:
index = self.index
if index is None:
# self.box_options
# self.box_attributes
self._elements = elements_to_expressions(
self, (self.radicand,), self.box_options
self, (self.radicand,), self.box_attributes
)
else:
self._elements = elements_to_expressions(
self, (self.radicand, index), self.box_options
self, (self.radicand, index), self.box_attributes
)
return self._elements

Expand All @@ -535,7 +547,7 @@ def eval(self, radicand, evaluation: Evaluation, options: dict):
def init(self, radicand, index=None, **options):
self.radicand = radicand
self.index = index
self.box_options = options
self.box_attributes = options


class StyleBox(BoxExpression):
Expand Down Expand Up @@ -567,11 +579,11 @@ def elements(self):
boxes = self.boxes
if style:
self._elements = elements_to_expressions(
self, (boxes, style), self.box_options
self, (boxes, style), self.box_attributes
)
else:
self._elements = elements_to_expressions(
self, (boxes,), self.box_options
self, (boxes,), self.box_attributes
)
return self._elements

Expand All @@ -596,10 +608,10 @@ def get_string_value(self) -> str:
def init(self, boxes, style=None, **options):
# This implementation supersedes Expression.process_style_box
if isinstance(boxes, StyleBox):
options.update(boxes.box_options)
options.update(boxes.box_attributes)
boxes = boxes.boxes
self.style = style
self.box_options = options
self.box_attributes = options
assert options is not None
self.boxes = boxes
assert isinstance(
Expand Down Expand Up @@ -635,7 +647,7 @@ class SubscriptBox(BoxExpression):
def elements(self):
if self._elements is None:
self._elements = elements_to_expressions(
self, (self.base, self.subindex), self.box_options
self, (self.base, self.subindex), self.box_attributes
)
return self._elements

Expand All @@ -648,7 +660,7 @@ def eval(self, a, b, evaluation: Evaluation, options: dict):
return SubscriptBox(a_box, b_box, **options)

def init(self, a, b, **options):
self.box_options = options.copy()
self.box_attributes = options.copy()
if not (isinstance(a, BoxElementMixin) and isinstance(b, BoxElementMixin)):
raise Exception((a, b), "are not boxes")
self.base = a
Expand All @@ -674,15 +686,15 @@ class SubsuperscriptBox(BoxExpression):
@property
def elements(self):
if self._elements is None:
# self.box_options
# self.box_attributes
self._elements = elements_to_expressions(
(
self,
self.base,
self.subindex,
self.superindex,
),
self.box_options,
self.box_attributes,
)
return self._elements

Expand All @@ -696,7 +708,7 @@ def eval(self, a, b, c, evaluation: Evaluation, options: dict):
return SubsuperscriptBox(a_box, b_box, c_box, **options)

def init(self, a, b, c, **options):
self.box_options = options.copy()
self.box_attributes = options.copy()
if not all(isinstance(x, BoxElementMixin) for x in (a, b, c)):
raise Exception((a, b, c), "are not boxes")
self.base = a
Expand Down Expand Up @@ -729,7 +741,7 @@ def elements(self):
self.base,
self.superindex,
),
self.box_options,
self.box_attributes,
)
return self._elements

Expand All @@ -742,7 +754,7 @@ def eval(self, a, b, evaluation: Evaluation, options: dict):
return SuperscriptBox(a_box, b_box, **options)

def init(self, a, b, **options):
self.box_options = options.copy()
self.box_attributes = options.copy()
if not all(isinstance(x, BoxElementMixin) for x in (a, b)):
raise Exception((a, b), "are not boxes")
self.base = a
Expand All @@ -766,7 +778,7 @@ class TagBox(BoxExpression):
summary_text = "box tag with a head"

def init(self, *elems, **kwargs):
self.box_options = kwargs
self.box_attributes = kwargs
self.form = elems[1]
self.boxes = elems[0]
assert isinstance(self.boxes, BoxElementMixin), f"{type(self.boxes)}"
Expand All @@ -780,7 +792,7 @@ def elements(self):
self.boxes,
self.form,
),
self.box_options,
self.box_attributes,
)
return self._elements

Expand Down
2 changes: 1 addition & 1 deletion mathics/format/box/outputforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def eval_mathmlform(expr: BaseElement, evaluation: Evaluation) -> BoxElementMixi

boxes = format_element(expr, evaluation, SymbolTraditionalForm)
try:
mathml = boxes.boxes_to_mathml(evaluation=evaluation, _indent_level=1)
mathml = boxes.boxes_to_mathml(evaluation=evaluation, indent_level=1)
except BoxError:
evaluation.message(
"General",
Expand Down
30 changes: 15 additions & 15 deletions mathics/format/render/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def interpretation_box(self, **options):

def pane_box(self, **options):
content = lookup_conversion_method(self.boxes, "latex")(self.boxes, **options)
options = self.box_options
options = self.box_attributes
size = options.get("System`ImageSize", SymbolAutomatic).to_python()

if size == "System`Automatic":
Expand Down Expand Up @@ -211,7 +211,7 @@ def pane_box(self, **options):


def fractionbox(self, **options) -> str:
_options = self.box_options.copy()
_options = self.box_attributes.copy()
_options.update(options)
options = _options
return "\\frac{%s}{%s}" % (
Expand All @@ -223,17 +223,17 @@ def fractionbox(self, **options) -> str:
add_conversion_fn(FractionBox, fractionbox)


def gridbox(self, elements=None, **box_options) -> str:
def gridbox(self, elements=None, **box_attributes) -> str:
def boxes_to_tex(box, **options):
return lookup_conversion_method(box, "latex")(box, **options)

if not elements:
elements = self._elements
evaluation = box_options.get("evaluation")
evaluation = box_attributes.get("evaluation")
items, options = self.get_array(elements, evaluation)
box_options.update(options)
box_options["inside_list"] = True
column_alignments = box_options["System`ColumnAlignments"].get_name()
box_attributes.update(options)
box_attributes["inside_list"] = True
column_alignments = box_attributes["System`ColumnAlignments"].get_name()
try:
column_alignments = {
"System`Center": "c",
Expand All @@ -251,12 +251,12 @@ def boxes_to_tex(box, **options):
result = r"\begin{array}{%s} " % (column_alignments * column_count)
for index, row in enumerate(items):
if isinstance(row, tuple):
result += " & ".join(boxes_to_tex(item, **box_options) for item in row)
result += " & ".join(boxes_to_tex(item, **box_attributes) for item in row)
else:
result += r"\multicolumn{%s}{%s}{%s}" % (
str(column_count),
column_alignments,
boxes_to_tex(row, **box_options),
boxes_to_tex(row, **box_attributes),
)
if index != len(items) - 1:
result += "\\\\ "
Expand All @@ -268,7 +268,7 @@ def boxes_to_tex(box, **options):


def sqrtbox(self, **options):
_options = self.box_options.copy()
_options = self.box_attributes.copy()
_options.update(options)
options = _options
if self.index:
Expand All @@ -285,7 +285,7 @@ def sqrtbox(self, **options):


def superscriptbox(self, **options):
_options = self.box_options.copy()
_options = self.box_attributes.copy()
_options.update(options)
options = _options
base_to_tex = lookup_conversion_method(self.base, "latex")
Expand Down Expand Up @@ -315,7 +315,7 @@ def superscriptbox(self, **options):


def subscriptbox(self, **options):
_options = self.box_options.copy()
_options = self.box_attributes.copy()
_options.update(options)
options = _options
base_to_tex = lookup_conversion_method(self.base, "latex")
Expand All @@ -330,7 +330,7 @@ def subscriptbox(self, **options):


def subsuperscriptbox(self, **options):
_options = self.box_options.copy()
_options = self.box_attributes.copy()
_options.update(options)
options = _options
base_to_tex = lookup_conversion_method(self.base, "latex")
Expand All @@ -348,7 +348,7 @@ def subsuperscriptbox(self, **options):


def rowbox(self, **options) -> str:
_options = self.box_options.copy()
_options = self.box_attributes.copy()
_options.update(options)
options = _options
parts_str = [
Expand Down Expand Up @@ -382,7 +382,7 @@ def rowbox(self, **options) -> str:


def stylebox(self, **options) -> str:
_options = self.box_options.copy()
_options = self.box_attributes.copy()
_options.update(options)
options = _options
return lookup_conversion_method(self.boxes, "latex")(self.boxes, **options)
Expand Down
Loading