Detect empty or null output from xstring's StrBetween












2















I have some code for formatting names. However, the ifempty macro from xifthen is unable to detect empty/null output from xstring macros such as StrBetween. The same goes for the equal {} method. How can I detect empty output from StrBetween? The below code for some reason compiles and does not work.



documentclass{article}

usepackage{xstring}
usepackage{xifthen}

newcommand{name}{John Doe}

newcommand{middleinitial}{%
StrBetween{name}{ }{.}
}

newcommand{test}{%
ifthenelse{isempty{middleinitial}}%
{Whoo hoo!}%
{Not whoo hoo.}
}

begin{document}

test

end{document}


Any ideas?










share|improve this question





























    2















    I have some code for formatting names. However, the ifempty macro from xifthen is unable to detect empty/null output from xstring macros such as StrBetween. The same goes for the equal {} method. How can I detect empty output from StrBetween? The below code for some reason compiles and does not work.



    documentclass{article}

    usepackage{xstring}
    usepackage{xifthen}

    newcommand{name}{John Doe}

    newcommand{middleinitial}{%
    StrBetween{name}{ }{.}
    }

    newcommand{test}{%
    ifthenelse{isempty{middleinitial}}%
    {Whoo hoo!}%
    {Not whoo hoo.}
    }

    begin{document}

    test

    end{document}


    Any ideas?










    share|improve this question



























      2












      2








      2








      I have some code for formatting names. However, the ifempty macro from xifthen is unable to detect empty/null output from xstring macros such as StrBetween. The same goes for the equal {} method. How can I detect empty output from StrBetween? The below code for some reason compiles and does not work.



      documentclass{article}

      usepackage{xstring}
      usepackage{xifthen}

      newcommand{name}{John Doe}

      newcommand{middleinitial}{%
      StrBetween{name}{ }{.}
      }

      newcommand{test}{%
      ifthenelse{isempty{middleinitial}}%
      {Whoo hoo!}%
      {Not whoo hoo.}
      }

      begin{document}

      test

      end{document}


      Any ideas?










      share|improve this question
















      I have some code for formatting names. However, the ifempty macro from xifthen is unable to detect empty/null output from xstring macros such as StrBetween. The same goes for the equal {} method. How can I detect empty output from StrBetween? The below code for some reason compiles and does not work.



      documentclass{article}

      usepackage{xstring}
      usepackage{xifthen}

      newcommand{name}{John Doe}

      newcommand{middleinitial}{%
      StrBetween{name}{ }{.}
      }

      newcommand{test}{%
      ifthenelse{isempty{middleinitial}}%
      {Whoo hoo!}%
      {Not whoo hoo.}
      }

      begin{document}

      test

      end{document}


      Any ideas?







      xstring xifthen






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago







      Adam Erickson

















      asked 3 hours ago









      Adam EricksonAdam Erickson

      1558




      1558






















          2 Answers
          2






          active

          oldest

          votes


















          3














          middleinitial is not expandable, and therefore you cannot properly test whether there is a middle initial in name or not. You'll have to store the output (the middle initial) first using the optional argument at the end of StrBetween{<str>}{<from>}{<to>}[<macro>] and then you can check for an empty argument:



          enter image description here



          documentclass{article}

          usepackage{xstring}

          newcommand{name}{John Doe}

          makeatletter
          newcommand{middleinitial}{%
          StrBetween{name}{ }{.}[@middleinitial]%
          }

          newcommand{test}{%
          middleinitial% Find middle initial
          % https://tex.stackexchange.com/q/53068/5764
          expandafterifexpandafterrelaxexpandafterdetokenizeexpandafter{@middleinitial}relax
          Whoo hoo!% No middle initial
          else
          Not whoo hoo.% A middle initial
          fi
          }
          makeatother

          begin{document}

          test % Whoo hoo.

          renewcommand{name}{John F. Doe}%
          test % Not whoo hoo!

          end{document}





          share|improve this answer
























          • What is this monstrosity: expandafterifexpandafterrelaxexpandafterdetokenizeexpandafter{@middleinitial}relax?

            – Adam Erickson
            1 hour ago



















          1














          Werner already explained, why middleinitial isn't expandable due to the usage of xstring macros.



          I present a shorter way in order to test for the emptiness of middleinitial by using ifblank from etoolbox package. There are still two expandafter statements required, but not a bunch of 5 of them. In addition, I find etoolbox much more convenient than ifthen or xifthen.



          expandafterifblankexpandafter{middleinitial}{true branch}{false branch} is necessary, because ifblank does not expand its first argument, i.e. without expandafter the macro 'sees' middleinitial, but not what the content which is stored in that macro. It must be expanded first before ifblank performs the test.



          The expandafter primitive looks ahead of ifblank, i.e. it ignores ifblank first and detects the { of the first argument. Unfortunately, this is not what is desired (and not expandable anyway), so we must jump over { again, i.e.



          expandafterifblankexpandafter{middleinitial}{...}{...}



          will allow TeX/LaTeX to 'jump' over ifblank to the second expandafter, that jumps over { to middleinitial and expands that sequence -- after this is done, TeX continues with ifblank{expanded version of middleinitial}{...}{...} and performs the test finally.



          documentclass{article}

          usepackage{xstring}
          usepackage{etoolbox}

          newcommand{name}{John C. Doe}

          newcommand{othername}{John Doe}


          newcommand{testinitial}[3]{%
          begingroup
          StrBetween{#1}{ }{.}[middleinitial]%
          expandafterifblankexpandafter{middleinitial}{#2}{#3}%
          endgroup
          }


          begin{document}



          testinitial{name}{Whoo hoo!}{Not whoo hoo.}

          testinitial{othername}{Whoo hoo!}{Not whoo hoo.}

          end{document}


          enter image description here






          share|improve this answer

























            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',
            autoActivateHeartbeat: false,
            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%2f469915%2fdetect-empty-or-null-output-from-xstrings-strbetween%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            middleinitial is not expandable, and therefore you cannot properly test whether there is a middle initial in name or not. You'll have to store the output (the middle initial) first using the optional argument at the end of StrBetween{<str>}{<from>}{<to>}[<macro>] and then you can check for an empty argument:



            enter image description here



            documentclass{article}

            usepackage{xstring}

            newcommand{name}{John Doe}

            makeatletter
            newcommand{middleinitial}{%
            StrBetween{name}{ }{.}[@middleinitial]%
            }

            newcommand{test}{%
            middleinitial% Find middle initial
            % https://tex.stackexchange.com/q/53068/5764
            expandafterifexpandafterrelaxexpandafterdetokenizeexpandafter{@middleinitial}relax
            Whoo hoo!% No middle initial
            else
            Not whoo hoo.% A middle initial
            fi
            }
            makeatother

            begin{document}

            test % Whoo hoo.

            renewcommand{name}{John F. Doe}%
            test % Not whoo hoo!

            end{document}





            share|improve this answer
























            • What is this monstrosity: expandafterifexpandafterrelaxexpandafterdetokenizeexpandafter{@middleinitial}relax?

              – Adam Erickson
              1 hour ago
















            3














            middleinitial is not expandable, and therefore you cannot properly test whether there is a middle initial in name or not. You'll have to store the output (the middle initial) first using the optional argument at the end of StrBetween{<str>}{<from>}{<to>}[<macro>] and then you can check for an empty argument:



            enter image description here



            documentclass{article}

            usepackage{xstring}

            newcommand{name}{John Doe}

            makeatletter
            newcommand{middleinitial}{%
            StrBetween{name}{ }{.}[@middleinitial]%
            }

            newcommand{test}{%
            middleinitial% Find middle initial
            % https://tex.stackexchange.com/q/53068/5764
            expandafterifexpandafterrelaxexpandafterdetokenizeexpandafter{@middleinitial}relax
            Whoo hoo!% No middle initial
            else
            Not whoo hoo.% A middle initial
            fi
            }
            makeatother

            begin{document}

            test % Whoo hoo.

            renewcommand{name}{John F. Doe}%
            test % Not whoo hoo!

            end{document}





            share|improve this answer
























            • What is this monstrosity: expandafterifexpandafterrelaxexpandafterdetokenizeexpandafter{@middleinitial}relax?

              – Adam Erickson
              1 hour ago














            3












            3








            3







            middleinitial is not expandable, and therefore you cannot properly test whether there is a middle initial in name or not. You'll have to store the output (the middle initial) first using the optional argument at the end of StrBetween{<str>}{<from>}{<to>}[<macro>] and then you can check for an empty argument:



            enter image description here



            documentclass{article}

            usepackage{xstring}

            newcommand{name}{John Doe}

            makeatletter
            newcommand{middleinitial}{%
            StrBetween{name}{ }{.}[@middleinitial]%
            }

            newcommand{test}{%
            middleinitial% Find middle initial
            % https://tex.stackexchange.com/q/53068/5764
            expandafterifexpandafterrelaxexpandafterdetokenizeexpandafter{@middleinitial}relax
            Whoo hoo!% No middle initial
            else
            Not whoo hoo.% A middle initial
            fi
            }
            makeatother

            begin{document}

            test % Whoo hoo.

            renewcommand{name}{John F. Doe}%
            test % Not whoo hoo!

            end{document}





            share|improve this answer













            middleinitial is not expandable, and therefore you cannot properly test whether there is a middle initial in name or not. You'll have to store the output (the middle initial) first using the optional argument at the end of StrBetween{<str>}{<from>}{<to>}[<macro>] and then you can check for an empty argument:



            enter image description here



            documentclass{article}

            usepackage{xstring}

            newcommand{name}{John Doe}

            makeatletter
            newcommand{middleinitial}{%
            StrBetween{name}{ }{.}[@middleinitial]%
            }

            newcommand{test}{%
            middleinitial% Find middle initial
            % https://tex.stackexchange.com/q/53068/5764
            expandafterifexpandafterrelaxexpandafterdetokenizeexpandafter{@middleinitial}relax
            Whoo hoo!% No middle initial
            else
            Not whoo hoo.% A middle initial
            fi
            }
            makeatother

            begin{document}

            test % Whoo hoo.

            renewcommand{name}{John F. Doe}%
            test % Not whoo hoo!

            end{document}






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 2 hours ago









            WernerWerner

            439k659651658




            439k659651658













            • What is this monstrosity: expandafterifexpandafterrelaxexpandafterdetokenizeexpandafter{@middleinitial}relax?

              – Adam Erickson
              1 hour ago



















            • What is this monstrosity: expandafterifexpandafterrelaxexpandafterdetokenizeexpandafter{@middleinitial}relax?

              – Adam Erickson
              1 hour ago

















            What is this monstrosity: expandafterifexpandafterrelaxexpandafterdetokenizeexpandafter{@middleinitial}relax?

            – Adam Erickson
            1 hour ago





            What is this monstrosity: expandafterifexpandafterrelaxexpandafterdetokenizeexpandafter{@middleinitial}relax?

            – Adam Erickson
            1 hour ago











            1














            Werner already explained, why middleinitial isn't expandable due to the usage of xstring macros.



            I present a shorter way in order to test for the emptiness of middleinitial by using ifblank from etoolbox package. There are still two expandafter statements required, but not a bunch of 5 of them. In addition, I find etoolbox much more convenient than ifthen or xifthen.



            expandafterifblankexpandafter{middleinitial}{true branch}{false branch} is necessary, because ifblank does not expand its first argument, i.e. without expandafter the macro 'sees' middleinitial, but not what the content which is stored in that macro. It must be expanded first before ifblank performs the test.



            The expandafter primitive looks ahead of ifblank, i.e. it ignores ifblank first and detects the { of the first argument. Unfortunately, this is not what is desired (and not expandable anyway), so we must jump over { again, i.e.



            expandafterifblankexpandafter{middleinitial}{...}{...}



            will allow TeX/LaTeX to 'jump' over ifblank to the second expandafter, that jumps over { to middleinitial and expands that sequence -- after this is done, TeX continues with ifblank{expanded version of middleinitial}{...}{...} and performs the test finally.



            documentclass{article}

            usepackage{xstring}
            usepackage{etoolbox}

            newcommand{name}{John C. Doe}

            newcommand{othername}{John Doe}


            newcommand{testinitial}[3]{%
            begingroup
            StrBetween{#1}{ }{.}[middleinitial]%
            expandafterifblankexpandafter{middleinitial}{#2}{#3}%
            endgroup
            }


            begin{document}



            testinitial{name}{Whoo hoo!}{Not whoo hoo.}

            testinitial{othername}{Whoo hoo!}{Not whoo hoo.}

            end{document}


            enter image description here






            share|improve this answer






























              1














              Werner already explained, why middleinitial isn't expandable due to the usage of xstring macros.



              I present a shorter way in order to test for the emptiness of middleinitial by using ifblank from etoolbox package. There are still two expandafter statements required, but not a bunch of 5 of them. In addition, I find etoolbox much more convenient than ifthen or xifthen.



              expandafterifblankexpandafter{middleinitial}{true branch}{false branch} is necessary, because ifblank does not expand its first argument, i.e. without expandafter the macro 'sees' middleinitial, but not what the content which is stored in that macro. It must be expanded first before ifblank performs the test.



              The expandafter primitive looks ahead of ifblank, i.e. it ignores ifblank first and detects the { of the first argument. Unfortunately, this is not what is desired (and not expandable anyway), so we must jump over { again, i.e.



              expandafterifblankexpandafter{middleinitial}{...}{...}



              will allow TeX/LaTeX to 'jump' over ifblank to the second expandafter, that jumps over { to middleinitial and expands that sequence -- after this is done, TeX continues with ifblank{expanded version of middleinitial}{...}{...} and performs the test finally.



              documentclass{article}

              usepackage{xstring}
              usepackage{etoolbox}

              newcommand{name}{John C. Doe}

              newcommand{othername}{John Doe}


              newcommand{testinitial}[3]{%
              begingroup
              StrBetween{#1}{ }{.}[middleinitial]%
              expandafterifblankexpandafter{middleinitial}{#2}{#3}%
              endgroup
              }


              begin{document}



              testinitial{name}{Whoo hoo!}{Not whoo hoo.}

              testinitial{othername}{Whoo hoo!}{Not whoo hoo.}

              end{document}


              enter image description here






              share|improve this answer




























                1












                1








                1







                Werner already explained, why middleinitial isn't expandable due to the usage of xstring macros.



                I present a shorter way in order to test for the emptiness of middleinitial by using ifblank from etoolbox package. There are still two expandafter statements required, but not a bunch of 5 of them. In addition, I find etoolbox much more convenient than ifthen or xifthen.



                expandafterifblankexpandafter{middleinitial}{true branch}{false branch} is necessary, because ifblank does not expand its first argument, i.e. without expandafter the macro 'sees' middleinitial, but not what the content which is stored in that macro. It must be expanded first before ifblank performs the test.



                The expandafter primitive looks ahead of ifblank, i.e. it ignores ifblank first and detects the { of the first argument. Unfortunately, this is not what is desired (and not expandable anyway), so we must jump over { again, i.e.



                expandafterifblankexpandafter{middleinitial}{...}{...}



                will allow TeX/LaTeX to 'jump' over ifblank to the second expandafter, that jumps over { to middleinitial and expands that sequence -- after this is done, TeX continues with ifblank{expanded version of middleinitial}{...}{...} and performs the test finally.



                documentclass{article}

                usepackage{xstring}
                usepackage{etoolbox}

                newcommand{name}{John C. Doe}

                newcommand{othername}{John Doe}


                newcommand{testinitial}[3]{%
                begingroup
                StrBetween{#1}{ }{.}[middleinitial]%
                expandafterifblankexpandafter{middleinitial}{#2}{#3}%
                endgroup
                }


                begin{document}



                testinitial{name}{Whoo hoo!}{Not whoo hoo.}

                testinitial{othername}{Whoo hoo!}{Not whoo hoo.}

                end{document}


                enter image description here






                share|improve this answer















                Werner already explained, why middleinitial isn't expandable due to the usage of xstring macros.



                I present a shorter way in order to test for the emptiness of middleinitial by using ifblank from etoolbox package. There are still two expandafter statements required, but not a bunch of 5 of them. In addition, I find etoolbox much more convenient than ifthen or xifthen.



                expandafterifblankexpandafter{middleinitial}{true branch}{false branch} is necessary, because ifblank does not expand its first argument, i.e. without expandafter the macro 'sees' middleinitial, but not what the content which is stored in that macro. It must be expanded first before ifblank performs the test.



                The expandafter primitive looks ahead of ifblank, i.e. it ignores ifblank first and detects the { of the first argument. Unfortunately, this is not what is desired (and not expandable anyway), so we must jump over { again, i.e.



                expandafterifblankexpandafter{middleinitial}{...}{...}



                will allow TeX/LaTeX to 'jump' over ifblank to the second expandafter, that jumps over { to middleinitial and expands that sequence -- after this is done, TeX continues with ifblank{expanded version of middleinitial}{...}{...} and performs the test finally.



                documentclass{article}

                usepackage{xstring}
                usepackage{etoolbox}

                newcommand{name}{John C. Doe}

                newcommand{othername}{John Doe}


                newcommand{testinitial}[3]{%
                begingroup
                StrBetween{#1}{ }{.}[middleinitial]%
                expandafterifblankexpandafter{middleinitial}{#2}{#3}%
                endgroup
                }


                begin{document}



                testinitial{name}{Whoo hoo!}{Not whoo hoo.}

                testinitial{othername}{Whoo hoo!}{Not whoo hoo.}

                end{document}


                enter image description here







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 12 mins ago

























                answered 30 mins ago









                Christian HupferChristian Hupfer

                149k14194390




                149k14194390






























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f469915%2fdetect-empty-or-null-output-from-xstrings-strbetween%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

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'