2

In my preamble I have:

\newcommand{\myvecb}[1]{\vb{#1}}

Suppose in some math context I use:

\myvecb{F}_y

while in some other I have:

\myvecb{F_T}

My problem

I have noticed that in the future I may want to change all \myvecb{F}_y to \myvecb{F_y} (or, in some other context, perhaps vice versa). Instead of doing everything manually (because a simple regex replace may too complicated for some reason), is it possible to define \myvecb command definition such that I can manipulate the stated-problem within the definition?

  • 2
    Yes. \makeatletter \newcommand*\myvecb[1]{\@ifnextchar_{\myvecbaux{#1}}{\vb{#1}}} \def\myvecbaux#1_#2{\vb{#1_#2}} \makeatother. – Manuel Jul 6 '17 at 10:53
  • @Manuel Thank you. Is your solution just for one character? (For instance I may have \myvecb{F}_{A|B}. – blackened Jul 6 '17 at 11:20
  • 1
    Ah, add braces around #2 in \vb{#1_{#2}}. – Manuel Jul 6 '17 at 11:33
  • Super easy with xparse, I think the new e type was made for this. – daleif Jul 6 '17 at 19:23
2

This checks if a _ is following and in that case it grabs the following _{..} and puts it as a subscript inside \vb{..}.

\makeatletter
\newcommand*\myvecb[1]{\@ifnextchar_{\myvecbaux{#1}}{\vb{#1}‌​}}
\def\myvecbaux#1_#2{\vb{#1_{#2}}}
\makeatother
| improve this answer | |

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.