TransWikia.com

How create/redefine document command where command name is variable?

TeX - LaTeX Asked on December 16, 2020

This question connected with question about tagging of sections. I found solution,how to do it,but i dont know,how create/redefine document command,if command is argument of some other command. When i try to redefine it,i get an error:

LaTeX3 Error: First argument of ‘RenewDocumentCommand’ must be a command.

Help me please fix this. Also i have some doubts:

  1. Should i use [{##2}] or simple [##2] for the second argument.
  2. should i use {##3} or simple ##3 for third argument? Thanks everybody for any help.
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}

2 Answers

I am guessing this is what you want to do.

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}

If you want to know more about LaTeX3, you can refer to a tutorial here.

Correct answer by Alan Xiang on December 16, 2020

I solved my problem,and here my code. I am not understood,why for * argument ifvalue command not fail,even without *,but for [] it's failed. @ulrike-fischerhow in your opinion i can fix situation with unwanted spaces around the sections?

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:NoRenewDocumentCommand {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}

Answered by Aleksandr Kozlovskiy on December 16, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP