Macro expansion inside href












4















I want to make a shortcut for



href{tel:0123456789}{01,23,45,67,89}


I can use StrSubstitute from the xstring package as



StrSubstitute{01 23 45 67 89}{ }{,}


inside the second argument to href. But the same thing with



StrSubstitute{01 23 45 67 89}{ }{}


in the first argument won't work.



I think, I understand that this is an issue with macro expansion order. But how can I get LaTeX to first expand StrSubstitute into a string that can be parsed by href?



Here is a minimal example of what I actually want to achieve:



documentclass{minimal}

usepackage{xstring}
usepackage{hyperref}

newcommandphone[1]{href{tel:StrSubstitute{#1}{ }{}}{StrSubstitute{#1}{ }{,}}}

begin{document}

href{tel:0123456789}{01,23,45,67,89}

href{StrSubstitute{01 23 45 67 89}{ }{}}{StrSubstitute{01 23 45 67 89}{ }{,}}

phone{01 23 45 67 89}

end{document}




Edit: The pairwise pattern is not important, since different countries have different number format conventions. I really only want to replace/remove spaces (and maybe other things).










share|improve this question





























    4















    I want to make a shortcut for



    href{tel:0123456789}{01,23,45,67,89}


    I can use StrSubstitute from the xstring package as



    StrSubstitute{01 23 45 67 89}{ }{,}


    inside the second argument to href. But the same thing with



    StrSubstitute{01 23 45 67 89}{ }{}


    in the first argument won't work.



    I think, I understand that this is an issue with macro expansion order. But how can I get LaTeX to first expand StrSubstitute into a string that can be parsed by href?



    Here is a minimal example of what I actually want to achieve:



    documentclass{minimal}

    usepackage{xstring}
    usepackage{hyperref}

    newcommandphone[1]{href{tel:StrSubstitute{#1}{ }{}}{StrSubstitute{#1}{ }{,}}}

    begin{document}

    href{tel:0123456789}{01,23,45,67,89}

    href{StrSubstitute{01 23 45 67 89}{ }{}}{StrSubstitute{01 23 45 67 89}{ }{,}}

    phone{01 23 45 67 89}

    end{document}




    Edit: The pairwise pattern is not important, since different countries have different number format conventions. I really only want to replace/remove spaces (and maybe other things).










    share|improve this question



























      4












      4








      4


      1






      I want to make a shortcut for



      href{tel:0123456789}{01,23,45,67,89}


      I can use StrSubstitute from the xstring package as



      StrSubstitute{01 23 45 67 89}{ }{,}


      inside the second argument to href. But the same thing with



      StrSubstitute{01 23 45 67 89}{ }{}


      in the first argument won't work.



      I think, I understand that this is an issue with macro expansion order. But how can I get LaTeX to first expand StrSubstitute into a string that can be parsed by href?



      Here is a minimal example of what I actually want to achieve:



      documentclass{minimal}

      usepackage{xstring}
      usepackage{hyperref}

      newcommandphone[1]{href{tel:StrSubstitute{#1}{ }{}}{StrSubstitute{#1}{ }{,}}}

      begin{document}

      href{tel:0123456789}{01,23,45,67,89}

      href{StrSubstitute{01 23 45 67 89}{ }{}}{StrSubstitute{01 23 45 67 89}{ }{,}}

      phone{01 23 45 67 89}

      end{document}




      Edit: The pairwise pattern is not important, since different countries have different number format conventions. I really only want to replace/remove spaces (and maybe other things).










      share|improve this question
















      I want to make a shortcut for



      href{tel:0123456789}{01,23,45,67,89}


      I can use StrSubstitute from the xstring package as



      StrSubstitute{01 23 45 67 89}{ }{,}


      inside the second argument to href. But the same thing with



      StrSubstitute{01 23 45 67 89}{ }{}


      in the first argument won't work.



      I think, I understand that this is an issue with macro expansion order. But how can I get LaTeX to first expand StrSubstitute into a string that can be parsed by href?



      Here is a minimal example of what I actually want to achieve:



      documentclass{minimal}

      usepackage{xstring}
      usepackage{hyperref}

      newcommandphone[1]{href{tel:StrSubstitute{#1}{ }{}}{StrSubstitute{#1}{ }{,}}}

      begin{document}

      href{tel:0123456789}{01,23,45,67,89}

      href{StrSubstitute{01 23 45 67 89}{ }{}}{StrSubstitute{01 23 45 67 89}{ }{,}}

      phone{01 23 45 67 89}

      end{document}




      Edit: The pairwise pattern is not important, since different countries have different number format conventions. I really only want to replace/remove spaces (and maybe other things).







      hyperref xstring href






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 27 at 6:24







      DLichti

















      asked Feb 26 at 21:41









      DLichtiDLichti

      1176




      1176






















          3 Answers
          3






          active

          oldest

          votes


















          4














          Expand the string substitution first by storing it in an argument, which you can then use with hyperref's href:



          documentclass{article}

          usepackage{xstring}
          usepackage{hyperref}

          newcommandphone[1]{%
          StrSubstitute{#1}{ }{}[firstarg]% Store first substitution in firstarg
          StrSubstitute{#1}{ }{,}[secondarg]% Store second substitution in secondarg
          href{tel:firstarg}{secondarg}% Use stored arguments in href
          }

          begin{document}

          href{tel:0123456789}{01,23,45,67,89}

          phone{01 23 45 67 89}

          end{document}





          share|improve this answer































            3














            You can't use StrSubstitute in those places, because it doesn't produce the string after the substitution, but rather a fairly complicated set of instructions to produce that string.



            A more complicated solution that avoids the need to input spaces between pairs of digits, so it will work even if you forget them.



            documentclass{article}

            usepackage{xparse}
            usepackage{hyperref}

            ExplSyntaxOn

            NewDocumentCommand{phone}{m}
            {
            dlichti_phone:n { #1 }
            }

            tl_new:N l_dlichti_phone_href_tl
            tl_new:N l_dlichti_phone_print_tl

            cs_new_protected:Nn dlichti_phone:n
            {
            tl_set:Nx l_dlichti_phone_href_tl { #1 }
            % remove all spaces
            tl_replace_all:Nnn l_dlichti_phone_href_tl { ~ } { }
            % save a copy
            tl_set_eq:NN l_dlichti_phone_print_tl l_dlichti_phone_href_tl
            % insert a thin space between any pair of digits
            regex_replace_all:nnN
            { ([0-9][0-9]) } % two digits followed by another digit
            { 1c{,} } % the same with , in between
            l_dlichti_phone_print_tl
            % remove the trailing ,
            regex_replace_once:nnN { c{,} Z } { } l_dlichti_phone_print_tl
            dlichti_phone_href:VVV
            c_colon_str
            l_dlichti_phone_href_tl
            l_dlichti_phone_print_tl
            }
            cs_new_protected:Nn dlichti_phone_href:nnn
            {
            href{tel#1#2}{#3}
            }
            cs_generate_variant:Nn dlichti_phone_href:nnn { VVV }

            ExplSyntaxOff

            begin{document}

            phone{01 23 45 67 89}

            phone{0123456789}

            end{document}


            enter image description here






            share|improve this answer

































              2














              The xstring commands are not expandable so can't in general be used inline in other commands. You can use a simple expandable replacement here.



              documentclass{minimal}


              usepackage{hyperref}

              makeatletter
              defzza#1 {#1zza}
              defzzb#1 {#1,zzb}
              newcommandphone[1]{{def!##1{}def$##1##2{}href{tel:zza#1! }{zzb#1$ }}}
              makeatother

              begin{document}

              href{tel:0123456789}{01,23,45,67,89}

              phone{01 23 45 67 89}

              end{document}





              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%2f476835%2fmacro-expansion-inside-href%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









                4














                Expand the string substitution first by storing it in an argument, which you can then use with hyperref's href:



                documentclass{article}

                usepackage{xstring}
                usepackage{hyperref}

                newcommandphone[1]{%
                StrSubstitute{#1}{ }{}[firstarg]% Store first substitution in firstarg
                StrSubstitute{#1}{ }{,}[secondarg]% Store second substitution in secondarg
                href{tel:firstarg}{secondarg}% Use stored arguments in href
                }

                begin{document}

                href{tel:0123456789}{01,23,45,67,89}

                phone{01 23 45 67 89}

                end{document}





                share|improve this answer




























                  4














                  Expand the string substitution first by storing it in an argument, which you can then use with hyperref's href:



                  documentclass{article}

                  usepackage{xstring}
                  usepackage{hyperref}

                  newcommandphone[1]{%
                  StrSubstitute{#1}{ }{}[firstarg]% Store first substitution in firstarg
                  StrSubstitute{#1}{ }{,}[secondarg]% Store second substitution in secondarg
                  href{tel:firstarg}{secondarg}% Use stored arguments in href
                  }

                  begin{document}

                  href{tel:0123456789}{01,23,45,67,89}

                  phone{01 23 45 67 89}

                  end{document}





                  share|improve this answer


























                    4












                    4








                    4







                    Expand the string substitution first by storing it in an argument, which you can then use with hyperref's href:



                    documentclass{article}

                    usepackage{xstring}
                    usepackage{hyperref}

                    newcommandphone[1]{%
                    StrSubstitute{#1}{ }{}[firstarg]% Store first substitution in firstarg
                    StrSubstitute{#1}{ }{,}[secondarg]% Store second substitution in secondarg
                    href{tel:firstarg}{secondarg}% Use stored arguments in href
                    }

                    begin{document}

                    href{tel:0123456789}{01,23,45,67,89}

                    phone{01 23 45 67 89}

                    end{document}





                    share|improve this answer













                    Expand the string substitution first by storing it in an argument, which you can then use with hyperref's href:



                    documentclass{article}

                    usepackage{xstring}
                    usepackage{hyperref}

                    newcommandphone[1]{%
                    StrSubstitute{#1}{ }{}[firstarg]% Store first substitution in firstarg
                    StrSubstitute{#1}{ }{,}[secondarg]% Store second substitution in secondarg
                    href{tel:firstarg}{secondarg}% Use stored arguments in href
                    }

                    begin{document}

                    href{tel:0123456789}{01,23,45,67,89}

                    phone{01 23 45 67 89}

                    end{document}






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 26 at 22:28









                    WernerWerner

                    449k729961703




                    449k729961703























                        3














                        You can't use StrSubstitute in those places, because it doesn't produce the string after the substitution, but rather a fairly complicated set of instructions to produce that string.



                        A more complicated solution that avoids the need to input spaces between pairs of digits, so it will work even if you forget them.



                        documentclass{article}

                        usepackage{xparse}
                        usepackage{hyperref}

                        ExplSyntaxOn

                        NewDocumentCommand{phone}{m}
                        {
                        dlichti_phone:n { #1 }
                        }

                        tl_new:N l_dlichti_phone_href_tl
                        tl_new:N l_dlichti_phone_print_tl

                        cs_new_protected:Nn dlichti_phone:n
                        {
                        tl_set:Nx l_dlichti_phone_href_tl { #1 }
                        % remove all spaces
                        tl_replace_all:Nnn l_dlichti_phone_href_tl { ~ } { }
                        % save a copy
                        tl_set_eq:NN l_dlichti_phone_print_tl l_dlichti_phone_href_tl
                        % insert a thin space between any pair of digits
                        regex_replace_all:nnN
                        { ([0-9][0-9]) } % two digits followed by another digit
                        { 1c{,} } % the same with , in between
                        l_dlichti_phone_print_tl
                        % remove the trailing ,
                        regex_replace_once:nnN { c{,} Z } { } l_dlichti_phone_print_tl
                        dlichti_phone_href:VVV
                        c_colon_str
                        l_dlichti_phone_href_tl
                        l_dlichti_phone_print_tl
                        }
                        cs_new_protected:Nn dlichti_phone_href:nnn
                        {
                        href{tel#1#2}{#3}
                        }
                        cs_generate_variant:Nn dlichti_phone_href:nnn { VVV }

                        ExplSyntaxOff

                        begin{document}

                        phone{01 23 45 67 89}

                        phone{0123456789}

                        end{document}


                        enter image description here






                        share|improve this answer






























                          3














                          You can't use StrSubstitute in those places, because it doesn't produce the string after the substitution, but rather a fairly complicated set of instructions to produce that string.



                          A more complicated solution that avoids the need to input spaces between pairs of digits, so it will work even if you forget them.



                          documentclass{article}

                          usepackage{xparse}
                          usepackage{hyperref}

                          ExplSyntaxOn

                          NewDocumentCommand{phone}{m}
                          {
                          dlichti_phone:n { #1 }
                          }

                          tl_new:N l_dlichti_phone_href_tl
                          tl_new:N l_dlichti_phone_print_tl

                          cs_new_protected:Nn dlichti_phone:n
                          {
                          tl_set:Nx l_dlichti_phone_href_tl { #1 }
                          % remove all spaces
                          tl_replace_all:Nnn l_dlichti_phone_href_tl { ~ } { }
                          % save a copy
                          tl_set_eq:NN l_dlichti_phone_print_tl l_dlichti_phone_href_tl
                          % insert a thin space between any pair of digits
                          regex_replace_all:nnN
                          { ([0-9][0-9]) } % two digits followed by another digit
                          { 1c{,} } % the same with , in between
                          l_dlichti_phone_print_tl
                          % remove the trailing ,
                          regex_replace_once:nnN { c{,} Z } { } l_dlichti_phone_print_tl
                          dlichti_phone_href:VVV
                          c_colon_str
                          l_dlichti_phone_href_tl
                          l_dlichti_phone_print_tl
                          }
                          cs_new_protected:Nn dlichti_phone_href:nnn
                          {
                          href{tel#1#2}{#3}
                          }
                          cs_generate_variant:Nn dlichti_phone_href:nnn { VVV }

                          ExplSyntaxOff

                          begin{document}

                          phone{01 23 45 67 89}

                          phone{0123456789}

                          end{document}


                          enter image description here






                          share|improve this answer




























                            3












                            3








                            3







                            You can't use StrSubstitute in those places, because it doesn't produce the string after the substitution, but rather a fairly complicated set of instructions to produce that string.



                            A more complicated solution that avoids the need to input spaces between pairs of digits, so it will work even if you forget them.



                            documentclass{article}

                            usepackage{xparse}
                            usepackage{hyperref}

                            ExplSyntaxOn

                            NewDocumentCommand{phone}{m}
                            {
                            dlichti_phone:n { #1 }
                            }

                            tl_new:N l_dlichti_phone_href_tl
                            tl_new:N l_dlichti_phone_print_tl

                            cs_new_protected:Nn dlichti_phone:n
                            {
                            tl_set:Nx l_dlichti_phone_href_tl { #1 }
                            % remove all spaces
                            tl_replace_all:Nnn l_dlichti_phone_href_tl { ~ } { }
                            % save a copy
                            tl_set_eq:NN l_dlichti_phone_print_tl l_dlichti_phone_href_tl
                            % insert a thin space between any pair of digits
                            regex_replace_all:nnN
                            { ([0-9][0-9]) } % two digits followed by another digit
                            { 1c{,} } % the same with , in between
                            l_dlichti_phone_print_tl
                            % remove the trailing ,
                            regex_replace_once:nnN { c{,} Z } { } l_dlichti_phone_print_tl
                            dlichti_phone_href:VVV
                            c_colon_str
                            l_dlichti_phone_href_tl
                            l_dlichti_phone_print_tl
                            }
                            cs_new_protected:Nn dlichti_phone_href:nnn
                            {
                            href{tel#1#2}{#3}
                            }
                            cs_generate_variant:Nn dlichti_phone_href:nnn { VVV }

                            ExplSyntaxOff

                            begin{document}

                            phone{01 23 45 67 89}

                            phone{0123456789}

                            end{document}


                            enter image description here






                            share|improve this answer















                            You can't use StrSubstitute in those places, because it doesn't produce the string after the substitution, but rather a fairly complicated set of instructions to produce that string.



                            A more complicated solution that avoids the need to input spaces between pairs of digits, so it will work even if you forget them.



                            documentclass{article}

                            usepackage{xparse}
                            usepackage{hyperref}

                            ExplSyntaxOn

                            NewDocumentCommand{phone}{m}
                            {
                            dlichti_phone:n { #1 }
                            }

                            tl_new:N l_dlichti_phone_href_tl
                            tl_new:N l_dlichti_phone_print_tl

                            cs_new_protected:Nn dlichti_phone:n
                            {
                            tl_set:Nx l_dlichti_phone_href_tl { #1 }
                            % remove all spaces
                            tl_replace_all:Nnn l_dlichti_phone_href_tl { ~ } { }
                            % save a copy
                            tl_set_eq:NN l_dlichti_phone_print_tl l_dlichti_phone_href_tl
                            % insert a thin space between any pair of digits
                            regex_replace_all:nnN
                            { ([0-9][0-9]) } % two digits followed by another digit
                            { 1c{,} } % the same with , in between
                            l_dlichti_phone_print_tl
                            % remove the trailing ,
                            regex_replace_once:nnN { c{,} Z } { } l_dlichti_phone_print_tl
                            dlichti_phone_href:VVV
                            c_colon_str
                            l_dlichti_phone_href_tl
                            l_dlichti_phone_print_tl
                            }
                            cs_new_protected:Nn dlichti_phone_href:nnn
                            {
                            href{tel#1#2}{#3}
                            }
                            cs_generate_variant:Nn dlichti_phone_href:nnn { VVV }

                            ExplSyntaxOff

                            begin{document}

                            phone{01 23 45 67 89}

                            phone{0123456789}

                            end{document}


                            enter image description here







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Feb 26 at 22:56

























                            answered Feb 26 at 22:32









                            egregegreg

                            730k8819293243




                            730k8819293243























                                2














                                The xstring commands are not expandable so can't in general be used inline in other commands. You can use a simple expandable replacement here.



                                documentclass{minimal}


                                usepackage{hyperref}

                                makeatletter
                                defzza#1 {#1zza}
                                defzzb#1 {#1,zzb}
                                newcommandphone[1]{{def!##1{}def$##1##2{}href{tel:zza#1! }{zzb#1$ }}}
                                makeatother

                                begin{document}

                                href{tel:0123456789}{01,23,45,67,89}

                                phone{01 23 45 67 89}

                                end{document}





                                share|improve this answer






























                                  2














                                  The xstring commands are not expandable so can't in general be used inline in other commands. You can use a simple expandable replacement here.



                                  documentclass{minimal}


                                  usepackage{hyperref}

                                  makeatletter
                                  defzza#1 {#1zza}
                                  defzzb#1 {#1,zzb}
                                  newcommandphone[1]{{def!##1{}def$##1##2{}href{tel:zza#1! }{zzb#1$ }}}
                                  makeatother

                                  begin{document}

                                  href{tel:0123456789}{01,23,45,67,89}

                                  phone{01 23 45 67 89}

                                  end{document}





                                  share|improve this answer




























                                    2












                                    2








                                    2







                                    The xstring commands are not expandable so can't in general be used inline in other commands. You can use a simple expandable replacement here.



                                    documentclass{minimal}


                                    usepackage{hyperref}

                                    makeatletter
                                    defzza#1 {#1zza}
                                    defzzb#1 {#1,zzb}
                                    newcommandphone[1]{{def!##1{}def$##1##2{}href{tel:zza#1! }{zzb#1$ }}}
                                    makeatother

                                    begin{document}

                                    href{tel:0123456789}{01,23,45,67,89}

                                    phone{01 23 45 67 89}

                                    end{document}





                                    share|improve this answer















                                    The xstring commands are not expandable so can't in general be used inline in other commands. You can use a simple expandable replacement here.



                                    documentclass{minimal}


                                    usepackage{hyperref}

                                    makeatletter
                                    defzza#1 {#1zza}
                                    defzzb#1 {#1,zzb}
                                    newcommandphone[1]{{def!##1{}def$##1##2{}href{tel:zza#1! }{zzb#1$ }}}
                                    makeatother

                                    begin{document}

                                    href{tel:0123456789}{01,23,45,67,89}

                                    phone{01 23 45 67 89}

                                    end{document}






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Feb 26 at 22:31

























                                    answered Feb 26 at 22:17









                                    David CarlisleDavid Carlisle

                                    497k4111441890




                                    497k4111441890






























                                        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%2f476835%2fmacro-expansion-inside-href%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

                                        Probability when a professor distributes a quiz and homework assignment to a class of n students.

                                        Aardman Animations

                                        Are they similar matrix