Skip to content
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

Support \gdef (macros) in KaTeX #426

Closed
elbaro opened this issue Apr 23, 2019 · 37 comments
Closed

Support \gdef (macros) in KaTeX #426

elbaro opened this issue Apr 23, 2019 · 37 comments
Labels
Area: Math Issue: Feature Something brand new. Res: Fixed Fix is checked in, but it might be a few weeks until a release.

Comments

@elbaro
Copy link

elbaro commented Apr 23, 2019

What is the problem?

Katex provides \gdef, which defines a global macro. This plugin seems not supporting \gdef.

How can I reproduce it?

$$
\gdef\plim{\operatorname{plim}}
\plim asdf
$$

$$
\plim asdf
$$

The second macro does not render.

@upupming
Copy link
Contributor

upupming commented Apr 23, 2019 via email

@elbaro
Copy link
Author

elbaro commented Apr 23, 2019

I think it is a problem in the plugin. Katex supports \gdef. You can try the code below in your browser. It works in my browser.

<!DOCTYPE html>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" integrity="sha384-dbVIfZGuN1Yq7/1Ocstc1lUEm+AT+/rCkibIcC/OmWo5f0EA48Vf8CytHzGrSwbQ" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js" integrity="sha384-2BKqo+exmr9su6dir+qCw08N2ZKRucY4PrGQPPWU1A7FtlCGjmEGFqXCv5nyM5Ij" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js" integrity="sha384-kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI" crossorigin="anonymous"
    onload="renderMathInElement(document.body);"></script>

<body>
    $$
    \gdef\plim{\operatorname{plim}}
    \plim asdf
    $$

    $$
    \plim asdf
    $$

</body>

This may be because the plugin does not call a rendering function with macros option, but I am not sure.

@yzhang-gh
Copy link
Owner

You are right. We haven't used the macro option.

https://katex.org/docs/supported.html#macros

@yzhang-gh yzhang-gh added the Issue: Feature Something brand new. label Apr 23, 2019
@yzhang-gh yzhang-gh changed the title Support \gdef in katex Support \gdef (macros) in KaTeX Apr 23, 2019
@PierreMarchand20
Copy link
Contributor

It would be also nice to be able to store all the user-defined macros in an external file and load them in Katex. It is possible with the ‘macro’ option.

@PierreMarchand20
Copy link
Contributor

