Skip to content

Support \boxed{} in TeX input and convert it to MathML #294

Description

@ClimberBear

Description

texmath currently fails to parse the standard LaTeX/amsmath command \boxed{...}.

I would like to request support for \boxed, particularly for the MathML output writer.

\boxed{...} is a standard mathematical construction provided by amsmath. Its purpose is to draw a rectangular box around its argument. For MathML output, the natural representation appears to be Presentation MathML's menclose element with notation="box".

This is particularly useful when converting mathematical documents containing matrices or submatrices where a rectangular block of entries is highlighted.

Version

I am currently using:

texmath 0.13.2

The problem is encountered through:

Pandoc 3.10.1
pandoc ... -t epub3 --mathml

However, the problem is reproducible with the standalone texmath executable, so it appears to be a texmath parsing/conversion issue rather than a Pandoc-specific issue.

Minimal reproducible example

Input:

\boxed{x}

Command:

echo '\boxed{x}' | texmath -f tex -t mathml

Current behavior:

texmath fails to parse the expression, with an error of the form:

[WARNING] Could not convert TeX math \boxed{x}, rendering as TeX:
  \boxed{x}
         ^
  unexpected "{"
  expecting "%", "\\label", "\\tag", "\\nonumber", whitespace or "\\allowbreak"

Expected behavior

I would expect \boxed{x} to be accepted and converted to Presentation MathML representing a box around x.

A natural representation would be:

<math display="block" xmlns="http://www.w3.org/1998/Math/MathML">
  <menclose notation="box">
    <mi>x</mi>
  </menclose>
</math>

I am not suggesting that this exact serialization is required; the important point is that the semantic/visual effect of \boxed{...} should be represented by an appropriate MathML construct.

Matrix example

A more representative example from an actual mathematical document is:

\boxed{
  \begin{matrix}
    2 & 1 \\
    4 & 1
  \end{matrix}
}

The expected MathML could be structurally equivalent to:

<menclose notation="box">
  <mtable>
    <mtr>
      <mtd><mn>2</mn></mtd>
      <mtd><mn>1</mn></mtd>
    </mtr>
    <mtr>
      <mtd><mn>4</mn></mtd>
      <mtd><mn>1</mn></mtd>
    </mtr>
  </mtable>
</menclose>

Again, the exact MathML serialization is left to the implementation. The important requirement is that the 2 x 2 matrix remains a single boxed mathematical object.

Real-world example

The command is useful in a larger matrix such as:

B'_2 =
\begin{pmatrix}
  1 &
  \boxed{
    \begin{matrix}
      2 & 1 \\
      4 & 1
    \end{matrix}
  }
  & 0 & -1 \\
  2 & & 2 & 3 \\
  3 & 6 & 1 & 4 & 7
\end{pmatrix}

The original source document uses a rectangular box to highlight the four entries

         2  1
         4  1

as a submatrix.

This is not merely decorative in the source document: the box visually identifies a particular 2 x 2 block of the matrix.

Why MathML menclose seems appropriate

Presentation MathML provides the menclose element specifically for enclosing mathematical content with visual notation.

For a simple rectangular box:

<menclose notation="box">...</menclose>

appears to be the direct MathML equivalent of:

\boxed{...}

This would also avoid requiring the TeX reader to emulate the box using lower-level positioning or spacing constructs.

In particular, I would prefer a representation based on the structure of the mathematical object rather than a workaround involving \vphantom, \smash, \raisebox, etc.

Interaction with matrices

It would be useful if \boxed could accept arbitrary math content, including:

  • simple expressions;
  • fractions;
  • matrices;
  • array;
  • pmatrix;
  • nested mathematical structures.

For example:

\boxed{\frac{a}{b}}

and:

\boxed{
  \begin{pmatrix}
    a & b \\
    c & d
  \end{pmatrix}
}

should ideally both be accepted.

The box should enclose the complete resulting mathematical object, rather than treating the contents as ordinary sequential tokens.

LaTeX semantics

In standard LaTeX/amsmath, \boxed is essentially a mathematical boxing operation around its argument.

I am not asking for arbitrary TeX box-layout support. The requested feature is specifically the standard mathematical construct:

\boxed{<math expression>}

with a suitable representation in the target math formats.

For MathML, menclose notation="box" seems to provide the appropriate abstraction.

Pandoc use case

The immediate use case is EPUB3 generation with MathML:

pandoc prueba.md -t epub3 --mathml -o prueba.epub

Pandoc delegates TeX math conversion to texmath.

Without \boxed support, a source expression such as:

\boxed{
  \begin{matrix}
    2 & 1 \\
    4 & 1
  \end{matrix}
}

causes Pandoc to emit:

[WARNING] Could not convert TeX math ...

and the equation cannot be converted to MathML.

As a workaround, I have had to rewrite the source using more complicated combinations of array, matrix, and other constructs. Those workarounds are significantly less maintainable and are specific to the limitations of the TeX parser rather than the mathematical content itself.

Supporting \boxed would therefore allow the original LaTeX to be preserved while producing structurally appropriate MathML.

Possible implementation

I have not inspected the texmath internals deeply enough to propose a concrete patch, so I would defer to the maintainers regarding the appropriate implementation.

Conceptually, however, I would expect something along these lines:

  1. Add \boxed to the TeX reader's supported commands/macros.

  2. Parse one required math argument.

  3. Represent the box in the internal math representation in a way that preserves the fact that the argument is enclosed.

  4. Add the corresponding MathML writer behavior:

    boxed-expression
        ->
    <menclose notation="box">...</menclose>
    
  5. Ensure that the existing writers either:

    • support the construct appropriately, or
    • have a defined fallback if the target format has no direct equivalent.

The important point is that the parser should treat \boxed{...} as a single mathematical construct rather than interpreting boxed as ordinary input after an unknown control sequence.

Test cases

At minimum, I think the following should be covered by regression tests:

1. Simple expression

\boxed{x}

2. Fraction

\boxed{\frac{a}{b}}

3. Matrix

\boxed{
  \begin{matrix}
    2 & 1 \\
    4 & 1
  \end{matrix}
}

4. Nested expression

\boxed{\left(\frac{a+b}{c+d}\right)}

5. Larger real-world matrix

B'_2 =
\begin{pmatrix}
  1 &
  \boxed{
    \begin{matrix}
      2 & 1 \\
      4 & 1
    \end{matrix}
  }
  & 0 & -1 \\
  2 & & 2 & 3 \\
  3 & 6 & 1 & 4 & 7
\end{pmatrix}

For the MathML writer, the tests should verify that the boxed content is represented by an enclosing construct such as:

<menclose notation="box">...</menclose>

rather than simply dropping the box or treating the contents as ordinary math.

Related work

There is precedent in texmath for adding support for individual LaTeX/AMS commands where the existing parser does not recognize them. For example, support for commands such as \mathds and \hdots was added following issue #126.

The present request is similar: \boxed is a well-established mathematical LaTeX construct, and there is a reasonably direct representation in MathML.

Summary

Please consider adding support for:

\boxed{<math expression>}

with MathML output along the lines of:

<menclose notation="box">
  ...
</menclose>

This would allow standard LaTeX mathematical source to be converted to MathML without requiring document authors to replace \boxed with parser-specific workarounds.

AI Notice

I have prepared this feature request helped by AI (ChatGPT 5.5), but I can confirm that I have executed all the relevant code in my laptop, using a Macbook M4 and pandoc.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions