Merge double subscripts in macro with second subscript passed as argument

Here, I search the first argument for _. If not found, I use #1_{#2}. If found (breaking down #1 as #3_#4), it uses #3_{#4,#2}.

Thanks to Mico for pointing out that comma was being set in \scriptscriptstyle. EDITED to fix.

\documentclass{article}
\newcommand\des[2]{\desaux{#1}{#2}#1_\relax}
\def\desaux#1#2#3_#4\relax{%
  \ifx\relax#4\relax
    #1_{#2}
  \else
    #3_{\stripus#4,#2}
  \fi
}
\def\stripus#1_{#1}
\begin{document}
$\des{Q}{1}$

$\des{Q_c}{1}$

$Q_{c,1}$ for comparison

$\des{Q_{ab}}{c}$
\end{document}

enter image description here

SUPPLEMENT

Based on comments of the OP, it appears that he desires interaction with \mathbf under certain scenarios (when #2 is empty). and with \bar on demand. While I am not sure if this could address all the needs, such things can be done directly in the \des macro, rather than interacting with a 2nd macro.

EDITED to also handle the bolding of greek letters (amsbsy added, with use of \boldsymbol)

For example,

\documentclass{article}
\usepackage{ifthen,amsbsy}
\newcommand\des[3][]{\desaux{#2}{#3}#2_\relax{#1}}
\def\desaux#1#2#3_#4\relax#5{%
  \ifx\relax#4\relax
    \ifx\relax#2\relax
      \boldsymbol{\mathbf{#5#1}}
    \else
      #5#1_{#2}
    \fi
  \else
    \ifx\relax#2\relax
      \boldsymbol{\mathbf{#5#3_{\stripus#4}}}
    \else
      #5#3_{\stripus#4,#2}
    \fi
  \fi
}
\def\stripus#1_{#1}
\begin{document}
$\des{Q}{1}$\par
$\des{Q_c}{1}$\par
$\des{Q}{}$\par
$\des{Q_c}{}$\par
$\des[\bar]{Q}{1}$\par
$\des[\bar]{Q_c}{1}$\par
$\des[\bar]{Q}{}$\par
$\des[\bar]{Q_c}{}$

$\des{\psi}{}$\par
$\des{\psi_\alpha}{}$\par
$\des[\bar]{\psi_\alpha}{}$
\end{document}

enter image description here


For the sake of variety, here's a LuaLaTeX-based solution. It sets up a LaTeX macro called \des, which takes two arguments and calls a Lua function called desfn which, in turn, does most of the work with the help of two Lua string functions (string.find and string.sub).

enter image description here

\documentclass{article}
\usepackage{luacode} % for "luacode" environment and "\luastring" macro
\begin{luacode}
function desfn ( u , v )
   w = u:find ( "_" )
   if w then -- found an underscore (_) char.
      tex.sprint ( u:sub(1,w-1) .. "_{" .. u:sub(w+1) .. "," .. v .. "}" )
   else
      tex.sprint ( u .. "_{" .. v .. "}" )
   end
end
\end{luacode}

%% LaTeX side code:
\newcommand\des[2]{\directlua{desfn(\luastring{#1},\luastring{#2})}}

\begin{document}
$\des{Q}{1}$ \quad $\des{Q_c}{1}$ \quad $\des{ABC_uvw}{123}$
\end{document}

Another solution based on LuaTeX, here by directly manipulating the math noads instead of using strings. This for example allows nesting calls to \des or contruction a version \appendsub which does not need the first argument:

\documentclass{article}
\usepackage{luacode}
\begin{luacode}
local oldsub
function storesub()
  local tail = tex.nest[tex.nest.ptr].tail
  oldsub = tail and tail.sub
  if oldsub then
    tail.sub = nil
  end
end
function recoversub(sep)
  if oldsub then
    local noad = node.new'noad'
    noad.nucleus = oldsub
    node.write(noad)
    oldsub = nil
    tex.sprint(sep)
  end
end
\end{luacode}

\newcommand\appendsub[1]{\directlua{storesub()}_{\directlua{recoversub','}#1}}
\newcommand\des[2]{#1\appendsub{#2}}

\begin{document}
$\des{Q}{1}$ \quad $\des{Q_a}{1}$ \quad $\des{Q_{ab}}{123}$ \quad $\des{\des{Q}{abc}}{123}$
\end{document}

enter image description here

Tags:

Subscripts