Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm using pdftex and I want to define a command for the typesetting of quantum mechanical operators that produces upright letters with a \hat accent. The problem is that the obvious solution

\newcommand*{\op}[1]{{\hat{\mathrm{#1}}}}

does only work for roman letters but I also use at least one operator that should be represented by a greek letter for which the \mathrm command produces rubbish output. So it would be nice to define a command that distinguishes between greek and roman letters via an if statement. Since I have very little experience with TeX I don't really know how to tackle this problem.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

As long as you strictly use one token in the argument of \op, here is a way:

\DeclareRobustCommand{\op}[1]{\hat{\ifcat#1\relax#1\else\mathrm{#1}\fi}}

The command is robust because it would break if used in captions. Use it only as

\op{A} \op{x} \op{\Gamma} \op{\beta}

You might consider also to add \mathop:

\DeclareRobustCommand{\op}[1]{%
  \mathop{\hat{\ifcat#1\relax#1\else\mathrm{#1}\fi}}\nolimits}

that will provide correct spacing of the symbol used as an operator.

share|improve this answer
    
Wow, that was quick. Thank you very much. This really solves my problem. I go with the first alternative since the \mathop command produces wrong output, when used with indices such as \op{H}_{i}. –  Philipp Jul 24 '11 at 17:13
    
Sorry, I forgot a key command. –  egreg Jul 24 '11 at 17:13
    
Thanks again. Now it works perfectly. –  Philipp Jul 24 '11 at 17:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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