4

I wish to define a new macro, \vartilde say, so that \vartilde{a} produces the following:

  • \tilde{a} while in inline maths mode, ie using $ ... $;
  • \widetilde{a} while in display maths mode, eg using \[ ... \].

Put another way, I'd like

$\vartilde{a} = \tilde{a}$ and \[ \vartilde{a} = \widetilde{a} \].

One could think of this as a variable width tilde.

Sam OT
  • 1,329
  • redefining \tilde especially with a text component isn't the best idea in the world. for some reasons, see Textual substitution of macros in a LaTeX file. on the other hand \vartilde isn't unreasonable. – barbara beeton Dec 18 '17 at 18:16
  • It's not clear: \tilde is not legal in text mode. – egreg Dec 18 '17 at 18:46
  • @barbarabeeton ok, thanks -- \vartilde isn't exactly difficult to write! – Sam OT Dec 18 '17 at 20:07
  • @egreg yes, fair enough, it's not 100% clear. Further, Mico has misinterpreted it also, so while it seems obvious to me it appears that you're not alone in your confusion! (clear to me as I know what I meant! =P) -- let me edit... – Sam OT Dec 18 '17 at 20:08
  • Thanks for your comment. If you see in my OP, I'm using $ symbols. I didn't know about using \( and \) -- although it is a very natural counterpart to \[ and \]. There is, of course, no harm to changing to \(...\) if necessary! – Sam OT Dec 18 '17 at 21:15

3 Answers3

5

