1

in my document I have to type expressions that contain letters with superscripts, enclosed between parentheses, such as

a^{(k,i)}, b^{(k,i)}, \gamma^{(k,i)}

and after a while it became really annoying for me to type those parentheses '(' and ')' each time.

I defined new variables

\newcommand{\ca}[2]{a^{(#1, #2)}}
\newcommand{\cb}[2]{b^{(#1, #2)}}
\newcommand{\cgamma}[2]{\gamma^{(#1, #2)}}

and used \ca k i or \cgamma k i. This really made it way easier for me to type equations, however it lacks flexibility. First, I have to define a new variable for each letter. Also, if I want to use subscripts and type \c_3{k,i}, then it is broken.

My question is, what is the right way to achieve this functionality ?

EDIT : After searching on the internet for a while, I found a solution

\documentclass{article}
\usepackage{amsmath}
\usepackage{etoolbox}

\makeatletter
\newcommand{\removeabs}[1]{%
  \ifcat\relax\noexpand#1%
    \expandafter\expandafter\expandafter\@gobble\expandafter\string
  \fi
  #1%
}
\makeatother

\newcommand{\doublesupscr}[1]{\expandafter\newcommand\csname c\removeabs{#1}\endcsname[2]{#1^{(##1, ##2)}}}

\newcommand{\singlesupscr}[1]{\expandafter\newcommand\csname c\removeabs{#1}\endcsname[1]{#1^{(##1)}}}


\forcsvlist{\doublesupscr}{a,b,c}
\forcsvlist{\singlesupscr}{\eta, \gamma}


\begin{document}
\begin{equation*}
    \ca k i, \cb k i, \cc k i, \ceta i, \cgamma i
\end{equation*}
\end{document}

which creates the following output output of code

However, I would like to type something like \ca_3 k i and still get the correct output. Is there a way for newcommand to ignore the subscript as an argument ?

2
  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. Apr 8, 2020 at 13:30
  • 2
    $\ca ki_3$ will work to get a subscript 3. Apr 8, 2020 at 15:48

1 Answer 1

1

Possibly something like this?

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\NewDocumentCommand{\pv}{m e{_} m}{%
  #1\IfValueT{#2}{_{#2}}^{(#3)}%
}

\begin{document}

$\pv{a}_3{k,i}+\pv{b}{k}+\pv{c}{k,l,m}_4$

\end{document}

enter image description here

You must log in to answer this question.

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