-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add mhchem extension #1436
Add mhchem extension #1436
Conversation
To test this PR, I have made four modifications to the code of First, the MathJax wrapper is replaced with a wrapper that includes callbacks from KaTeX
Second, I replaced Third, I modified four lines of code in order to change
Fourth, I simplified the code for the reaction arrows, since KaTeX provides a full set of extensible arrows and work around hacks are not needed.
I’ve done a local build with those changes and the code in this PR. With that, I can reproduce every formula on the The code here is only a suggestion. @mhchem owns this work and he can adopt it, change it, or discard it just as he chooses. |
Codecov Report
@@ Coverage Diff @@
## master #1436 +/- ##
=========================================
Coverage ? 93.67%
=========================================
Files ? 80
Lines ? 4666
Branches ? 813
=========================================
Hits ? 4371
Misses ? 261
Partials ? 34
Continue to review full report at Codecov.
|
@ronkok this is super cool! I'm glad that you were able to adapt the MathJax extension without too much work. Could you post some example renderings? |
This is splendid! What I see here, seems very reasonable to me. So, I would leave the creation of the first version up to you. You decide, where to place the files, how to include them, etc. After that, I'd like to have control over the mhchem core for fixes and updates. You decide, how you would like to have that. Maybe a file at I am looking forward to having mhchem support in KaTeX and working with you. |
@mhchem That sounds very reasonable to me. Let's give @kevinbarabash a chance to review this PR. If he approves, then I'll work up a PR to create an |
As most of macro expansions don't need to access KaTeX internals and |
@mhchem I think having things live at |
src/macros.js
Outdated
|
||
defineMacro("\\pu", function(context) { | ||
return mhChemExpansion(context, "pu"); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think macro definitions should also be in contrib
too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think macro definitions should also be in
contrib
too.
I would like that, too, so let's discuss what would be required to make that happen. At present, KaTeX exposes __defineMacro: defineMacro
, but that only enables one to define a macro in the KaTeX macro language. KaTeX does not support branching macro code such as \if … \else … \fi
, so the complexity of mhchem
is out of reach by that method.
I needed a way to (1) capture the functions \ce
and \pu
, (2) reconstruct the string in the argument to those functions, (3) pass the string to the external mhchem
extension, and (4) receive the expanded macro or an error message. This PR does that.
Maybe I should have added an abstraction layer that would enable other similar extensions to do something similar. Such an abstraction layer would expose a _defineComplexMacro
callback and would enable complexMacro.expand(argString: string, functionName: string) : {expansion: string, errorMsg: string}
Then I could have used that exposed capability to define \ce
and \pu
in the contrib
folder
Maybe that's what's people mean when they talk of a plugin architecture. I don't know. I'm out of my depth here. There are different ways to do this and I'm probably not the best person to do decide which way is best. That's why I opened this PR by describing it as a proof of concept, not a finished product. I would welcome instruction. If someone wanted to step in and define that proposed callback function, I would be fine with that, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__defineMacro
works just like defineMacro
. Its second argument can be a function, as above. Is there something more you need?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defineMacro
. Its second argument can be a function, as above.
I might be able to work with that. Let me get back to you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update: I can indeed pass a function in _defineMacro
, just as @edemaine suggests. In fact, no modifications are necessary to macros.js
at all. It works fine.
Before, instructions to the web site administrator were to place the reference to mhchem.js
in the <head>
of the HTML page. As now proposed, they will have to do that, and also write the following into their KaTeX rendering options:
macros: {
"\\ce": function(context) {return mhchem.expand(context.consumeArgs(1)[0], "ce")},
"\\pu": function(context) {return mhchem.expand(context.consumeArgs(1)[0], "pu")},
"\\tripledash": "\\vphantom{-}\\raisebox{2mu}{$\\mkern2mu\\tiny\\text{-}\\mkern1mu\\text{-}\\mkern1mu\\text{-}\\mkern2mu$}"
}
In exchange for that extra installation complexity, we get cleaner code in macros.js
. Is that a good trade off?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like it. In particular, the \\tripledash
part is part of internals a user should not never see. Also, the other definitions do not make me happy. What will happen when you will change the internals some time in the future?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like it much either. And now that I look back, I think that @ylemkimon wanted to suggest something to move \ce
definition into contrib
, not into a rendering option. But I don't know how to accomplish such a thing. If anyone can tell me how, I'll take a crack at it.
Otherwise, I think we are better off to keep the code in macros.js
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it impossible to do following in the extension:
const mhChemExpansion = <omitted>;
katex.__defineMacro("\\ce", function(context) {
return mhChemExpansion(context, "ce");
});
katex.__defineMacro("\\pu", function(context) {
return mhChemExpansion(context, "pu");
});
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One minor thing. I would prefer the all-lowercase spelling of mhchem over mhChem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ylemkimon Thank you for the instruction!
The latest commit moves all the code from Thank you to @ylemkimon and @edemaine for instruction on how to better arrange things. It's a better PR now. |
The KaTeX linter says that nearly every line of |
@ronkok Put |
A couple other quick comments:
|
I'll go further. There is no particular reason for In the KaTeX repo, there would still be a In that arrangement, there are no lint issues, no licensing conflicts, and @mhchem gets to keep control over his code. Comments? |
I would lean more toward just the main mhchem module being "off site", and just the KaTeX-related tweaks being here. I was wondering whether Personally, I serve KaTeX files from my own server, not CDN. (Debatable whether that's a good idea.) This would be one reason that it'd be more convenient to have things in the KaTeX distribution, but avoiding the main duplication would be nice. |
The previously existing version of
@mhchem makes the |
Which could be created if need be. To repeat myself: It's your decision where the files live. I don't have a preference. (As long as I can keep the behavior of mhchem for MathJax and mhchem for KaTeX aligned, I am happy.) |
So I will confess a personal interest. We now have a |
That seems like best path forward. If this module was published then it could be required by the MathJax plugin and the KaTeX extension. @mhchem how much would is involved in this? |
I found it. I see you've addressed it in #1781. I'll review that right now. |
#1781 has been reviewed and merged. Rebasing this PR should fix the |
Codecov Report
@@ Coverage Diff @@
## master #1436 +/- ##
=======================================
Coverage 93.66% 93.66%
=======================================
Files 80 80
Lines 4653 4653
Branches 807 807
=======================================
Hits 4358 4358
Misses 261 261
Partials 34 34
Continue to review full report at Codecov.
|
@ronkok thanks for all of the changes. I'm excited about support for mhchem and even more excited that you were able to do it as an extension. Even though mhchem is a fairly standard LaTeX package I think this PR will serve as a good example how people might extend KaTeX is less standard ways via extensions. |
@kevinbarabash You are very welcome. This, I think, is a good pause point. I have submitted PR's that supported 140 TeX control words, supported 600 Unicode characters, fixed 9 rendering bugs (not counting the ones I created), and added working examples to the documentation. I think I've done most of what I came here to do. Let me know if you find bugs in the code I submitted. Beyond that, my involvement henceforward will be much less that it has been the past 18 months. To you, to @edemaine and @ylemkimon, I've appreciated the support and encouragement. You have a great library going here. |
@ronkok thanks for all of your many contributions and thanks for the heads up that you'll be moving on to other things. I appreciate your offer to support the code that you've submitted. If you end up using KaTeX in any future projects, I'd love to hear what you're doing with it. 🙂 |
@ronkok Thank you for your great contributions! It was a pleasure to work with you 😄 |
@ronkok Hooray! Thank you so much, that was quite some work! Thanks! Okay, I'll take it from here. If I update mhchem, I will update it for both, MathJax and KaTeX, from now on. The setup looks straight forward for me (I'd modify mhchem.js, file a pull request and your workflow does minification etc.). @ronkok I feel, you deserve a copyright line. I will add a line "Copyright (c) 2018 Ron Kok" with the next update. Once again, Thank you! |
* A proposal for support of mhchem * Create extension * Add index * Remove arrow function * Fix lint errors in macros.js * Remove mhchem from build * Move code from macros.js to mhchem.js * Add minified mhchem * Remove arrow head mods. Disable eslint. * Fix space handling * Update code to mhchem v3.3.0 * Tweak arrows to reduce diff * Raise \tripledash * Edit docs and webpack * Update package.json * Remove wrap. Remove manual. * Correct second brush stroke in \xrightleftarrows * Added patch file * Add import line * Add mhchem to eslint ignore
@kevinbarabash 1½ years ago, you said you would like to hear if I used KaTeX in any future projects. Well, now I have. I put up https://hurmet.app/ a few days ago and I'm quite happy with it. |
@ronkok neat! It reminds me of juypter notebooks. I like how it allows you to computations on taels in a very succinct way and over course it's nice to see nicely typeset math. 🙂 |
This is a proposal more than it is a pull request. Or perhaps it’s a proof of concept. The proposed code allows KaTeX to interact with a proposedmhchem
extension. Such amhchem
extension would be installed in a way similar to the auto-render extension or the copy-tex extension. That is, web site administrators would list themhchem-for-katex.js
file in the<head>
of their page.I invite comments. I expect that @edemaine can write a better helper function to recreate the argument to be passed to the extension. I’m sure that @mhchem can improve on the edits that I will suggest to themhchem
code.It’s pretty simple, really.mhchem
expands a macro. So I’ve written a suggested wrapper around the mhchem code that exposes a object with a single method:mhchem.expand(str: string, stateMachine: "ce"|"pu") : {expansion: string, errorMsg: string}
.The code in this PR checks for existence of the object, then calls the function.General revision (Nov 3): This PR adds support for the
\ce
and\pu
functions from themhchem
package. However, this PR makes no revisions to any core KaTeX files. Instead, it exploits the fact that KaTeX exposes thedefineMacro
function. All themhchem
code is a separate file. So web page administrators will be able to choose whether or not to includemhchem
as an extension.As in the original version of this PR, the code is mostly a copy of the
mhchem
package for MathJax. I still have made only four modifications, as described just below.