A command challenge












6















Here's a little challenge. The goal is to create a command that takes is two or three arguments without using square brackets for optional arguments. The first and second argument are separated by a comma and a space, while the second and the third (if present) are separated by a dash. This means that both of the following inputs should be acceptable



mycommand{arg_1, arg_2}


and



mycommand{arg_1, arg_2-arg_3}


The definition of what mycommand does with the two/three arguments is irrelevant to the problem.










share|improve this question


















  • 1





    That's one argument from a TeX point of view ... I guess you want a parser for that one document-level argument.

    – Joseph Wright
    4 hours ago











  • Try defmycommand#1,space#2-#3{...}.

    – John Kormylo
    48 mins ago


















6















Here's a little challenge. The goal is to create a command that takes is two or three arguments without using square brackets for optional arguments. The first and second argument are separated by a comma and a space, while the second and the third (if present) are separated by a dash. This means that both of the following inputs should be acceptable



mycommand{arg_1, arg_2}


and



mycommand{arg_1, arg_2-arg_3}


The definition of what mycommand does with the two/three arguments is irrelevant to the problem.










share|improve this question


















  • 1





    That's one argument from a TeX point of view ... I guess you want a parser for that one document-level argument.

    – Joseph Wright
    4 hours ago











  • Try defmycommand#1,space#2-#3{...}.

    – John Kormylo
    48 mins ago
















6












6








6


1






Here's a little challenge. The goal is to create a command that takes is two or three arguments without using square brackets for optional arguments. The first and second argument are separated by a comma and a space, while the second and the third (if present) are separated by a dash. This means that both of the following inputs should be acceptable



mycommand{arg_1, arg_2}


and



mycommand{arg_1, arg_2-arg_3}


The definition of what mycommand does with the two/three arguments is irrelevant to the problem.










share|improve this question














Here's a little challenge. The goal is to create a command that takes is two or three arguments without using square brackets for optional arguments. The first and second argument are separated by a comma and a space, while the second and the third (if present) are separated by a dash. This means that both of the following inputs should be acceptable



mycommand{arg_1, arg_2}


and



mycommand{arg_1, arg_2-arg_3}


The definition of what mycommand does with the two/three arguments is irrelevant to the problem.







macros expl3 xparse






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 4 hours ago









noibenoibe

935




935








  • 1





    That's one argument from a TeX point of view ... I guess you want a parser for that one document-level argument.

    – Joseph Wright
    4 hours ago











  • Try defmycommand#1,space#2-#3{...}.

    – John Kormylo
    48 mins ago
















  • 1





    That's one argument from a TeX point of view ... I guess you want a parser for that one document-level argument.

    – Joseph Wright
    4 hours ago











  • Try defmycommand#1,space#2-#3{...}.

    – John Kormylo
    48 mins ago










1




1





That's one argument from a TeX point of view ... I guess you want a parser for that one document-level argument.

– Joseph Wright
4 hours ago





That's one argument from a TeX point of view ... I guess you want a parser for that one document-level argument.

– Joseph Wright
4 hours ago













Try defmycommand#1,space#2-#3{...}.

– John Kormylo
48 mins ago







Try defmycommand#1,space#2-#3{...}.

– John Kormylo
48 mins ago












3 Answers
3






active

oldest

votes


















7














You can do it with xparse:



documentclass{article}
usepackage{xparse}

