-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 \eqref and \label #2003
Comments
This issue is partly discussed in #350. I agree that |
We might be to insert a pass on the parse tree to insert labels/refs as needed before doing the render pass. |
@kevinbarabash Neat idea. One challenge is that labels and refs will generally span multiple equations, so this would need to be done at the autorender level. But maybe if we work out the interface for how autorender could do it, we could get a more general API... |
any update on that ? referencing equations, tables and figures + labels would be super useful and help make the change from mathjax ! |
Inspired by this issue: falgon/roki-web#34 ATTENTION: UPDATE: The previous trust setting code is wrong, as @nschloe said, KaTeX option: trust: (context) => ['\\htmlId', '\\href'].includes(context.command),
macros: {
"\\eqref": "\\href{###1}{(\\text{#1})}",
"\\ref": "\\href{###1}{\\text{#1}}",
"\\label": "\\htmlId{#1}{}"
} example: Search by definition $\eqref{1-1}$ or $\ref{1-1}$.
$$
\begin{aligned} \sin 2\theta = 2\sin \theta \cos \theta \\ = \cfrac{2 \tan \theta}{1+\tan^2 \theta} \end{aligned} \label{1-1}\tag{1-1}
$$ Used in the same way as LaTeX or MathJax. |
@zhangpeng96 Your |
I just converted a small note of mine from LaTeX to Markdown + KaTeX + Plotly.js on a GitHub Wiki page, https://github.com/nschloe/colorio/wiki/On-the-conversion-from-OSA-UCS-to-CIEXYZ. The only thing that was really tedious is the equation labeling. Voting for this in KaTeX! |
Thank you! I have corrected it. 😆 |
Is #2003 (comment) a satisfactory solution? Can it be made part of Katex? |
From my testing, it is not fully satisfactory as the In addition, use of
|
@zardini123 The warning message can be suppressed by setting |
Is this the correct way to add this to the auto-renderer extension?
|
Yes, I have tried it and it works! |
@zhangpeng96 Nice workaround, applied to docsify-latex, thanks! |
@zhangpeng96's approach of converting Perhaps KaTeX should officially do this, and (perhaps in the auto-render extension) offer a postprocessing phase to turn these labels into numbers if desired? I also wonder whether |
In the near-term, please at least make \label be pass-thru and show the equation rendering instead of a Parse Error message. In general you should be tolerant of any tex-like components you can't process. |
Any update on this feature? As I use a Hugo theme, Coder, which implements KaTeX as <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.4/dist/katex.min.js"
integrity="sha384-PwRUT/YqbnEjkZO0zZxNqcxACrXe+j766U2amXcgMg5457rve2Y7I6ZJSm2A0mS4" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.4/dist/contrib/auto-render.min.js"
integrity="sha384-+VBxd3r6XgURycqtZ117nYw44OOcIax56Z4dCRWbxyPt0Koah1uHoK0o4+/RRE05" crossorigin="anonymous"
onload="renderMathInElement(document.body,
{
delimiters: [
{left: '$$', right: '$$', display:true},
{left: '$', right: '$', display:false},
{left: '\\(', right: '\\)', display: false},
{left: '\\[', right: '\\]', display: true}
]
}
);"></script> the feature still does not work. Thank you for any information. |
I think it might be nearly impossible for katex itself to ship a full solution, especially when equations are numbered by CSS counters, which are inflexible and totally inaccessible to javascript. Also katex doesn't know the global numbering scheme if multiple snippets of latex are passed through katex render methods individually and assembled into a larger document. Of course that's why CSS counters are a neat solution for displaying the numbered equations. Anyway I am working in the scenario of assembling katex outputs, namely in TiddlyWiki5, and I can say that implementing I took @zhangpeng96's clever workaround as the starting point. The rest is javascript and css to replace the labels with numbers -- merely a realization of what has been said in the discussion so far @edemaine. My conclusion (cf. the hoops I had to jump through) is that Summary:
|
@tavin This looks great! It's been a while since I've thought about this issue, but I wonder if you'd be willing to take a stab at adapting your PR for the KaTeX repo? I could see the following approach:
The main question is where to locate that function. I think it makes sense in contrib/auto-render, and maybe auto-render could also call it by default, but importantly it should also be callable separately for those not using auto-render, for more complex applications like TiddlyWiki. What do you think? |
@edemaine I would like to help but I can't say how soon, as I'm completely unfamiliar with the katex codebase. Also imo the "resolution" aspect should be a separate issue and PR. Yes the main idea for now is to enrich |
The following equation
still does not render with latest KaTeX 0.16.9 - "KaTeX parse error: Undefined control sequence: \label at position 77: …p\right)^{n-k} \̲l̲a̲b̲e̲l̲{binom2} \end{e…". |
I succeeded! Thanks to zhangpeng96's #2033 (comment). The following is my code. Put it in 'demo.html' and open with browser, then \eqref \eq and \label will be rendered as the following screen shot. <!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title>KaTeX-DOM Eq. Ref Test</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css" integrity="sha384-AfEj0r4/OFrOo5t7NnNe46zW/tFgW6x/bCJG8FqQCEo3+Aro6EYUG4+cU+KJWu/X" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.js" integrity="sha384-g7c+Jr9ZivxKLnZTDUhnkOnsh30B4H0rpLUpJ4jAIKs4fnJI+sEnkvrMWph2EDg4" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/auto-render.min.js" integrity="sha384-mll67QQFJfxn0IYznZYonOWZ644AWYC+Pt2cHqMaRhXVrursRwvLnLaebdGIlYNa" crossorigin="anonymous"
onload="renderMathInElement(document.body);"></script>
<script src="main.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function() {
renderMathInElement(document.body, {
// auto-render specific keys
delimiters: [
{left: '$$', right: '$$', display: true},
{left: '$', right: '$', display: false},
{left: '\\(', right: '\\)', display: false},
{left: '\\[', right: '\\]', display: true}
],
// render equation with bug
throwOnError : false,
// support eqref
trust: (context) => ['\\htmlId', '\\href'].includes(context.command),
macros: {
"\\eqref": "\\href{###1}{(\\text{#1})}",
"\\ref": "\\href{###1}{\\text{#1}}",
"\\label": "\\htmlId{#1}{}"
}
});
});
</script>
</head>
<body>
In Eq. $\eqref{eq:1}$ or $\ref{eq:1}$.<br/>
$$
\begin{aligned}
\sin 2\theta = 2\sin \theta \cos \theta \\ = \cfrac{2 \tan \theta}{1+\tan^2 \theta}
\end{aligned}
\label{eq:1} \tag{1}
$$
</body>
</html> ![]() By the way, as shown in the screen shot above, this solution has a drawback: |
Do I understand correctly that this issue is not resolved for about 4 years, while such a solution already works in MathJax? I was going to use katex for my website but the lack of this feature is a huge drawback :( |
yzhang-gh/vscode-markdown#985 (comment) The reference name is shown with arguments of @hyliang96 <i>eqref command:</i> $\eqref{8}$, <i>ref command:</i> $\ref{8}$.
<br>
$$
\begin{aligned} \sin 2\theta = 2\sin \theta \cos \theta \\ = \cfrac{2 \tan \theta}{1+\tan^2 \theta} \end{aligned} \label{8}\tag{8}
$$ 暂时想不到更优雅的实现方式,后面我再试试 |
I had a go at implementing a solution which may be useful as an interim measure for some developers. It works by doing a pre-process scan of the environments and keeps track of the equation number and tags for use in replacing the \eqref's with links. It handles It doesn't play nicely with internal environments (like https://bourne2learn.com/math/katex/katex-label-tag-eqref.php There's a brief explanation below the demo, and the code used. Hope it helps. |
It is near 4 years passed while we chatting here.
in the main KaTeX codebase. |
I'd like to kindly remind those that are demanding a solution to this feature request, that this is open-source software, and is completely free to use. Instead, the most effective way to get what you want is to submit a pull-request yourself. Open source is one of the most beautiful achievements ever to be created by humankind. It would be sad to see (more) developers leave it because of inconsiderate or disrespectful behavior. |
The solutions offered so far don't appear to have a satisfactory numbering system for the eqref's, so I pursued with it some more. So I have a new version which merges my pre-processing approach to keeping track of equation numbers, with the macro approach suggested earlier: #2003 (comment) Now we have:
Here it is: https://bourne2learn.com/math/katex/katex-label-tag-eqref-macro.php After thinking about it, it seems to me:
I also updated my earlier non-macro proposal, and it handles https://bourne2learn.com/math/katex/katex-label-tag-eqref.php Regards |
I was able to create a working $$
\begin{align}
ax^2 + bx + c &= 0 \label{eq:quadratic-general} \\
x^2 + \frac{b}{a}x + \frac{c}{a} &= 0 \label{eq:quadratic-normalized} \\
x^2 + \frac{b}{a}x &= -\frac{c}{a} \label{eq:quadratic-half} \\
x^2 + \frac{b}{a}x + \left(\frac{b}{2a}\right)^2 &= -\frac{c}{a} + \left(\frac{b}{2a}\right)^2 \label{eq:quadratic-complete} \\
\left(x + \frac{b}{2a}\right)^2 &= \frac{b^2 - 4ac}{4a^2} \label{eq:quadratic-squared} \\
x + \frac{b}{2a} &= \pm \sqrt{\frac{b^2 - 4ac}{4a^2}} \label{eq:quadratic-sqrt} \\
x &= -\frac{b}{2a} \pm \sqrt{\frac{b^2 - 4ac}{4a^2}} \label{eq:quadratic-solve} \\
x &= \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} \label{eq:quadratic-final}
\end{align}
$$
Reference equations $\eqref{eq:quadratic-general}$, $\eqref{eq:quadratic-normalized}$, $\eqref{eq:quadratic-half}$, $\eqref{eq:quadratic-complete}$, $\eqref{eq:quadratic-squared}$, $\eqref{eq:quadratic-sqrt}$, $\eqref{eq:quadratic-solve}$, and $\eqref{eq:quadratic-final}$ in the text. ![]() Checkout the detailed implementation at https://github.com/branyang02/notie/blob/main/src/utils/MarkdownProcessor.ts Also feel free to checkout the website: https://notie-markdown.vercel.app/examples/auto-label However, it is rather not a clean implementation and required a lot of regex matching. Similar to @mbourne's idea, I implemented a custom equation numbering logic for the |
Very neat solution, @branyang02 ! Your question prompted me to revisit my solution, which I did, and have revised it so that it now handles each line in your quadratic equation example (which I added at the bottom of my demo, and which it didn't handle earlier). https://bourne2learn.com/math/katex/katex-label-tag-eqref.php I no longer create extra spans, instead I gave the display mode span an id based on the label (where there was only one equation in the display mode span), and gave each equation number an id (where there was more than one equation in the environment). I also set a scroll margin so the user is more likely to see the whole of the equation (rather then only the bits below the top of the equation number) in the cases of multi-line environments. Regards |
Is your feature request related to a problem? Please describe.
I am trying to migrate from Mathjax to Katex, however,
\eqref
and\label
are not supported for the moment. Any roadmap on this?Describe the solution you'd like:
Support
\eqref
and\lable
for equation referenceLink to or name of a (La)TeX package that provides the same feature:
KaTeX's general goal is to render as LaTeX does.
Describe alternatives you've considered:
A clear and concise description of any alternative solutions or features you've considered.
Additional context:
Add any other context or screenshots about the feature request here.
The text was updated successfully, but these errors were encountered: