How to test if a macro's value (set via kvoptions in a package) is empty or blank (e.g. using etoolbox)?
up vote
1
down vote
favorite
I have no idea, why this test fails and how to get it to work:
documentclass{article}
usepackage{filecontents}
begin{filecontents}{myTestSty.sty}
NeedsTeXFormat{LaTeX2e}[1994/06/01]
ProvidesPackage{myTestSty}[2018/11/29 v. 1.0 myTestSty]
RequirePackage{kvoptions}
SetupKeyvalOptions{family=@myTestSty,prefix=@myTestSty@}
DeclareStringOption{myKey}
defmyKey{@myTestSty@myKey}
ProcessKeyvalOptions*
end{filecontents}
usepackage{etoolbox}
usepackage[%
%myKey={Hello world!},
myKey={} %<-- blank?
]{myTestSty}
begin{document}
myKey: --{myKey}--
myKey: ifblank{myKey}{blank}{not-blank}
end{document}
etoolbox kvoptions
add a comment |
up vote
1
down vote
favorite
I have no idea, why this test fails and how to get it to work:
documentclass{article}
usepackage{filecontents}
begin{filecontents}{myTestSty.sty}
NeedsTeXFormat{LaTeX2e}[1994/06/01]
ProvidesPackage{myTestSty}[2018/11/29 v. 1.0 myTestSty]
RequirePackage{kvoptions}
SetupKeyvalOptions{family=@myTestSty,prefix=@myTestSty@}
DeclareStringOption{myKey}
defmyKey{@myTestSty@myKey}
ProcessKeyvalOptions*
end{filecontents}
usepackage{etoolbox}
usepackage[%
%myKey={Hello world!},
myKey={} %<-- blank?
]{myTestSty}
begin{document}
myKey: --{myKey}--
myKey: ifblank{myKey}{blank}{not-blank}
end{document}
etoolbox kvoptions
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have no idea, why this test fails and how to get it to work:
documentclass{article}
usepackage{filecontents}
begin{filecontents}{myTestSty.sty}
NeedsTeXFormat{LaTeX2e}[1994/06/01]
ProvidesPackage{myTestSty}[2018/11/29 v. 1.0 myTestSty]
RequirePackage{kvoptions}
SetupKeyvalOptions{family=@myTestSty,prefix=@myTestSty@}
DeclareStringOption{myKey}
defmyKey{@myTestSty@myKey}
ProcessKeyvalOptions*
end{filecontents}
usepackage{etoolbox}
usepackage[%
%myKey={Hello world!},
myKey={} %<-- blank?
]{myTestSty}
begin{document}
myKey: --{myKey}--
myKey: ifblank{myKey}{blank}{not-blank}
end{document}
etoolbox kvoptions
I have no idea, why this test fails and how to get it to work:
documentclass{article}
usepackage{filecontents}
begin{filecontents}{myTestSty.sty}
NeedsTeXFormat{LaTeX2e}[1994/06/01]
ProvidesPackage{myTestSty}[2018/11/29 v. 1.0 myTestSty]
RequirePackage{kvoptions}
SetupKeyvalOptions{family=@myTestSty,prefix=@myTestSty@}
DeclareStringOption{myKey}
defmyKey{@myTestSty@myKey}
ProcessKeyvalOptions*
end{filecontents}
usepackage{etoolbox}
usepackage[%
%myKey={Hello world!},
myKey={} %<-- blank?
]{myTestSty}
begin{document}
myKey: --{myKey}--
myKey: ifblank{myKey}{blank}{not-blank}
end{document}
etoolbox kvoptions
etoolbox kvoptions
asked 5 hours ago
lAtExFaN
567418
567418
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Three errors:
- you have to expand
@myTestSty@myKeywhen definingmyKey; - you have to expand
myKeyto test whether its replacement text is empty; - the definition of
myKeyshould go after processing the options.
documentclass{article}
usepackage{filecontents}
begin{filecontents}{myTestSty.sty}
NeedsTeXFormat{LaTeX2e}[1994/06/01]
ProvidesPackage{myTestSty}[2018/11/29 v. 1.0 myTestSty]
RequirePackage{kvoptions}
SetupKeyvalOptions{family=@myTestSty,prefix=@myTestSty@}
DeclareStringOption{myKey}
ProcessKeyvalOptions*
edefmyKey{expandonce{@myTestSty@myKey}} % <--- expand
end{filecontents}
usepackage{etoolbox}
usepackage[%
%myKey={Hello world!},
myKey={} %<-- blank?
]{myTestSty}
begin{document}
myKey: X{myKey}X
myKey: expandafterifblankexpandafter{myKey}{blank}{not-blank}
end{document}

