8

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.

8

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

  • Seems to work perfectly and I've not yet found side effects. Thank you! – jan Oct 1 '13 at 10:28
  • @jan I am glad it helps, and I hope you won't go through side effects, but you never know... ;) – Ludovic C. Oct 1 '13 at 10:32
  • Unfortunately I found the first. Doing this on a vector which is – jan Oct 1 '13 at 12:53
  • 1
    That went out incomplete, some symbols are declared as vectors, i.e. \newcommand{\Rcvbits}{\boldsymbol{b_r}}, when applying the macro to those (\Test{\RcvBits}) the hat is placed in front of the first symbol and not over it. – jan Oct 2 '13 at 11:48
  • 1
    @jan Should the hat be bold too? – Ludovic C. Oct 2 '13 at 15:40
1

The quick and dirty way would be to separate the symbol from the sub-script

\newcommand{\Est}[2]{\hat{#1}_{#2}}
  • 1
    But that would still exclude using $\Est{\DNoise}$ ? The problem is that the document is containing expressions like this (also with different arguments than \DNoise) multiple times and fixing \Est would be easier that searching and replacing every single occurence. – jan Oct 1 '13 at 9:09

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.