![expl3 编写的一段代码有误({ O{} m } 的正确用法)](https://linux22.com/image/454285/expl3%20%E7%BC%96%E5%86%99%E7%9A%84%E4%B8%80%E6%AE%B5%E4%BB%A3%E7%A0%81%E6%9C%89%E8%AF%AF%EF%BC%88%7B%20O%7B%7D%20m%20%7D%20%E7%9A%84%E6%AD%A3%E7%A1%AE%E7%94%A8%E6%B3%95%EF%BC%89.png)
请考虑下面这段我复制并修改过的代码cas-common.sty
:
\tl_new:N \g_stm_mydate_day_tl
\tl_new:N \g_stm_mydate_year_tl
\cs_new:Npn \date_parse:n #1 { \date_parse_aux:w #1 \q_stop }
\cs_new:Npn \date_parse_aux:w #1 / #2 / #3 \q_stop
{ <do something with the date> } %<------ What does this? It is a warning or it is ready to write a command instead of text?
\NewDocumentCommand \storemydate { O{} m }
{
\tl_if_eq:nnTF { #1 } { day }
{ \tl_if_gput_right:Nn \g_stm_mydate_day_tl { #2 } }
{ \tl_if_gput_right:Nn \g_stm_mydate_year_tl { #2 } }
}
首先,第三和第四个代码是起什么作用的?
其次,我知道最后一段代码是一个命令,我可以使用它,\storemydate[optional]{argument}
而且似乎第一个返回 true 比较,第二个返回 false 结果。但是当我使用\storemydate[day]{02 September}
它时会返回Undefined control sequence. \storemydate[day]{02 Sep}
错误。(Pdflatex
TexLive 2021)
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\tl_new:N \g_stm_mydate_day_tl
\tl_new:N \g_stm_mydate_year_tl
\cs_new:Npn \date_parse:n #1 { \date_parse_aux:w #1 \q_stop }
\cs_new:Npn \date_parse_aux:w #1 / #2 / #3 \q_stop
{ <do something with the date> } %<------ What does this? It is a warning or it is ready to write a command instead of text?
\NewDocumentCommand \storemydate { O{} m }
{
\tl_if_eq:nnTF { #1 } { day }
{ \tl_if_gput_right:Nn \g_stm_mydate_day_tl { #2 } }
{ \tl_if_gput_right:Nn \g_stm_mydate_year_tl { #2 } }
}
\ExplSyntaxOff
\begin{document}
\section{Test}
\storemydate[day]{02 Sep}
\end{document}
答案1
您没有显示错误消息,即
! Undefined control sequence.
<argument> \tl_if_gput_right:Nn
\g_stm_mydate_day_tl {02 Sep}
l.24 \storemydate[day]{02 Sep}
?
显示\tl_if_gput_right:Nn
未定义,可能是拼写错误\tl_gput_right:Nn
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\tl_new:N \g_stm_mydate_day_tl
\tl_new:N \g_stm_mydate_year_tl
\cs_new:Npn \date_parse:n #1 { \date_parse_aux:w #1 \q_stop }
\cs_new:Npn \date_parse_aux:w #1 / #2 / #3 \q_stop
{ <do something with the date> } %<------ What does this? It is a warning or it is ready to write a command instead of text?
\NewDocumentCommand \storemydate { O{} m }
{
\tl_if_eq:nnTF { #1 } { day }
{ \tl_gput_right:Nn \g_stm_mydate_day_tl { #2 } }
{ \tl_gput_right:Nn \g_stm_mydate_year_tl { #2 } }
}
\ExplSyntaxOff
\begin{document}
\section{Test}
\storemydate[day]{02 Sep}
\end{document}
运行无错误,并将主要参数存储在\g_stm_mydate_year_tl
除非[day]
使用。
\usepackage{xparse}
仅在旧版本中才需要定义\NewDocumentCommand
\cs_new:Npn \date_parse:n #1 { \date_parse_aux:w #1 \q_stop }
\cs_new:Npn \date_parse_aux:w #1 / #2 / #3 \q_stop
{ <do something with the date> } %<------ What does this? It is a warning or it is ready to write a command instead of text?
这里未使用草图来拆分 yyyy/mm/dd 格式的日期,以便未编写的代码<do something with the date>
可以用作#1
年份、#2
月份和#3
日期