这个问题与关于标记部分的问题有关。我找到了解决方案,知道如何做,但我不知道如何创建/重新定义文档命令,如果命令是其他命令的参数。当我尝试重新定义它时,我收到错误:
LaTeX3 错误:“\RenewDocumentCommand”的第一个参数必须是命令。
请帮我解决这个问题。此外,我还有一些疑问:
- 我应该使用 [{##2}] 还是简单的 [##2] 作为第二个参数。
- 我应该使用 {##3} 还是简单的 ##3 作为第三个参数?感谢大家的帮助。
\RequirePackage[patches]{pdfresources}
\DeclareDocumentMetadata{pdfversion=2.0}
\documentclass{article}
\usepackage{tagpdf}
\tagpdfsetup{tabsorder=structure,uncompress,activate-all,add-new-tag=Title/P,interwordspace=true}
\ExplSyntaxOn
\makeatletter
\prop_gset_from_keyval:Nn{\g__sections_prop}{chapter=H1,section=H2,subsection=H3,subsubsection=H4}
\prop_map_inline:Nn \g__sections_prop{
\cs_set_eq:cc{orig@#1}{#1}
\RenewDocumentCommand #1{s o m}{
\tagstructbegin{tag=#2}
\tagmcbegin{tag=#2}
\use:x{
\exp_not:c {orig@#1}
\IfValueT {##1} { ##1 }
\IfValueT {##2} { [{##2}]}
{##3}
}
\tagmcend
\tagstructend
}
}
\makeatother
\ExplSyntaxOff
\author{Alexandr Kozlovskiy}
\title{test}
\begin{document}
\tagstructbegin{tag=Document}
\tagstructbegin{tag=Title}
\tagmcbegin{tag=Title}
\maketitle
\tableofcontents
\tagmcend
\tagstructend
\section{test}
\tagstructbegin{tag=P}
\tagmcbegin{tag=P}
new test
\tagmcend
\tagstructend
\section{new test}
\tagstructbegin{tag=P}
\tagmcbegin{tag=P}
new test again
\tagmcend
\tagstructend
\tagstructend
\end{document}
答案1
我猜这就是你想做的事。
\documentclass{article}
\usepackage{expl3}
\usepackage{xparse}
% let's declare two commands
\newcommand{\testcmda}{}
\newcommand{\testcmdb}{}
% declare a variable to store command name
\newcommand{\cmdname}{}
\ExplSyntaxOn
% a function to renew commands
\newcommand{\myrenew}[2]{
\exp_args:Nc \RenewDocumentCommand{#1}{m}{
\par my~name:~#1
\par args~2~from outer:~#2
\par args~1~from~inner:~##1
}
}
\ExplSyntaxOff
\begin{document}
\renewcommand{\cmdname}{testcmda}
\myrenew{\cmdname}{a}
\testcmda{b}
% you can type variable name directly as well
\myrenew{testcmda}{c}
\testcmda{d}
\renewcommand{\cmdname}{testcmdb}
\myrenew{\cmdname}{e}
\testcmdb{f}
\end{document}
如果你想了解更多关于 LaTeX3 的知识,可以参考教程这里。
答案2
我解决了我的问题,这是我的代码。我不明白,为什么对于 * 参数 ifvalue 命令不会失败,即使没有 *,但对于 [] 却失败了。@ulrike-fischer 您认为我该如何解决部分周围出现不需要的空格的情况?
\RequirePackage[patches]{pdfmanagement}
\DeclareDocumentMetadata{pdfversion=2.0}
\documentclass{article}
\usepackage{tagpdf}
\tagpdfsetup{tabsorder=structure,uncompress,activate-all,add-new-tag=Title/P,interwordspace=true}
\ExplSyntaxOn
\makeatletter
\prop_gset_from_keyval:Nn{\g__sections_prop}{section=H2,subsection=H3,subsubsection=H4}
\prop_map_inline:Nn \g__sections_prop{
\cs_set_eq:cc{orig@#1}{#1}
\exp_args:No\RenewDocumentCommand {\cs:w #1 \cs_end:}{s o m}{
\tagstructbegin{tag=#2}
\tagmcbegin{tag=#2}
\use:x{
\exp_not:c {orig@#1}
\IfBooleanT {##1} { * }
\IfValueT {##2} { [##2]}
{##3}
}
\tagmcend
\tagstructend
}
}
\makeatother
\ExplSyntaxOff
\author{Alexandr Kozlovskiy}
\title{test}
\begin{document}
\tagstructbegin{tag=Document}
\tagstructbegin{tag=Title}
\tagmcbegin{tag=Title}
\maketitle
\tableofcontents
\tagmcend
\tagstructend
\section{test}
\tagstructbegin{tag=P}
\tagmcbegin{tag=P}
new test
\tagmcend
\tagstructend
\section{new test}
\tagstructbegin{tag=P}
\tagmcbegin{tag=P}
new test again
\tagmcend
\tagstructend
\tagstructend
\end{document}