6

I am working a lot with hatted indices such that

\( \hat{\mu} \)

A way to make this quicker would be to define the command.

\newcommand{\hmu}{\hat{\mu}}

but I would need to do this for pretty much every Greek letter separately. Is there a "nice" way to do this in one go? So for example, if I write \hrho it automatically understands \hat{\rho}?

Thanks in advance.

  • Welcome to TeX.SX! You'd have to define all those macros, as TeX would understand \hmu as one control sequence (not \h with argument mu) which has to be defined in order to work. – Skillmon Jul 22 '18 at 17:31
  • Note that \h\rho is only one character longer. In unicode-math, you could alternatively input Greek letters followed by U+0302, the combining hat accent, but this is less convenient to enter at the keyboard. – Davislor Jul 22 '18 at 19:34
8

You can select a character that you don't need in math mode, make it active and define it to be the \hat command. In total you have to type the same number of characters as in your approach with a separate \h command for each Greek letter.

Put the following lines into the preamble of your document to use @ for this purpose.

\begingroup
  \lccode`~=`@
  \lowercase{\endgroup
    \let~\hat
  }%
\mathcode`@="8000

Then $@\alpha$ will produce an alpha with hat, whereas name@gmail.com outside of math-mode will still produce an @ sign. Here is an example.

\documentclass{article}
\begingroup
  \lccode`~=`@
  \lowercase{\endgroup
    \let~\hat
  }%
\mathcode`@="8000
\begin{document}
\( @\alpha@\beta@\mu \)

name@gmail.com
\end{document}

enter image description here

For an explanation, see Defining characters as macros

7

The following makes the definitions for the Greek letters that have a special macro (so not for o or an uppercase alpha) with a little less lines of code (a for loop over all the macro names):

\documentclass[]{article}

\makeatletter
\newcommand*\defhatgreek[1]
  {%
    \expandafter\newcommand\expandafter*\csname h#1\endcsname
      {\hat{\csname #1\endcsname}}%
  }
\@for\zz:=%
  {%
    alpha,beta,gamma,delta,epsilon,varepsilon,zeta,eta,theta,vartheta,kappa,%
    lambda,mu,nu,xi,pi,varpi,rho,varrho,sigma,varsigma,tau,upsilon,phi,varphi,%
    chi,psi,omega,%
    Gamma,Delta,Theta,Lambda,Xi,Pi,Sigma,Upsilon,Phi,Psi,Omega%
  }\do
  {%
    \expandafter\defhatgreek\expandafter{\zz}%
  }
\makeatother

\begin{document}
$\halpha + \hbeta = \hgamma + \hdelta$
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.