You can shorten the test by defining
newcommand{ifblanke}[1]{%
expandafterifblankexpandafter{#1}%
}
and call
ifblanke{myKey}{blank}{not~blank}
A different approach with l3keys2e:
documentclass{article}
usepackage{filecontents}
begin{filecontents}{myTestSty.sty}
NeedsTeXFormat{LaTeX2e}[1994/06/01]
ProvidesPackage{myTestSty}[2018/11/29 v. 1.0 myTestSty]
RequirePackage{expl3,l3keys2e,xparse}
ExplSyntaxOn
keys_define:nn { mytest/main }
{
myKey .tl_set:N = myKey,
}
ProcessKeysPackageOptions { mytest/main }
NewExpandableDocumentCommand{IfBlankTF}{smmm}
{
IfBooleanTF{#1}
{
tl_if_blank:VTF #2 { #3 } { #4 }
}
{
tl_if_blank:nTF #2 { #3 } { #4 }
}
}
ExplSyntaxOff
end{filecontents}
usepackage[%
%myKey={Hello world!},
myKey={} %<-- blank?
]{myTestSty}
begin{document}
myKey: X{myKey}X
myKey: IfBlankTF*{myKey}{blank}{not-blank}
end{document}
Ahhh, thank you very much @egreg. Now I see.
– lAtExFaN
5 hours ago
Is it possible to shorten the test? E.g. introduce some kind of a helper-macro or wrapper that holds the expandafter ... expandafter?
– lAtExFaN
5 hours ago
@lAtExFaN Yes, I added it, together with a different approach.
– egreg
5 hours ago
Hmm, "blank" seems to be hard-coded ;-) I mean: If I comment-inmyKey={Hello World!}the output also saysblank? Thel3keys2ecode works fine.
– lAtExFaN
4 hours ago
1
@lAtExFaN That was the third error.;-)
– egreg
4 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Three errors:
- you have to expand
@myTestSty@myKeywhen definingmyKey; - you have to expand
myKeyto test whether its replacement text is empty; - the definition of
myKeyshould go after processing the options.
documentclass{article}
usepackage{filecontents}
begin{filecontents}{myTestSty.sty}
NeedsTeXFormat{LaTeX2e}[1994/06/01]
ProvidesPackage{myTestSty}[2018/11/29 v. 1.0 myTestSty]
RequirePackage{kvoptions}
SetupKeyvalOptions{family=@myTestSty,prefix=@myTestSty@}
DeclareStringOption{myKey}
ProcessKeyvalOptions*
edefmyKey{expandonce{@myTestSty@myKey}} % <--- expand
end{filecontents}
usepackage{etoolbox}
usepackage[%
%myKey={Hello world!},
myKey={} %<-- blank?
]{myTestSty}
begin{document}
myKey: X{myKey}X
myKey: expandafterifblankexpandafter{myKey}{blank}{not-blank}
end{document}

