20

As I discovered through the non-bug latex/4253, TeX has a (mis)feature whereby it ignores braces around a \mathaccent construction: ${a_b}_c$ works, but ${\tilde a_b}_c$ fails with a "Double subscript" error. Braces in {a_b} turn that into an atom, which can receive a second subscript, but braces in {\tilde a_b} have no effect. See a question about why this happens for details.

Since ${{}\tilde a_b}_c$ works fine (TeX keeps the surrounding braces), I am wondering whether defining \tilde as {}\mathaccent... rather than \mathaccent... would be better. Will such a leading brace group have any ill effect on spacing (or other things)? Is there a better definition that would not result in confusing double superscript errors?

EDIT: Several commenters ask me incredulously why anyone would want this. I couldn't come up with a good case for double subscripts, only for double superscripts: the square of the derivative of b is typeset {b'}^2 or b'{}^2. What about the square of the derivative of \tilde{b}?

ways to typeset b tilde prime squared

Here, it is possible to get a decent result with \tilde{b}{{}'}^2 (line marked with a star), but I don't think the input is natural. For an example that makes things worse, say \widetilde denotes the Fourier transform, and that I want to take the Fourier transform of b(t)/t, then take its derivative squared. Naturally I would type {\widetilde{\biggl[\frac{b}{t}\biggr]}'}^2. Instead, I need to type \widetilde{\biggl[\frac{b}{r}\biggr]}{\vphantom{\frac{b}{r}}'}^2 or something like that...

That said, I am mostly interested in knowing if adding {} to the start of the definition of \tilde (and \widetilde) would mess up math spacing.

  • Would it be worth adding the TeXbook context here? I wonder why in the LaTeX bug report Frank feels that {}\tilde is not 'nice'. – Joseph Wright Jun 06 '13 at 07:19
  • @HendrikVogt I agree that this is typically not going to look nice, and that may be a reason for Knuth to make TeX behave that way, who knows. I can probably think up a case where it could be more natural to type things in this way, though, and as you and others commented in the question you link to, one would not expect TeX to produce an error there. – Bruno Le Floch Jun 06 '13 at 09:25
  • I can't understand what ${a_b}_c$ or ${{}\tilde{a_b}}_c$ should mean, to begin with, and how a reader should parse the result. – egreg Jun 06 '13 at 09:48
  • @Bruno: It would be great if you could think up such a case and add it to the question! (I've deleted my previous comment as it was obsolete.) – Hendrik Vogt Jun 06 '13 at 10:12
  • 2
    how does this differ in meaning from $\tilde a_{b_c}$? (it does differ in appearance.) – barbara beeton Jun 06 '13 at 12:57
  • @HendrikVogt The specific case I had in the question is not sensible, but with ' being a superscript, it is easy to encounter cases of double superscripts with variations of b'^2. – Bruno Le Floch Jun 07 '13 at 08:56
  • \tilde{b}'^2 works and this is, AFAIK, the normal way to typeset derivatives squared; {\tilde{b}'}^2 doesn't work, but even if it did, the result would give a "prime squared" that's meaningless (sixth line in your example). – egreg Jun 07 '13 at 09:05
  • @egreg Ah, \tilde{b}'^2 simply works because ' does some magic that I had forgotten. This would fail with \tilde{b}'_i^2, which could easily happen if b is a vector (of course, interchanging subscript and superscript works but is very far from the semantics). I disagree that you would get "prime-squared": what is squared is the whole derivative, which is meaningful. – Bruno Le Floch Jun 07 '13 at 09:50
  • @Bruno: The \tilde{b}'_i^2 is the only example so far that I might find convincing; I didn't have time to think about it yet. By the way, I agree with egreg about the "prime squared": in the sixth line of your example, it clearly looks as if the prime is squared (even if the TeX markup speaks diffenrently). A simple \tilde{b}'^2 is much better. – Hendrik Vogt Jun 07 '13 at 20:09

1 Answers1

1

This revised solution addresses the requirement to be able to scale the result to different math style sizes. To do that, features of the scalerel package are employed to import the current math style into the revised definition of \tilde.

And, as with the original solution, it provides the desired syntax and does it without frontloading the \tilde with a {}, and it readily takes subsequent sub and superscripts. A key feature of this solution is that the revised definition starts with the original \tilde definition, and ends on the argument of the \tilde, so that it should behave relative to adjacent math matter in expected ways.

\documentclass[]{article}%
\usepackage{scalerel}
\let\savetilde\tilde
\def\tilde#1{%
 \ThisStyle{\savetilde{\phantom{\SavedStyle#1}}%
  \setbox0=\hbox{$\SavedStyle#1$}\kern-\wd0#1}}
\parskip .8ex
\begin{document}
${\tilde a_b}_c$\par
${\tilde{b}'}^2$ No BOOM!\par
$\tilde b'^2$ Natural input\par
$(\tilde{b}')^2$ correct, per tohecz\par
$\scriptstyle{\tilde a_b}_c$\par
$\scriptstyle{\tilde{b}'}^2$ No BOOM!\par
$\scriptstyle\tilde b'^2$ Natural input\par
$\scriptstyle(\tilde{b}')^2$ correct, per tohecz\par
$\scriptscriptstyle{\tilde a_b}_c$\par
$\scriptscriptstyle{\tilde{b}'}^2$ No BOOM!\par
$\scriptscriptstyle\tilde b'^2$ Natural input\par
$\scriptscriptstyle(\tilde{b}')^2$ correct, per tohecz\par
Would otherwise cause tilde errors:\par
${\tilde{a}^b}^c$
${{\tilde{a}}^b}^c$
\end{document}

enter image description here

The last line in the above MWE were given by egreg as examples of what breaks with \tilde at Double superscript error involving tilde. They work with the current redefinition.


ORIGINAL SOLUTION (does not scale with smaller math styles) It provides the desired syntax; it does it without frontloading the \tilde with a {}, and it readily takes subsequent sub and superscripts.

\documentclass[]{article}%
\usepackage{stackengine}
\stackMath
\let\svtilde\tilde
\def\tilde#1{\stackengine{0pt}{}{\svtilde{\phantom{#1}}}{O}{l}{F}{T}{L}#1%
}
\parskip 1ex
\begin{document}
${\tilde a_b}_c$\par
${\tilde{b}'}^2$ No BOOM!\par
$\tilde b'^2$ Natural input\par
$(\tilde{b}')^2$ correct, per tohecz
\end{document}

enter image description here

This answer is the same approach I took at A simple solution to using widebar on subscripted symbol with small space with the subscript

  • Of course, the correct thing to do is (\tilde{b}')^2. – yo' Mar 01 '14 at 22:03
  • @tohecz This question is more related to \mathaccent handling of TeX instead of math notation I think. They are probably examples for L3 code development. – percusse Mar 01 '14 at 22:16