How do you put a hat(^) symbol over a letter in LaTeX?

Symbol Hat
Type of symbol Mathematics
Package (requirement) No
Argument Yes, x
Latex command \hat{x}
Example \hat{o} → ô

Not only mathematically, but hat symbol is also used in different languages. For this, there is by default \hat{} a command in latex. For example, you can see the use of this symbol in the case of a unit vector.

\documentclass{article}
\begin{document}
$$\hat{r} = \frac{\vec{r}}{\mid \vec{r} \mid}$$
$$\hat{v} = \frac{\textbf{v}}{\mid \textbf{v} \mid}$$
$$\hat{i}, \hat{j}, \hat{k}$$
\end{document}

Output :

Since the unit vector is a topic of physics. So, \vu{arg} macros are being created for the unit vector in the physics package. This \vu{arg} command is considered to be the short form of the \vectorunit command.

\documentclass{article}
\usepackage{physics}
\begin{document}
$$\vu{r} = \cos\theta \vu{i} + \sin\theta \vu{j}$$
$$\vu{r}_x = \cos\theta \vu{i}, \vu{r}_y = \sin\theta \vu{j}$$
$$\abs{\vu{r}} = \sqrt{\cos^2\theta + \sin^2\theta} = 1$$
$$\left ( \hat{i},\hat{j},\hat{k} \right )$$
\end{document}

Output :

Center the hat symbol above the letter with subscript

You must write the syntax correctly to use hat symbols and subscripts together. If you write like this \hat{U_i}, the hat symbol will not be in the center of the letter in the output. To print the hat symbol in the center you should write it like this \hat{U}_i. Same format for superscript. take a look

\documentclass{article}
\usepackage{MnSymbol}
\begin{document}
$$\verb|\hat{U_i}|\longrightarrow \hat{U_i}$$
$$\verb|\hat{U}_i|\longrightarrow \hat{U}_i$$
$$\verb|\hat{M^n}|\longrightarrow \hat{M^n}$$
$$\verb|\hat{M}_n|\longrightarrow \hat{M}^n$$
\end{document}

Output :