texmath icon indicating copy to clipboard operation
texmath copied to clipboard

Support Cancellation of Terms in MathML

Open NV-Codes opened this issue 11 months ago • 1 comments

Description

It would be very useful for Pandoc to be able to convert the \cancel, \bcancel, and \xcancel LaTeX commands to MathML as described below (from https://github.com/NSoiffer/MathCAT/issues/341):

The way I would suggest is to use menclose . It has the attribute notation with many options. For cancellation, one or both values updiagonalstrike downdiagonalstrike would make sense.

<math>
   <menclose notation='downdiagonalstrike'>  <mfrac><mn>3</mn><mn>2</mn></mfrac> </menclose>
</math>

It currently seems that, when typing math in Pandoc's Markdown, base LaTeX and amsmath commands are able to be converted to MathML. The three commands listed above are part of the cancel package.

NV-Codes avatar Feb 01 '25 01:02 NV-Codes

One problem with <menclose> is that it's not part of MathML Core and so has poor support in browsers (only Firefox supports it). However, there have been discussions about adding it to MathML Core: https://github.com/w3c/mathml-core/issues/245 (and I personally would definitely be in favor of that).

tmke8 avatar Mar 02 '25 11:03 tmke8

Hi, I've been looking through the code for #269. It looks like the MathML reader and writer already supportmenclose for \boxed, so I think it could be added for \cancel, \bcancel, and xcancel. I'm not sure what's the best way to go about this, but I'm thinking we could add a new type ECancel, which takes the expression being cancelled and two booleans - one for \cancel and another for \bcancel. If both bools are true, then we have \xcancel.

https://github.com/jgm/texmath/blob/9d02b5a5936d91a739d95a260feac29818533127/src/Text/TeXMath/Writers/MathML.hs#L239-L240

SeanESCA avatar Aug 09 '25 07:08 SeanESCA

Yes, one could add a new constructor ECancel. (Rather than booleans, it might be better to add a custom data type to customize.)

However, this is a big change: one would need to support the new constructor in every input and output format.

jgm avatar Aug 11 '25 17:08 jgm

If someone could describe the difference between \cancel \bcancel and \xcancel, that would be helpful.

jgm avatar Aug 11 '25 17:08 jgm

Yes, one could add a new constructor ECancel. (Rather than booleans, it might be better to add a custom data type to customize.)

The custom data type could store the command name, so it can take values cancel, xcancel, or bcancel. Would that work?

Image

In MathML, \cancel{251636} is equivalent to:

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
  <menclose notation="updiagonalstrike">
    <mn>251636</mn>
  </menclose>
</math>

For \bcancel{251636}, notation is set to downdiagonalstrike; and for \xcancel{251636}, both updiagonalstrike and downdiagonalstrike are used. In OMML, pasting the MathML in Word gives:

<m:oMathPara>
    <m:oMath>
        <m:borderBox>
            <m:borderBoxPr>
                <m:hideTop m:val="1" />
                <m:hideBot m:val="1" />
                <m:hideLeft m:val="1" />
                <m:hideRight m:val="1" />
                <m:strikeBLTR m:val="1" />
                <m:ctrlPr>
                    <w:rPr>
                        <w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math" />
                    </w:rPr>
                </m:ctrlPr>
            </m:borderBoxPr>
            <m:e>
                <m:r>
                    <w:rPr>
                        <w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math" />
                    </w:rPr>
                    <m:t>251636</m:t>
                </m:r>
            </m:e>
        </m:borderBox>
    </m:oMath>
</m:oMathPara>

For \bcancel{251636}, strikeTLBR is used instead of strikeBLTR; and for \xcancel{251636}, both are used. Typst also has a cancel command, but I am not sure if it has an equivalent for \bcancel and \xcancel.

SeanESCA avatar Aug 12 '25 09:08 SeanESCA

The custom data type could store the command name, so it can take values cancel, xcancel, or bcancel. Would that work?

This sounds too TeX-centric. This is a neutral data structure that can be converted to/from various formats. I would suggest a data type like

data StrokeType = ForwardSlash | BackwardSlash | XSlash

jgm avatar Aug 12 '25 18:08 jgm

I can give this a go to implement the new constructor if no work has been started.

SeanESCA avatar Aug 12 '25 19:08 SeanESCA

Go ahead!

jgm avatar Aug 12 '25 19:08 jgm