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

Check if Katex macro persistence can work #1105

Open
laurent22 opened this Issue Jan 7, 2019 · 3 comments

Comments

Projects
None yet
2 participants
@eddiemundo

This comment has been minimized.

Copy link

eddiemundo commented Jan 7, 2019

I think the problem is that some object needs to be passed to katex.renderToString as the macro option every time katex.renderToString is called. Mentioned in https://katex.org/docs/options.html, under macros: object it says:

This object will be modified if the LaTeX code defines its own macros via \gdef, which enables consecutive calls to KaTeX to share state.

So I think these changes are needed to make \gdef work (I haven't tested them):

class MdToHtml_Katex {

	constructor() {
		this.cache_ = {};
		this.assetsLoaded_ = false;
                // initial empty macro object
                this.macros = {};
	}

       // ...

	processContent(renderedTokens, content, tagType) {
		try {
			const cacheKey = tagType + '_' + content;
			let renderered = null;

			if (this.cache_[cacheKey]) {
				renderered = this.cache_[cacheKey];
			} else {
				renderered = katex.renderToString(content, {
					displayMode: tagType === 'block',
                                        // here we put the (mutatable) macro object
                                        macros: this.macros
				});
				this.cache_[cacheKey] = renderered;
			}

			if (tagType === 'block') renderered = '<p>' + renderered + '</p>';

			renderedTokens.push(renderered);
		} catch (error) {
			renderedTokens.push('Cannot render Katex content: ' + error.message);
		}
		return renderedTokens;
	}

        // ...
}
@laurent22

This comment has been minimized.

Copy link
Owner

laurent22 commented Jan 15, 2019

I've tried the two examples on the forum but for both the app says that it's invalid Katex code.

$$
\gdef\matrix#1{\begin{bmatrix}#1\end{bmatrix}}
$$
$$
\matrix{1&2&4\1&2&3}
$$
$$
\gdef\matrix#1{\begin{bmatrix}#1\end{bmatrix}}
\matrix{1&2&4\1&2&3}
$$

Any chance you could post examples that work and expected result?

@eddiemundo

This comment has been minimized.

Copy link

eddiemundo commented Jan 16, 2019

Yes sorry I didn't use code blocks so a backslash got eaten.

katex_gdef

The below should display a matrix but doesn't. It has a "global macro" defined in the first pair of $$, and this macro should be able to be used in the second pair of $$.

$$
\gdef\matrix#1{\begin{bmatrix}#1\end{bmatrix}}
$$
$$
\matrix{1&2&4\\1&2&3}
$$

The below does display a matrix, but because the usage of the defined macro is in the same $$ pair.

$$
\gdef\matrix#1{\begin{bmatrix}#1\end{bmatrix}}
\matrix{1&2&4\\1&2&3}
$$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment