Katex: support \tag

Created on 20 Sep 2017  路  9Comments  路  Source: KaTeX/KaTeX

Suggested as a precursor to full equation numbering support in #350, it has AFAICT the following behavior:

  • add a \tag command will force the math to typeset in display mode but respects the current style
  • only one \tag command is allowed per math block
  • the content of the \tag is typeset in text mode (not sure if entering math mode is allowed or not, my assumption would be not)
  • the content of the \tag is right aligned and typset within parens, e.g. \tag{3.1c} yields (3.1c)
enhancement

All 9 comments

  • I've been unable to get \tag to work in inline math mode (both $...$ and \(...\)). I get ! Package amsmath Error: \tag not allowed here. So I think it makes sense to raise a similar error in KaTeX. (Can't imagine how the styling would make sense in inline mode.)
  • I also found that \tag doesn't work in $$...$$ (same error), only \[...\]. Weird. I kind of understand why, but I don't think we can/need to inherit the same limitation.
  • Repeated \tag indeed produces ! Package amsmath Error: Multiple \tag.
  • You can definitely enter math mode within the \tag argument. Indeed, \tag{$\star$} is common form. But we support that already, if we parse \tag like \text.
  • \tag* omits the the parens, so we should probably support that too.

One challenge here is getting the CSS layout right. To implement \hfill <displaymath> \hfill <tag>, I think we need flexbox, right?

Another challenge is carrying the \tag, which can appear anywhere in the equation, to the end. Variable carrying is somewhat related to \setlength (#687), though at least with \tag the setting is global.

\Huge
\[a + b\] \\
\[a + b \tag{c}\]

produces
screen shot 2017-09-20 at 9 06 28 am
which leads me to believe that we can absolutely position the tag to the right. \hfill <displaymath> \hfill <tag> I think would have a + b position slightly off center iiuc.

For what it's worth, I would argue that the equation number is not part of the math typesetting at all, and could simply produce an additional HTML element within katex-display. That way, the tag can easily be styled and positioned without the risk of throwing the math off.

Besides, it would be nice if the tags could function as anchors in some automatic way.

@kevinbarabash Oh, right -- that's convenient! Maybe a float would be better (with a clear afterward), so that we can push it to the next line on overfill?

\[\tag{x+y+$x+y$+x+y+x} x+y+x+y+x+y+x+y+x+y+x+y+x+y+x+y+x+y+x+y+x+y \]

xpy

@ntessore Yes, of course we can use a class like katex-tag that enables the user to restyle as they desire. But I'd like to have a good default formatting, like we do for katex-display.

@edemaine finally a legit use of float ^_^

I did some experiments with this. Unfortunately, there seems to be no perfect answer...

  1. float: right leads to bad vertical alignment: floats are always pushed as high as possible, so we can't align them to the baseline.
  2. position: absolute and position: relative can cause tag to overlap with a wide formula (no go-to-second-line behavior), and I don't see how to get it to vertically align to the baseline while also doing some horizontal centering.
  3. Flexbox seems like the best bet, though it won't do LaTeX's horizontal centering as @kevinbarabash found -- the best we can do is the equivalent of \hfill <equation> \hfill <tag>. (I tried putting an invisible copy of the tag on the left side, but there are circumstances where that doesn't do the right horizontal spacing.)

Option 3 seems the best compromise, though -- for short tags, it's only slightly off in horizontal spacing.

Here are two screenshots of Option 3 at different screen widths, showing the move-tag-to-next-line behavior possible with flexbox:

tag_wide
tag_not

which leads me to believe that we can absolutely position the tag to the right.

In some document class, we can specify leqno option to place tags (and equation numberings) on the left.

For example,

\documentclass[leqno]{article}
\usepackage{amsmath,amssymb}
\pagestyle{empty}
\begin{document}
\[
 \int_{-\infty}^{\infty} e^{-x^{2}} \mathrm{d}x = \sqrt{\pi} \tag{\ensuremath{\star}}
\]
\end{document}

results in:

2017-10-19 17 55 30

So, it might be appropriate to provide the option to specify tag/numbering position.

\tag and \tag* are now on master branch, and 0.10.0-alpha.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shaunc picture shaunc  路  4Comments

mbourne picture mbourne  路  3Comments

OisinMoran picture OisinMoran  路  4Comments

pvnr0082t picture pvnr0082t  路  4Comments

asmeurer picture asmeurer  路  3Comments