(I rewrote this answer completely after the OP clarified the posting's objective.)

Assuming you use \( and \) (rather than $) to initiate and terminate inline-math mode, you could achieve your objective by (a) patching these two commands so that a toggle is set to either true or false, respectively, and (b) defining the \vartilde macro so that it invokes either \tilde or \widetilde depending on whether the toggle is true or not.

enter image description here

\documentclass{article}
\newif\ifinlinemath\inlinemathfalse % initialize to "false"
\usepackage{etoolbox} % for \pretocmd and \apptocmd macros
\pretocmd{\(}{\inlinemathtrue}{}{}
\apptocmd{\)}{\inlinemathfalse}{}{}
\newcommand{\vartilde}[1]{\ifinlinemath\tilde{#1}\else\widetilde{#1}\fi}

\begin{document}
\centering
\( \tilde{X} \quad \vartilde{X} \quad \widetilde{X} \)

\[ \tilde{X} \quad \vartilde{X} \quad \widetilde{X} \]

\( \tilde{X} \quad \vartilde{X} \quad \widetilde{X} \)
\end{document}

Two asides:

  • I'm not knowledgeable enough to figure out how to do this if $, rather than \( and \), is used to initiate and terminate inline math mode.

  • Speaking for myself, I think that using different tilde styles for one and the same variable, depending only on whether the material occurs in inline math mode or display math mode, is bound to utterly confuse your readers. (Expecting your readers to appreciate the difference between inline and display math mode might be a tad unrealistic...) I certainly wouldn't do this in my own documents.


Addendum, to address the OP's comment that using \widetilde for inline math material "an push the line down". AFAICT, this can only happen if you have an unusually small value for \baselineskip. For most Roman fonts -- blackletter fonts, such as any Fraktur-type text font, are a different matter -- the baselineskip is usually set to 120% of the nominal font size. E.g., if 10pt is the nominal font size and Computer Modern is in use, the baselineskip is set to 12pt. This should be more than enough to keep TeX from widening the interline spacing when either \widetilde or \widehat are used in conjunction with uppercase letters.

In the following screenshot, the left-hand column uses no accents, the middle column uses \tilde and \hat, and the right-hand column uses \widetilde and \widehat. AFAICT, there's no difference in the line spacing; in particular, the "wide" accents didn't modify the line spacing.

enter image description here

\documentclass{article}
\usepackage{multicol}
\newcommand\blurb{Once upon a time, there was \dots\ }
\begin{document}
\begin{multicols}{3}
\blurb $X$ $X$ $X$ $X$ \blurb $X$ $X$ $X$ $X$ \blurb $X$ $X$ $X$ $X$ \blurb $X$ $X$ $X$ $X$ \blurb $X$ $X$ $X$ $X$ \blurb $X$ $X$ $X$ $X$ 

\blurb $\tilde{X}$ $\tilde{X}$ $\tilde{X}$ $\tilde{X}$ \blurb $\tilde{X}$ $\tilde{X}$ $\tilde{X}$ $\tilde{X}$ \blurb $\tilde{X}$ $\tilde{X}$ $\tilde{X}$ $\tilde{X}$ \blurb $\tilde{X}$ $\tilde{X}$ $\tilde{X}$ $\tilde{X}$ \blurb $\tilde{X}$ $\tilde{X}$ $\tilde{X}$ $\tilde{X}$ \blurb $\tilde{X}$ $\tilde{X}$ $\tilde{X}$ $\tilde{X}$ 

\blurb $\widetilde{X}$ $\widetilde{X}$ $\widetilde{X}$ $\widetilde{X}$ \blurb $\widetilde{X}$ $\widetilde{X}$ $\widetilde{X}$ $\widetilde{X}$ \blurb $\widetilde{X}$ $\widetilde{X}$ $\widetilde{X}$ $\widetilde{X}$ \blurb $\widetilde{X}$ $\widetilde{X}$ $\widetilde{X}$ $\widetilde{X}$ \blurb $\widetilde{X}$ $\widetilde{X}$ $\widetilde{X}$ $\widetilde{X}$ \blurb $\widetilde{X}$ $\widetilde{X}$ $\widetilde{X}$ $\widetilde{X}$ 
\end{multicols}

\begin{multicols}{3}
\blurb $X$ $X$ $X$ $X$ \blurb $X$ $X$ $X$ $X$ \blurb $X$ $X$ $X$ $X$ \blurb $X$ $X$ $X$ $X$ \blurb $X$ $X$ $X$ $X$ \blurb $X$ $X$ $X$ $X$ 

\blurb $\hat{X}$ $\hat{X}$ $\hat{X}$ $\hat{X}$ \blurb $\hat{X}$ $\hat{X}$ $\hat{X}$ $\hat{X}$ \blurb $\hat{X}$ $\hat{X}$ $\hat{X}$ $\hat{X}$ \blurb $\hat{X}$ $\hat{X}$ $\hat{X}$ $\hat{X}$ \blurb $\hat{X}$ $\hat{X}$ $\hat{X}$ $\hat{X}$ \blurb $\hat{X}$ $\hat{X}$ $\hat{X}$ $\hat{X}$ 

\blurb $\widehat{X}$ $\widehat{X}$ $\widehat{X}$ $\widehat{X}$ \blurb $\widehat{X}$ $\widehat{X}$ $\widehat{X}$ $\widehat{X}$ \blurb $\widehat{X}$ $\widehat{X}$ $\widehat{X}$ $\widehat{X}$ \blurb $\widehat{X}$ $\widehat{X}$ $\widehat{X}$ $\widehat{X}$ \blurb $\widehat{X}$ $\widehat{X}$ $\widehat{X}$ $\widehat{X}$ \blurb $\widehat{X}$ $\widehat{X}$ $\widehat{X}$ $\widehat{X}$ 
\end{multicols}
\end{document}
Mico
  • 506,678
  • Apologies for the ambiguous question, but this isn't what I meant. I want to use an if statement to say "if display maths" or "if inline maths". See the edit to my OP. Apologies again – Sam OT Dec 18 '17 at 20:12
  • @SamT - I've completely rewritten my answer. – Mico Dec 18 '17 at 21:08
  • Thank you very much for this solution, and thank you for your final aside. (I am only a PhD student, yet to publish, so I appreciate insight from experienced people!) My reason for wanting to try this is the following. The standard \tilde is too small, I feel, particularly when used inline. However, when \widetilde is used inline, it either increases the whitespace between its line and the line above (which then looks very bad, I feel), or the tilde is almost closer to the line above than the symbol it is 'accenting'. Given this comment, do you still feel it would be unhelpful? Thanks :) – Sam OT Dec 18 '17 at 21:20
  • @SamT - My rule of thumb is to use \tilde with lowercase-letter variables and \widetilde with uppercase-letter variables, especially if they're wider than average. E.g., I'd write \tilde{x} and \widetilde{X}, but also \tilde{I}. Same rule for \hat and \widehat: \hat{\sigma} and \widehat{\Sigma}. However, this is just a rule of thumb, and I don't claim that it's based on some grand optimization of a multi-dimensional typographic objective function. Plus, I'm sure fervent testimonies can be obtained from satisfied writers who'd always prefer \tilde{X} to \widetilde{X}... – Mico Dec 18 '17 at 21:26
  • 1
    That is an interesting point of view, thank you. Personally I just hate the fact that \widetilde inline can push the line down, so always look to use \tilde inline. Thank you for your comments, and for your answer on how to implement: even though I won't actually implement it now, I'm always looking to improve my TeX knowledge, so I'm genuinely grateful! :) – Sam OT Dec 18 '17 at 21:30
  • @SamT - I've posted an addendum to (gently, hopefully) question your claim that using \widetilde on uppercase-letter variable names in inline mode somehow increases the line spacing. This claim should only ever be true if the value of baselineskip is unusually small (in which case there will be lots of other problems with the document...). – Mico Dec 18 '17 at 21:49
  • (Sorry, only just seen this!) Interesting point. However, I've just tested it, and if I have \tfrac12 (or \frac12) on the previous line, then using \widetilde does increase the vertical linespacing: however, \tilde does too, just not by so much. \ Note also in your example at one point how close the \widetilde is to the comma above it; it's not as bad with the usual \tilde. – Sam OT Dec 24 '17 at 17:45
  • @SamT - It looks like you're trying to shift the conversation by introducing new evidence which wasn't available so far. (Up until now, you only mentioned accents placed above symbols.) I believe my earlier main recommendation -- not to switch between \tilde and \widetilde depending on whether the variable ("X", say) occurs in display or text situations -- is still appropriate. For fractional expressions, there is a well-known, clear-cut recommendation: don't write \frac{a}{b}; instead, use "inline fraction notation", i.e., a/b. You can look this up in the TeXbook if you wish. – Mico Dec 24 '17 at 23:19
  • @SamT - Regarding your other comment, about the proximity of the widetilde accent to a comma in the line above it: If this is a real concern in your document, I can only suggest that you increase the baselineskip slightly. E.g., if the main font size is 10pt and the default baselineskip is 12pt, consider increasing it to 13pt. This is not just a theoretical recommendation; this was exactly what was done for the math textbook "Concrete Mathematics", by Graham, Knuth, and Patashnik. I continue to believe that switching from widetilde to tilde for inline math material would be a mistake. – Mico Dec 25 '17 at 01:32
  • Thanks for your response :) -- I don't really understand what you mean "by shifting the conversation" though: I was talking about documents in general, and previous lines aren't necessarily just text or above-accents. I don't know what the TeXbook is, but could you give me a reference to your recommendation for inline fractions? Personally, I find them aesthetically very unappealing, but I'm interested in other people's views! Similarly with increasing the baselineskip: I find this gives an unpleasant look, but others may not – Sam OT Dec 26 '17 at 16:34
  • (Incidentally, I do still agree with your recommendation of not swapping between wide and standard tilde, as I mentioned on the other answers. I am appreciative for your answer, still, as it helps me learn more about TeX so in the future I can do these things myself without having to call upon the experts on here! :) – Sam OT Dec 26 '17 at 16:35
  • @SamT - The TeXbook is the title of a book (about TeX — surprise!) by Donald E. Knuth. “Who is Donald E. Knuth?”, I can here you ask. The creator of TeX, as well as an all-around seriously famous mathematician and math educator. Any decent library should have a copy of this book. Check out, in particular, the chapters about math typesetting. – Mico Dec 26 '17 at 17:50
5

I'm not sure it's a good idea.

\documentclass{article}
\usepackage{amsmath}

\newcommand{\vartilde}[1]{\mathpalette\dovartilde{#1}}
\newcommand{\dovartilde}[2]{%
  \ifx#1\displaystyle\widetilde{#2}\else\tilde{#2}\fi
}

\begin{document}

\begin{center}% just to get it above the other one
$\vartilde{a}$
\end{center}
\[
\vartilde{a}
\]

\end{document}

enter image description here

See The mysteries of \mathpalette for more information about \mathpalette.

egreg
  • 1,121,712
  • Thanks very much, that's exactly what I was looking for -- I thought it would be something to do with if displaystyle, but didn't know how to do it. – Sam OT Dec 18 '17 at 21:20
  • Could you elaborate on why you feel it's not a good idea? (I am only a PhD student, yet to publish, so I appreciate insight from experienced people!) My reason for wanting to try this is the following. The standard \tilde is too small, I feel, particularly when used inline. However, when \widetilde is used inline, it either increases the whitespace between its line and the line above (which then looks very bad, I feel), or the tilde is almost closer to the line above than the symbol it is 'accenting'. Given this comment, do you still feel it would be unhelpful? Thanks :) – Sam OT Dec 18 '17 at 21:21
  • 1
    @SamT Beginners tend to overuse \widetilde (and \longrightarrow too), feeling that \tilde is “too short”. It isn't; to the contrary, \widetilde is often too big (like in the picture above). Your readers will be surprised seeing different tildes for the same letter. – egreg Dec 18 '17 at 21:22
  • So, ironically, I don't have much of an issue with it -- maybe I'd prefer it slightly longer, but the disadvantage caused by moving the linespacing I feel is major -- but my supervisor changed all the \tildes in my document to \widetilde. Thank you for your comments – Sam OT Dec 18 '17 at 21:24
  • 1
    @SamT Somebody gets attached to their own ideas when young… ;-) – egreg Dec 18 '17 at 21:25
  • Haha yeah =P -- thank you for your advice. Given then consensus of answers by extremely experienced people has been to not implement this, I shall follow that advice. Nevertheless, I am always trying to learn more about how TeX works -- and you in particular have been very helpful in my learning -- so I am genuinely appreciative of your explanation of how to implement this (as well as your advising me not to!) :) – Sam OT Dec 18 '17 at 21:27
4

Your conditional setting can be achieved using \mathchoice, which easily distinguishes between math in the various (four) styles:

enter image description here

\documentclass{article}

\newcommand{\vartilde}[1]{
  \mathchoice
    {\widetilde{#1}}% \displaystyle
    {\tilde{#1}}% \textsyle
    {\tilde{#1}}% \scriptstyle
    {\tilde{#1}}% \scriptscriptstyle
}

\begin{document}

Reference: $\tilde{X}\ \widetilde{X}$

Consider
\[
  \tilde{X} \quad \vartilde{X} \quad \widetilde{X}
\]
where $\vartilde{X}$ changes with the math style.

\end{document}

Note how the above example also shows the drawback of using the varied display. The symbol (or notation) doesn't remain the same when used in the different styles next to one another, which might not be ideal.

Werner
  • 603,163
  • Thanks very much. I wouldn't use \tilde and \vartilde in display mode. The idea would be to only use \vartilde. – Sam OT Dec 18 '17 at 21:25
  • Given that both egreg and Mico, who each have an enormous amount of experience, have recommended against doing using such a variation, I think I'll probably not implement it. Thank you for your answer, however. I'm always trying to learn more about TeX, so I am genuinely appreciative :) – Sam OT Dec 18 '17 at 21:25
  • 1
    @SamT: They're both presented as reference. – Werner Dec 18 '17 at 21:26