Skip to content
Open
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
27 changes: 27 additions & 0 deletions mathics/builtin/box/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,33 @@ def eval_display(boxexpr, evaluation):
return boxexpr.elements[0]


class PaneBox(BoxExpression):
Copy link
Member

@rocky rocky Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the class called PaneBox when the WMA link goes to InterpretationBox?

I can't find PaneBox mentioned in WMA docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In[1]:= MakeBoxes[OutputForm[a^2/b],StandardForm]                               

                                                    2
                                                   a
Out[1]= InterpretationBox[PaneBox[" 2\na\n--\nb"], --, Editable -> False]
                                                   b

A PaneBox is a block of multiline text, which by default is "centered".
It can be produced by applying MakeBoxes to a Pane expression:

In[2]:= MakeBoxes[Pane["hola\nHello",10],StandardForm] 
Out[2]= PaneBox["hola\nHello", ImageSize -> 10]

Copy link
Member

@rocky rocky Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok - thanks for the information. Given this, it seems this is more like a Pane than it is an InterpretationBox. I see that Information[PaneBox] gives information.

Are there any other Boxes in this category? (Like ParentBox, it exists but is not documented). I can't find any, but I feel we've encountered this kind of situation where there is something that exists but is not documented. If so, how did we handle things there?

But as for changing the link say from InterpretationBox to Pane (if that turns out to be the better thing to do), that is not urgent right now.

This PR is large and I think this PR should come after we work out some simple basics of MakeBoxes.

Also, the PR title is misleading because a lot of this is about character-based formatting on top of stuff related to MakeBoxes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably this can be reformulated in small pieces. One of them is #1316

"""
<url>
:WMA link:
https://reference.wolfram.com/language/ref/InterpretationBox.html</url>

<dl>
<dt>'PaneBox[expr]'
<dd> is a low-level box construct, used in OutputForm.
</dl>

"""

attributes = A_HOLD_ALL_COMPLETE | A_PROTECTED | A_READ_PROTECTED
summary_text = "box associated to panel"

def apply_display_form(boxexpr, form, evaluation, expression):
"""ToExpression[boxexpr_PaneBox, form_]"""
return Expression(expression.head, boxexpr.elements[0], form).evaluate(
evaluation
)

def apply_display(boxexpr, evaluation):
"""DisplayForm[boxexpr_PaneBox]"""
return boxexpr.elements[0]


class RowBox(BoxExpression):
"""
<url>
Expand Down
6 changes: 6 additions & 0 deletions mathics/builtin/forms/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
StringLParen,
StringRParen,
eval_baseform,
eval_makeboxes_outputform,
eval_mathmlform,
eval_tableform,
eval_texform,
Expand Down Expand Up @@ -490,8 +491,13 @@ class OutputForm(FormBaseClass):
= -Graphics-
"""

formats = {"OutputForm[s_String]": "s"}
summary_text = "plain-text output format"

def eval_makeboxes(self, expr, form, evaluation):
"""MakeBoxes[OutputForm[expr_], form_]"""
return eval_makeboxes_outputform(expr, evaluation, form)


class PythonForm(FormBaseClass):
"""
Expand Down
5 changes: 4 additions & 1 deletion mathics/core/atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1081,10 +1081,13 @@ def __str__(self) -> str:
return '"%s"' % self.value

def atom_to_boxes(self, f, evaluation):
return self.make_boxes(f.get_name())

def make_boxes(self, f):
from mathics.eval.makeboxes import _boxed_string

inner = str(self.value)
if f in SYSTEM_SYMBOLS_INPUT_OR_FULL_FORM:
if f in ("System`InputForm", "System`FullForm"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can instance (f, SymblInputForm, SymbolFullForm) be used instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, this method receives strings instead symbols as input. We can change this, too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I would not be surprised if there is a conversion or access function to go from an Expression symbol into a string. If so, this was one of the many ways that the original code was hideously slow.)

Copy link
Member

@rocky rocky Sep 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW I noticed recently that Expression.replace_vars is another place where strings are used instead of Symbol.

inner = '"' + inner.replace("\\", "\\\\") + '"'
return _boxed_string(inner, **{"System`ShowStringCharacters": SymbolTrue})
return String('"' + inner + '"')
Expand Down
9 changes: 8 additions & 1 deletion mathics/eval/makeboxes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
_boxed_string,
eval_generic_makeboxes,
eval_makeboxes,
eval_makeboxes_outputform,
format_element,
int_to_string_shorter_repr,
to_boxes,
Expand All @@ -18,19 +19,25 @@
eval_tableform,
eval_texform,
)
from mathics.eval.makeboxes.precedence import builtins_precedence, parenthesize
from mathics.eval.makeboxes.precedence import (
builtins_precedence,
compare_precedence,
parenthesize,
)

__all__ = [
"NumberForm_to_String",
"StringLParen",
"StringRParen",
"_boxed_string",
"builtins_precedence",
"compare_precedence",
"do_format",
"eval_baseform",
"eval_generic_makeboxes",
"eval_infix",
"eval_makeboxes",
"eval_makeboxes_outputform",
"eval_mathmlform",
"eval_postprefix",
"eval_tableform",
Expand Down
35 changes: 33 additions & 2 deletions mathics/eval/makeboxes/makeboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from mathics.core.evaluation import Evaluation
from mathics.core.expression import Expression
from mathics.core.symbols import Atom, Symbol, SymbolFullForm, SymbolMakeBoxes
from mathics.core.systemsymbols import SymbolStandardForm
from mathics.core.systemsymbols import SymbolOutputForm, SymbolStandardForm
from mathics.eval.makeboxes.formatvalues import do_format
from mathics.eval.makeboxes.precedence import parenthesize

Expand Down Expand Up @@ -126,8 +126,21 @@ def int_to_string_shorter_repr(value: int, form: Symbol, max_digits=640):
return String(value_str)


def eval_makeboxes_outputform(expr, evaluation, form):
"""
Build a 2D text representation of the expression.
Copy link
Member