You can shorten the test by defining
newcommand{ifblanke}[1]{%
expandafterifblankexpandafter{#1}%
}
and call
ifblanke{myKey}{blank}{not~blank}
A different approach with l3keys2e:
documentclass{article}
usepackage{filecontents}
begin{filecontents}{myTestSty.sty}
NeedsTeXFormat{LaTeX2e}[1994/06/01]
ProvidesPackage{myTestSty}[2018/11/29 v. 1.0 myTestSty]
RequirePackage{expl3,l3keys2e,xparse}
ExplSyntaxOn
keys_define:nn { mytest/main }
{
myKey .tl_set:N = myKey,
}
ProcessKeysPackageOptions { mytest/main }
NewExpandableDocumentCommand{IfBlankTF}{smmm}
{
IfBooleanTF{#1}
{
tl_if_blank:VTF #2 { #3 } { #4 }
}
{
tl_if_blank:nTF #2 { #3 } { #4 }
}
}
ExplSyntaxOff
end{filecontents}
usepackage[%
%myKey={Hello world!},
myKey={} %<-- blank?
]{myTestSty}
begin{document}
myKey: X{myKey}X
myKey: IfBlankTF*{myKey}{blank}{not-blank}
end{document}
Ahhh, thank you very much @egreg. Now I see.
– lAtExFaN
5 hours ago
Is it possible to shorten the test? E.g. introduce some kind of a helper-macro or wrapper that holds the expandafter ... expandafter?
– lAtExFaN
5 hours ago
@lAtExFaN Yes, I added it, together with a different approach.
– egreg
5 hours ago
Hmm, "blank" seems to be hard-coded ;-) I mean: If I comment-inmyKey={Hello World!}the output also saysblank? Thel3keys2ecode works fine.
– lAtExFaN
4 hours ago
1
@lAtExFaN That was the third error.;-)
– egreg
4 hours ago
add a comment |
up vote
3
down vote
accepted
Three errors:
- you have to expand
@myTestSty@myKeywhen definingmyKey; - you have to expand
myKeyto test whether its replacement text is empty; - the definition of
myKeyshould go after processing the options.
documentclass{article}
usepackage{filecontents}
begin{filecontents}{myTestSty.sty}
NeedsTeXFormat{LaTeX2e}[1994/06/01]
ProvidesPackage{myTestSty}[2018/11/29 v. 1.0 myTestSty]
RequirePackage{kvoptions}
SetupKeyvalOptions{family=@myTestSty,prefix=@myTestSty@}
DeclareStringOption{myKey}
ProcessKeyvalOptions*
edefmyKey{expandonce{@myTestSty@myKey}} % <--- expand
end{filecontents}
usepackage{etoolbox}
usepackage[%
%myKey={Hello world!},
myKey={} %<-- blank?
]{myTestSty}
begin{document}
myKey: X{myKey}X
myKey: expandafterifblankexpandafter{myKey}{blank}{not-blank}
end{document}