% split at the comma
NewDocumentCommand{mycommand}{ >{SplitArgument{1}{,}}m }{%
mycommandA#1%
}
% do something with #1 and split the second part at the hyphen
NewDocumentCommand{mycommandA}{ m >{SplitArgument{1}{-}}m }{%
Main is #1%
IfNoValueTF{#2}{.}{; mycommandB#2}%
}
% do something with the second part
NewDocumentCommand{mycommandB}{mm}{%
Secondary is #1IfValueT{#2}{ plus #2}.%
}

begin{document}

mycommand{arg1}

mycommand{arg1, arg2}

mycommand{arg1, arg2-arg3}

end{document}


enter image description here



An advantage: it's irrelevant if you forget the space after the comma.






share|improve this answer































    10














    enter image description here



    documentclass{article}

    defmycommand#1{xmycommand#1--relax}
    defxmycommand#1,#2-#3-#4relax{[#1][#2][#3]}


    begin{document}

    $mycommand{arg_1, arg_2}$

    $mycommand{arg_1, arg_2-arg3}$

    end{document}





    share|improve this answer



















    • 6





      Even without electricity. I'm impressed!

      – marmot
      4 hours ago











    • How about adding some mortar to the bricks!?

      – Werner
      4 hours ago











    • @Werner where I come from we prefer this style: derbyshireuk.net/derbyshire_drystonewalls.html

      – David Carlisle
      4 hours ago











    • @DavidCarlisle: It seems drafty... :D

      – Werner
      4 hours ago






    • 2





      @noibe oh no a disaster! I don't mind not getting the tick (I have more than enough points) but seeing points go to egreg is so depressing:)

      – David Carlisle
      3 hours ago



















    2














    A listofitems approach.



    documentclass{article}
    usepackage{listofitems}
    newcommandmycommand[1]{setsepchar{,/-}readlist*myargs{#1}%
    noindent Arg 1 is myargs[1]\
    Arg 2 is myargs[2,1]\
    Arg 3 is ifnumlistlenmyargs[2]=1relax Absentelsemyargs[2,2]fi%
    }
    begin{document}

    mycommand{arg1, arg2}

    and

    mycommand{arg1, arg2-arg3}

    end{document}


    enter image description here






    share|improve this answer























      Your Answer








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

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

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


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f473965%2fa-command-challenge%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 can do it with xparse:



      documentclass{article}
      usepackage{xparse}

      % split at the comma
      NewDocumentCommand{mycommand}{ >{SplitArgument{1}{,}}m }{%
      mycommandA#1%
      }
      % do something with #1 and split the second part at the hyphen
      NewDocumentCommand{mycommandA}{ m >{SplitArgument{1}{-}}m }{%
      Main is #1%
      IfNoValueTF{#2}{.}{; mycommandB#2}%
      }
      % do something with the second part
      NewDocumentCommand{mycommandB}{mm}{%
      Secondary is #1IfValueT{#2}{ plus #2}.%
      }

      begin{document}

      mycommand{arg1}

      mycommand{arg1, arg2}

      mycommand{arg1, arg2-arg3}

      end{document}


      enter image description here



      An advantage: it's irrelevant if you forget the space after the comma.






      share|improve this answer




























        7














        You can do it with xparse:



        documentclass{article}
        usepackage{xparse}

        % split at the comma
        NewDocumentCommand{mycommand}{ >{SplitArgument{1}{,}}m }{%
        mycommandA#1%
        }
        % do something with #1 and split the second part at the hyphen
        NewDocumentCommand{mycommandA}{ m >{SplitArgument{1}{-}}m }{%
        Main is #1%
        IfNoValueTF{#2}{.}{; mycommandB#2}%
        }
        % do something with the second part
        NewDocumentCommand{mycommandB}{mm}{%
        Secondary is #1IfValueT{#2}{ plus #2}.%
        }

        begin{document}

        mycommand{arg1}

        mycommand{arg1, arg2}

        mycommand{arg1, arg2-arg3}

        end{document}


        enter image description here



        An advantage: it's irrelevant if you forget the space after the comma.






        share|improve this answer


























          7












          7








          7







          You can do it with xparse:



          documentclass{article}
          usepackage{xparse}

          % split at the comma
          NewDocumentCommand{mycommand}{ >{SplitArgument{1}{,}}m }{%
          mycommandA#1%
          }
          % do something with #1 and split the second part at the hyphen
          NewDocumentCommand{mycommandA}{ m >{SplitArgument{1}{-}}m }{%
          Main is #1%
          IfNoValueTF{#2}{.}{; mycommandB#2}%
          }
          % do something with the second part
          NewDocumentCommand{mycommandB}{mm}{%
          Secondary is #1IfValueT{#2}{ plus #2}.%
          }

          begin{document}

          mycommand{arg1}

          mycommand{arg1, arg2}

          mycommand{arg1, arg2-arg3}

          end{document}


          enter image description here



          An advantage: it's irrelevant if you forget the space after the comma.






          share|improve this answer













          You can do it with xparse:



          documentclass{article}
          usepackage{xparse}

          % split at the comma
          NewDocumentCommand{mycommand}{ >{SplitArgument{1}{,}}m }{%
          mycommandA#1%
          }
          % do something with #1 and split the second part at the hyphen
          NewDocumentCommand{mycommandA}{ m >{SplitArgument{1}{-}}m }{%
          Main is #1%
          IfNoValueTF{#2}{.}{; mycommandB#2}%
          }
          % do something with the second part
          NewDocumentCommand{mycommandB}{mm}{%
          Secondary is #1IfValueT{#2}{ plus #2}.%
          }

          begin{document}

          mycommand{arg1}

          mycommand{arg1, arg2}

          mycommand{arg1, arg2-arg3}

          end{document}


          enter image description here



          An advantage: it's irrelevant if you forget the space after the comma.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 4 hours ago









          egregegreg

          718k8719043200




          718k8719043200























              10














              enter image description here



              documentclass{article}

              defmycommand#1{xmycommand#1--relax}
              defxmycommand#1,#2-#3-#4relax{[#1][#2][#3]}


              begin{document}

              $mycommand{arg_1, arg_2}$

              $mycommand{arg_1, arg_2-arg3}$

              end{document}





              share|improve this answer



















              • 6





                Even without electricity. I'm impressed!

                – marmot
                4 hours ago











              • How about adding some mortar to the bricks!?

                – Werner
                4 hours ago











              • @Werner where I come from we prefer this style: derbyshireuk.net/derbyshire_drystonewalls.html

                – David Carlisle
                4 hours ago











              • @DavidCarlisle: It seems drafty... :D

                – Werner
                4 hours ago






              • 2





                @noibe oh no a disaster! I don't mind not getting the tick (I have more than enough points) but seeing points go to egreg is so depressing:)

                – David Carlisle
                3 hours ago
















              10














              enter image description here



              documentclass{article}

              defmycommand#1{xmycommand#1--relax}
              defxmycommand#1,#2-#3-#4relax{[#1][#2][#3]}


              begin{document}

              $mycommand{arg_1, arg_2}$

              $mycommand{arg_1, arg_2-arg3}$

              end{document}





              share|improve this answer



















              • 6





                Even without electricity. I'm impressed!

                – marmot
                4 hours ago











              • How about adding some mortar to the bricks!?

                – Werner
                4 hours ago











              • @Werner where I come from we prefer this style: derbyshireuk.net/derbyshire_drystonewalls.html

                – David Carlisle
                4 hours ago











              • @DavidCarlisle: It seems drafty... :D

                – Werner
                4 hours ago






              • 2





                @noibe oh no a disaster! I don't mind not getting the tick (I have more than enough points) but seeing points go to egreg is so depressing:)

                – David Carlisle
                3 hours ago














              10












              10








              10







              enter image description here



              documentclass{article}

              defmycommand#1{xmycommand#1--relax}
              defxmycommand#1,#2-#3-#4relax{[#1][#2][#3]}


              begin{document}

              $mycommand{arg_1, arg_2}$

              $mycommand{arg_1, arg_2-arg3}$

              end{document}





              share|improve this answer













              enter image description here



              documentclass{article}

              defmycommand#1{xmycommand#1--relax}
              defxmycommand#1,#2-#3-#4relax{[#1][#2][#3]}


              begin{document}

              $mycommand{arg_1, arg_2}$

              $mycommand{arg_1, arg_2-arg3}$

              end{document}






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 4 hours ago









              David CarlisleDavid Carlisle

              489k4111321880




              489k4111321880








              • 6





                Even without electricity. I'm impressed!

                – marmot
                4 hours ago











              • How about adding some mortar to the bricks!?

                – Werner
                4 hours ago











              • @Werner where I come from we prefer this style: derbyshireuk.net/derbyshire_drystonewalls.html

                – David Carlisle
                4 hours ago











              • @DavidCarlisle: It seems drafty... :D

                – Werner
                4 hours ago






              • 2





                @noibe oh no a disaster! I don't mind not getting the tick (I have more than enough points) but seeing points go to egreg is so depressing:)

                – David Carlisle
                3 hours ago














              • 6





                Even without electricity. I'm impressed!

                – marmot
                4 hours ago











              • How about adding some mortar to the bricks!?

                – Werner
                4 hours ago











              • @Werner where I come from we prefer this style: derbyshireuk.net/derbyshire_drystonewalls.html

                – David Carlisle
                4 hours ago











              • @DavidCarlisle: It seems drafty... :D

                – Werner
                4 hours ago






              • 2





                @noibe oh no a disaster! I don't mind not getting the tick (I have more than enough points) but seeing points go to egreg is so depressing:)

                – David Carlisle
                3 hours ago








              6




              6





              Even without electricity. I'm impressed!

              – marmot
              4 hours ago





              Even without electricity. I'm impressed!

              – marmot
              4 hours ago













              How about adding some mortar to the bricks!?

              – Werner
              4 hours ago





              How about adding some mortar to the bricks!?

              – Werner
              4 hours ago













              @Werner where I come from we prefer this style: derbyshireuk.net/derbyshire_drystonewalls.html

              – David Carlisle
              4 hours ago





              @Werner where I come from we prefer this style: derbyshireuk.net/derbyshire_drystonewalls.html

              – David Carlisle
              4 hours ago













              @DavidCarlisle: It seems drafty... :D

              – Werner
              4 hours ago





              @DavidCarlisle: It seems drafty... :D

              – Werner
              4 hours ago




              2




              2





              @noibe oh no a disaster! I don't mind not getting the tick (I have more than enough points) but seeing points go to egreg is so depressing:)

              – David Carlisle
              3 hours ago





              @noibe oh no a disaster! I don't mind not getting the tick (I have more than enough points) but seeing points go to egreg is so depressing:)

              – David Carlisle
              3 hours ago











              2














              A listofitems approach.



              documentclass{article}
              usepackage{listofitems}
              newcommandmycommand[1]{setsepchar{,/-}readlist*myargs{#1}%
              noindent Arg 1 is myargs[1]\
              Arg 2 is myargs[2,1]\
              Arg 3 is ifnumlistlenmyargs[2]=1relax Absentelsemyargs[2,2]fi%
              }
              begin{document}

              mycommand{arg1, arg2}

              and

              mycommand{arg1, arg2-arg3}

              end{document}


              enter image description here






              share|improve this answer




























                2














                A listofitems approach.



                documentclass{article}
                usepackage{listofitems}
                newcommandmycommand[1]{setsepchar{,/-}readlist*myargs{#1}%
                noindent Arg 1 is myargs[1]\
                Arg 2 is myargs[2,1]\
                Arg 3 is ifnumlistlenmyargs[2]=1relax Absentelsemyargs[2,2]fi%
                }
                begin{document}

                mycommand{arg1, arg2}

                and

                mycommand{arg1, arg2-arg3}

                end{document}


                enter image description here






                share|improve this answer


























                  2












                  2








                  2







                  A listofitems approach.



                  documentclass{article}
                  usepackage{listofitems}
                  newcommandmycommand[1]{setsepchar{,/-}readlist*myargs{#1}%
                  noindent Arg 1 is myargs[1]\
                  Arg 2 is myargs[2,1]\
                  Arg 3 is ifnumlistlenmyargs[2]=1relax Absentelsemyargs[2,2]fi%
                  }
                  begin{document}

                  mycommand{arg1, arg2}

                  and

                  mycommand{arg1, arg2-arg3}

                  end{document}


                  enter image description here






                  share|improve this answer













                  A listofitems approach.



                  documentclass{article}
                  usepackage{listofitems}
                  newcommandmycommand[1]{setsepchar{,/-}readlist*myargs{#1}%
                  noindent Arg 1 is myargs[1]\
                  Arg 2 is myargs[2,1]\
                  Arg 3 is ifnumlistlenmyargs[2]=1relax Absentelsemyargs[2,2]fi%
                  }
                  begin{document}

                  mycommand{arg1, arg2}

                  and

                  mycommand{arg1, arg2-arg3}

                  end{document}


                  enter image description here







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 3 hours ago









                  Steven B. SegletesSteven B. Segletes

                  154k9198405




                  154k9198405






























                      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%2f473965%2fa-command-challenge%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