[Tex/LaTex] Putting \hat over first character of subscripted variable

macrosmath-modesubscripts

I'm using a set of commands to define abstractions of mathematical notations that often occur during my thesis such as:

\newcommand{\DNoise}{n_d}

which would equal some distortion noise. This allows me to quickly change the notation throughout the document with just a change in one place and has proven invaluable so far. Now there is another command

\newcommand{\Est}[1]{\hat{#1}}

which is supposed to put a hat over another symbol to denote it's estimated. Using this with single symbols is all fine but when using it with symbols that have a subscript, the following happens:

Problem

The left one is obtained by \Est{\DNoise} and the right one by \Est{n}_d which is possible but requires to break the pattern used throughout the document. The question is if there is a way to redefine \Est so that it produces the left result even when passed a variable with subscript.

Best Answer

A way to go is

\documentclass{article}

\usepackage{amsmath}

\newcommand{\DNoise}{n_d}

\newcommand{\Est}[1]{\hat{#1}}
\newcommand{\Test}[1]{\expandafter\hat#1}    

\begin{document}

$ \DNoise, \Est{\DNoise}, \Test{\DNoise}$
\end{document}

This produces this output

Output

Related Question