I am looking at how to add the macros option to katex, but it seems that the module used in this repo neilsustc/markdown-it-katex is not really maintained (waylonflinn/markdown-it-katex#23). So I suspect that the version of katex used is too old to do that.

Anyway, it may be worth it to change this dependency by another one (see previous link).

@PierreMarchand20
Copy link
Contributor

My bad, I can use the macros option anyway, but still, it may be a good idea to change this dependency.

@yzhang-gh
Copy link
Owner

The author said he would update it (last commented 2 days ago, in the issue you mentioned). (Actually, neilsustc/markdown-it-katex is my personal fork of that.)

yzhang-gh added a commit that referenced this issue May 12, 2019
@yzhang-gh
Copy link
Owner

Thanks to @PierreMarchand20, we now have this feature in the dev build (new option markdown.extension.katex.macros).

See macros part in the options page.

@yzhang-gh
Copy link
Owner

Seems we introduced a new problem

image

@PierreMarchand20
Copy link
Contributor

PierreMarchand20 commented May 13, 2019

That is weird, we only added an option when calling markdown-it-katex. I will try with my version. FYI, there is also an option concerning the delimiters that we could add (not documented yet KaTeX/KaTeX#1944).

@PierreMarchand20
Copy link
Contributor

I cannot reproduce this issue :-/

image

@upupming
Copy link
Contributor

Everything is well on my side, too (latest commit * d485552 🐜 #436 cont.):

image

@upupming
Copy link
Contributor

@yzhang-gh According to the docs , you can see the error of KaTeX by setting the throwOnError to true at here:

throwOnError: false,

For example, this:
image
will throw an error in the debug console:
image

@upupming
Copy link
Contributor

After that test, the error shows on my laptop, too 😭
image
I will try to fix it.

@upupming
Copy link
Contributor

upupming commented May 13, 2019

This is really strange, I have tested this code:

const katex = require('katex')

let html = katex.renderToString('1', {
  throwOnError: true,
  displayMode: false,
  macros:{},
});

console.log(html);

It works very well. And our code did the same thing but got an error:
image

Update: I got it, there must be something wrong with the VSCode extendMarkdownIt, when we print to html, everything works as expected, but VSCode rendering is not as expected.

@upupming
Copy link
Contributor

upupming commented May 13, 2019

Okay, I have fixed it, this is really a small problem. The get<object> actually does not return an Object, instead, it returns a string.

We should use getConfiguration instead!

upupming added a commit to upupming/vscode-markdown that referenced this issue May 13, 2019
@PierreMarchand20
Copy link
Contributor

PierreMarchand20 commented May 13, 2019

That is weird no ? oO I used it this way because I thought that if we added other settings about katex, we needed to use the get function.

@PierreMarchand20
Copy link
Contributor

Looking at the definition:

/**
* Return a value from this configuration.
*
* @param section Configuration name, supports _dotted_ names.
* @return The value `section` denotes or `undefined`.
*/
get<T>(section: string): T | undefined;

it should return an object no ?

@upupming
Copy link
Contributor

upupming commented May 13, 2019

@PierreMarchand20 Yes, that is really interesting.
VSCode doesn't document too much about this. I have a test:

workspace.getConfiguration('markdown.extension.katex').get<object>('macros') as unknown as string == '{}'
// true

So, although the return type is an object, it is actually is a string.

@PierreMarchand20
Copy link
Contributor

PierreMarchand20 commented May 13, 2019

Actually, I think the issue comes when the parameter markdown.extension.katex.macros has no value in the settings. At least, I have been able to reproduce the issue like this.

@upupming
Copy link
Contributor

Yes, and now I have fixed it in the PR #443

@PierreMarchand20
Copy link
Contributor

PierreMarchand20 commented May 13, 2019

But then, I think we can leave the get function no ? There must be a mistake in the default value or something.

@upupming
Copy link
Contributor

I have been inspired by another VSCode plugin mdmath actually, it works very well:
https://github.com/goessner/mdmath/blob/e2f42ff70b1903e37c420b70896abde2f1ec7f57/extension.js#L32

@upupming
Copy link
Contributor

But then, I think we can leave the get function no ? There must be a mistake in the default value or something.

You are right, I am going to fix the default value of markdown.extension.katex.macros in package.json.

@PierreMarchand20
Copy link
Contributor

But it seems that the default value I put is the same as in the package you mentioned

@PierreMarchand20
Copy link
Contributor

Ah no I see ^^here's why it was a string I guess

@PierreMarchand20
Copy link
Contributor

I have been inspired by another VSCode plugin mdmath actually, it works very well:
https://github.com/goessner/mdmath/blob/e2f42ff70b1903e37c420b70896abde2f1ec7f57/extension.js#L32

I actually looked at this repo when I did my pull request, but I did not understand why it needed all this to get the macros.

@upupming
Copy link
Contributor

upupming commented May 13, 2019

Haha, anyway we have fixed it, thanks for your help @PierreMarchand20 !

Have a nice day ❤️

@PierreMarchand20
Copy link
Contributor

Well thank you for fixing my mistake ! Second time I try to do something in typescript ^^

But you did fix a typo at first in your pull request that could be interesting to fix !!!

@upupming
Copy link
Contributor

Yep, I have fixed it again~

@yzhang-gh
Copy link
Owner

Interesting

FYI, there is also an option concerning the delimiters that we could add (not documented yet KaTeX/KaTeX#1944).

Currently, the delimiters are handled by markdown-it-katex (called in the conversion from Markdown to HTML). The KaTeX auto-renderer is a post-HTML script, as I remember. There may be something we can borrow but only a few I guess.

"default": "{}" -> "default": {}

I actually have seen the same mistake recently (also from a PR, lol).

But you did fix a typo at first in your pull request that could be interesting to fix !!!

🤣

@upupming
Copy link
Contributor

I am so sorry, just realized what mistake I made, thanks for pointing it out!

@yzhang-gh
Copy link
Owner

Nonono, I also didn't notice that. Thanks for all the contributions 👍

@PierreMarchand20
Copy link
Contributor

it does not work for me with the last update. I am trying to understand why, but in the meantime, here's my setting :

"markdown.extension.katex.macros": {
    "\\bfx" : "\\mathbf{x}",
    "\\bfy" : "\\mathbf{y}", 
    "\\bfn" : "\\mathbf{n}", 
    "\\bfu" : "\\mathbf{u}",
    "\\bfv" : "\\mathbf{v}",
    "\\bft" : "\\mathbf{t}",
    "\\bff" : "\\mathbf{f}",
    "\\bfe" : "\\mathbf{e}",

    "\\bfA" : "\\mathbf{A}", 
    "\\bfR" : "\\mathbf{R}", 
    "\\bfD" : "\\mathbf{D}", 
    "\\bfI" : "\\mathbf{I}", 
    "\\bfM" : "\\mathbf{M}", 
    "\\bfV" : "\\mathbf{V}", 
    "\\bfW" : "\\mathbf{W}", 
    "\\bfC" : "\\mathbf{C}", 
    "\\bfP" : "\\mathbf{P}",  
    "\\bfZ" : "\\mathbf{Z}", 

    "\\bbN" : "\\mathbb{N}",
    "\\bbR" : "\\mathbb{R}",
    "\\bbC" : "\\mathbb{C}",
    "\\wH"  : "\\widetilde{H}",


    "\\calD" : "\\mathcal{D}",
    "\\calT" : "\\mathcal{T}",
    "\\calR" : "\\mathcal{R}",
    "\\calP" : "\\mathcal{P}",
    "\\calV" : "\\mathcal{V}",
    "\\calW" : "\\mathcal{W}",
    "\\calJ" : "\\mathcal{J}",
    "\\calE" : "\\mathcal{E}",

    "\\diff" : "\\mathop{}\\!\\mathrm{d}",
    "\\supp"  : "\\operatorname{supp}",
    "\\grad"  : "\\operatorname{grad}",   
    "\\Span"  : "\\operatorname{span}",   
    "\\range" : "\\operatorname{range}", 
    "\\diag"  : "\\operatorname{diag}", 
    "\\cond"  : "\\operatorname{cond}", 
    "\\dist"  : "\\operatorname{dist}"
  },

@PierreMarchand20
Copy link
Contributor

fixed in #473, the option for giving macros to katex is macros and not userMacros

@Hadron67
Copy link

I'm still having this issue in v2.4.0. It seems that #473 isn't included in the release, i've checked ~/.vscode/extensions/yzhang.markdown-all-in-one-2.4.0/dist/extension.js, the macro option name is still userMacros. Reinstalling doesn't work.

In addition, the check Object.keys(userMacros).length !== 0 is possibly unnecessary, since the macro option should always be presented for \gdef to work.

@yzhang-gh
Copy link
Owner

The fix has not been shipped yet. You can try out our dev build.

As for the check Object.keys(userMacros).length !== 0, it is for the non-macros users. For that case, there will be some unexpected results even if we pass in an empty {}.

@Lemmingh Lemmingh added Area: Math Res: Fixed Fix is checked in, but it might be a few weeks until a release. labels Jun 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Math Issue: Feature Something brand new. Res: Fixed Fix is checked in, but it might be a few weeks until a release.
Projects
None yet
Development

No branches or pull requests

6 participants