
Soy principiante en Latex3. La prueba MWE es:
\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}
Y en esta etapa está funcionando bien. Imprime "201" como se esperaba.
Luego intenté ver qué hay dentro de la \my_mod
función, para poder comprender mejorlátex3. Escribí:
\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}
Y en esta etapa TexStudio no pudo compilar y dar como resultadoSecuencia de control Indefinido. \mostrar{3}{7}error. Debo mencionar que se produjo el mismo error cuando cambié \cs_show:c
la función con \cs_show:N
.
¿Cómo puedo utilizar \cs_show
la función de forma adecuada?
Respuesta1
\cs_show:N
simplemente registra la definición de la siguiente secuencia de control y no muestra su "funcionamiento".
Tenga en cuenta también que \my_mod:nn
y \my_mod
no están relacionados de ninguna manera en lo que respecta a TeX. Usted define el primero, pero no el segundo y al mostrarlo se producirá "indefinido".
De hecho, puedes ver el funcionamiento 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}
Esto \hbox
es para evitar efectos espurios debidos a \everypar
que TeX intenta componer el resultado. Verás 63 pasos, de los cuales muestro sólo algunos.
======== 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 =====]
Ya que estás aprendiendo, unos cuantos consejos. La función \my_mod:nn
debería ser protected
, porque haces una tarea. Pero puedes evitarlo:
\cs_new:Npn \my_mod:nn #1 #2
{
\int_eval:n { (#1) - \int_div_truncate:nn {#1}{#2} * (#2) }
}