I often use \newcommand
to define variables, notations or operations on variables.
Sometimes I have to add accents, sub- and/or superscripts to a defined variable, like in the following test example:
\documentclass{article}
\usepackage{fixmath}
\usepackage{ushort}
\usepackage{bm}
\DeclareSymbolFont{bmaccents}{OT1}{cmr}{m}{n}
\DeclareMathAccent{\check}{\mathord}{bmaccents}{"14}
\newcommand{\tns}[1]{\ushort{\bm{#1}}} % Notation for variable
\newcommand{\var}{\tns{\Omega}_L^\mathrm{K}} % Variable
\newcommand{\proj}[1]{\check{#1}} % Accent on variable
\newcommand{\drv}[2]{#1_{,#2}} % Derivative of variable
\newcommand{\ind}[2]{^{[#1]}_\mathrm{#2}} % Indices for variable
\renewcommand{\arraystretch}{1.5}
\begin{document}
\noindent
\begin{tabular}{@{} l l l l l @{}}
& Desired code & Actual result & Desired result & Comment\\
%
Variable & \verb|\var|&
$\var$ &
$\ushort{\bm{\Omega}}_{L}^{\mathrm{K}}$ &
ok\\
%
Add subscript & \verb|\var_l| &
$\var_l$ &
$\ushort{\bm{\Omega}}_{Ll}^{\mathrm{K}}$ &
not ok, error\\
%
Add superscript & \verb|\var_l^k|&
$\var_l^k$ &
$\ushort{\bm{\Omega}}_{Ll}^{\mathrm{K}k}$ &
not ok, error\\
%
\ldots\\
%
All together & $^\star$ &
$\drv{\proj{\var_l^k\ind{b}{h}_1^2}}{\xi}$ &
$\ushort{\bm{\proj{\Omega}}}_{Ll\mathrm{h}1,\xi}^{\mathrm{K}k[b]2}$ &
not ok, error\\
\end{tabular}
\bigskip\\
$^\star$: \verb|\drv{\proj{\var_l^k\ind{b}{h}_1^2}}{\xi}|
\end{document}
First I use the defined variable \var
with the notation \tns
. Then I add an arbitrary subscript _l
. Doing so, this produces the wrong result and a Double subscript.
error. The same problem holds for adding arbitrary accents, sub- and superscripts to the defined variable. Note that this is only an example. The order and number of accents, sub- and superscripts is not fixed.
So is there a possibility (package) do get the desired result using my defined commands, i.e. adding accents only on the variable itself and merging all sub- and superscripts without Double subscript.
and Double superscript.
errors in a robust way?
What I first tried is to use \DeclareDocumentCommand
from the xparse package and specifying different arguments. But this is not very flexible.
I also found the following both questions:
- Defining a newcommand with sub- or superscript and avoiding "double subscript" error
- Merge double subscripts in macro
The answers therein address only some parts of my problem.
Thank you very much for your help.