LuaTeX 改变数字的 catcode

LuaTeX 改变数字的 catcode

我正在尝试使用 \ifdefine \r@labelname 检查标签是否已定义。标签是十六进制数字,如 215Bh,这不起作用。所以我尝试将数字的 catcode 更改为 11

但我收到了错误,\catcode`\2=11因为 1 不再是数字:

./mwe.tex:12: Missing number, treated as zero.

如果我重新排序 catcode 行以更改末尾的代码 1,则在切换回时会遇到问题\catcodetable 0

% !TeX program = lualatex
% !TeX encoding = UTF-8 Unicode
\documentclass{scrartcl}
\usepackage{hyperref}
\begin{document}
    \label{test}
    \label{215Ah}
    \initcatcodetable 3
    \makeatletter
    \catcode`\0=11
    \catcode`\1=11
    \catcode`\2=11
    \catcode`\3=11
    \catcode`\4=11
    \catcode`\5=11
    \catcode`\6=11
    \catcode`\7=11
    \catcode`\8=11
    \catcode`\9=11
    \ifdefined \r@test test is  defined \fi
    \ifdefined \r@215Ah  215Ah is  defined \fi
    \catcodetable 0
    \makeatother
\end{document}

答案1

您可以使用 LaTeX 内部\@ifundefined

\documentclass{scrartcl}
\usepackage{hyperref}
\begin{document}
    \label{test}
    \label{215Ah}
    \makeatletter
    test is \@ifundefined{r@test}{undefined}{defined}.
    215Ah is \@ifundefined{r@215Ah}{undefined}{defined}.
    tongo is \@ifundefined{r@tongo}{undefined}{defined}.
    \makeatother
\end{document}

或者不用\makeatletter\makeatother你可以使用\ifcsname

\documentclass{scrartcl}
\usepackage{hyperref}
\begin{document}
    \label{test}
    \label{215Ah}
    test is \ifcsname r@test\endcsname defined\else undefined\fi.
    215Ah is \ifcsname r@215Ah\endcsname defined\else undefined\fi.
    tongo is \ifcsname r@tongo\endcsname defined\else undefined\fi.
\end{document}

但如果你真的想摆弄\catcode你可以使用:

\documentclass{scrartcl}
\usepackage{hyperref}
\newcommand*{\makeatanddigitsletter}{%
  \makeatletter
  \catcode`\0=\catcode`\@
  \catcode`\1=\catcode`\@
  \catcode`\2=\catcode`\@
  \catcode`\3=\catcode`\@
  \catcode`\4=\catcode`\@
  \catcode`\5=\catcode`\@
  \catcode`\6=\catcode`\@
  \catcode`\7=\catcode`\@
  \catcode`\8=\catcode`\@
  \catcode`\9=\catcode`\@
}
  
\begin{document}
\label{test}
\label{215Ah}
\begingroup
  \makeatanddigitsletter
  test is \ifdefined \r@test defined\else undefined\fi.
  215Ah is \ifdefined \r@215Ah defined\else undefined\fi.
\endgroup
\end{document}
 

答案2

您可以使用 catcode 表,但需要一些技巧。

\documentclass{article}

\begingroup
\def\setcodeletter#1{\catcode`#1=11 }
\setcodeletter @
\setcodeletter 0
\setcodeletter 1
\setcodeletter 2
\setcodeletter 3
\setcodeletter 4
\setcodeletter 5
\setcodeletter 6
\setcodeletter 7
\setcodeletter 8
\setcodeletter 9
\savecatcodetable \string 3
\endgroup

\expandafter\def\csname r@215Ah\endcsname{AAA} % emulate having read the .aux file

\begin{document}

\begingroup
\catcodetable 3
\ifdefined \r@test test is defined\else test is undefined\fi

\ifdefined \r@215Ah 215Ah is defined\else 215Ah is undefined\fi
\endgroup

\end{document}

在此处输入图片描述

手册对此进行了说明,并\savecatcodetable在全球范围内适用。因此该命令\setcodeletter将消失。

请注意\catcodetable 0,因为它没有为括号和其他特殊字符设置必要的 catcode(与 iniTeX 启动时的分配相同)。

相关内容