TeX - LaTeX Asked by Shalom Alecheim on February 18, 2021
I have to define a lot of text variables with some default value.
I do it next way:
defname#1{gdef@name{#1}}%
name{Alex}
I tried to automatize this process, but it is not working
newcommand{DefineAndSet}[2]{%
defcsname #1endcsname ##1{gdef@csname #1endcsname{##1}}%
csname #1endcsname{#2}
}
My MNWE:
documentclass{article}
newcommand{DefineAndSet}[2]{%
defcsname #1endcsname ##1{gdef@csname #1endcsname{##1}}%
csname #1endcsname{#2}
}
begin{document}
DefineAndSet{name}{Alex}
name
end{document}
There are several errors in your code: you need to build the symbolic token with csname
before def
or gdef
see it. And the @
should go inside csname
.
newcommand{DefineAndSet}[2]{%
expandafterdefcsname #1endcsname ##1{expandaftergdefcsname @#1endcsname{##1}}%
csname #1endcsname{#2}%
}
On the other hand, I don't see the need for the @name
command.
newcommand{DefineAndSet}[2]{%
expandaftergdefcsname #1endcsname{#2}%
#2%
}
would do what you need. With a problem, though: you're not checking whether name
(or whatever is produced from the first argument to DefineAndSet
) is already defined. This can be overcome by
makeatletter
newcommand{DefineAndSet}[2]{%
@ifdefinable{#1}{gdef#1{#2}}#2%
}
makeatother
to be simply called by
DefineAndSet{name}{Alex}
Correct answer by egreg on February 18, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP