
Sou iniciante em Latex3. O teste 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}
E nesta fase está funcionando bem. Imprime "201" conforme esperado.
Em seguida tentei ver o que há dentro da \my_mod
função, para poder entender melhor sobreLátex3. Escrevi:
\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}
E nesta fase o TexStudio não conseguiu compilar o rendimentoSequência de controle indefinida. \mostrar{3}{7}erro. Devo mencionar que ocorreu o mesmo erro quando mudei \cs_show:c
de função com \cs_show:N
.
Como posso usar \cs_show
a função de maneira adequada?
Responder1
\cs_show:N
apenas registra a definição da seguinte sequência de controle e não mostra seu “funcionamento”.
Observe também que \my_mod:nn
e \my_mod
não estão relacionados de forma alguma no que diz respeito ao TeX. Você define o primeiro, mas não o último e mostrá-lo produzirá “indefinido”.
Você pode ver o funcionamento, na verdade, usando 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}
O \hbox
objetivo é evitar efeitos espúrios devido ao \everypar
momento em que o TeX está tentando compor o resultado. Você verá 63 etapas, das quais mostro apenas algumas.
======== 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 =====]
Já que você está aprendendo, alguns conselhos. A função \my_mod:nn
deveria ser protected
, porque você faz uma tarefa. Mas você pode evitá-lo:
\cs_new:Npn \my_mod:nn #1 #2
{
\int_eval:n { (#1) - \int_div_truncate:nn {#1}{#2} * (#2) }
}