Skip to content

Quantity literals in input cells aren't formatted the way the front end normally displays them #90

Description

@derendel

As requested by review, quantities in input cells should be formatted. Below is Claude's summary and proposed fix.

Summary: Quantity literals in input cells aren't formatted the way the front end normally displays them

Symptom: In notebooks built by MarkdownToNotebook, an input cell containing a Quantity[...] literal renders as the raw constructor call [LongDash] e.g. Quantity[1, "Meters"^3/"Seconds"] [LongDash] instead of the compact typeset form a real Wolfram Front End shows the moment that line is evaluated (e.g. 1 m³/s). Output cells are unaffected [LongDash] they already render Quantity correctly via its normal TemplateBox["Quantity", ...] display, since they come from live-evaluated results and go through the kernel's ordinary formatting.

Root cause: inputBoxes[code_String] (MarkdownToNotebook.wl, ~line 1387) builds input-cell boxes primarily via FrontEnd\ReparseBoxStructurePacket[trimmed], which is a literal syntax reparse [LongDash] it converts typed characters into 2D box structure (fractions, superscripts, etc.) without ever invoking MakeBoxes/Format on the parsed expression. That's exactly the step a live Front End performs automatically after evaluating a line containing a "boxed" input type (Quantity, and the same would presumably apply to DateObject, Entity, etc.), but the reparse path here never does it, so those literals stay as raw function-call syntax. Confirmed this isn't Quantity`-specific handling gone wrong [LongDash] MTN currently has no special-casing for any pretty-printed input type.

Proposed fix: after computing boxes from the reparse, walk it and replace each Quantity[...] sub-box with its real typeset form [LongDash] safe because Quantity[value, unit] is a pure constructor with no side effects:

wolfram
quantityBoxReplace[boxes_] := boxes //. RowBox[{"Quantity", "[", args__, "]"}] :>
Module[{expr},
expr = Quiet @ ReleaseHold[MakeExpression[RowBox[{"Quantity", "[", args, "]"}], StandardForm]];
If[Head[expr] === Quantity, ToBoxes[expr, StandardForm], RowBox[{"Quantity", "[", args, "]"}]]
];

Applied to the existing boxes right after the reparse call (and after reactivateStringLinearSyntax @ fixBlankHead[boxes]) in inputBoxes, wherever that pipeline succeeds.

Verified before/after (rendered from the same source line, SedimentationBasinDesign[Quantity[1, "Meters"^3/"Seconds"], <|"TargetSurfaceOverflowRate" -> Quantity[0.015, "Meters"/"Seconds"], ...|>]):

  • Before: SedimentationBasinDesign[Quantity[1, "Meters"^3/"Seconds"], <|"TargetSurfaceOverflowRate" -> Quantity[0.015, "Meters"/"Seconds"], "ParticleDiameter" -> Quantity[0.0001, "Meters"], ...
  • After: SedimentationBasinDesign[1 m³/s, <|"TargetSurfaceOverflowRate" -> 0.015 m/s, "ParticleDiameter" -> 0.0001 m, ...

Everything else in the line (function head, association braces, string keys) is untouched [LongDash] only the Quantity[...] sub-expressions get replaced.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions