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

Check if Katex macro persistence can work #1105

Closed
laurent22 opened this issue Jan 7, 2019 · 4 comments
Closed

Check if Katex macro persistence can work #1105

laurent22 opened this issue Jan 7, 2019 · 4 comments
Labels
desktop All desktop platforms enhancement Feature requests and code enhancements

Comments

@laurent22
Copy link
Owner

https://discourse.joplin.cozic.net/t/is-there-a-way-to-get-katex-global-macros-working/1316

@laurent22 laurent22 added enhancement Feature requests and code enhancements essential desktop All desktop platforms labels Jan 7, 2019
@eddiemundo
Copy link

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
Copy link
Owner Author

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
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}
$$

@laurent22
Copy link
Owner Author

The solution you posted above worked.

tfinnberg pushed a commit to tfinnberg/joplin that referenced this issue Mar 12, 2019
@lock lock bot locked and limited conversation to collaborators Oct 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
desktop All desktop platforms enhancement Feature requests and code enhancements
Projects
None yet
Development

No branches or pull requests

2 participants