# Adding accents, sub- and superscripts to defined variables

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\\
%
$\var_l$ &
$\ushort{\bm{\Omega}}_{Ll}^{\mathrm{K}}$ &
not ok, error\\
%
$\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:

Thank you very much for your help.

Perhaps I am missing the boat here, but it seems to me like the desired output (that of sub and superscripts mashed leftward en masse) is particularly difficult to mentally digest, because associated things that I would expect to be vertically aligned are not.

So I will take the liberty of suggesting a slightly different approach. My goal is that sub- and superscripts of \drvand \ind will be aligned, that things like _l^k after a variable should be aligned.

If this is not, in the end, desired, I will delete the answer, but I put it out for consideration.

Rather than using the standard sup- and superscript mechanism of LaTeX to set these things, I string together a series of \stackanchors. As a result, two syntax changes are required:

1. things like _l^k are replaced with \foo{k}{l}; and

2. if the desired argument of \proj is not a greek letter, but a \tns of a greek letter, then use \projX instead of \proj.

Thus, the proposed syntax becomes:

$\drv{\projX{\var\foo{k}{l}\ind{b}{h}\foo{2}{1}}}{\xi}$


Here is the MWE. As you can see, all the sub- and super- items sit on their own respective baselines, and the inverse carat sits atop the bolded Omega. The difference from what the OP said was desired is the alignment of groups: K/L, k/l, [b]/h and 2/1.

\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{\proj}[1]{\check{#1}}               % Accent on variable

\usepackage{stackengine}
\stackMath
\def\stacktype{L}
% \tns UNCHANGED
\newcommand\var{\tns\Omega%
\stackanchor[.6\baselineskip]{\scriptstyle\mathrm{K}}{\scriptstyle L}}
% \proj UNCHANGED, BUT \projX ADDED
\newcommand{\projX}[1]{\expandafter\projhelpA#1}%
\newcommand{\projhelpA}[2]{#1{\check#2}}%
\newcommand\drv[2]{#1\stackanchor[.6\baselineskip]{}{\scriptstyle,#2}}
\newcommand\ind[2]{%
\stackanchor[.6\baselineskip]{\scriptstyle[#1]}{\scriptstyle\mathrm{#2}}}
\newcommand\foo[2]{\stackanchor[.6\baselineskip]{\scriptstyle#1}{\scriptstyle#2}}
\parskip1em
\begin{document}
$\var$\par
$\var\foo{}{l}$\par
$\var\foo{k}{l}$\par
$\drv{\projX{\var\foo{k}{l}\ind{b}{h}\foo{2}{1}}}{\xi}$
\end{document}


• Ok, this is an alternative solution to my problem not merging sub- and superscripts. But I don't have always the same groups (k/l etc.). Sometimes I only have to add one subscript. This was only an example. I edited my question. – Michael Jun 7 '16 at 13:50
• @Michael I have revised my answer to show how to get the results you show, using my altered syntax – Steven B. Segletes Jun 7 '16 at 16:07
• But is it possible to merge the sub- and superscripts like they appear (for example \var_l_k^i_o^c\ind{b}{h}_1^2^3 or any other combination) and best without using the stackengine package. The result should be like: I use for example \var, which has the main symbol \Omega with added \tns notation. If I now add arbitrary sub- or superscripts, they should be merged and appended to \var. Surrounding \var (with or without added sub- and superscripts) with an accent command should add the accent always on the main symbol (in this example \Omega). – Michael Jun 26 '16 at 19:43

I'm not sure I fully understand your setup. To me, it looks like you're OK with the second expression, viz.,

$\ushort{\bm{\proj{\Omega}}}_{Ll\mathrm{h}1,\xi}^{\mathrm{K}k[b]2}$


except that it can't be used, as is, in "moving" arguments of instructions such as \section and \caption.

To fix this situation, I suggest you (a) load the etoolbox package and (b) issue the instruction \robustify{\ushort}.

\documentclass{article}
\usepackage{fixmath,ushort,bm}

\usepackage{etoolbox} % for "\robustify" macro
\robustify{\ushort}

\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]#2}}   % Indices for variable

\begin{document}

\noindent
$\ushort{\bm{\proj{\Omega}}}_{Ll\mathrm{h}1,\xi}^{\mathrm{K}k[b]2}$

\section{$\ushort{\proj{\bm{\Omega}}}_{Ll\mathrm{h}1,\xi}^{\mathrm{K}k[b]2}$}

\begin{figure}[h!]
\caption{$\ushort{\proj{\bm{\Omega}}}_{Ll\mathrm{h}1,\xi}^{\mathrm{K}k[b]2}$}
\end{figure}

\end{document}

• I want to use my first (desired) expression \drv{\proj{\var_l^k\ind{b}{h}_1^2}}{\xi} for a compact and flexible notation. What I ment with robust, is the ability to use my commands also in captions and so on. I edited my question. – Michael Jun 7 '16 at 13:46