You can shorten the test by defining
newcommand{ifblanke}[1]{%
expandafterifblankexpandafter{#1}%
}
and call
ifblanke{myKey}{blank}{not~blank}
A different approach with l3keys2e:
documentclass{article}
usepackage{filecontents}
begin{filecontents}{myTestSty.sty}
NeedsTeXFormat{LaTeX2e}[1994/06/01]
ProvidesPackage{myTestSty}[2018/11/29 v. 1.0 myTestSty]
RequirePackage{expl3,l3keys2e,xparse}
ExplSyntaxOn
keys_define:nn { mytest/main }
{
myKey .tl_set:N = myKey,
}
ProcessKeysPackageOptions { mytest/main }
NewExpandableDocumentCommand{IfBlankTF}{smmm}
{
IfBooleanTF{#1}
{
tl_if_blank:VTF #2 { #3 } { #4 }
}
{
tl_if_blank:nTF #2 { #3 } { #4 }
}
}
ExplSyntaxOff
end{filecontents}
usepackage[%
%myKey={Hello world!},
myKey={} %<-- blank?
]{myTestSty}
begin{document}
myKey: X{myKey}X
myKey: IfBlankTF*{myKey}{blank}{not-blank}
end{document}
Ahhh, thank you very much @egreg. Now I see.
– lAtExFaN
5 hours ago
Is it possible to shorten the test? E.g. introduce some kind of a helper-macro or wrapper that holds the expandafter ... expandafter?
– lAtExFaN
5 hours ago
@lAtExFaN Yes, I added it, together with a different approach.
– egreg
5 hours ago
Hmm, "blank" seems to be hard-coded ;-) I mean: If I comment-inmyKey={Hello World!}the output also saysblank? Thel3keys2ecode works fine.
– lAtExFaN
4 hours ago
1
@lAtExFaN That was the third error.;-)
– egreg
4 hours ago
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Three errors:
- you have to expand
@myTestSty@myKeywhen definingmyKey; - you have to expand
myKeyto test whether its replacement text is empty; - the definition of
myKeyshould go after processing the options.
documentclass{article}
usepackage{filecontents}
begin{filecontents}{myTestSty.sty}
NeedsTeXFormat{LaTeX2e}[1994/06/01]
ProvidesPackage{myTestSty}[2018/11/29 v. 1.0 myTestSty]
RequirePackage{kvoptions}
SetupKeyvalOptions{family=@myTestSty,prefix=@myTestSty@}
DeclareStringOption{myKey}
ProcessKeyvalOptions*
edefmyKey{expandonce{@myTestSty@myKey}} % <--- expand
end{filecontents}
usepackage{etoolbox}
usepackage[%
%myKey={Hello world!},
myKey={} %<-- blank?
]{myTestSty}
begin{document}
myKey: X{myKey}X
myKey: expandafterifblankexpandafter{myKey}{blank}{not-blank}
end{document}