@rocky rocky Sep 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If 2D is synonymous with str(...) on line 136, then something feels very wrong about putting this in mathics.eval.makeboxes. 2D is a formatting kind of thing, not a Boxing kind of thing.

Copy link
Member

@rocky rocky Sep 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this more. For high-level form handling and boxing, which largely involve Mathics3 (simple and compound) expressions, these should be done in separate directories, e.g., mathics.form and mathics.box.

"""
from mathics.builtin.box.layout import InterpretationBox, PaneBox
from mathics.format.outputform import expression_to_outputform_text

text_outputform = str(expression_to_outputform_text(expr, evaluation, form))
elem1 = PaneBox(String(text_outputform))
elem2 = Expression(SymbolOutputForm, expr)
return InterpretationBox(elem1, elem2)


def eval_fullform_makeboxes(
self, expr, evaluation: Evaluation, form=SymbolStandardForm
expr, evaluation: Evaluation, form=SymbolStandardForm
) -> Optional[BaseElement]:
"""
This function takes the definitions provided by the evaluation
Expand Down Expand Up @@ -220,9 +233,27 @@ def format_element(
Applies formats associated to the expression, and then calls Makeboxes
"""
evaluation.is_boxing = True
while element.get_head() is form:
element = element.elements[0]

if element.has_form("FullForm", 1):
return eval_fullform_makeboxes(element.elements[0], evaluation)

# In order to work like in WMA, `format_element`
# should evaluate `MakeBoxes[element//form, StandardForm]`
# Then, MakeBoxes[expr_, StandardForm], for any expr,
# should apply Format[...] rules, and then
# MakeBoxes[...] rules. These rules should be stored
# as FormatValues[...]
# As a first step in that direction, let's mimic this behaviour
# just for the case of OutputForm:
if element.has_form("OutputForm", 1):
return eval_makeboxes_outputform(element.elements[0], evaluation, form)

expr = do_format(element, evaluation, form)
if expr is None:
return None

result = Expression(SymbolMakeBoxes, expr, form)
result_box = result.evaluate(evaluation)
if isinstance(result_box, String):
Expand Down
12 changes: 12 additions & 0 deletions mathics/format/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from mathics.builtin.box.layout import (
FractionBox,
GridBox,
InterpretationBox,
PaneBox,
RowBox,
SqrtBox,
StyleBox,
Expand Down Expand Up @@ -133,6 +135,16 @@ def render(format, string, in_text=False):
add_conversion_fn(String, string)


def interpretation_panebox(self, **options):
return lookup_conversion_method(self.elements[0], "latex")(
self.elements[0], **options
)


add_conversion_fn(InterpretationBox, interpretation_panebox)
add_conversion_fn(PaneBox, interpretation_panebox)


def fractionbox(self, **options) -> str:
_options = self.box_options.copy()
_options.update(options)
Expand Down
12 changes: 12 additions & 0 deletions mathics/format/mathml.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from mathics.builtin.box.layout import (
FractionBox,
GridBox,
InterpretationBox,
PaneBox,
RowBox,
SqrtBox,
StyleBox,
Expand Down Expand Up @@ -110,6 +112,16 @@ def render(format, string):
add_conversion_fn(String, string)


def interpretation_panebox(self, **options):
return lookup_conversion_method(self.elements[0], "latex")(
self.elements[0], **options
)


add_conversion_fn(InterpretationBox, interpretation_panebox)
add_conversion_fn(PaneBox, interpretation_panebox)


def fractionbox(self, **options) -> str:
_options = self.box_options.copy()
_options.update(options)
Expand Down
Loading
Loading