0

Say I'd like to follow a convention in my article, e.g. "Summation indices will be denoted by Latin letters i, j, k" or "Free parameters will be denoted by Greek letters \alpha, \beta, \gamma .... I might want to change this convention at any time, so I've been defining new commands for them e.g. \NewDocumentCommand{\indexi}{i}, ... \indexj, \indexk ... etc.

Is there some kind of idiomatic way to handle these, as it gets quite cumbersome? Ideally I'd like to do something like define a sequence e.g. \index: default -> i, 2 -> j, 3 -> k and refer to them as \index, \index[2], \index[3] etc.

5 Answers5

4

You can use \afterassignment TeX primitive and simplify the syntax to \index or \index2 or \index3 etc.

\newcount\inum
\def\index{\afterassignment\indexA \inum=0} % the standard LaTeX meaning of \index is re-defined here
\def\indexA{\ifcase\inum i\or i\or j\or k\or l\or m\else x\fi}

$\index,\quad \index1,\quad \index2,\quad \index3$

The result is the same as given by mbert.

wipet
  • 74,238
  • Please at least warn about the use of \index here. The result will certainly not be the same if somebody also tries to use standard \index. The problem might be obvious to you, but it will not be so for everybody. – cfr Nov 01 '23 at 18:33
  • IMHO, everybody understand that if the \index is used in another meaning in the document then the same control sequence cannot be used in different meaning too. And OP asked for creating \index macro. I supposed, that OP doesn't use \index in standard meaning, otherwise he would not want to redefine it. And your comment is enough warning. – wipet Nov 01 '23 at 19:27
  • In my experience, almost any claim which begins 'everybody understands that $\theta$' is false. The exceptions are not cases where $\theta$ involves technology of any kind. – cfr Nov 01 '23 at 19:44
  • Yes, you are right. – wipet Nov 01 '23 at 19:47
2

Perhaps a bit overkill but you can use an l3 property list to do this. Note you shouldn't use \index as the command since that's the command to enter index entries. I use \myindex below.

\documentclass{article}