You can shorten the test by defining
newcommand{ifblanke}[1]{%
expandafterifblankexpandafter{#1}%
}
and call
ifblanke{myKey}{blank}{not~blank}
A different approach with l3keys2e:
documentclass{article}
usepackage{filecontents}
begin{filecontents}{myTestSty.sty}
NeedsTeXFormat{LaTeX2e}[1994/06/01]
ProvidesPackage{myTestSty}[2018/11/29 v. 1.0 myTestSty]
RequirePackage{expl3,l3keys2e,xparse}
ExplSyntaxOn
keys_define:nn { mytest/main }
{
myKey .tl_set:N = myKey,
}
ProcessKeysPackageOptions { mytest/main }
NewExpandableDocumentCommand{IfBlankTF}{smmm}
{
IfBooleanTF{#1}
{
tl_if_blank:VTF #2 { #3 } { #4 }
}
{
tl_if_blank:nTF #2 { #3 } { #4 }
}
}
ExplSyntaxOff
end{filecontents}
usepackage[%
%myKey={Hello world!},
myKey={} %<-- blank?
]{myTestSty}
begin{document}
myKey: X{myKey}X
myKey: IfBlankTF*{myKey}{blank}{not-blank}
end{document}
Three errors:
- you have to expand
@myTestSty@myKeywhen definingmyKey; - you have to expand
myKeyto test whether its replacement text is empty; - the definition of
myKeyshould go after processing the options.
documentclass{article}
usepackage{filecontents}
begin{filecontents}{myTestSty.sty}
NeedsTeXFormat{LaTeX2e}[1994/06/01]
ProvidesPackage{myTestSty}[2018/11/29 v. 1.0 myTestSty]
RequirePackage{kvoptions}
SetupKeyvalOptions{family=@myTestSty,prefix=@myTestSty@}
DeclareStringOption{myKey}
ProcessKeyvalOptions*
edefmyKey{expandonce{@myTestSty@myKey}} % <--- expand
end{filecontents}
usepackage{etoolbox}
usepackage[%
%myKey={Hello world!},
myKey={} %<-- blank?
]{myTestSty}
begin{document}
myKey: X{myKey}X
myKey: expandafterifblankexpandafter{myKey}{blank}{not-blank}
end{document}

You can shorten the test by defining
newcommand{ifblanke}[1]{%
expandafterifblankexpandafter{#1}%
}
and call
ifblanke{myKey}{blank}{not~blank}
A different approach with l3keys2e:
documentclass{article}
usepackage{filecontents}
begin{filecontents}{myTestSty.sty}
NeedsTeXFormat{LaTeX2e}[1994/06/01]
ProvidesPackage{myTestSty}[2018/11/29 v. 1.0 myTestSty]
RequirePackage{expl3,l3keys2e,xparse}
ExplSyntaxOn
keys_define:nn { mytest/main }
{
myKey .tl_set:N = myKey,
}
ProcessKeysPackageOptions { mytest/main }
NewExpandableDocumentCommand{IfBlankTF}{smmm}
{
IfBooleanTF{#1}
{
tl_if_blank:VTF #2 { #3 } { #4 }
}
{
tl_if_blank:nTF #2 { #3 } { #4 }
}
}
ExplSyntaxOff
end{filecontents}
usepackage[%
%myKey={Hello world!},
myKey={} %<-- blank?
]{myTestSty}
begin{document}
myKey: X{myKey}X
myKey: IfBlankTF*{myKey}{blank}{not-blank}
end{document}
edited 4 hours ago
answered 5 hours ago
egreg
701k8618653140
701k8618653140
Ahhh, thank you very much @egreg. Now I see.
– lAtExFaN
5 hours ago
Is it possible to shorten the test? E.g. introduce some kind of a helper-macro or wrapper that holds the expandafter ... expandafter?
– lAtExFaN
5 hours ago
@lAtExFaN Yes, I added it, together with a different approach.
– egreg
5 hours ago
Hmm, "blank" seems to be hard-coded ;-) I mean: If I comment-inmyKey={Hello World!}the output also saysblank? Thel3keys2ecode works fine.
– lAtExFaN
4 hours ago
1
@lAtExFaN That was the third error.;-)
– egreg
4 hours ago
add a comment |
Ahhh, thank you very much @egreg. Now I see.
– lAtExFaN
5 hours ago
Is it possible to shorten the test? E.g. introduce some kind of a helper-macro or wrapper that holds the expandafter ... expandafter?
– lAtExFaN
5 hours ago
@lAtExFaN Yes, I added it, together with a different approach.
– egreg
5 hours ago
Hmm, "blank" seems to be hard-coded ;-) I mean: If I comment-inmyKey={Hello World!}the output also saysblank? Thel3keys2ecode works fine.
– lAtExFaN
4 hours ago
1
@lAtExFaN That was the third error.;-)
– egreg
4 hours ago
Ahhh, thank you very much @egreg. Now I see.
– lAtExFaN
5 hours ago
Ahhh, thank you very much @egreg. Now I see.
– lAtExFaN
5 hours ago
Is it possible to shorten the test? E.g. introduce some kind of a helper-macro or wrapper that holds the expandafter ... expandafter?
– lAtExFaN
5 hours ago
Is it possible to shorten the test? E.g. introduce some kind of a helper-macro or wrapper that holds the expandafter ... expandafter?
– lAtExFaN
5 hours ago
@lAtExFaN Yes, I added it, together with a different approach.
– egreg
5 hours ago
@lAtExFaN Yes, I added it, together with a different approach.
– egreg
5 hours ago
Hmm, "blank" seems to be hard-coded ;-) I mean: If I comment-in
myKey={Hello World!} the output also says blank? The l3keys2e code works fine.– lAtExFaN
4 hours ago
Hmm, "blank" seems to be hard-coded ;-) I mean: If I comment-in
myKey={Hello World!} the output also says blank? The l3keys2e code works fine.– lAtExFaN
4 hours ago
1
1
@lAtExFaN That was the third error.
;-)– egreg
4 hours ago
@lAtExFaN That was the third error.
;-)– egreg
4 hours ago
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f462468%2fhow-to-test-if-a-macros-value-set-via-kvoptions-in-a-package-is-empty-or-blan%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown