Macro expansion inside href












3















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}









share|improve this question



























    3















    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}









    share|improve this question

























      3












      3








      3








      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}









      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}






      hyperref xstring href






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 hours ago









      DLichtiDLichti

      605




      605






















          3 Answers
          3






          active

          oldest

          votes


















          2














          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

































            1














            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































              1














              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









                2














                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














                  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












                    2








                    2







                    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 1 hour ago

























                    answered 1 hour ago









                    egregegreg

                    722k8719163217




                    722k8719163217























                        1














                        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




























                          1














                          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


























                            1












                            1








                            1







                            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 1 hour ago









                            WernerWerner

                            445k699811686




                            445k699811686























                                1














                                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






























                                  1














                                  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




























                                    1












                                    1








                                    1







                                    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 1 hour ago

























                                    answered 1 hour ago









                                    David CarlisleDavid Carlisle

                                    492k4111371885




                                    492k4111371885






























                                        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

                                        How to label and detect the document text images

                                        Vallis Paradisi

                                        Tabula Rosettana