
我是 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
函數內部有什麼,這樣我就可以更好地理解乳膠3。我寫:
\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) }
}