1

I would like vector variables to be bold faced, and scalar variables to be in plain font. The components of vector variables should then follow the scalar (nonbold) convention. To this end, using the embellishments of xparse I can, say, define a command \x as

\usepackage{bm} % for \bm command
\usepackage{xparse}

\NewDocumentCommand{\x}{e{_^}}{%
    \IfValueTF{#1}{x_{#1}}{\bm x_{#1}}% plain if subscript provided, bold otherwise
    \IfValueT{#2}{^{#2}}%
}%

that prints a plain "x" symbol if a subscript is provided or a bold "x" otherwise.

So far so good. The problem arises when I want to combine this with a math accent command, such as \bar: desired and actual outcomes

Is there any way to make the commands \x (and similarly defined \y, etc.) "compatible" with \bar and friends? I tried with some \expandafter before \bar but no combination seemed to work.

Edit after egreg's solution

Following @egreg's idea of passing the accent as optional argument, I came with a simple modification of my original macro that also seems to do the trick. I don't know if it is TeXnically sound, but to make it more reader friendly I directly pass a possible accent command to a first optional argument.

\NewDocumentCommand{\x}{O{} t{'} e{_^}}{% #1: accent command  #2: catch prime (')  #3: subscript  #4: superscript
    % apply optional (accent) #1
    % apply \vec if subscript #3 is empty
    \IfValueTF{#3}{%
        % check if subscript is provided but is empty
        \ifstrempty{#3}{\vec{#1{x}}}{#1{x}_{#3}}%
    }{%
        \vec{#1{x}}%
    }%
    % add superscript
    \IfValueT{#4}{^{#4}}%
    % add prime (')
    \IfBooleanT{#2}{'}%
}%

I also included a token argument to catch a possible prime ', so that it properly commutes with subscripts (\x_i' and \x_i' are treated equally). Of course, it is not compatible with repeated primes (\x_i'' is correctly typed in plain font whereas \x''_i is printed in bold), but typing primes at the end works as expected.

In order to create similar commands, I wrapped the code into a \defineBoldVariableCommand{\commandName}{symbol}. The MWE given below produces the following output:

Output of MWE

\documentclass{article}

\usepackage{bm}% for bold math command \bm
\usepackage{etoolbox}% for \ifstrempty
\usepackage{xparse}% for \NewDocumentCommand

% ================================================================
\renewcommand{\vec}{\bm}% redefine \vec as bold math

\newcommand{\defineBoldVariableCommand}[2]{% #1: cmdname  #2: symbol
    \let#1\relax%  overwrites definition, if command already defined
    \NewDocumentCommand{#1}{O{} t{'} e{_^}}{% ##1: accent command  ##2: catch prime (')  ##3: subscript  ##4: superscript
        % apply optional (accent) ##1
        % apply \vec if subscript ##3 is empty
        \IfValueTF{##3}{%
            % check if subscript is provided but is empty
            \ifstrempty{##3}{\vec{##1{#2}}}{##1{#2}_{##3}}%
        }{%
            \vec{##1{#2}}%
        }%
        % add superscript
        \IfValueT{##4}{^{##4}}%
        % add prime (')
        \IfBooleanT{##2}{'}%
    }%
}%

\defineBoldVariableCommand{\x}{x}
% ================================================================

\begin{document}
    No (or empty) subscript: variable is bold

    \begin{tabular}{@{}ll@{}}
        $\x = \x_{}$ & \verb|\x = \x_{}|
    \\
        $\x^k$       & \verb|\x^k|
    \\
        $\x[\bar]$   & \verb|\x[\bar]|
    \\
        $\x[\bar]^k$ & \verb|\x[\bar]^k|
    \\
        $\x'$        & \verb|\x'|
    \\
        $\x[\bar]'$  & \verb|\x[\bar]'|
    \end{tabular}

    \vspace{.5cm}%
    Nonempty subscript: variable is in plain font

    \begin{tabular}{@{}ll@{}}
        $\x_i$                        & \verb|\x_i|
    \\
        $\x_i^k = \x^k_i$             & \verb|\x_i^k = \x^k_i|
    \\
        $\x[\bar]_i$                  & \verb|\x[\bar]_i|
    \\
        $\x[\bar]^k_i = \x[\bar]_i^k$ & \verb|\x[\bar]^k_i = \x[\bar]_i^k|
    \\
        $\x'_i = \x_i'$               & \verb|\x'_i = \x_i'|
    \\
        $\x[\bar]'_i = \x[\bar]_i'$   & \verb|\x[\bar]'_i = \x[\bar]_i'|
    \end{tabular}
\end{document}

Edit #2. Simpler solution

Another possibility, that produces exactly the same outcome but with less work is given by suitably redefining \vec instead:

% ================================================================
\RenewDocumentCommand{\vec}{m O{} t{'} e{_^}}{%
    #2{%
        \IfValueTF{#4}{\ifstrempty{#4}{\bm}{}}{\bm}%
        {#1}%
    }%
    \IfValueT{#4}{_{#4}}%
    \IfValueT{#5}{^{#5}}%
    \IfBooleanT{#3}{'}%
}

\newcommand{\x}{\vec{x}}
% ================================================================
3
  • You should always provide a small but complete example that can be used for test. \NewDocumentCommand produces protected commands so you can't expand them. Generally I think that the idea behind your command is bad. Why don't you type simply x if you want a normal scalar x and e.g. \vec{x} if you want a vector? Instead you hide semantic differences behind some private syntax convention that only you can understand (and probably will have forgotten in a year) which means that nobody else will be able to properly understand your source. Commented Oct 30, 2023 at 8:41
  • @UlrikeFischer I understand, thanks, but I don't intend to make the source public. I am writing notes and I was hoping to have a way of globally changing the notation of variables should I decide at some point that bold does not work well
    – AndreasT
    Commented Oct 30, 2023 at 9:36
  • source can go public in more ways then sending the tex around. E.g. when tagging math the source can be embedded into the pdf: tex.stackexchange.com/a/696032/2388 Commented Oct 30, 2023 at 9:53

1 Answer 1

1

Don't indulge in input such as \bar x or \bm y. It's sloppy and misleading.

What you have is quite the same as if you typed \bar{\x}_i and of course \x isn't able to see the _.

I can offer a command that looks forward to see whether the main letter has to be adorned with an accent.

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

\NewDocumentCommand{\vv}{moe{_^}}{%
  \IfNoValueTF{#3}{%
    \makevv{\bm{#1}}{#2}%
  }{%
    \makevv{#1}{#2}_{#3}%
  }%
  \IfValueT{#4}{^{#4}}%
}

\NewDocumentCommand{\makevv}{mm}{%
  \StringCase{#2}{
    {h}{\hat}
    {c}{\check}
    {d}{\dot}
    {dd}{\ddot}
    {ddd}{\dddot}
    {b}{\bar}
  }{#1}%
}

\ExplSyntaxOn
\cs_set_eq:NN \StringCase \str_case:nn
\ExplSyntaxOff

\NewDocumentCommand{\x}{}{\vv{x}}

\begin{document}

$\vv{y}+\x$

$\vv{y}_i+\x_i$

$\vv{y}[d]+\x[b]$

$\vv{y}[dd]_i+\x[b]_i$

\end{document}

enter image description here

You can see the abbreviations for the accents in the code.

3
  • Thanks @egreg. But is there any reason why \bm x is sloppy whereas x_i (as opposed to x_{i}) ins't? Aren't they both single-char arguments after all?
    – AndreasT
    Commented Oct 30, 2023 at 9:40
  • 1
    @AndreasT Yes, also _i instead of _{i} is a bit sloppy. Try x_\notin and see why.
    – egreg
    Commented Oct 30, 2023 at 9:59
  • Thank you very much for the answer and the comments. May I ask if the alternative in my edited answer is viable? I accept that passing the accent as an argument is the way to go, and I thought that passing the desired macro verbatim would be more intuitive and less laborious
    – AndreasT
    Commented Oct 31, 2023 at 2:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .