You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Following the PR for a better katex support (see #633 and #634), I would like to add another feature: macros!
Macros add the possibility to define a new command, to avoid repeating the same boilerplate code for each block/inline you have in the document.
For example, if you have a function f with three arguments, a, b and c and you wish to colorize the three for the sake of clarity, you have to write
Since you might have to repeat the same lhs each line, you can define a macro to avoid repeating yourself:
$$\newcommand{\a}{\textcolor{red}{a}}\newcommand{\b}{\textcolor{green}{b}}\renewcommand{\c}{\textcolor{blue}{c}} % we have to renew here since \c is already defined for ç.f(\a, \b, \c) = ...% or even\newcommand{\fabc}{f(\textcolor{red}{c}, \textcolor{green}{b}, \textcolor{blue}{c})}\fabc = ...$$
This works perfectly fine, but with a major drawback: its scope is limited to the math block it is in. If you wish to use it in another block in a post you have to redefine it, which can be a pain to maintain and a long boilerplate for simple inline shortcodes.
In the previous case, if you want to explain each line in standard markdown, each block requires more macro boilerplate than actual content.
Fortunately, KaTeX provides another way to define macros, which are global and can be used in any block, without having to redefine them each time: as a parameter in its renderMathInElement function.
My proposal is to add a new shortcode, katexmacros, which would be used as follows:
that would define macros for the whole page, meaning the previous bloc would be written as:
$$f(\a, \b, \c) = ...$$
making the actual content or the page much more readable, especially when it's interlacing katex and markdown.
I have already implemented this feature in my fork: you can see the relevant commit here.
To do it, I created a katex partial. The vendor partial includes it if no katexmacros shortcode is found in the page (but katexinline or katexblock is, see #634 for the origin of those shortcode), with an empty macro object.
Otherwise, it is the role of the katexmacros shortcode to include the partial with its content as a parameter.
My only issue is that I have to escape the macro definition, but that's a minor issue compared to the benefits.
I'll gladly create a PR if you deem this feature useful.
The equations are much more readable, but we have to redefine the macros each time we want to use them in a new block, meaning the page as a whole is less readable.
The equations are much more readable and we don't have to redefine the macros each time we want to use them in a new block, meaning the page as a whole is more readable.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Following the PR for a better katex support (see #633 and #634), I would like to add another feature: macros!
Macros add the possibility to define a new command, to avoid repeating the same boilerplate code for each block/inline you have in the document.
For example, if you have a function
f
with three arguments,a
,b
andc
and you wish to colorize the three for the sake of clarity, you have to writeSince you might have to repeat the same lhs each line, you can define a macro to avoid repeating yourself:
This works perfectly fine, but with a major drawback: its scope is limited to the math block it is in. If you wish to use it in another block in a post you have to redefine it, which can be a pain to maintain and a long boilerplate for simple inline shortcodes.
In the previous case, if you want to explain each line in standard markdown, each block requires more macro boilerplate than actual content.
Fortunately, KaTeX provides another way to define macros, which are global and can be used in any block, without having to redefine them each time: as a parameter in its
renderMathInElement
function.My proposal is to add a new shortcode,
katexmacros
, which would be used as follows:that would define macros for the whole page, meaning the previous bloc would be written as:
making the actual content or the page much more readable, especially when it's interlacing katex and markdown.
I have already implemented this feature in my fork: you can see the relevant commit here.
To do it, I created a
katex
partial. Thevendor
partial includes it if nokatexmacros
shortcode is found in the page (but katexinline or katexblock is, see #634 for the origin of those shortcode), with an empty macro object.Otherwise, it is the role of the
katexmacros
shortcode to include the partial with its content as a parameter.My only issue is that I have to escape the macro definition, but that's a minor issue compared to the benefits.
I'll gladly create a PR if you deem this feature useful.
Example with no macros at all
The equations are not very readable, especially when they are long and complex.
Example with newcommands
The equations are much more readable, but we have to redefine the macros each time we want to use them in a new block, meaning the page as a whole is less readable.
Example with katexmacros
The equations are much more readable and we don't have to redefine the macros each time we want to use them in a new block, meaning the page as a whole is more readable.
Have a nice day and thank you for congo! :)
Beta Was this translation helpful? Give feedback.
All reactions