question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Yet another request for equation reference

See original GitHub issue

Before requesting a feature, please search for existing issues.

Is your feature request related to a problem? Please describe. Equation labeling and reference (\label and \eqref) are handy features in technical and scientific writing. The features have been requested in issue #350, #571 and #2003, but little further progress seems to be made.

Describe the solution you’d like:

The full \label and \eqref requires three things:

  • \tag to create equation tags, which is already implemented
  • \label to give a label to an equation, and to automatically add a \tag to that equation
  • \eqref to refer to the equation

A possible reason that the last two features are not implemented may be that what exactly the number \eqref displays cannot be determined by a single scan of the page: in LaTeX you need to compile the file twice to get everything done, causing performance overhead. (I’m not familiar with the inner implementation of KaTeX so sorry for anything inaccurate in my comments.)

A more practical solution may be to implement \label first: KaTeX keeps a global counter, and when KaTeX meets a math block with \label{the-label}, the counter +1 and a \tag is inserted, and an anchor with an id like katex-eq-the-label is also inserted.

\eqref can then be implemented in a progressive enhancement manner:

  1. The costless approach is to forget about \eqref and let users just use a hyperlink with the target #katex-eq-the-label and specify the hyperlink text by themselves. In this way equations themselves are automatically numbered, but users still have to check the number of an equation when refering to that equation.
  2. A slight enhancement is to include \eqref in a very restricted manner: for example, \eqref is included but the only legit way to use it is $\eqref{the-label}$ with nothing else in the block. KaTeX ignores these \eqrefs when displaying math, and does a fast second pass to find all $\eqref{the-label}$ and replace them with hyperlinks whose targets are the labels and whose hyperlink texts are the numbers of equations. In this way the performance may still be acceptable because the second pass doesn’t start the whole math engine. This should cover most of the equation reference demands.
  3. Maybe one day the full \eqref is implemented.

Describe alternatives you’ve considered: Create a markdown-it plugin that does the same work; this is outside the discussion about KaTeX itself, though.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
ronkokcommented, Feb 19, 2021

@wujinq You are correct in that \ref requires a pass through the entire document.

As it happens, I’ve already implemented \label and \ref in my new library, Temml. I could back-port the work from there into KaTeX. Before I start work, I’d like to hear from others what they think about how the implementation works.

In order to support \label and \ref, I exposed a new function, postProcess() . It does the pass through the entire document noted above. A website administrator who wanted to support \ref would have to make a call to katex.postProcess() after all the calls to katex.render are complete. The source code in a web page might look something like this:

const macros = {};
for (let element of mathElements) {
    katex.render(element.textContent, element, { macros });
}
katex.postProcess(document.body);

For the other contributors here, please let me know what you think of that approach. If it is acceptable, I can do the back-port.

3reactions
N0rbertcommented, Jul 21, 2021
\begin{equation}
  g\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k}
\label{binom2}
\end{equation}

still does not work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Make equation reference different to equation tag - TeX
What I would like to do is modify the referencing so that I can assign the correct label to each sub equation, and...
Read more >
How to create a cross-reference to an equation in Word
See how to create a cross-reference of the equation numbers that are not captions below. Traditionally, tables and figures are referenced in the...
Read more >
How to combine multiple cross-references - Suzanne S. Barnhill
How to combine multiple cross-references. Users often ask if there is a way to make Word combine cross-references to captions* without repeating the...
Read more >
LaTeX Tips: Displayed Math
Similarly, if a reference to an equation occurs right after that equation, a number may not be necessary; one could get by saying...
Read more >
p-adic Number -- from Wolfram MathWorld
Yet another application is provided by the Mahler-Lech theorem. Every rational x ... -adic numbers are useful in solving Diophantine equations.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found