# Check if Katex macro persistence can work #1105

Open
opened this Issue Jan 7, 2019 · 3 comments

Projects
None yet
2 participants
Owner

### 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 = '

' + renderered + '

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

### laurent22 commented Jan 15, 2019 • edited

 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 commented Jan 16, 2019 • edited

 Yes sorry I didn't use code blocks so a backslash got eaten. 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}