How to test if argument is a single space?Looping over stringsHow to capitalise every letter in odd position as in memes?Can you write a macro such that each character will be the next color of the rainbowSplit single argument in multiple partsWhy do braces around a single-letter argument of a macro affect its output?def'ed plain-text argument behaves differently from plain texthow to determine the number of characters in the argument of a commandUsing the compulsory argument for the optional argument?Omitting argument in command definitionDefine macro whose argument is a wordSine squared with parentheses for argumentWhat is this newcommand argument?Macro that takes any latex code as an argument

How strong are Wi-Fi signals?

what kind of chord progession is this?

My employer faked my resume to acquire projects

Popcorn is the only acceptable snack to consume while watching a movie

Where's this lookout in Nova Scotia?

Is the taxi route omitted in low visibility (LVP)?

The art of clickbait captions

Why is this Simple Puzzle impossible to solve?

Compaq Portable vs IBM 5155 Portable PC

Caught 2 students cheating together on the final exam that I proctored

How to Pin Point Large File eating space in Fedora 18

What is a really good book for complex variables?

Is the field of q-series 'dead'?

How to illustrate the Mean Value theorem?

How to know if a folder is a symbolic link?

Using credit/debit card details vs swiping a card in a payment (credit card) terminal

How to deal with a colleague who is being aggressive?

Employer asking for online access to bank account - Is this a scam?

Could a 19.25mm revolver actually exist?

How to patch glass cuts in a bicycle tire?

Looking for a soft substance that doesn't dissolve underwater

Did the UK Government ask for the Irish backstop?

A steel cutting sword?

Why did David Cameron offer a referendum on the European Union?



How to test if argument is a single space?


Looping over stringsHow to capitalise every letter in odd position as in memes?Can you write a macro such that each character will be the next color of the rainbowSplit single argument in multiple partsWhy do braces around a single-letter argument of a macro affect its output?def'ed plain-text argument behaves differently from plain texthow to determine the number of characters in the argument of a commandUsing the compulsory argument for the optional argument?Omitting argument in command definitionDefine macro whose argument is a wordSine squared with parentheses for argumentWhat is this newcommand argument?Macro that takes any latex code as an argument













7















I am currently writing a document, that sometimes requires me to alternate the colors of the letters in a sentence. I thought it would be easy to find a way to create a macro that does that, but I've run into some problems.



MWE:



% !TeX program = xelatex
% !TeX spellcheck = en_US

documentclass[11pt,a4paper,english,twoside,notitlepage]book

usepackagefontspec
usepackagelmodern
usepackage[english,main=english]babel
usepackagexcolor
usepackageetoolbox


definecolorpurpleHTML2B0057
definecolorcandyHTMLFF0000

newcounteralternate

%the macro in question
newcommandcolset[1]%
ifnumvaluealternate=0 colorcandy#1setcounteralternate1%
elsecolorpurple#1setcounteralternate0%
fi


%snippet for the loop taken from https://tex.stackexchange.com/questions/359189/looping-over-strings
%iterates over the supplied string and replaces every letter with colset<letter>
defgobblecharletxchar=
defassignthencheckafterassignmentxloopgobblechar
defxloop%
ifxrelaxxchar
letnext=relax
else
colsetxcharletnext=assignthencheck
fi
next
defmarkletters#1setcounteralternate0assignthencheck#1relax

begindocument
marklettersHello World
enddocument


Output: Output



