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}









share|improve this question


























    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}









    share|improve this question
























      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}









      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 5 hours ago









      lAtExFaN

      567418




      567418






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Three errors:




          1. you have to expand @myTestSty@myKey when defining myKey;

          2. you have to expand myKey to test whether its replacement text is empty;

          3. the definition of myKey should 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}


          enter image description here



          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}





          share|improve this answer























          • 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-in myKey={Hello World!} the output also says blank? The l3keys2e code works fine.
            – lAtExFaN
            4 hours ago








          • 1




            @lAtExFaN That was the third error. ;-)
            – egreg
            4 hours ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "85"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          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

























          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:




          1. you have to expand @myTestSty@myKey when defining myKey;

          2. you have to expand myKey to test whether its replacement text is empty;

          3. the definition of myKey should 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}


          enter image description here



          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}





          share|improve this answer























          • 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-in myKey={Hello World!} the output also says blank? The l3keys2e code works fine.
            – lAtExFaN
            4 hours ago








          • 1




            @lAtExFaN That was the third error. ;-)
            – egreg
            4 hours ago















          up vote
          3
          down vote



          accepted










          Three errors:




          1. you have to expand @myTestSty@myKey when defining myKey;

          2. you have to expand myKey to test whether its replacement text is empty;

          3. the definition of myKey should 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}


          enter image description here



          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}





          share|improve this answer























          • 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-in myKey={Hello World!} the output also says blank? The l3keys2e code works fine.
            – lAtExFaN
            4 hours ago








          • 1




            @lAtExFaN That was the third error. ;-)
            – egreg
            4 hours ago













          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          Three errors:




          1. you have to expand @myTestSty@myKey when defining myKey;

          2. you have to expand myKey to test whether its replacement text is empty;

          3. the definition of myKey should 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}


          enter image description here



          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}





          share|improve this answer














          Three errors:




          1. you have to expand @myTestSty@myKey when defining myKey;

          2. you have to expand myKey to test whether its replacement text is empty;

          3. the definition of myKey should 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}


          enter image description here



          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}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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-in myKey={Hello World!} the output also says blank? The l3keys2e code 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










          • 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-in myKey={Hello World!} the output also says blank? The l3keys2e code 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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Entries order in /etc/network/interfaces

          新発田市

          Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)