0

For example, I would like to replace all \hat{N} by \widehat{N}, but leaves \hat{#1} unchanged for #1=anything else.

I wish to implement it at macro level, and not replacing the text in the document, because these replacements are bind to a particular use of package. I don't want to undo the replacement whenever the corresponding package is not used.

Joe Li
  • 165
  • 1
    While this is possible at the macro level, using the editor's search/replace will be much easier. – campa Sep 02 '22 at 10:20
  • I don't know what you mean by other \hat{}. \let\hat\widehat would assign \widehat to \hat meaning from the point of definition onward all \hat would act as \widehat until the end of a document or the next re-definition. You can narrow this effect to a group, e.g. {\let\hat\widehat ...} – Celdor Sep 02 '22 at 10:36
  • @campa Yes, at Macro level, that's what I meant. – Joe Li Sep 02 '22 at 10:42
  • @Celdor Thanks, I update the question a little and hope it makes it clear. – Joe Li Sep 02 '22 at 10:43

1 Answers1

2

You don't want to do it. Notwithstanding that a respected user recommended it, it's not right (at least in my opinion).

Anyway, you can redefine \hat to look for its argument and see whether

  1. it's more than one token (use \widehat)
  2. it's a single uppercase Latin letter (use \widehat)
  3. it's a single command denoting an uppercase Greek letter (use \widehat)

In all other cases use \hat.

\documentclass{article}
\usepackage{amsmath}

\NewCommandCopy{\amsmathhat}{\hat}

\ExplSyntaxOn

\RenewDocumentCommand{\hat}{m} { \joeli_hat:n { #1 } }

\clist_const:Nn \c__joeli_hat_greek_clist { \Gamma,\Delta,\Theta,\Kappa,\Lambda,\Xi,\Pi,\Sigma,\Upsilon,\Phi,\Omega }

\cs_new_protected:Nn \joeli_hat:n { \tl_if_single:nTF { #1 } {% just one token __joeli_hat_single:N #1 } { \widehat{#1} } }

\cs_new_protected:Nn __joeli_hat_single:N { \token_if_cs:NTF #1 { __joeli_hat_greek:N #1 } { \str_if_eq:eeTF { #1 } { \str_uppercase:n { #1 } } { \widehat{#1} } { \amsmathhat{#1} } } }

\cs_new_protected:Nn __joeli_hat_greek:N { \clist_if_in:NnTF \c__joeli_hat_greek_clist { #1 } { \widehat{#1} } { \amsmathhat{#1} } }

\ExplSyntaxOff

\begin{document}

$\hat{a}\hat{A}\hat{b}\hat{B}\hat{\gamma}\hat{\Gamma}\hat{ABC}$

\end{document}

enter image description here

Compare with the standard

enter image description here

egreg
  • 1,121,712
  • If you are referring to me then I must edit my answer, since I definitely do not recommend it. I just showed a way how to do it. – campa Sep 02 '22 at 11:00
  • Thank you both. But what is the reason against this approach? The full story is that, originally I use either \hat or \widehat for many letters. Currently I'm experiencing the font package mtpro2, which in addition comes with \what and \wwhat with the width in between \hat and \widehat. I do not want to modify the document, so that the document works with or without the use of package mtpro2. What is the best practice then? – Joe Li Sep 02 '22 at 11:08
  • @campa Not at all! I saw such a recommendation from Mico. – egreg Sep 02 '22 at 11:58