定义一类符号的最佳约定

定义一类符号的最佳约定

假设我想在我的文章中遵循一个惯例,例如“求和指标将用拉丁字母表示i, j, k”或“自由参数将用希腊字母表示” \alpha, \beta, \gamma ...。我可能随时想改变这个惯例,所以我一直在为它们定义新的命令,例如\NewDocumentCommand{\indexi}{i}, ... \indexj, \indexk ...等等。

有没有一种惯用的方法来处理这些问题,因为它变得相当麻烦?理想情况下,我想做一些事情,比如定义一个序列,然后\index: default -> i, 2 -> j, 3 -> k将它们称为\index, \index[2], \index[3]等。

答案1

您可以使用\afterassignmentTeX 原语并将语法简化为\index\index2\index3等。

\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$

结果与 mbert 给出的结果相同。

答案2

也许有点过头了,但您可以使用 l3 属性列表来执行此操作。请注意,您不应使用\index作为命令,因为这是输入索引条目的命令。我使用\myindex下面的命令。

\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}

索引

答案3

我最初的回答解决了如何从序列中提取索引项的问题。尽管有评论,但它只允许您一次提取单个项。但是,它允许将更复杂的项包含在序列中。

但实际上,您并不希望序列中存在任意项。您只想将整数转换为某种格式,就像 LaTeX 中许多其他地方对计数器进行格式化一样。也就是说,这基本上就像选择是否要用字母标记部分或用罗马数字标记枚举项。唯一真正的复杂之处在于希腊字母不在默认格式选项中。因此,对于这种情况,我们需要\sudhir_int_to_greek:n使用第 184 页中解释的方法进行添加interface3.pdf

\myindex[<positive integer>]

以当前格式显示整数。默认情况下,这是拉丁字母中的小写字母。如果没有给出参数,则1使用。

\setmyindex{<format>}

设置当前格式。<format>应为alphAlpharabic之一greek。可以按照所示模式轻松扩展:标准计数器格式的简单副本,例如Alph或从头开始设置,例如greek

我只提供了前五个希腊字母,但如果您想添加大写希腊字母或其他内容,您可以轻松扩展它并复制定义。

然后

$\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]$

将产生

整数表示

代码:

\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}

答案4

LaTeX 2ε 中已经有一个命令\index,所以不要让我们使用该名称,而是让我们分别命名命令\Summationindex\FreeParameter

expl3 让\tl_item:nn您选择标记列表中的一项。您可以使用它来定义从数字到符号的映射:

\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}

您还可以考虑通过 expl3 实现从数字到符号的映射,\int_case:nnF在这种情况下,如果未定义映射,则更容易引发错误消息。

\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}

在此处输入图片描述

相关内容