Repository: Khan/KaTeX
Pull request #876 (2017-09-12 02:49:26)

Use mpadded for \raisebox MathML

ronkok: You know, I'm not sure I know how to reliably document MathML outcomes. For what it's worth, here's a screenshot.
![raiseboxml](https://user-images.githubusercontent.com/16403058/30307500-68e81b08-9733-11e7-8414-e151e61886ca.PNG)

kevinbarabash: @ronkok see https://github.com/Khan/KaTeX/blob/master/test/mathml-spec.js for examples of snapshot tests.  After you add a test mathml-spec.js that uses `.toMatchSnapshot()`, run `npm test` and `jest` add a new snapshot to `__snapshots__/mathml-spec.js.snap`.
ronkok: > Can you add a snapshot test for this?

It turns out that the answer to that question is no, I can't. My attempt to `npm test` failed on the `make lint flow test`.
kevinbarabash: > It turns out that the answer to that question is no, I can't. My attempt to npm test failed on the make lint flow test.

I can send you a PR for the snapshot test.  Can you post the console output of the failure from `npm test`?
ronkok: Output as requested: 
>Windows PowerShell
>Copyright (C) 2016 Microsoft Corporation. All rights reserved.
>
>PS C:\Users\ronko> cd C:\Users\ronko\OneDrive\Documents\GitHub\KaTeX
>PS C:\Users\ronko\OneDrive\Documents\GitHub\KaTeX> npm test
>
> katex@0.8.4-pre test C:\Users\ronko\OneDrive\Documents\GitHub\KaTeX
> make lint flow test
>
>make i info Invoking lint target
(node:8744) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): undefined
>make × ERR  not found: make-$(NIS)

kevinbarabash: @ronkok thanks.  It looks like `make` isn't found.  Does that sounds right?  I think we should be able to modify the scripts in `package.json` to run commands in `node_modules/.bin` directly.  The benefit of the Makefile at the moment is that it ensures that dependencies are up to date.  I wonder if there's a way to do without using `make`.
ronkok: I think that's right. `make` is not, I believe, part of Windows. There are ways to get equivalents running in Windows and no doubt someone like @edemaine knows all about them. The problem here is that you are dealing with an amateur.

Go ahead and search for a `make` alternative if that is something with long term value. Don't do that on my account. I have 3 or 4 small PRs left to submit and then my work here will be done.
ronkok: One small step forward.

Patch:
diff --git a/src/buildHTML.js b/src/buildHTML.js
index 5bd163fd..631f5f19 100644
--- a/src/buildHTML.js
+++ b/src/buildHTML.js
@@ -1640,7 +1640,7 @@ groupTypes.raisebox = function(group, options) {
         value: [{
             type: "text",
             value: {
-                body: group.value.body,
+                body: group.value.value,
                 font: "mathrm", // simulate \textrm
             },
         }],
diff --git a/src/buildMathML.js b/src/buildMathML.js
index 5b27200a..c2607b13 100644
--- a/src/buildMathML.js
+++ b/src/buildMathML.js
@@ -604,9 +604,13 @@ groupTypes.mclass = function(group, options) {
     return new mathMLTree.MathNode("mstyle", inner);
 };
 
-// Transforms (translation/rotation) don't seem to have a representation
-// in MathML, so just treat them like \text{...}
-groupTypes.raisebox = groupTypes.text;
+groupTypes.raisebox = function(group, options) {
+    const node = new mathMLTree.MathNode(
+        "mpadded", [buildGroup(group.value.body, options)]);
+    const dy = group.value.dy.value.number + group.value.dy.value.unit;
+    node.setAttribute("voffset", dy);
+    return node;
+};
 
 /**
  * Takes a list of nodes, builds them, and returns a list of the generated
diff --git a/src/functions.js b/src/functions.js
index 4545070f..91c6a542 100644
--- a/src/functions.js
+++ b/src/functions.js
@@ -673,6 +673,7 @@ defineFunction(["\\raisebox"], {
     return {
         type: "raisebox",
         dy: amount,
-        body: ordargument(body),
+        body: body,
+        value: ordargument(body),
     };
 });


Test patch:
diff --git a/test/__snapshots__/mathml-spec.js.snap b/test/__snapshots__/mathml-spec.js.snap
index de5a4589..2b52ca14 100644
--- a/test/__snapshots__/mathml-spec.js.snap
+++ b/test/__snapshots__/mathml-spec.js.snap
@@ -74,6 +74,27 @@ exports[`A MathML builder should make prime operators into <mo> nodes 1`] = `
 
 `;
 
+exports[`A MathML builder should use <mpadded> for raisebox 1`] = `
+
+<math>
+  <semantics>
+    <mrow>
+      <mpadded voffset="0.25em">
+        <mrow>
+          <mtext>
+            b
+          </mtext>
+        </mrow>
+      </mpadded>
+    </mrow>
+    <annotation encoding="application/x-tex">
+      \\raisebox{0.25em}{b}
+    </annotation>
+  </semantics>
+</math>
+
+`;
+
 exports[`A MathML builder should use <msupsub> for regular operators 1`] = `
 
 <math>
diff --git a/test/mathml-spec.js b/test/mathml-spec.js
index d9e00b91..6565c099 100644
--- a/test/mathml-spec.js
+++ b/test/mathml-spec.js
@@ -50,4 +50,8 @@ describe("A MathML builder", function() {
     it('should use <msupsub> for regular operators', () => {
         expect(getMathML("\\textstyle\\sum_a^b")).toMatchSnapshot();
     });
+
+    it('should use <mpadded> for raisebox', () => {
+        expect(getMathML("\\raisebox{0.25em}{b}")).toMatchSnapshot();
+    });
 });