\ExplSyntaxOn \prop_new:N \g_sudhir_indices_prop \prop_set_from_keyval:Nn \g_sudhir_indices_prop { 1 = i, 2 = j, 3 = k } \NewDocumentCommand { \myindex } { O{1} } { \prop_item:Nn \g_sudhir_indices_prop { #1 } } \ExplSyntaxOff

\begin{document} $\myindex,\quad \myindex[1],\quad \myindex[2],\quad \myindex[3]$ \end{document}

indices

mbert
  • 4,171
2

My original answer addressed how to pull out an indexed item from a sequence. Despite the comments, it only allowed you to pull out a single item at a time. However, it allowed more complex items to be included in the sequence.

In fact, however, you don't want arbitrary items in sequences. You just want to convert an integer into some format in the same way counters are formatted in many other places in LaTeX. That is, this is basically like choosing whether you want your sections labelled with letters or your enumerated items labelled with roman numerals. The only real complication is that Greek letters are not among the default format options. For that case, therefore, we need to add \sudhir_int_to_greek:n using the method explained on page 184 of interface3.pdf.

\myindex[<positive integer>]

displays the integer in the current format. By default, this is a small letter from the Latin alphabet. If no argument is given, 1 is used.

\setmyindex{<format>}

sets the current format. <format> should be one of alph, Alph, arabic or greek. This can be easily extended following the patterns shown: a simple copy for a standard counter format such as Alph or setup from scratch for a case such as greek.

I've only provided the first five Greek letters, but you can extend it easily and copy the definition if you'd like to add uppercase Greek or whatever.

Then

$\myindex \myindex[1] \myindex[2] \myindex[3]$

\setmyindex{Alph} $\myindex \myindex[1] \myindex[2] \myindex[3]$

\setmyindex{arabic} $\myindex \myindex[1] \myindex[2] \myindex[3]$

\setmyindex{greek} $\myindex \myindex[1] \myindex[2] \myindex[3]$

\setmyindex{alph} $\myindex \myindex[1] \myindex[2] \myindex[3]$

will produce

integer representations

Code:

\documentclass{article}
% ateb: https://tex.stackexchange.com/a/700165/ 
\ExplSyntaxOn
\cs_new_protected_nopar:Nn \sudhir_myindex:n
{
  \sudhir_int_to_alph:n { #1 }
}
\cs_new_protected_nopar:Nn \sudhir_setmyindex:n 
{
  \cs_set_eq:Nc \sudhir_myindex:n { sudhir_int_to_#1:n } 
}
\cs_new:Npn \sudhir_int_to_greek:n #1
{% interface3.pdf page 184
  \int_to_symbols:nnn { #1 } { 5 }
  {
    { 1 } { \ensuremath{\alpha} }
    { 2 } { \ensuremath{\beta} }
    { 3 } { \ensuremath{\gamma} }
    { 4 } { \ensuremath{\delta} }
    { 5 } { \ensuremath{\epsilon} }
  }
}
\cs_new_eq:NN \sudhir_int_to_arabic:n \int_to_arabic:n
\cs_new_eq:NN \sudhir_int_to_alph:n \int_to_alph:n
\cs_new_eq:NN \sudhir_int_to_Alphc:n \int_to_Alph:n
\NewDocumentCommand \myindex { O {1} }
{%
  \sudhir_myindex:n { #1 }
}
\NewDocumentCommand \setmyindex { m }
{
  \sudhir_setmyindex:n { #1 }
}
\ExplSyntaxOff
\begin{document}
$\myindex \myindex[1] \myindex[2] \myindex[3]$

\setmyindex{Alph} $\myindex \myindex[1] \myindex[2] \myindex[3]$

\setmyindex{arabic} $\myindex \myindex[1] \myindex[2] \myindex[3]$

\setmyindex{greek} $\myindex \myindex[1] \myindex[2] \myindex[3]$

\setmyindex{alph} $\myindex \myindex[1] \myindex[2] \myindex[3]$

\end{document}

cfr
  • 198,882
  • @AlanMunn I just came back to delete this. But, actually, I'm not outputting more than one element. I just think this is a bad way to do this. – cfr Nov 01 '23 at 17:36
  • Scnr: iirc there has been some discussion about \ensuremath. ;-) – Ulrich Diez Nov 01 '23 at 20:21
  • @UlrichDiez What's Scnr? Is \ensuremath another thing I don't know about? Re. \int_to_symbols:nnn. That's my understanding. At least, I don't know if that reflects the implementation exactly, but it is designed to work as if that is what's going on. So (from the docs) 27 is mapped to aa by \int_to_alph:n, which would be implemented by \int_to_symbols:nnn { #1 } { 26 } { {1}{a} {2}{b} ...}. – cfr Nov 01 '23 at 21:04
  • 1
    @cfr scnr = sorry, could not resist. ;-) || Regarding \ensuremath you mght be interested in egreg's answer to When not to use \ensuremath for math macro? – Ulrich Diez Nov 01 '23 at 21:10
  • 1
    @UlrichDiez You are probably right about the OP's usage. It would not be at all unusual for me to want to use something like <term>\textsubscript{<index>}. Strictly speaking, I suppose <index> should use \textgreek{} if a Greek letter is wanted, but it doesn't really seem necessary when the letters are being used as symbols more than anything else. I don't think it's unreasonable to use \ensuremath{} in such a case. Indeed, I initially set it up with $...$ before realising the OP probably wanted to use this in maths mode. That possibility had not initially occurred to me. – cfr Nov 01 '23 at 21:21
  • @UlrichDiez Is also needed for what exactly? – cfr Nov 02 '23 at 00:20
  • @UlrichDiez OK. If this was intended to provide a representation of any natural number, you'd need that. But isn't it just being used as an identifier? If I needed to distinguish 31 senses of a term, I wouldn't do it with subscripts anyway. More than 5, sure - that was just an example - but there's a limit to what you can reasonably keep track of or expect your readers to keep track of. I have the distinct feeling we're talking about completely different things .... – cfr Nov 02 '23 at 02:25
  • 1
    I was talking about how \int_to_symbols:nnn{⟨integer expression⟩} {⟨total symbols⟩}{⟨value to symbol mapping⟩} seems to work: Converts the result of ⟨integer expression⟩ to representation in positional number system with base ⟨total symbols⟩ and digits 1.. ⟨total symbols⟩ and then maps each digit according to ⟨value to symbol mapping⟩. – Ulrich Diez Nov 02 '23 at 02:47
  • @UlrichDiez Yes, I think so. – cfr Nov 02 '23 at 03:12
2

Give symbolic names to your classes of objects and then you can associate commands to them.

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\defineclass}{mm} {% #1 = class name, #2 = symbols \seq_new:c { g_aps_class_#1_seq } \seq_gset_from_clist:cn { g_aps_class_#1_seq } { #2 } }

\NewDocumentCommand{\useclass}{mm} {% #1 = control sequence, #2 = class \NewDocumentCommand{#1}{O{1}} { \seq_item:cn { g_aps_class_#2_seq } { ##1 } } }

\ExplSyntaxOff

% define your classes \defineclass{ijk}{i,j,k,l} \defineclass{greek}{\alpha,\beta,\gamma,\delta,\varepsilon}

% associate classes with commands \useclass{\sumind}{ijk} \useclass{\freepar}{greek}

\begin{document}

[ \sum_{\sumind\ge1}\sumind x^{\sumind}=x\sum_{\sumind[2]\ge0}(\sumind[2]+1)x^{\sumind[2]} ]

[ \freepar+\freepar[2] ]

\end{document}

enter image description here

egreg
  • 1,121,712
1

There is already a command \index in LaTeX 2ε, so don't let us use that name but let's name the commands \Summationindex resp. \FreeParameter.

expl3's \tl_item:nn let's you select an item of a token list. You can use that for defining mapping from numbers to symbols:

\documentclass{article}

\ExplSyntaxOn \NewDocumentCommand\Summationindex{O{1}}{ \tl_item:nn { {a}{b}{c}{d}{e}{f}{g}{h}{i}{j}{k}{l}{m}{n}{o}{p}{q}{r}{s}{t}{u}{v}{w}{x}{y}{z} } {((#1)+8)} % <- Let's add 8 so that \Summationindex[1] yields i. } \NewDocumentCommand\FreeParameter{O{1}}{ \tl_item:nn { {\alpha}{\beta}{\gamma}{\delta}{\epsilon}% probably \varepsilon {\zeta}{\eta}{\theta}% probably \vartheta? {\iota}{\kappa}{\lambda}{\mu}{\nu}{\xi}{o}{\pi}{\rho}% probably \varrho {\sigma}{\tau}{\upsilon}{\phi}% probably \varphi {\chi}{\psi}{\omega}% } {(#1)} } \ExplSyntaxOff

\begin{document}

Testing \verb|\Summationindex[-7] to \Summationindex[18]|:

\bigskip

\noindent\verb|$\Summationindex$|: $\Summationindex$

\bigskip

\noindent\begin{tabular}{|l|l|l|}% \hline \verb|$\Summationindex[-7]$|: $\Summationindex[-7]$&% \verb|$\Summationindex[-6]$|: $\Summationindex[-6]$&% \verb|$\Summationindex[-5]$|: $\Summationindex[-5]$\\hline \verb|$\Summationindex[-4]$|: $\Summationindex[-4]$&% \verb|$\Summationindex[-3]$|: $\Summationindex[-3]$&% \verb|$\Summationindex[-2]$|: $\Summationindex[-2]$\\hline \verb|$\Summationindex[-1]$|: $\Summationindex[-1]$&% \verb|$\Summationindex[0]$|: $\Summationindex[0]$&% \verb|$\Summationindex[1]$|: $\Summationindex[1]$\\hline \verb|$\Summationindex[2]$|: $\Summationindex[2]$&% \verb|$\Summationindex[3]$|: $\Summationindex[3]$&% \verb|$\Summationindex[4]$|: $\Summationindex[4]$\\hline \verb|$\Summationindex[5]$|: $\Summationindex[5]$&% \verb|$\Summationindex[6]$|: $\Summationindex[6]$&% \verb|$\Summationindex[7]$|: $\Summationindex[7]$\\hline \verb|$\Summationindex[8]$|: $\Summationindex[8]$&% \verb|$\Summationindex[9]$|: $\Summationindex[9]$&% \verb|$\Summationindex[10]$|: $\Summationindex[10]$\\hline \verb|$\Summationindex[11]$|: $\Summationindex[11]$&% \verb|$\Summationindex[12]$|: $\Summationindex[12]$&% \verb|$\Summationindex[13]$|: $\Summationindex[13]$\\hline \verb|$\Summationindex[14]$|: $\Summationindex[14]$&% \verb|$\Summationindex[15]$|: $\Summationindex[15]$&% \verb|$\Summationindex[16]$|: $\Summationindex[16]$\\hline \verb|$\Summationindex[17]$|: $\Summationindex[17]$&% \verb|$\Summationindex[18]$|: $\Summationindex[18]$&% \\hline \end{tabular}

\bigskip

Testing \verb|\FreeParameter[1] to \FreeParameter[24]|:

\bigskip

\noindent\verb|$\FreeParameter$|: $\FreeParameter$

\bigskip

\noindent\begin{tabular}{|l|l|l|}% \hline \verb|$\FreeParameter[1]$|: $\FreeParameter[1]$&% \verb|$\FreeParameter[2]$|: $\FreeParameter[2]$&% \verb|$\FreeParameter[3]$|: $\FreeParameter[3]$\\hline \verb|$\FreeParameter[4]$|: $\FreeParameter[4]$&% \verb|$\FreeParameter[5]$|: $\FreeParameter[5]$&% \verb|$\FreeParameter[6]$|: $\FreeParameter[6]$\\hline \verb|$\FreeParameter[7]$|: $\FreeParameter[7]$&% \verb|$\FreeParameter[8]$|: $\FreeParameter[8]$&% \verb|$\FreeParameter[9]$|: $\FreeParameter[9]$\\hline \verb|$\FreeParameter[10]$|: $\FreeParameter[10]$&% \verb|$\FreeParameter[11]$|: $\FreeParameter[11]$&% \verb|$\FreeParameter[12]$|: $\FreeParameter[12]$\\hline \verb|$\FreeParameter[13]$|: $\FreeParameter[13]$&% \verb|$\FreeParameter[14]$|: $\FreeParameter[14]$&% \verb|$\FreeParameter[15]$|: $\FreeParameter[15]$\\hline \verb|$\FreeParameter[16]$|: $\FreeParameter[16]$&% \verb|$\FreeParameter[17]$|: $\FreeParameter[17]$&% \verb|$\FreeParameter[18]$|: $\FreeParameter[18]$\\hline \verb|$\FreeParameter[19]$|: $\FreeParameter[19]$&% \verb|$\FreeParameter[20]$|: $\FreeParameter[20]$&% \verb|$\FreeParameter[21]$|: $\FreeParameter[21]$\\hline \verb|$\FreeParameter[22]$|: $\FreeParameter[22]$&% \verb|$\FreeParameter[23]$|: $\FreeParameter[23]$&% \verb|$\FreeParameter[24]$|: $\FreeParameter[24]$\\hline \end{tabular}

\end{document}

You can also consider implementing mapping from number to symbol via expl3's \int_case:nnF where implementing raising an error-message in case a mapping is not defined is more easy.

\documentclass{article}

\ExplSyntaxOn % ---------------------------------------------------------------------------------------- % Use expl3-infrastructure for providing error-message in case mapping from number to % symbol is undefined: % ........................................................................................ \prop_gput:Nnn \g_msg_module_type_prop { MyStuff } {} \prop_gput:Nnn \g_msg_module_name_prop { MyStuff } {Macro-Defined-In-Preamble:} \msg_new:nnnn {MyStuff} {No mapping defined} {Macro~#1:~No~mapping~defined~for~number~#2.} {Make~sure~that~the~definition~of~macro~#1~also~provides~a~mapping~for~the~number~#2.} \cs_new:Npn \NoMappingDefinedError #1#2 { \exp_args:Nne \use:nn { \msg_error:nnnn {MyStuff} {No mapping defined} }{{\iow_char:N \ \cs_to_str:N#1}}{#2} }% % ---------------------------------------------------------------------------------------- \NewDocumentCommand\Summationindex{O{1}}{ \int_case:nnF {(#1)} { {-7}{a} {-6}{b} {-5}{c} {-4}{d} {-3}{e} {-2}{f} {-1}{g} {0}{h} {1}{i} {2}{j} {3}{k} {4}{l} {5}{m} {6}{n} {7}{o} {8}{p} {9}{q} {10}{r} {11}{s} {12}{t} {13}{u} {14}{v} {15}{w} {16}{x} {17}{y} {18}{z} } {\NoMappingDefinedError{\Summationindex}{(#1)}} }

\NewDocumentCommand\FreeParameter{O{1}}{ \int_case:nnF {(#1)} { {1}{\alpha} {2}{\beta} {3}{\gamma} {4}{\delta} {5}{\epsilon}% probably \varepsilon {6}{\zeta} {7}{\eta}
{8}{\theta}% probably \vartheta? {9}{\iota} {10}{\kappa} {11}{\lambda} {12}{\mu} {13}{\nu} {14}{\xi} {15}{o} {16}{\pi}
{17}{\rho}% probably \varrho {18}{\sigma} {19}{\tau} {20}{\upsilon} {21}{\phi}% probably \varphi {22}{\chi} {23}{\psi} {24}{\omega} } {\NoMappingDefinedError{\FreeParameter}{(#1)}} }

\ExplSyntaxOff

\begin{document}

Testing \verb|\Summationindex[-7] to \Summationindex[18]|:

\bigskip

\noindent\verb|$\Summationindex$|: $\Summationindex$

\bigskip

\noindent\begin{tabular}{|l|l|l|}% \hline \verb|$\Summationindex[-7]$|: $\Summationindex[-7]$&% \verb|$\Summationindex[-6]$|: $\Summationindex[-6]$&% \verb|$\Summationindex[-5]$|: $\Summationindex[-5]$\\hline \verb|$\Summationindex[-4]$|: $\Summationindex[-4]$&% \verb|$\Summationindex[-3]$|: $\Summationindex[-3]$&% \verb|$\Summationindex[-2]$|: $\Summationindex[-2]$\\hline \verb|$\Summationindex[-1]$|: $\Summationindex[-1]$&% \verb|$\Summationindex[0]$|: $\Summationindex[0]$&% \verb|$\Summationindex[1]$|: $\Summationindex[1]$\\hline \verb|$\Summationindex[2]$|: $\Summationindex[2]$&% \verb|$\Summationindex[3]$|: $\Summationindex[3]$&% \verb|$\Summationindex[4]$|: $\Summationindex[4]$\\hline \verb|$\Summationindex[5]$|: $\Summationindex[5]$&% \verb|$\Summationindex[6]$|: $\Summationindex[6]$&% \verb|$\Summationindex[7]$|: $\Summationindex[7]$\\hline \verb|$\Summationindex[8]$|: $\Summationindex[8]$&% \verb|$\Summationindex[9]$|: $\Summationindex[9]$&% \verb|$\Summationindex[10]$|: $\Summationindex[10]$\\hline \verb|$\Summationindex[11]$|: $\Summationindex[11]$&% \verb|$\Summationindex[12]$|: $\Summationindex[12]$&% \verb|$\Summationindex[13]$|: $\Summationindex[13]$\\hline \verb|$\Summationindex[14]$|: $\Summationindex[14]$&% \verb|$\Summationindex[15]$|: $\Summationindex[15]$&% \verb|$\Summationindex[16]$|: $\Summationindex[16]$\\hline \verb|$\Summationindex[17]$|: $\Summationindex[17]$&% \verb|$\Summationindex[18]$|: $\Summationindex[18]$&% \\hline \end{tabular}

\bigskip

Testing \verb|\FreeParameter[1] to \FreeParameter[24]|:

\bigskip

\noindent\verb|$\FreeParameter$|: $\FreeParameter$

\bigskip

\noindent\begin{tabular}{|l|l|l|}% \hline \verb|$\FreeParameter[1]$|: $\FreeParameter[1]$&% \verb|$\FreeParameter[2]$|: $\FreeParameter[2]$&% \verb|$\FreeParameter[3]$|: $\FreeParameter[3]$\\hline \verb|$\FreeParameter[4]$|: $\FreeParameter[4]$&% \verb|$\FreeParameter[5]$|: $\FreeParameter[5]$&% \verb|$\FreeParameter[6]$|: $\FreeParameter[6]$\\hline \verb|$\FreeParameter[7]$|: $\FreeParameter[7]$&% \verb|$\FreeParameter[8]$|: $\FreeParameter[8]$&% \verb|$\FreeParameter[9]$|: $\FreeParameter[9]$\\hline \verb|$\FreeParameter[10]$|: $\FreeParameter[10]$&% \verb|$\FreeParameter[11]$|: $\FreeParameter[11]$&% \verb|$\FreeParameter[12]$|: $\FreeParameter[12]$\\hline \verb|$\FreeParameter[13]$|: $\FreeParameter[13]$&% \verb|$\FreeParameter[14]$|: $\FreeParameter[14]$&% \verb|$\FreeParameter[15]$|: $\FreeParameter[15]$\\hline \verb|$\FreeParameter[16]$|: $\FreeParameter[16]$&% \verb|$\FreeParameter[17]$|: $\FreeParameter[17]$&% \verb|$\FreeParameter[18]$|: $\FreeParameter[18]$\\hline \verb|$\FreeParameter[19]$|: $\FreeParameter[19]$&% \verb|$\FreeParameter[20]$|: $\FreeParameter[20]$&% \verb|$\FreeParameter[21]$|: $\FreeParameter[21]$\\hline \verb|$\FreeParameter[22]$|: $\FreeParameter[22]$&% \verb|$\FreeParameter[23]$|: $\FreeParameter[23]$&% \verb|$\FreeParameter[24]$|: $\FreeParameter[24]$\\hline \end{tabular}

\end{document}

enter image description here

Ulrich Diez
  • 28,770