A newcommand for more than one formula

  \newcommand\zz[3]{z_{(h_{#1},h_{#2},h_{#3})} 

then you can use and \zz{1}{2}{3} and \zz{4}{5}{6} or even \zz123 and \zz456


It's not difficult to extend it to any number of subscripts:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\zz}{m}
 {
  z
  \sp{}\sb
   {
    \clist_map_inline:nn { #1 }
     {
      \seq_put_right:Nn \l_fahim_z_subscripts_seq { h\sb{##1} }
     }
    (\seq_use:Nn \l_fahim_z_subscripts_seq { , })
   }
 }
\seq_new:N \l_fahim_z_subscripts_seq
\ExplSyntaxOff

\begin{document}

$\zz{1,2,3}+\zz{4,5,6}=\zz{1,2,3,4,5,6}$

\end{document}

enter image description here

This can be generalized to different base letters and different processing of the subscripts. The trailing optional argument sets how to treat each item in the comma separated list, see the examples.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\zz}{O{z}mO{h\sb{##1}}}
 {
  #1
  \sp{}\sb
   {
    \cs_set_protected:Nn \__fahim_z_subscript:n { #3 }
    \clist_map_inline:nn { #2 }
     {
      \seq_put_right:Nn \l_fahim_z_subscripts_seq { \__fahim_z_subscript:n { ##1 } }
     }
    (\seq_use:Nn \l_fahim_z_subscripts_seq { , })
   }
 }
\seq_new:N \l_fahim_z_subscripts_seq
\ExplSyntaxOff

\begin{document}

$\zz{1,2,3}+\zz{4,5,6}=\zz{1,2,3,4,5,6}$

$\zz[Z]{1,2,3}[k_{#1}]$

$\zz{1,2,3}[(#1)]$

\end{document}

enter image description here

For more complex settings, I suggest a key-value syntax. Here the keys are var (for the name of the variable), outer to set the overall setting (default is just adding the parentheses) and inner for the sequence of actual subscripts. See the given examples. At any moment you can issue \zzset to change (in the current scope) one or more of the values.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\zz}{O{}m}
 {
  \group_begin:
  \keys_set:nn { fahim/zz } { #1 }
  \fahim_zz:n { #2 }
  \group_end:
 }

\NewDocumentCommand{\zzset}{m}
 {
  \keys_set:nn { fahim/zz } { #1 }
 }

\keys_define:nn { fahim/zz }
 {
  var .tl_set:N = \l__fahim_zz_var_tl,
  outer .code:n = \cs_set_protected:Nn \__fahim_zz_outer:n { #1 },
  inner .code:n = \cs_set_protected:Nn \__fahim_zz_inner:n { #1 },
 }

\seq_new:N \l__fahim_zz_subscripts_seq

\cs_new_protected:Nn \fahim_zz:n
 {
  \tl_use:N \l__fahim_zz_var_tl
  \sp{} % a dummy superscript to lower the subscript
  \sb
   {
    \__fahim_zz_outer:n
     {
      \clist_map_inline:nn { #1 }
       {
        \seq_put_right:Nn \l__fahim_zz_subscripts_seq { \__fahim_zz_inner:n { ##1 } }
       }
      \seq_use:Nn \l__fahim_zz_subscripts_seq { , }
     }
   }
 }
\ExplSyntaxOff

% initialize
\zzset{
  var=z,
  outer=(#1),
  inner=h_{#1},
}

\begin{document}

$\zz{1,2,3}+\zz{4,5,6}=\zz{1,2,3,4,5,6}$

$\zz[var=Z,inner=k_{#1}]{1,2,3}$

$\zz[outer=i(#1)]{1,2,3}$

$\zz[outer=i(#1),inner=k_{#1}]{1,2,3}$

\end{document}

enter image description here


It's not difficult to extend it to any number of subscripts and without using ExplSyntaxOn:

\def\zz#1{\zzA#1,,}
\def\zzA#1,{z_\bgroup(h_{#1}\zzB}
\def\zzB#1,{\ifx\end#1\end)\egroup \else ,h_{#1}\expandafter\zzB\fi}

$\zz{1,2,3}+\zz{4,5,6}=\zz{1,2,3,4,5,6}$

\bye

EDIT If you need to add an ``index letter'' (i, j like in your comment), then it is possible to do using this code:

\def\zz#1#{z_\bgroup#1\zzI}
\def\zzI#1{\zzA#1,,}
\def\zzA#1,{(h_{#1}\zzB}
\def\zzB#1,{\ifx\end#1\end)\egroup \else ,h_{#1}\expandafter\zzB\fi}

$\zz i{1,2,3}+\zz j{4,5,6}=\zz{1,2,3,4,5,6}$

\bye

Tags:

Macros