The issue is that I have found no way to skip the spaces (i.e. the "W" in "World" should be purple, not red). I thought it would be easy to simply check if the argument is a space and just skip the rest of the macro, but after hours of searching and just trial-and-error (approaches included etoolbox's ifblank#1, constructs like ifx#1 or even ifnum`#1=32, creating temporary macros [e.g. edeftemp#1ifblanktemp]) I am still no step closer, and I'm definitely out of my depth on this one.



Is there a way to check if the argument of a macro is only a space? If no, are there any other ways to construct this macro? (I'm using XeLaTeX via MiKTeX)



Thanks!










share|improve this question






















  • Thanks for the reference to my cuestion :-)

    – Raoul Kessels
    May 20 at 7:24















7















I am currently writing a document, that sometimes requires me to alternate the colors of the letters in a sentence. I thought it would be easy to find a way to create a macro that does that, but I've run into some problems.



MWE:



% !TeX program = xelatex
% !TeX spellcheck = en_US

documentclass[11pt,a4paper,english,twoside,notitlepage]book

usepackagefontspec
usepackagelmodern
usepackage[english,main=english]babel
usepackagexcolor
usepackageetoolbox


definecolorpurpleHTML2B0057
definecolorcandyHTMLFF0000

newcounteralternate

%the macro in question
newcommandcolset[1]%
ifnumvaluealternate=0 colorcandy#1setcounteralternate1%
elsecolorpurple#1setcounteralternate0%
fi


%snippet for the loop taken from https://tex.stackexchange.com/questions/359189/looping-over-strings
%iterates over the supplied string and replaces every letter with colset<letter>
defgobblecharletxchar=
defassignthencheckafterassignmentxloopgobblechar
defxloop%
ifxrelaxxchar
letnext=relax
else
colsetxcharletnext=assignthencheck
fi
next
defmarkletters#1setcounteralternate0assignthencheck#1relax

begindocument
marklettersHello World
enddocument


Output: Output



The issue is that I have found no way to skip the spaces (i.e. the "W" in "World" should be purple, not red). I thought it would be easy to simply check if the argument is a space and just skip the rest of the macro, but after hours of searching and just trial-and-error (approaches included etoolbox's ifblank#1, constructs like ifx#1 or even ifnum`#1=32, creating temporary macros [e.g. edeftemp#1ifblanktemp]) I am still no step closer, and I'm definitely out of my depth on this one.



Is there a way to check if the argument of a macro is only a space? If no, are there any other ways to construct this macro? (I'm using XeLaTeX via MiKTeX)



Thanks!










share|improve this question






















  • Thanks for the reference to my cuestion :-)

    – Raoul Kessels
    May 20 at 7:24













7












7








7








I am currently writing a document, that sometimes requires me to alternate the colors of the letters in a sentence. I thought it would be easy to find a way to create a macro that does that, but I've run into some problems.



MWE:



% !TeX program = xelatex
% !TeX spellcheck = en_US

documentclass[11pt,a4paper,english,twoside,notitlepage]book

usepackagefontspec
usepackagelmodern
usepackage[english,main=english]babel
usepackagexcolor
usepackageetoolbox


definecolorpurpleHTML2B0057
definecolorcandyHTMLFF0000

newcounteralternate

%the macro in question
newcommandcolset[1]%
ifnumvaluealternate=0 colorcandy#1setcounteralternate1%
elsecolorpurple#1setcounteralternate0%
fi


%snippet for the loop taken from https://tex.stackexchange.com/questions/359189/looping-over-strings
%iterates over the supplied string and replaces every letter with colset<letter>
defgobblecharletxchar=
defassignthencheckafterassignmentxloopgobblechar
defxloop%
ifxrelaxxchar
letnext=relax
else
colsetxcharletnext=assignthencheck
fi
next
defmarkletters#1setcounteralternate0assignthencheck#1relax

begindocument
marklettersHello World
enddocument


Output: Output



The issue is that I have found no way to skip the spaces (i.e. the "W" in "World" should be purple, not red). I thought it would be easy to simply check if the argument is a space and just skip the rest of the macro, but after hours of searching and just trial-and-error (approaches included etoolbox's ifblank#1, constructs like ifx#1 or even ifnum`#1=32, creating temporary macros [e.g. edeftemp#1ifblanktemp]) I am still no step closer, and I'm definitely out of my depth on this one.



Is there a way to check if the argument of a macro is only a space? If no, are there any other ways to construct this macro? (I'm using XeLaTeX via MiKTeX)



Thanks!










share|improve this question














I am currently writing a document, that sometimes requires me to alternate the colors of the letters in a sentence. I thought it would be easy to find a way to create a macro that does that, but I've run into some problems.



MWE:



% !TeX program = xelatex
% !TeX spellcheck = en_US

documentclass[11pt,a4paper,english,twoside,notitlepage]book

usepackagefontspec
usepackagelmodern
usepackage[english,main=english]babel
usepackagexcolor
usepackageetoolbox


definecolorpurpleHTML2B0057
definecolorcandyHTMLFF0000

newcounteralternate

%the macro in question
newcommandcolset[1]%
ifnumvaluealternate=0 colorcandy#1setcounteralternate1%
elsecolorpurple#1setcounteralternate0%
fi


%snippet for the loop taken from https://tex.stackexchange.com/questions/359189/looping-over-strings
%iterates over the supplied string and replaces every letter with colset<letter>
defgobblecharletxchar=
defassignthencheckafterassignmentxloopgobblechar
defxloop%
ifxrelaxxchar
letnext=relax
else
colsetxcharletnext=assignthencheck
fi
next
defmarkletters#1setcounteralternate0assignthencheck#1relax

begindocument
marklettersHello World
enddocument


Output: Output



The issue is that I have found no way to skip the spaces (i.e. the "W" in "World" should be purple, not red). I thought it would be easy to simply check if the argument is a space and just skip the rest of the macro, but after hours of searching and just trial-and-error (approaches included etoolbox's ifblank#1, constructs like ifx#1 or even ifnum`#1=32, creating temporary macros [e.g. edeftemp#1ifblanktemp]) I am still no step closer, and I'm definitely out of my depth on this one.



Is there a way to check if the argument of a macro is only a space? If no, are there any other ways to construct this macro? (I'm using XeLaTeX via MiKTeX)



Thanks!







macros arguments






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked May 19 at 22:50









GrzzlwmpfGrzzlwmpf

433




433












  • Thanks for the reference to my cuestion :-)

    – Raoul Kessels
    May 20 at 7:24

















  • Thanks for the reference to my cuestion :-)

    – Raoul Kessels
    May 20 at 7:24
















Thanks for the reference to my cuestion :-)

– Raoul Kessels
May 20 at 7:24





Thanks for the reference to my cuestion :-)

– Raoul Kessels
May 20 at 7:24










3 Answers
3






active

oldest

votes


















7














You are not iterating using macro arguments, so you do not need to test a macro argument, just test the token you have already:



enter image description here



documentclass[11pt,a4paper,english,twoside,notitlepage]book

usepackagefontspec
usepackagelmodern
usepackage[english,main=english]babel
usepackagexcolor
usepackageetoolbox


definecolorpurpleHTML2B0057
definecolorcandyHTMLFF0000

newcounteralternate

%the macro in question
newcommandcolset[1]%
ifnumvaluealternate=0 colorcandy#1setcounteralternate1%
elsecolorpurple#1setcounteralternate0%
fi

makeatletter
%snippet for the loop taken from https://tex.stackexchange.com/questions/359189/looping-over-strings
%iterates over the supplied string and replaces every letter with colset<letter>
defgobblecharletxchar=
defassignthencheckafterassignmentxloopgobblechar
defxloop%
ifxrelaxxchar
letnext=relax
else
ifx@sptokenxcharsetcounteralternatenumexpr1-valuealternatefi
colsetxcharletnext=assignthencheck
fi
next
makeatother
defmarkletters#1setcounteralternate0assignthencheck#1relax

begindocument
marklettersHello World
enddocument





share|improve this answer






























    4














    You can substitute spaces with something that expands to a space.



    documentclass[11pt,a4paper,english,twoside,notitlepage]book

    usepackagefontspec
    usepackagelmodern
    usepackage[english,main=english]babel
    usepackagexcolor

    definecolorpurpleHTML2B0057
    definecolorcandyHTMLFF0000

    ExplSyntaxOn
    NewDocumentCommandmarklettersm

    int_zero:N l_tmpa_int
    tl_set:Nn l_tmpa_tl #1
    % replace spaces with something different
    tl_replace_all:Nnn l_tmpa_tl ~ c_space_tl
    tl_map_inline:Nn l_tmpa_tl

    tl_if_blank:eTF ##1
    ~ % don't advance the counter and issue a space

    textcolor int_if_odd:nTF l_tmpa_int purple candy ##1
    int_incr:N l_tmpa_int



    prg_generate_conditional_variant:Nnn tl_if_blank:n e T,F,TF,p
    ExplSyntaxOff

    begindocument

    marklettersHello World

    enddocument


    enter image description here






    share|improve this answer






























      0














      You asked for a routine for checking whether a macro argument consists a single space although the afterassignment-let-loop in your example does not iterate macro-argument-wise but token-wise. Besides this, with that loop you cannot have (La)TeX "look" at tokens of whatsoever macro-arguments but you can have (La)TeX "look" at the meaning of the control word token xchar. You cannot exactly deduce the kind of token from which xchar got its meaning: This could indeed have been an explicit space character token. But this could as well have been an implicit space token, i.e., some control sequence which was let equal to the explicit space character token, like @sptoken.



      Thus there is some likelihood that my answer/my example below is not of any use to you at all.

      Nonetheless it might be of use to people who stumble over your question while indeed being in need of a routine for detecting (explicit) space tokens in macro arguments.



      The example below provides two routines:



      The macro UD@CheckWhetherLeadingSpace can be used for finding out whether the very first token of a macro argument is an explicit space character token (character code 32, category code 10).

      The gist of UD@CheckWhetherLeadingSpace is: Append a space token (for ensuring that there is at least one), then gather everything till the first space token, then see whether "emptiness" was gathered. Some brace-hacking is used for removing the remainder.



      The macro UD@CheckWhetherSingleSpace can be used for finding out whether a macro argument consists of a single explicit space character token (character code 32, category code 10).

      The gist of UD@CheckWhetherSingleSpace is: Apply UD@CheckWhetherLeadingSpace. In case there is a leading space, check whether you get emptiness after removing it.



      These macros work only on explicit space character tokens (character code 32, category code 10). Neither do they work on implicit space characters, nor do they work on character tokens of character code 32 but category code differing from 10, nor do they work on so-called "funny spaces" (character code differing from 32, category code 10 - iirc "funny spaces" can come into being only by changing the lccode or uccode of the space character and then applying lowercase respective uppercase to the space character (and then probably applying let for obtaining an implicit from an explicit funny space funny space)...).



      These macros don't require e-TeX-extensions and are intended to work in full-expansion-contexts etc as well.



      Due to romannumeral-expansion you get the result after triggering two expansion-steps/after having UD@CheckWhetherLeadingSpace/UD@CheckWhetherSingleSpace "hit" by two expandafter.



      (By the way:



      Be aware that (La)TeX skips explicit (non-funny) space tokens not nested in braces when gathering undelimited macro arguments. It does not skip them when gathering delimited macro arguments.



      E.g., with defthreeargs#1#2#3#1#2#3 and threeargs a b c you will get #1=a, #2=b, #3=c although there will be explicit space character tokens between a and b and between b and c.



      I think that's one of the reasons why the loop in the example within your question does not iterate macro-argument-wise but token-wise.)



      documentclass[a4paper]article
      makeatletter
      %%----------------------------------------------------------------------
      newcommandUD@firstoftwo[2]#1%
      newcommandUD@secondoftwo[2]#2%
      newcommandUD@exchange[2]#2#1%
      newcommandUD@gobblespace%
      UD@firstoftwodefUD@gobblespace %
      %%----------------------------------------------------------------------
      %% Check whether argument is empty:
      %%......................................................................
      %% UD@CheckWhetherNull<Argument which is to be checked>%
      %% <Tokens to be delivered in case that argument
      %% which is to be checked is empty>%
      %% <Tokens to be delivered in case that argument
      %% which is to be checked is not empty>%
      %% The gist of this macro comes from Robert R. Schneck's ifempty-macro:
      %% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
      newcommandUD@CheckWhetherNull[1]%
      romannumeral0expandafterUD@secondoftwostringexpandafter
      UD@secondoftwoexpandafterexpandafterstring#1expandafter
      UD@secondoftwostringexpandafterUD@firstoftwoexpandafterexpandafter
      UD@secondoftwostringexpandafterexpandafterUD@firstoftwo %
      UD@secondoftwoexpandafterexpandafterUD@firstoftwo UD@firstoftwo%
      %
      %%-----------------------------------------------------------------------------
      %% Check whether brace-balanced argument starts with a space-token
      %%.............................................................................
      %% UD@CheckWhetherLeadingSpace<Argument which is to be checked>%
      %% <Tokens to be delivered in case <argument
      %% which is to be checked>'s 1st token is a
      %% space-token>%
      %% <Tokens to be delivered in case <argument
      %% which is to be checked>'s 1st token is not
      %% a space-token>%
      newcommandUD@CheckWhetherLeadingSpace[1]%
      romannumeral0UD@CheckWhetherNull#1%
      expandafterexpandafterUD@firstoftwo UD@secondoftwo%
      expandafterUD@secondoftwostringUD@CheckWhetherLeadingSpaceB.#1 %
      %
      newcommandUD@CheckWhetherLeadingSpaceB%
      longdefUD@CheckWhetherLeadingSpaceB#1 %
      expandafterUD@CheckWhetherNullexpandafterUD@secondoftwo#1%
      UD@exchangeUD@firstoftwoUD@exchangeUD@secondoftwo%
      UD@exchange expandafterexpandafterexpandafterexpandafter
      expandafterexpandafterexpandafterexpandafterexpandafter
      expandafterexpandafterUD@secondoftwoexpandafterstring%
      %
      %%-----------------------------------------------------------------------------
      %% Check whether brace-balanced argument consists only of a single space-token
      %%.............................................................................
      newcommandUD@CheckWhetherSingleSpace[1]%
      romannumeral0UD@CheckWhetherLeadingSpace#1%
      expandafterUD@CheckWhetherNullexpandafterUD@gobblespace#1%
      UD@exchangeUD@firstoftwoUD@exchangeUD@secondoftwo%
      UD@exchangeUD@secondoftwoUD@exchange expandafter%
      %
      %makeatother

      begindocument

      UD@CheckWhetherLeadingSpace textLeading explicit space tokenNo leading explicit space token

      UD@CheckWhetherLeadingSpace Leading explicit space tokenNo leading explicit space token

      UD@CheckWhetherLeadingSpacetextLeading explicit space tokenNo leading explicit space token

      % empty argument:
      UD@CheckWhetherLeadingSpaceLeading explicit space tokenNo leading explicit space token

      % two space tokens:
      expandafterUD@CheckWhetherLeadingSpaceexpandafter@firstofone %
      Leading explicit space tokenNo leading explicit space token

      noindenthrulefill

      UD@CheckWhetherSingleSpace textSingle explicit space tokenNot a single explicit space token

      UD@CheckWhetherSingleSpace Single explicit space tokenNot a single explicit space token

      UD@CheckWhetherSingleSpacetextSingle explicit space tokenNot a single explicit space token

      % empty argument:
      UD@CheckWhetherSingleSpaceSingle explicit space tokenNot a single explicit space token

      % two space tokens:
      expandafterUD@CheckWhetherSingleSpaceexpandafter@firstofone %
      Single explicit space tokenNot a single explicit space token

      enddocument


      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%2f491668%2fhow-to-test-if-argument-is-a-single-space%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        7














        You are not iterating using macro arguments, so you do not need to test a macro argument, just test the token you have already:



        enter image description here



        documentclass[11pt,a4paper,english,twoside,notitlepage]book

        usepackagefontspec
        usepackagelmodern
        usepackage[english,main=english]babel
        usepackagexcolor
        usepackageetoolbox


        definecolorpurpleHTML2B0057
        definecolorcandyHTMLFF0000

        newcounteralternate

        %the macro in question
        newcommandcolset[1]%
        ifnumvaluealternate=0 colorcandy#1setcounteralternate1%
        elsecolorpurple#1setcounteralternate0%
        fi

        makeatletter
        %snippet for the loop taken from https://tex.stackexchange.com/questions/359189/looping-over-strings
        %iterates over the supplied string and replaces every letter with colset<letter>
        defgobblecharletxchar=
        defassignthencheckafterassignmentxloopgobblechar
        defxloop%
        ifxrelaxxchar
        letnext=relax
        else
        ifx@sptokenxcharsetcounteralternatenumexpr1-valuealternatefi
        colsetxcharletnext=assignthencheck
        fi
        next
        makeatother
        defmarkletters#1setcounteralternate0assignthencheck#1relax

        begindocument
        marklettersHello World
        enddocument





        share|improve this answer



























          7














          You are not iterating using macro arguments, so you do not need to test a macro argument, just test the token you have already:



          enter image description here



          documentclass[11pt,a4paper,english,twoside,notitlepage]book

          usepackagefontspec
          usepackagelmodern
          usepackage[english,main=english]babel
          usepackagexcolor
          usepackageetoolbox


          definecolorpurpleHTML2B0057
          definecolorcandyHTMLFF0000

          newcounteralternate

          %the macro in question
          newcommandcolset[1]%
          ifnumvaluealternate=0 colorcandy#1setcounteralternate1%
          elsecolorpurple#1setcounteralternate0%
          fi

          makeatletter
          %snippet for the loop taken from https://tex.stackexchange.com/questions/359189/looping-over-strings
          %iterates over the supplied string and replaces every letter with colset<letter>
          defgobblecharletxchar=
          defassignthencheckafterassignmentxloopgobblechar
          defxloop%
          ifxrelaxxchar
          letnext=relax
          else
          ifx@sptokenxcharsetcounteralternatenumexpr1-valuealternatefi
          colsetxcharletnext=assignthencheck
          fi
          next
          makeatother
          defmarkletters#1setcounteralternate0assignthencheck#1relax

          begindocument
          marklettersHello World
          enddocument





          share|improve this answer

























            7












            7








            7







            You are not iterating using macro arguments, so you do not need to test a macro argument, just test the token you have already:



            enter image description here



            documentclass[11pt,a4paper,english,twoside,notitlepage]book

            usepackagefontspec
            usepackagelmodern
            usepackage[english,main=english]babel
            usepackagexcolor
            usepackageetoolbox


            definecolorpurpleHTML2B0057
            definecolorcandyHTMLFF0000

            newcounteralternate

            %the macro in question
            newcommandcolset[1]%
            ifnumvaluealternate=0 colorcandy#1setcounteralternate1%
            elsecolorpurple#1setcounteralternate0%
            fi

            makeatletter
            %snippet for the loop taken from https://tex.stackexchange.com/questions/359189/looping-over-strings
            %iterates over the supplied string and replaces every letter with colset<letter>
            defgobblecharletxchar=
            defassignthencheckafterassignmentxloopgobblechar
            defxloop%
            ifxrelaxxchar
            letnext=relax
            else
            ifx@sptokenxcharsetcounteralternatenumexpr1-valuealternatefi
            colsetxcharletnext=assignthencheck
            fi
            next
            makeatother
            defmarkletters#1setcounteralternate0assignthencheck#1relax

            begindocument
            marklettersHello World
            enddocument





            share|improve this answer













            You are not iterating using macro arguments, so you do not need to test a macro argument, just test the token you have already:



            enter image description here



            documentclass[11pt,a4paper,english,twoside,notitlepage]book

            usepackagefontspec
            usepackagelmodern
            usepackage[english,main=english]babel
            usepackagexcolor
            usepackageetoolbox


            definecolorpurpleHTML2B0057
            definecolorcandyHTMLFF0000

            newcounteralternate

            %the macro in question
            newcommandcolset[1]%
            ifnumvaluealternate=0 colorcandy#1setcounteralternate1%
            elsecolorpurple#1setcounteralternate0%
            fi

            makeatletter
            %snippet for the loop taken from https://tex.stackexchange.com/questions/359189/looping-over-strings
            %iterates over the supplied string and replaces every letter with colset<letter>
            defgobblecharletxchar=
            defassignthencheckafterassignmentxloopgobblechar
            defxloop%
            ifxrelaxxchar
            letnext=relax
            else
            ifx@sptokenxcharsetcounteralternatenumexpr1-valuealternatefi
            colsetxcharletnext=assignthencheck
            fi
            next
            makeatother
            defmarkletters#1setcounteralternate0assignthencheck#1relax

            begindocument
            marklettersHello World
            enddocument






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 19 at 23:11









            David CarlisleDavid Carlisle

            506k4211531905




            506k4211531905





















                4














                You can substitute spaces with something that expands to a space.



                documentclass[11pt,a4paper,english,twoside,notitlepage]book

                usepackagefontspec
                usepackagelmodern
                usepackage[english,main=english]babel
                usepackagexcolor

                definecolorpurpleHTML2B0057
                definecolorcandyHTMLFF0000

                ExplSyntaxOn
                NewDocumentCommandmarklettersm

                int_zero:N l_tmpa_int
                tl_set:Nn l_tmpa_tl #1
                % replace spaces with something different
                tl_replace_all:Nnn l_tmpa_tl ~ c_space_tl
                tl_map_inline:Nn l_tmpa_tl

                tl_if_blank:eTF ##1
                ~ % don't advance the counter and issue a space

                textcolor int_if_odd:nTF l_tmpa_int purple candy ##1
                int_incr:N l_tmpa_int



                prg_generate_conditional_variant:Nnn tl_if_blank:n e T,F,TF,p
                ExplSyntaxOff

                begindocument

                marklettersHello World

                enddocument


                enter image description here






                share|improve this answer



























                  4














                  You can substitute spaces with something that expands to a space.



                  documentclass[11pt,a4paper,english,twoside,notitlepage]book

                  usepackagefontspec
                  usepackagelmodern
                  usepackage[english,main=english]babel
                  usepackagexcolor

                  definecolorpurpleHTML2B0057
                  definecolorcandyHTMLFF0000

                  ExplSyntaxOn
                  NewDocumentCommandmarklettersm

                  int_zero:N l_tmpa_int
                  tl_set:Nn l_tmpa_tl #1
                  % replace spaces with something different
                  tl_replace_all:Nnn l_tmpa_tl ~ c_space_tl
                  tl_map_inline:Nn l_tmpa_tl

                  tl_if_blank:eTF ##1
                  ~ % don't advance the counter and issue a space

                  textcolor int_if_odd:nTF l_tmpa_int purple candy ##1
                  int_incr:N l_tmpa_int



                  prg_generate_conditional_variant:Nnn tl_if_blank:n e T,F,TF,p
                  ExplSyntaxOff

                  begindocument

                  marklettersHello World

                  enddocument


                  enter image description here






                  share|improve this answer

























                    4












                    4








                    4







                    You can substitute spaces with something that expands to a space.



                    documentclass[11pt,a4paper,english,twoside,notitlepage]book

                    usepackagefontspec
                    usepackagelmodern
                    usepackage[english,main=english]babel
                    usepackagexcolor

                    definecolorpurpleHTML2B0057
                    definecolorcandyHTMLFF0000

                    ExplSyntaxOn
                    NewDocumentCommandmarklettersm

                    int_zero:N l_tmpa_int
                    tl_set:Nn l_tmpa_tl #1
                    % replace spaces with something different
                    tl_replace_all:Nnn l_tmpa_tl ~ c_space_tl
                    tl_map_inline:Nn l_tmpa_tl

                    tl_if_blank:eTF ##1
                    ~ % don't advance the counter and issue a space

                    textcolor int_if_odd:nTF l_tmpa_int purple candy ##1
                    int_incr:N l_tmpa_int



                    prg_generate_conditional_variant:Nnn tl_if_blank:n e T,F,TF,p
                    ExplSyntaxOff

                    begindocument

                    marklettersHello World

                    enddocument


                    enter image description here






                    share|improve this answer













                    You can substitute spaces with something that expands to a space.



                    documentclass[11pt,a4paper,english,twoside,notitlepage]book

                    usepackagefontspec
                    usepackagelmodern
                    usepackage[english,main=english]babel
                    usepackagexcolor

                    definecolorpurpleHTML2B0057
                    definecolorcandyHTMLFF0000

                    ExplSyntaxOn
                    NewDocumentCommandmarklettersm

                    int_zero:N l_tmpa_int
                    tl_set:Nn l_tmpa_tl #1
                    % replace spaces with something different
                    tl_replace_all:Nnn l_tmpa_tl ~ c_space_tl
                    tl_map_inline:Nn l_tmpa_tl

                    tl_if_blank:eTF ##1
                    ~ % don't advance the counter and issue a space

                    textcolor int_if_odd:nTF l_tmpa_int purple candy ##1
                    int_incr:N l_tmpa_int



                    prg_generate_conditional_variant:Nnn tl_if_blank:n e T,F,TF,p
                    ExplSyntaxOff

                    begindocument

                    marklettersHello World

                    enddocument


                    enter image description here







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 19 at 23:05









                    egregegreg

                    744k8919473283




                    744k8919473283





















                        0














                        You asked for a routine for checking whether a macro argument consists a single space although the afterassignment-let-loop in your example does not iterate macro-argument-wise but token-wise. Besides this, with that loop you cannot have (La)TeX "look" at tokens of whatsoever macro-arguments but you can have (La)TeX "look" at the meaning of the control word token xchar. You cannot exactly deduce the kind of token from which xchar got its meaning: This could indeed have been an explicit space character token. But this could as well have been an implicit space token, i.e., some control sequence which was let equal to the explicit space character token, like @sptoken.



                        Thus there is some likelihood that my answer/my example below is not of any use to you at all.

                        Nonetheless it might be of use to people who stumble over your question while indeed being in need of a routine for detecting (explicit) space tokens in macro arguments.



                        The example below provides two routines:



                        The macro UD@CheckWhetherLeadingSpace can be used for finding out whether the very first token of a macro argument is an explicit space character token (character code 32, category code 10).

                        The gist of UD@CheckWhetherLeadingSpace is: Append a space token (for ensuring that there is at least one), then gather everything till the first space token, then see whether "emptiness" was gathered. Some brace-hacking is used for removing the remainder.



                        The macro UD@CheckWhetherSingleSpace can be used for finding out whether a macro argument consists of a single explicit space character token (character code 32, category code 10).

                        The gist of UD@CheckWhetherSingleSpace is: Apply UD@CheckWhetherLeadingSpace. In case there is a leading space, check whether you get emptiness after removing it.



                        These macros work only on explicit space character tokens (character code 32, category code 10). Neither do they work on implicit space characters, nor do they work on character tokens of character code 32 but category code differing from 10, nor do they work on so-called "funny spaces" (character code differing from 32, category code 10 - iirc "funny spaces" can come into being only by changing the lccode or uccode of the space character and then applying lowercase respective uppercase to the space character (and then probably applying let for obtaining an implicit from an explicit funny space funny space)...).



                        These macros don't require e-TeX-extensions and are intended to work in full-expansion-contexts etc as well.



                        Due to romannumeral-expansion you get the result after triggering two expansion-steps/after having UD@CheckWhetherLeadingSpace/UD@CheckWhetherSingleSpace "hit" by two expandafter.



                        (By the way:



                        Be aware that (La)TeX skips explicit (non-funny) space tokens not nested in braces when gathering undelimited macro arguments. It does not skip them when gathering delimited macro arguments.



                        E.g., with defthreeargs#1#2#3#1#2#3 and threeargs a b c you will get #1=a, #2=b, #3=c although there will be explicit space character tokens between a and b and between b and c.



                        I think that's one of the reasons why the loop in the example within your question does not iterate macro-argument-wise but token-wise.)



                        documentclass[a4paper]article
                        makeatletter
                        %%----------------------------------------------------------------------
                        newcommandUD@firstoftwo[2]#1%
                        newcommandUD@secondoftwo[2]#2%
                        newcommandUD@exchange[2]#2#1%
                        newcommandUD@gobblespace%
                        UD@firstoftwodefUD@gobblespace %
                        %%----------------------------------------------------------------------
                        %% Check whether argument is empty:
                        %%......................................................................
                        %% UD@CheckWhetherNull<Argument which is to be checked>%
                        %% <Tokens to be delivered in case that argument
                        %% which is to be checked is empty>%
                        %% <Tokens to be delivered in case that argument
                        %% which is to be checked is not empty>%
                        %% The gist of this macro comes from Robert R. Schneck's ifempty-macro:
                        %% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
                        newcommandUD@CheckWhetherNull[1]%
                        romannumeral0expandafterUD@secondoftwostringexpandafter
                        UD@secondoftwoexpandafterexpandafterstring#1expandafter
                        UD@secondoftwostringexpandafterUD@firstoftwoexpandafterexpandafter
                        UD@secondoftwostringexpandafterexpandafterUD@firstoftwo %
                        UD@secondoftwoexpandafterexpandafterUD@firstoftwo UD@firstoftwo%
                        %
                        %%-----------------------------------------------------------------------------
                        %% Check whether brace-balanced argument starts with a space-token
                        %%.............................................................................
                        %% UD@CheckWhetherLeadingSpace<Argument which is to be checked>%
                        %% <Tokens to be delivered in case <argument
                        %% which is to be checked>'s 1st token is a
                        %% space-token>%
                        %% <Tokens to be delivered in case <argument
                        %% which is to be checked>'s 1st token is not
                        %% a space-token>%
                        newcommandUD@CheckWhetherLeadingSpace[1]%
                        romannumeral0UD@CheckWhetherNull#1%
                        expandafterexpandafterUD@firstoftwo UD@secondoftwo%
                        expandafterUD@secondoftwostringUD@CheckWhetherLeadingSpaceB.#1 %
                        %
                        newcommandUD@CheckWhetherLeadingSpaceB%
                        longdefUD@CheckWhetherLeadingSpaceB#1 %
                        expandafterUD@CheckWhetherNullexpandafterUD@secondoftwo#1%
                        UD@exchangeUD@firstoftwoUD@exchangeUD@secondoftwo%
                        UD@exchange expandafterexpandafterexpandafterexpandafter
                        expandafterexpandafterexpandafterexpandafterexpandafter
                        expandafterexpandafterUD@secondoftwoexpandafterstring%
                        %
                        %%-----------------------------------------------------------------------------
                        %% Check whether brace-balanced argument consists only of a single space-token
                        %%.............................................................................
                        newcommandUD@CheckWhetherSingleSpace[1]%
                        romannumeral0UD@CheckWhetherLeadingSpace#1%
                        expandafterUD@CheckWhetherNullexpandafterUD@gobblespace#1%
                        UD@exchangeUD@firstoftwoUD@exchangeUD@secondoftwo%
                        UD@exchangeUD@secondoftwoUD@exchange expandafter%
                        %
                        %makeatother

                        begindocument

                        UD@CheckWhetherLeadingSpace textLeading explicit space tokenNo leading explicit space token

                        UD@CheckWhetherLeadingSpace Leading explicit space tokenNo leading explicit space token

                        UD@CheckWhetherLeadingSpacetextLeading explicit space tokenNo leading explicit space token

                        % empty argument:
                        UD@CheckWhetherLeadingSpaceLeading explicit space tokenNo leading explicit space token

                        % two space tokens:
                        expandafterUD@CheckWhetherLeadingSpaceexpandafter@firstofone %
                        Leading explicit space tokenNo leading explicit space token

                        noindenthrulefill

                        UD@CheckWhetherSingleSpace textSingle explicit space tokenNot a single explicit space token

                        UD@CheckWhetherSingleSpace Single explicit space tokenNot a single explicit space token

                        UD@CheckWhetherSingleSpacetextSingle explicit space tokenNot a single explicit space token

                        % empty argument:
                        UD@CheckWhetherSingleSpaceSingle explicit space tokenNot a single explicit space token

                        % two space tokens:
                        expandafterUD@CheckWhetherSingleSpaceexpandafter@firstofone %
                        Single explicit space tokenNot a single explicit space token

                        enddocument


                        enter image description here






                        share|improve this answer



























                          0














                          You asked for a routine for checking whether a macro argument consists a single space although the afterassignment-let-loop in your example does not iterate macro-argument-wise but token-wise. Besides this, with that loop you cannot have (La)TeX "look" at tokens of whatsoever macro-arguments but you can have (La)TeX "look" at the meaning of the control word token xchar. You cannot exactly deduce the kind of token from which xchar got its meaning: This could indeed have been an explicit space character token. But this could as well have been an implicit space token, i.e., some control sequence which was let equal to the explicit space character token, like @sptoken.



                          Thus there is some likelihood that my answer/my example below is not of any use to you at all.

                          Nonetheless it might be of use to people who stumble over your question while indeed being in need of a routine for detecting (explicit) space tokens in macro arguments.



                          The example below provides two routines:



                          The macro UD@CheckWhetherLeadingSpace can be used for finding out whether the very first token of a macro argument is an explicit space character token (character code 32, category code 10).

                          The gist of UD@CheckWhetherLeadingSpace is: Append a space token (for ensuring that there is at least one), then gather everything till the first space token, then see whether "emptiness" was gathered. Some brace-hacking is used for removing the remainder.



                          The macro UD@CheckWhetherSingleSpace can be used for finding out whether a macro argument consists of a single explicit space character token (character code 32, category code 10).

                          The gist of UD@CheckWhetherSingleSpace is: Apply UD@CheckWhetherLeadingSpace. In case there is a leading space, check whether you get emptiness after removing it.



                          These macros work only on explicit space character tokens (character code 32, category code 10). Neither do they work on implicit space characters, nor do they work on character tokens of character code 32 but category code differing from 10, nor do they work on so-called "funny spaces" (character code differing from 32, category code 10 - iirc "funny spaces" can come into being only by changing the lccode or uccode of the space character and then applying lowercase respective uppercase to the space character (and then probably applying let for obtaining an implicit from an explicit funny space funny space)...).



                          These macros don't require e-TeX-extensions and are intended to work in full-expansion-contexts etc as well.



                          Due to romannumeral-expansion you get the result after triggering two expansion-steps/after having UD@CheckWhetherLeadingSpace/UD@CheckWhetherSingleSpace "hit" by two expandafter.



                          (By the way:



                          Be aware that (La)TeX skips explicit (non-funny) space tokens not nested in braces when gathering undelimited macro arguments. It does not skip them when gathering delimited macro arguments.



                          E.g., with defthreeargs#1#2#3#1#2#3 and threeargs a b c you will get #1=a, #2=b, #3=c although there will be explicit space character tokens between a and b and between b and c.



                          I think that's one of the reasons why the loop in the example within your question does not iterate macro-argument-wise but token-wise.)



                          documentclass[a4paper]article
                          makeatletter
                          %%----------------------------------------------------------------------
                          newcommandUD@firstoftwo[2]#1%
                          newcommandUD@secondoftwo[2]#2%
                          newcommandUD@exchange[2]#2#1%
                          newcommandUD@gobblespace%
                          UD@firstoftwodefUD@gobblespace %
                          %%----------------------------------------------------------------------
                          %% Check whether argument is empty:
                          %%......................................................................
                          %% UD@CheckWhetherNull<Argument which is to be checked>%
                          %% <Tokens to be delivered in case that argument
                          %% which is to be checked is empty>%
                          %% <Tokens to be delivered in case that argument
                          %% which is to be checked is not empty>%
                          %% The gist of this macro comes from Robert R. Schneck's ifempty-macro:
                          %% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
                          newcommandUD@CheckWhetherNull[1]%
                          romannumeral0expandafterUD@secondoftwostringexpandafter
                          UD@secondoftwoexpandafterexpandafterstring#1expandafter
                          UD@secondoftwostringexpandafterUD@firstoftwoexpandafterexpandafter
                          UD@secondoftwostringexpandafterexpandafterUD@firstoftwo %
                          UD@secondoftwoexpandafterexpandafterUD@firstoftwo UD@firstoftwo%
                          %
                          %%-----------------------------------------------------------------------------
                          %% Check whether brace-balanced argument starts with a space-token
                          %%.............................................................................
                          %% UD@CheckWhetherLeadingSpace<Argument which is to be checked>%
                          %% <Tokens to be delivered in case <argument
                          %% which is to be checked>'s 1st token is a
                          %% space-token>%
                          %% <Tokens to be delivered in case <argument
                          %% which is to be checked>'s 1st token is not
                          %% a space-token>%
                          newcommandUD@CheckWhetherLeadingSpace[1]%
                          romannumeral0UD@CheckWhetherNull#1%
                          expandafterexpandafterUD@firstoftwo UD@secondoftwo%
                          expandafterUD@secondoftwostringUD@CheckWhetherLeadingSpaceB.#1 %
                          %
                          newcommandUD@CheckWhetherLeadingSpaceB%
                          longdefUD@CheckWhetherLeadingSpaceB#1 %
                          expandafterUD@CheckWhetherNullexpandafterUD@secondoftwo#1%
                          UD@exchangeUD@firstoftwoUD@exchangeUD@secondoftwo%
                          UD@exchange expandafterexpandafterexpandafterexpandafter
                          expandafterexpandafterexpandafterexpandafterexpandafter
                          expandafterexpandafterUD@secondoftwoexpandafterstring%
                          %
                          %%-----------------------------------------------------------------------------
                          %% Check whether brace-balanced argument consists only of a single space-token
                          %%.............................................................................
                          newcommandUD@CheckWhetherSingleSpace[1]%
                          romannumeral0UD@CheckWhetherLeadingSpace#1%
                          expandafterUD@CheckWhetherNullexpandafterUD@gobblespace#1%
                          UD@exchangeUD@firstoftwoUD@exchangeUD@secondoftwo%
                          UD@exchangeUD@secondoftwoUD@exchange expandafter%
                          %
                          %makeatother

                          begindocument

                          UD@CheckWhetherLeadingSpace textLeading explicit space tokenNo leading explicit space token

                          UD@CheckWhetherLeadingSpace Leading explicit space tokenNo leading explicit space token

                          UD@CheckWhetherLeadingSpacetextLeading explicit space tokenNo leading explicit space token

                          % empty argument:
                          UD@CheckWhetherLeadingSpaceLeading explicit space tokenNo leading explicit space token

                          % two space tokens:
                          expandafterUD@CheckWhetherLeadingSpaceexpandafter@firstofone %
                          Leading explicit space tokenNo leading explicit space token

                          noindenthrulefill

                          UD@CheckWhetherSingleSpace textSingle explicit space tokenNot a single explicit space token

                          UD@CheckWhetherSingleSpace Single explicit space tokenNot a single explicit space token

                          UD@CheckWhetherSingleSpacetextSingle explicit space tokenNot a single explicit space token

                          % empty argument:
                          UD@CheckWhetherSingleSpaceSingle explicit space tokenNot a single explicit space token

                          % two space tokens:
                          expandafterUD@CheckWhetherSingleSpaceexpandafter@firstofone %
                          Single explicit space tokenNot a single explicit space token

                          enddocument


                          enter image description here






                          share|improve this answer

























                            0












                            0








                            0







                            You asked for a routine for checking whether a macro argument consists a single space although the afterassignment-let-loop in your example does not iterate macro-argument-wise but token-wise. Besides this, with that loop you cannot have (La)TeX "look" at tokens of whatsoever macro-arguments but you can have (La)TeX "look" at the meaning of the control word token xchar. You cannot exactly deduce the kind of token from which xchar got its meaning: This could indeed have been an explicit space character token. But this could as well have been an implicit space token, i.e., some control sequence which was let equal to the explicit space character token, like @sptoken.



                            Thus there is some likelihood that my answer/my example below is not of any use to you at all.

                            Nonetheless it might be of use to people who stumble over your question while indeed being in need of a routine for detecting (explicit) space tokens in macro arguments.



                            The example below provides two routines:



                            The macro UD@CheckWhetherLeadingSpace can be used for finding out whether the very first token of a macro argument is an explicit space character token (character code 32, category code 10).

                            The gist of UD@CheckWhetherLeadingSpace is: Append a space token (for ensuring that there is at least one), then gather everything till the first space token, then see whether "emptiness" was gathered. Some brace-hacking is used for removing the remainder.



                            The macro UD@CheckWhetherSingleSpace can be used for finding out whether a macro argument consists of a single explicit space character token (character code 32, category code 10).

                            The gist of UD@CheckWhetherSingleSpace is: Apply UD@CheckWhetherLeadingSpace. In case there is a leading space, check whether you get emptiness after removing it.



                            These macros work only on explicit space character tokens (character code 32, category code 10). Neither do they work on implicit space characters, nor do they work on character tokens of character code 32 but category code differing from 10, nor do they work on so-called "funny spaces" (character code differing from 32, category code 10 - iirc "funny spaces" can come into being only by changing the lccode or uccode of the space character and then applying lowercase respective uppercase to the space character (and then probably applying let for obtaining an implicit from an explicit funny space funny space)...).



                            These macros don't require e-TeX-extensions and are intended to work in full-expansion-contexts etc as well.



                            Due to romannumeral-expansion you get the result after triggering two expansion-steps/after having UD@CheckWhetherLeadingSpace/UD@CheckWhetherSingleSpace "hit" by two expandafter.



                            (By the way:



                            Be aware that (La)TeX skips explicit (non-funny) space tokens not nested in braces when gathering undelimited macro arguments. It does not skip them when gathering delimited macro arguments.



                            E.g., with defthreeargs#1#2#3#1#2#3 and threeargs a b c you will get #1=a, #2=b, #3=c although there will be explicit space character tokens between a and b and between b and c.



                            I think that's one of the reasons why the loop in the example within your question does not iterate macro-argument-wise but token-wise.)



                            documentclass[a4paper]article
                            makeatletter
                            %%----------------------------------------------------------------------
                            newcommandUD@firstoftwo[2]#1%
                            newcommandUD@secondoftwo[2]#2%
                            newcommandUD@exchange[2]#2#1%
                            newcommandUD@gobblespace%
                            UD@firstoftwodefUD@gobblespace %
                            %%----------------------------------------------------------------------
                            %% Check whether argument is empty:
                            %%......................................................................
                            %% UD@CheckWhetherNull<Argument which is to be checked>%
                            %% <Tokens to be delivered in case that argument
                            %% which is to be checked is empty>%
                            %% <Tokens to be delivered in case that argument
                            %% which is to be checked is not empty>%
                            %% The gist of this macro comes from Robert R. Schneck's ifempty-macro:
                            %% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
                            newcommandUD@CheckWhetherNull[1]%
                            romannumeral0expandafterUD@secondoftwostringexpandafter
                            UD@secondoftwoexpandafterexpandafterstring#1expandafter
                            UD@secondoftwostringexpandafterUD@firstoftwoexpandafterexpandafter
                            UD@secondoftwostringexpandafterexpandafterUD@firstoftwo %
                            UD@secondoftwoexpandafterexpandafterUD@firstoftwo UD@firstoftwo%
                            %
                            %%-----------------------------------------------------------------------------
                            %% Check whether brace-balanced argument starts with a space-token
                            %%.............................................................................
                            %% UD@CheckWhetherLeadingSpace<Argument which is to be checked>%
                            %% <Tokens to be delivered in case <argument
                            %% which is to be checked>'s 1st token is a
                            %% space-token>%
                            %% <Tokens to be delivered in case <argument
                            %% which is to be checked>'s 1st token is not
                            %% a space-token>%
                            newcommandUD@CheckWhetherLeadingSpace[1]%
                            romannumeral0UD@CheckWhetherNull#1%
                            expandafterexpandafterUD@firstoftwo UD@secondoftwo%
                            expandafterUD@secondoftwostringUD@CheckWhetherLeadingSpaceB.#1 %
                            %
                            newcommandUD@CheckWhetherLeadingSpaceB%
                            longdefUD@CheckWhetherLeadingSpaceB#1 %
                            expandafterUD@CheckWhetherNullexpandafterUD@secondoftwo#1%
                            UD@exchangeUD@firstoftwoUD@exchangeUD@secondoftwo%
                            UD@exchange expandafterexpandafterexpandafterexpandafter
                            expandafterexpandafterexpandafterexpandafterexpandafter
                            expandafterexpandafterUD@secondoftwoexpandafterstring%
                            %
                            %%-----------------------------------------------------------------------------
                            %% Check whether brace-balanced argument consists only of a single space-token
                            %%.............................................................................
                            newcommandUD@CheckWhetherSingleSpace[1]%
                            romannumeral0UD@CheckWhetherLeadingSpace#1%
                            expandafterUD@CheckWhetherNullexpandafterUD@gobblespace#1%
                            UD@exchangeUD@firstoftwoUD@exchangeUD@secondoftwo%
                            UD@exchangeUD@secondoftwoUD@exchange expandafter%
                            %
                            %makeatother

                            begindocument

                            UD@CheckWhetherLeadingSpace textLeading explicit space tokenNo leading explicit space token

                            UD@CheckWhetherLeadingSpace Leading explicit space tokenNo leading explicit space token

                            UD@CheckWhetherLeadingSpacetextLeading explicit space tokenNo leading explicit space token

                            % empty argument:
                            UD@CheckWhetherLeadingSpaceLeading explicit space tokenNo leading explicit space token

                            % two space tokens:
                            expandafterUD@CheckWhetherLeadingSpaceexpandafter@firstofone %
                            Leading explicit space tokenNo leading explicit space token

                            noindenthrulefill

                            UD@CheckWhetherSingleSpace textSingle explicit space tokenNot a single explicit space token

                            UD@CheckWhetherSingleSpace Single explicit space tokenNot a single explicit space token

                            UD@CheckWhetherSingleSpacetextSingle explicit space tokenNot a single explicit space token

                            % empty argument:
                            UD@CheckWhetherSingleSpaceSingle explicit space tokenNot a single explicit space token

                            % two space tokens:
                            expandafterUD@CheckWhetherSingleSpaceexpandafter@firstofone %
                            Single explicit space tokenNot a single explicit space token

                            enddocument


                            enter image description here






                            share|improve this answer













                            You asked for a routine for checking whether a macro argument consists a single space although the afterassignment-let-loop in your example does not iterate macro-argument-wise but token-wise. Besides this, with that loop you cannot have (La)TeX "look" at tokens of whatsoever macro-arguments but you can have (La)TeX "look" at the meaning of the control word token xchar. You cannot exactly deduce the kind of token from which xchar got its meaning: This could indeed have been an explicit space character token. But this could as well have been an implicit space token, i.e., some control sequence which was let equal to the explicit space character token, like @sptoken.



                            Thus there is some likelihood that my answer/my example below is not of any use to you at all.

                            Nonetheless it might be of use to people who stumble over your question while indeed being in need of a routine for detecting (explicit) space tokens in macro arguments.



                            The example below provides two routines:



                            The macro UD@CheckWhetherLeadingSpace can be used for finding out whether the very first token of a macro argument is an explicit space character token (character code 32, category code 10).

                            The gist of UD@CheckWhetherLeadingSpace is: Append a space token (for ensuring that there is at least one), then gather everything till the first space token, then see whether "emptiness" was gathered. Some brace-hacking is used for removing the remainder.



                            The macro UD@CheckWhetherSingleSpace can be used for finding out whether a macro argument consists of a single explicit space character token (character code 32, category code 10).

                            The gist of UD@CheckWhetherSingleSpace is: Apply UD@CheckWhetherLeadingSpace. In case there is a leading space, check whether you get emptiness after removing it.



                            These macros work only on explicit space character tokens (character code 32, category code 10). Neither do they work on implicit space characters, nor do they work on character tokens of character code 32 but category code differing from 10, nor do they work on so-called "funny spaces" (character code differing from 32, category code 10 - iirc "funny spaces" can come into being only by changing the lccode or uccode of the space character and then applying lowercase respective uppercase to the space character (and then probably applying let for obtaining an implicit from an explicit funny space funny space)...).



                            These macros don't require e-TeX-extensions and are intended to work in full-expansion-contexts etc as well.



                            Due to romannumeral-expansion you get the result after triggering two expansion-steps/after having UD@CheckWhetherLeadingSpace/UD@CheckWhetherSingleSpace "hit" by two expandafter.



                            (By the way:



                            Be aware that (La)TeX skips explicit (non-funny) space tokens not nested in braces when gathering undelimited macro arguments. It does not skip them when gathering delimited macro arguments.



                            E.g., with defthreeargs#1#2#3#1#2#3 and threeargs a b c you will get #1=a, #2=b, #3=c although there will be explicit space character tokens between a and b and between b and c.



                            I think that's one of the reasons why the loop in the example within your question does not iterate macro-argument-wise but token-wise.)



                            documentclass[a4paper]article
                            makeatletter
                            %%----------------------------------------------------------------------
                            newcommandUD@firstoftwo[2]#1%
                            newcommandUD@secondoftwo[2]#2%
                            newcommandUD@exchange[2]#2#1%
                            newcommandUD@gobblespace%
                            UD@firstoftwodefUD@gobblespace %
                            %%----------------------------------------------------------------------
                            %% Check whether argument is empty:
                            %%......................................................................
                            %% UD@CheckWhetherNull<Argument which is to be checked>%
                            %% <Tokens to be delivered in case that argument
                            %% which is to be checked is empty>%
                            %% <Tokens to be delivered in case that argument
                            %% which is to be checked is not empty>%
                            %% The gist of this macro comes from Robert R. Schneck's ifempty-macro:
                            %% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
                            newcommandUD@CheckWhetherNull[1]%
                            romannumeral0expandafterUD@secondoftwostringexpandafter
                            UD@secondoftwoexpandafterexpandafterstring#1expandafter
                            UD@secondoftwostringexpandafterUD@firstoftwoexpandafterexpandafter
                            UD@secondoftwostringexpandafterexpandafterUD@firstoftwo %
                            UD@secondoftwoexpandafterexpandafterUD@firstoftwo UD@firstoftwo%
                            %
                            %%-----------------------------------------------------------------------------
                            %% Check whether brace-balanced argument starts with a space-token
                            %%.............................................................................
                            %% UD@CheckWhetherLeadingSpace<Argument which is to be checked>%
                            %% <Tokens to be delivered in case <argument
                            %% which is to be checked>'s 1st token is a
                            %% space-token>%
                            %% <Tokens to be delivered in case <argument
                            %% which is to be checked>'s 1st token is not
                            %% a space-token>%
                            newcommandUD@CheckWhetherLeadingSpace[1]%
                            romannumeral0UD@CheckWhetherNull#1%
                            expandafterexpandafterUD@firstoftwo UD@secondoftwo%
                            expandafterUD@secondoftwostringUD@CheckWhetherLeadingSpaceB.#1 %
                            %
                            newcommandUD@CheckWhetherLeadingSpaceB%
                            longdefUD@CheckWhetherLeadingSpaceB#1 %
                            expandafterUD@CheckWhetherNullexpandafterUD@secondoftwo#1%
                            UD@exchangeUD@firstoftwoUD@exchangeUD@secondoftwo%
                            UD@exchange expandafterexpandafterexpandafterexpandafter
                            expandafterexpandafterexpandafterexpandafterexpandafter
                            expandafterexpandafterUD@secondoftwoexpandafterstring%
                            %
                            %%-----------------------------------------------------------------------------
                            %% Check whether brace-balanced argument consists only of a single space-token
                            %%.............................................................................
                            newcommandUD@CheckWhetherSingleSpace[1]%
                            romannumeral0UD@CheckWhetherLeadingSpace#1%
                            expandafterUD@CheckWhetherNullexpandafterUD@gobblespace#1%
                            UD@exchangeUD@firstoftwoUD@exchangeUD@secondoftwo%
                            UD@exchangeUD@secondoftwoUD@exchange expandafter%
                            %
                            %makeatother

                            begindocument

                            UD@CheckWhetherLeadingSpace textLeading explicit space tokenNo leading explicit space token

                            UD@CheckWhetherLeadingSpace Leading explicit space tokenNo leading explicit space token

                            UD@CheckWhetherLeadingSpacetextLeading explicit space tokenNo leading explicit space token

                            % empty argument:
                            UD@CheckWhetherLeadingSpaceLeading explicit space tokenNo leading explicit space token

                            % two space tokens:
                            expandafterUD@CheckWhetherLeadingSpaceexpandafter@firstofone %
                            Leading explicit space tokenNo leading explicit space token

                            noindenthrulefill

                            UD@CheckWhetherSingleSpace textSingle explicit space tokenNot a single explicit space token

                            UD@CheckWhetherSingleSpace Single explicit space tokenNot a single explicit space token

                            UD@CheckWhetherSingleSpacetextSingle explicit space tokenNot a single explicit space token

                            % empty argument:
                            UD@CheckWhetherSingleSpaceSingle explicit space tokenNot a single explicit space token

                            % two space tokens:
                            expandafterUD@CheckWhetherSingleSpaceexpandafter@firstofone %
                            Single explicit space tokenNot a single explicit space token

                            enddocument


                            enter image description here







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered May 21 at 0:37









                            Ulrich DiezUlrich Diez

                            6,030620




                            6,030620



























                                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%2f491668%2fhow-to-test-if-argument-is-a-single-space%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

                                Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

                                Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                                Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form