# How to redefine \hat{H} as \hat{\cal H}?

I would like to redefine \hat{H} as \hat{\cal H} only for this particular letter (for Hamiltonian symbol in physics). Thank you very much.

-
Welcome to TeX.SX –  Papiro Jul 11 '13 at 12:41
I should note that \cal has been deprecated since 1994 and that \mathcal{H} is the preferred syntax. –  egreg Jul 11 '13 at 12:48
Whether or not it's possible, macro-ing away the syntax in a way such as requested is a bad idea in any language. Ultimately it is just a form of code obfuscation. –  Chris White Jul 11 '13 at 20:16

Well I guess that there are two ways of proceeding here:

• Use the "find and replace" tool of your editor to replace all the matches of \hat{H} by \hat{\mathcal{H}} or

• a bit more appealing... in the preamble of the document define a new command, say \newcommand*{\hham}{\hat{\mathcal{H}}}. Then use the "find and replace" tool of your editor to replace all the matches of \hat{H} by \hham

A MWE would be

\documentclass{article}
\usepackage{amsmath}
\newcommand*{\hham}{\hat{\mathcal{H}}}

\begin{document}

The relation,
$\hham\psi_n = E_n\psi_n$
is valid for every  eigenstate $\psi_n$ of the Hamiltonian $\hham$.
\end{document}


Cheers!

-
Thank you. I have wondered if there is any possibility to do so without having to overwrite all \hat{H} in the document using find/replace tool. But the second way seems quite suitable for me. Thank you again. –  Honza Zubáč Jul 11 '13 at 13:02
@HonzaZubáč: Yes, the newcommands are very helpful to define "structures" that you will use over and over through the document, specially because if you want to change all of them to another symbol, e.g. \mathcal{H} to \mathbb{H}, you need to do it once... at the definition. Changing subject, I'd like to remind you that you are invited to vote up the useful answers, and "check mark" the one you found accurate to your needs. Cheers. –  Dox Jul 11 '13 at 13:12

The easiest way is to define a new command \hatH:

\documentclass{article}
\newcommand*{\hatH}{\hat{\mathcal{H}}}
\begin{document}
$\hatH$
\end{document}


A redefinition of \hat is far more complicate, because of TeX rules in math. \hat expands to \mathaccent that does not parse its base as "argument" but as <math field>. The latter can be a math symbol or a sub formula. This cannot be parsed by a macro argument in a 100% compatible manner.

An approximation is the following example that tests the cases that an H or {H} is following \hat:

\documentclass{article}

\makeatletter
\newcommand*{\old@hat}{}
\let\old@hat\hat
\newcommand*{\hatH}{\old@hat{\mathcal{H}}}
\renewcommand*{\hat}{%
\@ifnextchar\bgroup{%
\new@hat
}{%
\@ifnextchar H{%
\expandafter\hatH\@gobble
}{%
\old@hat
}%
}
}
\newcommand*{\new@hat@H}{H}
\newcommand*{\new@hat@arg}{}
\newcommand*{\new@hat}[1]{%
\renewcommand*{\new@hat@arg}{#1}%
\ifx\new@hat@arg\new@hat@H
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{%
\hatH
}{%
\old@hat{#1}%
}%
}
\makeatother

\begin{document}
$\hatH \hat H \hat{H} \hat x \hat{x} \hat{ab}$
\end{document}


-
Shouldn't \cal H be \mathcal{H}? –  egreg Jul 11 '13 at 12:40
@egreg: \cal H is shorter and it is inside a sub formula, because the curly braces are not argument braces. \hat expands to \mathaccent that expects a <math field> that can be a symbol or a sub formula. –  Heiko Oberdiek Jul 11 '13 at 13:18
Deprecated commands such as \it or \cal are not guaranteed to be in all classes. –  egreg Jul 11 '13 at 13:20
@egreg: Thanks, I have already changed the answer accordingly (and extended with an approximated redefinition of \hat). –  Heiko Oberdiek Jul 11 '13 at 13:23