TeX - LaTeX Asked by BlackBird on January 7, 2021
I wanted to create a macro that outputs "A", and if "A" is empty, then output "B". This would be similar to Latex’s: ifdefempty{@author}{@authora}{oorientador}
.
unprotect
defauthor#1{gdef@author{#1}}
defauthora#1{gdef@authora{#1}}
defmything{
doifemptyelse{@author}{@authora}{@author}par
}
protect
%author{John Doe}
authora{Johnny Olsten}
starttext
mything
stoptext
You have to expand @author
if you want to use doifemptyelse
as it is. Also, you need to initialise @author
:
unprotect
def@author{}
defauthor#1{gdef@author{#1}}
defauthora#1{gdef@authora{#1}}
defmything{%
expandafterdoifemptyelseexpandafter{@author}{@authora}{@author}par
}
protect
%author{John Doe}
authora{Johnny Olsten}
starttext
mything
stoptext
Correct answer by Skillmon on January 7, 2021
Here's another approach.
ConTeXt provides getparameters[<prefix>][<key>=<value>]
so you automatically create @author
and @authora
. Thus, mything
will only print @authora
when @author
is undefined. This will works if you don't need to work with a previously defined @author
.
unprotect
defmything{doifdefinedelse{@author}{@author}{@authora}}
protect
starttext
%This defines @authora
getparameters[@][authora=Mammals]
%@author is still undefined
mything %Mammals
%This defines @author
getparameters[@][author=Birds and reptiles]
mything %Birds and reptiles
%This redefines @author and @authora
getparameters[@][author=A. U. Thor, authora=A. U. Thora]
mything %A. U. Thor
%You can also use @author and @authora
getvalue{@author} %A. U. Thor
getvalue{@authora}%A. U. Thora
%What if I want to clear an author field?
letvalue{@author}=undefined
mything %A. U. Thora
stoptext
If you want to stick to LaTeX syntax a wrapper (such as defauthor#1{getparameters[@][author=#1]}
) isn't difficult to write, but it's certainly more verbose and useless. In such a case you have Skillmon's solution. IMHO you should get rid of those @
s, but de gustibus non est disputandum.
Answered by Jairo A. del Rio on January 7, 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