5

I'm relatively inexperienced with LaTex and I'm trying to use \newcommand in a larger project. I have several cascaded \newcommands 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 \expandafters, 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

  • Welcome to TeX.SX! With the \expandafter trick you get \hat\vec{g}_g which is illegal. – egreg Oct 27 '15 at 10:01
  • I would use a special \hatgyrobias command rather – user31729 Oct 27 '15 at 10:09
  • That would probably the quickest way to achieve the desired result. However, I was hoping to get something modular that would allow reuse and changing of individual parts – Nicolas Oct 27 '15 at 12:40
4

The key was to set your redefinition of \vec in its own group (as denoted with the extra set of braces).

\documentclass{article}
\usepackage{bm}
\renewcommand{\vec}[1]{{\boldsymbol{#1}}}
\newcommand{\gyrobias}{\vec{b}_{g}}
\newcommand{\est}[1]{\hat#1}
\begin{document}
$\est{\gyrobias}$
\end{document}

enter image description here

If this approach does bad things to \vec in some other way, one could always apply the extra group to the definition of \gyrobias, instead:

\documentclass{article}
\usepackage{bm}
\renewcommand{\vec}[1]{\boldsymbol{#1}}
\newcommand{\gyrobias}{{\vec{b}}_{g}}
\newcommand{\est}[1]{\hat#1}
\begin{document}
$\est{\gyrobias}$
\end{document}

FOLLOW UP

With this follow up, I address the issue of getting a \hat or \dot over the intended letter, using the syntax of the OP. However, it does not address the issue of multiple diacritics over the same letter, as in \dot{\est{\camrot}}.

\documentclass{article}
\usepackage{bm}
\renewcommand{\vec}[1]{\boldsymbol{#1}}
\newcommand{\gyrobias}{{\vec{b}}_{g}}
\newcommand{\rquat}[2]{{\overline{q}}^{#1}_{#2}}
\newcommand{\camrot}{\rquat{O}{C}}
\newcommand{\pos}[5][]{{_{#5}}#1{\vec{#2}}_{#3 #4}}
\newcommand{\trans}[4][]{\pos[#1]{t}{#2}{#3}{#4}} %\trans{from}{to}{in}
\newcommand{\campos}[1][]{\trans[#1]{O}{C}{O}}
\newcommand{\est}[1]{\applydiacritic{\hat}{#1}}
\newcommand{\Dot}[1]{\applydiacritic{\dot}{#1}}
\newcommand\applydiacritic[2]{\ifx\campos#2\campos[#1]\else\expandafter#1#2\fi}
\begin{document}
$\est{\gyrobias}$\medskip

$\camrot\quad\est{\camrot}\quad\Dot{\camrot}$\medskip

$\campos\quad\est{\campos}\quad\Dot{\campos}$\medskip
\end{document}

enter image description here

The OP points out that the above code does not work with amsmath. Here is a fix for that, though I note that amsmath package affects the placement of diacritical marks over the letter "t", and I have verified that it is not related to my solution here, which can be seen in the first line of my output (when amsmath is inserted/removed).

\documentclass{article}
\usepackage{bm}
\usepackage{amsmath}% THIS PACKAGE EFFECTS PLACEMENT OF DIACRITICS OVER \boldsymbol{t}
\renewcommand{\vec}[1]{\boldsymbol{#1}}
\newcommand{\gyrobias}[1][]{#1{\vec{b}}_{g}}
\newcommand{\rquat}[3][]{#1{\overline{q}}^{#2}_{#3}}
\newcommand{\camrot}[1][]{\rquat[#1]{O}{C}}
\newcommand{\pos}[5][]{{_{#5}}#1{\vec{#2}}_{#3 #4}}
\newcommand{\trans}[4][]{\pos[#1]{t}{#2}{#3}{#4}} %\trans{from}{to}{in}
\newcommand{\campos}[1][]{\trans[#1]{O}{C}{O}}
\newcommand{\est}[1]{\applydiacritic{\hat}{#1}}
\newcommand{\mydot}[1]{\applydiacritic{\dot}{#1}}
\newcommand\applydiacritic[2]{%
  \ifx\gyrobias#2\gyrobias[#1]\else
  \ifx\campos#2\campos[#1]\else
  \ifx\camrot#2\camrot[#1]\else
  #1#2\fi\fi\fi}
\begin{document}
$\boldsymbol{t}\quad\hat{\boldsymbol t}\quad\dot{\boldsymbol t}$\medskip

$\gyrobias\quad\est{\gyrobias}\quad\mydot{\gyrobias}$\medskip

$\camrot\quad\est{\camrot}\quad\mydot{\camrot}$\medskip

$\campos\quad\est{\campos}\quad\mydot{\campos}$\medskip
\end{document}

enter image description here

  • @HarishKumar WIthout any package, \boldsymbol was undefined. The OP didn't tell me where to find it, so I searched the site, and answers using \boldsymbol loaded bm. If there is a preferred way, please let me know. – Steven B. Segletes Oct 27 '15 at 10:38
  • Both do the same with bm package. But one can directly use \bm{#1} instead. – user11232 Oct 27 '15 at 10:40
  • @HarishKumar I see. Thank you for the tip. I do note that \boldsymbol is a bit more descriptive than \bm, however. – Steven B. Segletes Oct 27 '15 at 10:45
  • Thank you for the fast response. I got it working with your second suggestion, but I had to modify the \est to \newcommand{\est}[1]{\expandafter\hat#1}. Now I have a slightly more complicated case that does not seem to work with this method. (See my edit) Can you assist me with this as well? – Nicolas Oct 27 '15 at 12:38
  • @Nicolas Please see my follow-up. It address one of your issues, but not the one of multiple diacritics. – Steven B. Segletes Oct 27 '15 at 13:26
2

I would suggest a different strategy: redefine \vec to have a *-variant for the hat form.

\documentclass{article}
\usepackage{amsmath,bm,xparse}

\RenewDocumentCommand{\vec}{sm}{%
  \IfBooleanTF{#1}
    {\hat{\bm{#2}}}
    {\bm{#2}}% 
}
\NewDocumentCommand{\gyrobias}{s}{%
  \IfBooleanTF{#1}
    {\vec*{b}_{g}}
    {\vec{b}_{g}}%
}

\begin{document}

$\vec{x}_{g}+\vec*{x}_{g}+\gyrobias+\gyrobias*$

\end{document}

enter image description here

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.