I'm relatively inexperienced with LaTex and I'm trying to use \newcommand
in a larger project.
I have several cascaded \newcommand
s that I can't get to behave the way I want:
\renewcommand{\vec}[1]{\boldsymbol{#1}}
\newcommand{\gyrobias}{\vec{b}_{g}}
\newcommand{\est}[1]{\hat#1}
With this I would like to be able to write $\est{\gyrobias}$
to get a hat over a bold b with a subscript g. This almost works, but the hat is over the whole expression, not just b.
A similar question here covers essentially this with the help of \expandafter
. So I tried the following
\newcommand{\est}[1]{\expandafter\hat#1}
But this gives me the following compilation error:
Extra }, or forgotten $ $\est{\gyrobias}
If I add curly braces to \hat{}
, which is what I'm used to, the text compiles fine, but the hat is not moved to where I want it.
\newcommand{\est}[1]{\expandafter\hat{#1}}
Removing the curly braces from \hat
to match the answer in the linked question and defining \gyrobias
without the \vect
gives the desired hat placement, but of course, now the b is no longer bold
\newcommand{\gyrobias}{b_{g}}
\newcommand{\est}[1]{\hat#1}
I suspect I need some more \expandafter
s, but I have no idea where as I don't fully understand what they do.
Edit
I got it to work with a slight modification to @steven-b-segletes answer:
\renewcommand{\vec}[1]{\boldsymbol{#1}}
\newcommand{\est}[1]{\expandafter\hat#1}
\newcommand{\gyrobias}{{\vec{b}}_{g}}
I would now like to achieve the same thing with a couple of slightly more complicated symbols:
\newcommand{\rquat}[2]{{\overline{q}}^{#1}_{#2}}
\newcommand{\camrot}{\rquat{O}{C}}
\newcommand{\pos}[4]{_{#4}\vec{#1}_{#2 #3}}
\newcommand{\trans}[3]{\pos{t}{#1}{#2}{#3}} %\trans{from}{to}{in}
\newcommand{\campos}{\trans{O}{C}{O}}
I want \est{\camrot}
and \est{\campos}
to place the hat only just above the "central letter". Currently it is set over the whole \rquat
/\trans
. Similarly \dot{\camrot}
and \dot{\est{\camrot}}
etc should be placed on top of the central letter only, and not the sub/super-scripts
\expandafter
trick you get\hat\vec{g}_g
which is illegal.\hatgyrobias
command rather