带有特殊字符的宏名称

带有特殊字符的宏名称

是否可以创建包含特殊字符的宏名称?

我想创建一些具有类似名称的宏\s1.4并且能够使用它们,就像\s1.4Test测试不被视为宏名称的一部分一样(基本上浮点数之后的任何内容都被排除在宏名称之外)

基本上,这是为了避免必须这样做\s{1.4}Text,这会添加两个额外的字符,并且对于我的目的来说\s1.4没有任何问题(我永远不会有类似的事情\s1.x,并且期望 x 不是宏的一部分,所以永远不会有任何歧义)。

顺便说一句,我使用 foreach 定义这些:

\makeatletter
\foreach \m in {1.1, 2.2} 
{
    \expandafter\protected@xdef\csname s\m\endcsname{$_\mathbf{\m}$}
}
\makeatother

答案1

在此处输入图片描述

普通的 TeX 示例,但它可以在 latex 中工作,\s更简单但需要. \t更复杂但可以接受整数。

\def\s#1.{%
  \def\tmp{#1.}%
  \afterassignment\xxs\count0= }

\def\xxs{\csname s\tmp\the\count0\endcsname}


%%%%%%%%%%%%%%%%%%%

\def\t{\afterassignment\xxt\count0= }

\def\xxt{\futurelet\tmp\xxxt}

\def\xxxt{%
\if.\noexpand\tmp
\expandafter\xxxxt
\else
\csname s\the\count0\expandafter\endcsname
\fi}

\def\xxxxt.{\afterassignment\xtx\count2= }

\def\xtx{\csname s\the\count0.\the\count2\endcsname}


%%%%%%%%%%%%%%%%%%%




\expandafter\def\csname s1.4\endcsname{one point four }
\expandafter\def\csname s2.6\endcsname{ten point six }
\expandafter\def\csname s5\endcsname{five }

%%%%%%%%%%%%%%%%%%%

\s1.4more text \s2.6 and more



\t1.4more text \t2.6 and more and \t5 yet more



\bye

答案2

在以下示例中,宏\s仅收集数字和点,并将它们排版为粗体下标,如问题中所示。\@ifnextchar无法使用 LaTeX,因为它在查找下一个标记时会吞噬空格。

\documentclass{article}
\usepackage{ltxcmds}[2011/04/14]

\makeatletter
\newcommand*{\s}{%
  \begingroup
  \let\tmp\ltx@empty
  \s@aux
}
\newcommand*{\s@aux}{%
  \ltx@ifnextchar@nospace{.}{%
    \edef\tmp{\tmp.}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{0}{%
    \edef\tmp{\tmp0}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{1}{%
    \edef\tmp{\tmp1}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{2}{%
    \edef\tmp{\tmp2}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{3}{%
    \edef\tmp{\tmp3}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{4}{%
    \edef\tmp{\tmp4}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{5}{%
    \edef\tmp{\tmp5}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{6}{%
    \edef\tmp{\tmp6}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{7}{%
    \edef\tmp{\tmp7}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{8}{%
    \edef\tmp{\tmp8}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{9}{%
    \edef\tmp{\tmp9}%
    \expandafter\s@aux\ltx@gobble
  }{%
    \expandafter\s@end\expandafter{\tmp}%
  }}}}}}}}}}}%
}
\newcommand*{\s@end}[1]{%
  \endgroup
  \ensuremath{_\mathbf{#1}}%
}
\makeatother

\begin{document}
  \s1.4Test and \s2more.
\end{document}

结果

更新

\s如果后面没有数字和点,或者数字/点字符串未定义,则可以轻松添加包含错误消息的映射层。

\documentclass{article}
\usepackage{pgffor}
\usepackage{ltxcmds}[2011/04/14]

\makeatletter
\foreach \m in {1.1, 1.4, 2, 2.2}{%
  \expandafter\protected@xdef\csname s\m\endcsname{$_\mathbf{\m}$}%
}

\newcommand*{\s}{%
  \begingroup
  \let\tmp\ltx@empty
  \s@aux
}
\newcommand*{\s@aux}{%
  \ltx@ifnextchar@nospace{.}{%
    \edef\tmp{\tmp.}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{0}{%
    \edef\tmp{\tmp0}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{1}{%
    \edef\tmp{\tmp1}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{2}{%
    \edef\tmp{\tmp2}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{3}{%
    \edef\tmp{\tmp3}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{4}{%
    \edef\tmp{\tmp4}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{5}{%
    \edef\tmp{\tmp5}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{6}{%
    \edef\tmp{\tmp6}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{7}{%
    \edef\tmp{\tmp7}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{8}{%
    \edef\tmp{\tmp8}%
    \expandafter\s@aux\ltx@gobble
  }{\ltx@ifnextchar@nospace{9}{%
    \edef\tmp{\tmp9}%
    \expandafter\s@aux\ltx@gobble
  }{%
    \ifx\tmp\@empty
      \@latex@error{\string\s: no digits/dots found}\@ehc
    \else
      \@ifundefined{s\tmp}{%
        \@latex@error{\string\s: Undefined `\tmp'}\@ehc
      }{%
        \@nameuse{s\tmp}%
      }%
    \fi 
    \endgroup
  }}}}}}}}}}}%
}
\makeatother

\begin{document}
  \s1.4Test and \s2more.
\end{document}

在下一个示例中,的解析参数\s是一个以一个或多个数字、可选的点和可选的数字开头的字符串。

\documentclass{article}
\usepackage{pgffor}
\usepackage{ltxcmds}[2011/04/14]

\makeatletter
\foreach \m in {1.1, 1.4, 2, 2.2}{%
  \expandafter\protected@xdef\csname s\m\endcsname{$_\mathbf{\m}$}%
}

\newcommand*{\s}{%
  \begingroup
  \let\s@tmp\ltx@empty
  \futurelet\s@tok\s@startnum
}
\newcommand*{\s@startnum}{%
  \s@digit\s@digitordot
  \s@next
}
\newcommand*{\s@digit}[1]{%
  \def\s@next{\s@add#1}%
  \ifx0\s@tok
  \else\ifx1\s@tok
  \else\ifx2\s@tok
  \else\ifx3\s@tok
  \else\ifx4\s@tok
  \else\ifx5\s@tok
  \else\ifx6\s@tok
  \else\ifx7\s@tok
  \else\ifx8\s@tok
  \else\ifx9\s@tok
  \else
    \let\s@next\s@end
  \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi
}
\newcommand*{\s@add}[2]{%
  \edef\s@tmp{\s@tmp#2}%
  \futurelet\s@tok
  #1%
}
\newcommand*{\s@digitordot}{%
  \let\s@next\s@end
  \ifx.\s@tok
    \def\s@next{\s@add\s@endnum}%
  \else
    \s@digit\s@digitordot
  \fi
  \s@next
}
\newcommand*{\s@endnum}{%
  \s@digit\s@endnum
  \s@next
}
\newcommand*{\s@end}{%
  \ifx\s@tmp\@empty   
    \@latex@error{\string\s: no digits/dots found}\@ehc
  \else
    \@ifundefined{s\s@tmp}{%
      \@latex@error{\string\s: Undefined `\s@tmp'}\@ehc
    }{%
      \@nameuse{s\s@tmp}%
    }%
  \fi 
  \endgroup
}
\makeatother

\begin{document}
  \s1.4Test and \s2more.
\end{document}

答案3

如果句点后的第二个数字只有一位数,则可以使用以下命令:

\def\s#1.#2{$_\mathbf{#1.#2}$}

相关内容