我是 Latex3 的初学者。测试 MWE 是:
\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse} % loads expl3
\begin{document}
\ExplSyntaxOn
\cs_set:Npn \my_mod:nn #1#2 {
% store #1//#2 in \l_tmpa_int
\int_set:Nn \l_tmpa_int { \int_div_truncate:nn {#1}{#2} }
% compute (#1)-\l_tmpa_int*(#2)
% make sure to surround operands with parentheses
% so that when #1 is an expression (e.g. 3-2)
% the order of arithmetic will not change
\int_eval:n { (#1) - \l_tmpa_int * (#2) }
}
% define LaTeX interface
\newcommand{\mymod}[2]{
\my_mod:nn {#1} {#2}
}
\ExplSyntaxOff
\mymod{5}{3}\mymod{6}{3}\mymod{7}{1+2}%201
\end{document}
并且在此阶段运行良好。它按预期打印“201”。
接下来我尝试看看\my_mod
函数内部有什么,这样我就能更好地理解Latex3。 我写:
\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse} % loads expl3
\begin{document}
\ExplSyntaxOn
\cs_set:Npn \my_mod:nn #1#2 {
% store #1//#2 in \l_tmpa_int
\int_set:Nn \l_tmpa_int { \int_div_truncate:nn {#1}{#2} }
% compute (#1)-\l_tmpa_int*(#2)
% make sure to surround operands with parentheses
% so that when #1 is an expression (e.g. 3-2)
% the order of arithmetic will not change
\int_eval:n { (#1) - \l_tmpa_int * (#2) }
}
\newcommand{\showw}[2]{
\cs_show:c \my_mod
}
% define LaTeX interface
\newcommand{\mymod}[2]{
\my_mod:nn {#1} {#2}
}
\ExplSyntaxOff
\mymod{5}{3}
\mymod{6}{3}
\mymod{7}{1+2}%201
\showw{3}{7}
\end{document}
此时 TexStudio 编译失败,结果未定义控制序列。\showw{3}{7}错误。我必须提到,当我\cs_show:c
用 更改函数时,出现了同样的错误\cs_show:N
。
我该如何\cs_show
正确使用功能?
答案1
\cs_show:N
仅记录以下控制序列的定义并不显示其“工作”。
还要注意,就 TeX 而言,\my_mod:nn
和没有任何关系。你定义了前者,但没有定义后者,显示它会产生“未定义”的结果。\my_mod
实际上,您可以使用来查看其工作原理unravel
。
\documentclass{article}
\usepackage{unravel}
\ExplSyntaxOn
\cs_set:Npn \my_mod:nn #1#2 {
% store #1//#2 in \l_tmpa_int
\int_set:Nn \l_tmpa_int { \int_div_truncate:nn {#1}{#2} }
% compute (#1)-\l_tmpa_int*(#2)
% make sure to surround operands with parentheses
% so that when #1 is an expression (e.g. 3-2)
% the order of arithmetic will not change
\int_eval:n { (#1) - \l_tmpa_int * (#2) }
}
\newcommand{\mymod}[2]{
\my_mod:nn {#1} {#2}
}
\ExplSyntaxOff
\begin{document}
\hbox{\unravel{\mymod{5}{3}}}
%\mymod{6}{3}
%\mymod{7}{1+2}%201
\end{document}
这\hbox
是为了避免\everypar
TeX 尝试排版结果时产生虚假效果。您将看到 63 个步骤,其中我只展示了几个。
======== Welcome to the unravel package ========
"<|" denotes the output to TeX's stomach.
"||" denotes tokens waiting to be used.
"|>" denotes tokens that we will act on.
Press <enter> to continue; 'h' <enter> for help.
||
|> \mymod {5}{3}
[===== Step 1 =====] \mymod = \long macro:#1#2->\my_mod:nn {#1}{#2}
||
|> \my_mod:nn {5}{3}
[===== Step 2 =====] \my_mod:nn = \long macro:#1#2->\int_set:Nn \l_t...
||
|> \int_set:Nn \l_tmpa_int {\int_div_truncate:nn {5}{3}}\int_eval:n
|> {(5)-\l_tmpa_int *(3)}
[===== Step 3 =====] \int_set:Nn = \protected\long macro:#1#2->#1=\_...
||
|> \l_tmpa_int =\__int_eval:w \int_div_truncate:nn {5}{3}\__int_eval_end:
|> \int_eval:n {(5)-\l_tmpa_int *(3)}
[...steps omitted...]
[===== Step 63 =====] )
|| \tex_the:D
|| \__int_eval:w (5)-1*(3)
|> \__int_eval_end:
[===== Step 64 =====] \tex_the:D =>2
||
|> 2
[===== Step 65 =====] 2
<| 2
||
|>
[===== End =====]
既然你正在学习,这里有几句建议。函数\my_mod:nn
应该是protected
,因为你要做一个作业。但你可以避免它:
\cs_new:Npn \my_mod:nn #1 #2
{
\int_eval:n { (#1) - \int_div_truncate:nn {#1}{#2} * (#2) }
}