newcommand: Using one parameter as the default for the otherDefining a command for a minted environmentOptional argument for newcommand?Switch on number of arguments given to a macroEnvironment with coordinate-like argumentsDefining a macro with three optional arguments in the form newmacroab[c]d[e][f] and newmacroab[c]d*[f]Set the default value of argument as parameter in newcommandUsing the compulsory argument for the optional argument?How to pass optional arguments to command?Second optional argument for newcommand to be the same of the first in case it is not specifiedLuaTeX: Call a Lua function with two optional arguments

Java - What do constructor type arguments mean when placed *before* the type?

Lifted its hind leg on or lifted its hind leg towards?

What do you call the infoboxes with text and sometimes images on the side of a page we find in textbooks?

Could solar power be utilized and substitute coal in the 19th century?

What is Sitecore Managed Cloud?

Teaching indefinite integrals that require special-casing

What will be the benefits of Brexit?

Can I create an upright 7-foot × 5-foot wall with the Minor Illusion spell?

Is it legal to discriminate due to the medicine used to treat a medical condition?

word describing multiple paths to the same abstract outcome

How can I raise concerns with a new DM about XP splitting?

How to interpret the phrase "t’en a fait voir à toi"?

Books on the History of math research at European universities

How will losing mobility of one hand affect my career as a programmer?

How to check participants in at events?

The One-Electron Universe postulate is true - what simple change can I make to change the whole universe?

Is there an Impartial Brexit Deal comparison site?

I2C signal and power over long range (10meter cable)

Visiting the UK as unmarried couple

How do ultrasonic sensors differentiate between transmitted and received signals?

Hostile work environment after whistle-blowing on coworker and our boss. What do I do?

Installing PowerShell on 32-bit Kali OS fails

Are taller landing gear bad for aircraft, particulary large airliners?

What is the term when two people sing in harmony, but they aren't singing the same notes?



newcommand: Using one parameter as the default for the other


Defining a command for a minted environmentOptional argument for newcommand?Switch on number of arguments given to a macroEnvironment with coordinate-like argumentsDefining a macro with three optional arguments in the form newmacroab[c]d[e][f] and newmacroab[c]d*[f]Set the default value of argument as parameter in newcommandUsing the compulsory argument for the optional argument?How to pass optional arguments to command?Second optional argument for newcommand to be the same of the first in case it is not specifiedLuaTeX: Call a Lua function with two optional arguments













9















I would like to define a macro that takes two arguments, one of which is optional. If it is not given, then it should take the other one as default value.



Unfortunately,



newcommandmycommand[2][#1] ... 


does not work, neither does



newcommandmycommand[2][#2] ... 


Does anybody know how to do this?










share|improve this question
























  • newcommandmycommand[2][default]...?

    – marmot
    Mar 8 at 6:40











  • The problem is that putting #1 or #2 as default value does not work. Using an arbitrary fixed default value works just fine, but is not what I want.

    – Bodo Manthey
    Mar 8 at 6:41
















9















I would like to define a macro that takes two arguments, one of which is optional. If it is not given, then it should take the other one as default value.



Unfortunately,



newcommandmycommand[2][#1] ... 


does not work, neither does



newcommandmycommand[2][#2] ... 


Does anybody know how to do this?










share|improve this question
























  • newcommandmycommand[2][default]...?

    – marmot
    Mar 8 at 6:40











  • The problem is that putting #1 or #2 as default value does not work. Using an arbitrary fixed default value works just fine, but is not what I want.

    – Bodo Manthey
    Mar 8 at 6:41














9












9








9


0






I would like to define a macro that takes two arguments, one of which is optional. If it is not given, then it should take the other one as default value.



Unfortunately,



newcommandmycommand[2][#1] ... 


does not work, neither does



newcommandmycommand[2][#2] ... 


Does anybody know how to do this?










share|improve this question
















I would like to define a macro that takes two arguments, one of which is optional. If it is not given, then it should take the other one as default value.



Unfortunately,



newcommandmycommand[2][#1] ... 


does not work, neither does



newcommandmycommand[2][#2] ... 


Does anybody know how to do this?







macros optional-arguments






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 13:45







Bodo Manthey

















asked Mar 8 at 6:38









Bodo MantheyBodo Manthey

1689




1689












  • newcommandmycommand[2][default]...?

    – marmot
    Mar 8 at 6:40











  • The problem is that putting #1 or #2 as default value does not work. Using an arbitrary fixed default value works just fine, but is not what I want.

    – Bodo Manthey
    Mar 8 at 6:41


















  • newcommandmycommand[2][default]...?

    – marmot
    Mar 8 at 6:40











  • The problem is that putting #1 or #2 as default value does not work. Using an arbitrary fixed default value works just fine, but is not what I want.

    – Bodo Manthey
    Mar 8 at 6:41

















newcommandmycommand[2][default]...?

– marmot
Mar 8 at 6:40





newcommandmycommand[2][default]...?

– marmot
Mar 8 at 6:40













The problem is that putting #1 or #2 as default value does not work. Using an arbitrary fixed default value works just fine, but is not what I want.

– Bodo Manthey
Mar 8 at 6:41






The problem is that putting #1 or #2 as default value does not work. Using an arbitrary fixed default value works just fine, but is not what I want.

– Bodo Manthey
Mar 8 at 6:41











3 Answers
3






active

oldest

votes


















10














Good news: you can do it very easily with xparse:



documentclassarticle
usepackagexparse

NewDocumentCommandfooO#2m%
Optional=``#1'', mandatory=``#2''par

NewDocumentCommandoofmO#1%
Mandatory=``#1'', optional=``#2''par


begindocument

foox

foo[y]x

oofx

oofx[y]

enddocument


The argument specifier O... takes as argument what to substitute as default when the argument itself doesn't appear at call time. This can well be a parameter token referring to another argument.



enter image description here






share|improve this answer






























    9














    You can use xparse to easily condition on whether or not an optional argument was present or not, and supply the appropriate combination to another (auxiliary) function. Here's an example:



    enter image description here



    documentclassarticle

    usepackagexparse

    newcommandprintthis[2]%
    Optional: #1; Mandatory: #2%


    NewDocumentCommandmycommando m%
    IfValueTF#1
    printthis#1#2% mycommand[..]...
    printthis#2#2% mycommand...


    begindocument

    mycommandfirst

    mycommand[first]second

    enddocument



    A slightly different version of this stems from the use of caption, where you can supply an optional argument for the LoT/LoF, but if you don't, the mandatory arguments is sent instead (similarly for sectional units with optional arguments destined for the ToC). This uses the kernel's @dblarg:



    documentclassarticle

    newcommandprintthis[2][]%
    Optional: #1; Mandatory: #2%


    makeatletter
    newcommandmycommand%
    @dblargprintthis

    makeatother

    begindocument

    mycommandfirst

    mycommand[first]second

    enddocument





    share|improve this answer























    • Great, thank you very much!

      – Bodo Manthey
      Mar 8 at 6:53


















    2














    This is an attempt to add protection as with other macros that process optional arguments:



    %%errorcontextlines=1000
    documentclass[a4paper]article

    makeatletter
    newcommandmakefirstmandatorytheoptional[1]%
    expandafterinnermakefirstmandatorytheoptional
    expandaftercsnamestring#1endcsname#1%
    %
    newcommandinnermakefirstmandatorytheoptional[2]%
    def#2%
    ifxprotect@typeset@protect
    expandafter@firstoftwo
    else
    expandafter@secondoftwo
    fi
    kernel@ifnextchar[#1@dblarg#1%
    protect#2%
    %
    %
    newcommandmycommand[2][dummyoptional]%
    This is taken for the optional argument: #1.\
    This is taken for the mandatory argument: #2.
    %
    makefirstmandatorytheoptionalmycommand%
    makeatother

    parindent=0ex
    parskip=medskipamount

    begindocument

    No optional argument given---verb|mycommandA|:

    mycommandA

    Optional argument "B" given---verb|mycommand[B]A|:

    mycommand[B]A

    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%2f478354%2fnewcommand-using-one-parameter-as-the-default-for-the-other%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









      10














      Good news: you can do it very easily with xparse:



      documentclassarticle
      usepackagexparse

      NewDocumentCommandfooO#2m%
      Optional=``#1'', mandatory=``#2''par

      NewDocumentCommandoofmO#1%
      Mandatory=``#1'', optional=``#2''par


      begindocument

      foox

      foo[y]x

      oofx

      oofx[y]

      enddocument


      The argument specifier O... takes as argument what to substitute as default when the argument itself doesn't appear at call time. This can well be a parameter token referring to another argument.



      enter image description here






      share|improve this answer



























        10














        Good news: you can do it very easily with xparse:



        documentclassarticle
        usepackagexparse

        NewDocumentCommandfooO#2m%
        Optional=``#1'', mandatory=``#2''par

        NewDocumentCommandoofmO#1%
        Mandatory=``#1'', optional=``#2''par


        begindocument

        foox

        foo[y]x

        oofx

        oofx[y]

        enddocument


        The argument specifier O... takes as argument what to substitute as default when the argument itself doesn't appear at call time. This can well be a parameter token referring to another argument.



        enter image description here






        share|improve this answer

























          10












          10








          10







          Good news: you can do it very easily with xparse:



          documentclassarticle
          usepackagexparse

          NewDocumentCommandfooO#2m%
          Optional=``#1'', mandatory=``#2''par

          NewDocumentCommandoofmO#1%
          Mandatory=``#1'', optional=``#2''par


          begindocument

          foox

          foo[y]x

          oofx

          oofx[y]

          enddocument


          The argument specifier O... takes as argument what to substitute as default when the argument itself doesn't appear at call time. This can well be a parameter token referring to another argument.



          enter image description here






          share|improve this answer













          Good news: you can do it very easily with xparse:



          documentclassarticle
          usepackagexparse

          NewDocumentCommandfooO#2m%
          Optional=``#1'', mandatory=``#2''par

          NewDocumentCommandoofmO#1%
          Mandatory=``#1'', optional=``#2''par


          begindocument

          foox

          foo[y]x

          oofx

          oofx[y]

          enddocument


          The argument specifier O... takes as argument what to substitute as default when the argument itself doesn't appear at call time. This can well be a parameter token referring to another argument.



          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 7:24









          egregegreg

          729k8819263235




          729k8819263235





















              9














              You can use xparse to easily condition on whether or not an optional argument was present or not, and supply the appropriate combination to another (auxiliary) function. Here's an example:



              enter image description here



              documentclassarticle

              usepackagexparse

              newcommandprintthis[2]%
              Optional: #1; Mandatory: #2%


              NewDocumentCommandmycommando m%
              IfValueTF#1
              printthis#1#2% mycommand[..]...
              printthis#2#2% mycommand...


              begindocument

              mycommandfirst

              mycommand[first]second

              enddocument



              A slightly different version of this stems from the use of caption, where you can supply an optional argument for the LoT/LoF, but if you don't, the mandatory arguments is sent instead (similarly for sectional units with optional arguments destined for the ToC). This uses the kernel's @dblarg:



              documentclassarticle

              newcommandprintthis[2][]%
              Optional: #1; Mandatory: #2%


              makeatletter
              newcommandmycommand%
              @dblargprintthis

              makeatother

              begindocument

              mycommandfirst

              mycommand[first]second

              enddocument





              share|improve this answer























              • Great, thank you very much!

                – Bodo Manthey
                Mar 8 at 6:53















              9














              You can use xparse to easily condition on whether or not an optional argument was present or not, and supply the appropriate combination to another (auxiliary) function. Here's an example:



              enter image description here



              documentclassarticle

              usepackagexparse

              newcommandprintthis[2]%
              Optional: #1; Mandatory: #2%


              NewDocumentCommandmycommando m%
              IfValueTF#1
              printthis#1#2% mycommand[..]...
              printthis#2#2% mycommand...


              begindocument

              mycommandfirst

              mycommand[first]second

              enddocument



              A slightly different version of this stems from the use of caption, where you can supply an optional argument for the LoT/LoF, but if you don't, the mandatory arguments is sent instead (similarly for sectional units with optional arguments destined for the ToC). This uses the kernel's @dblarg:



              documentclassarticle

              newcommandprintthis[2][]%
              Optional: #1; Mandatory: #2%


              makeatletter
              newcommandmycommand%
              @dblargprintthis

              makeatother

              begindocument

              mycommandfirst

              mycommand[first]second

              enddocument





              share|improve this answer























              • Great, thank you very much!

                – Bodo Manthey
                Mar 8 at 6:53













              9












              9








              9







              You can use xparse to easily condition on whether or not an optional argument was present or not, and supply the appropriate combination to another (auxiliary) function. Here's an example:



              enter image description here



              documentclassarticle

              usepackagexparse

              newcommandprintthis[2]%
              Optional: #1; Mandatory: #2%


              NewDocumentCommandmycommando m%
              IfValueTF#1
              printthis#1#2% mycommand[..]...
              printthis#2#2% mycommand...


              begindocument

              mycommandfirst

              mycommand[first]second

              enddocument



              A slightly different version of this stems from the use of caption, where you can supply an optional argument for the LoT/LoF, but if you don't, the mandatory arguments is sent instead (similarly for sectional units with optional arguments destined for the ToC). This uses the kernel's @dblarg:



              documentclassarticle

              newcommandprintthis[2][]%
              Optional: #1; Mandatory: #2%


              makeatletter
              newcommandmycommand%
              @dblargprintthis

              makeatother

              begindocument

              mycommandfirst

              mycommand[first]second

              enddocument





              share|improve this answer













              You can use xparse to easily condition on whether or not an optional argument was present or not, and supply the appropriate combination to another (auxiliary) function. Here's an example:



              enter image description here



              documentclassarticle

              usepackagexparse

              newcommandprintthis[2]%
              Optional: #1; Mandatory: #2%


              NewDocumentCommandmycommando m%
              IfValueTF#1
              printthis#1#2% mycommand[..]...
              printthis#2#2% mycommand...


              begindocument

              mycommandfirst

              mycommand[first]second

              enddocument



              A slightly different version of this stems from the use of caption, where you can supply an optional argument for the LoT/LoF, but if you don't, the mandatory arguments is sent instead (similarly for sectional units with optional arguments destined for the ToC). This uses the kernel's @dblarg:



              documentclassarticle

              newcommandprintthis[2][]%
              Optional: #1; Mandatory: #2%


              makeatletter
              newcommandmycommand%
              @dblargprintthis

              makeatother

              begindocument

              mycommandfirst

              mycommand[first]second

              enddocument






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 8 at 6:47









              WernerWerner

              448k719931699




              448k719931699












              • Great, thank you very much!

                – Bodo Manthey
                Mar 8 at 6:53

















              • Great, thank you very much!

                – Bodo Manthey
                Mar 8 at 6:53
















              Great, thank you very much!

              – Bodo Manthey
              Mar 8 at 6:53





              Great, thank you very much!

              – Bodo Manthey
              Mar 8 at 6:53











              2














              This is an attempt to add protection as with other macros that process optional arguments:



              %%errorcontextlines=1000
              documentclass[a4paper]article

              makeatletter
              newcommandmakefirstmandatorytheoptional[1]%
              expandafterinnermakefirstmandatorytheoptional
              expandaftercsnamestring#1endcsname#1%
              %
              newcommandinnermakefirstmandatorytheoptional[2]%
              def#2%
              ifxprotect@typeset@protect
              expandafter@firstoftwo
              else
              expandafter@secondoftwo
              fi
              kernel@ifnextchar[#1@dblarg#1%
              protect#2%
              %
              %
              newcommandmycommand[2][dummyoptional]%
              This is taken for the optional argument: #1.\
              This is taken for the mandatory argument: #2.
              %
              makefirstmandatorytheoptionalmycommand%
              makeatother

              parindent=0ex
              parskip=medskipamount

              begindocument

              No optional argument given---verb|mycommandA|:

              mycommandA

              Optional argument "B" given---verb|mycommand[B]A|:

              mycommand[B]A

              enddocument


              enter image description here






              share|improve this answer



























                2














                This is an attempt to add protection as with other macros that process optional arguments:



                %%errorcontextlines=1000
                documentclass[a4paper]article

                makeatletter
                newcommandmakefirstmandatorytheoptional[1]%
                expandafterinnermakefirstmandatorytheoptional
                expandaftercsnamestring#1endcsname#1%
                %
                newcommandinnermakefirstmandatorytheoptional[2]%
                def#2%
                ifxprotect@typeset@protect
                expandafter@firstoftwo
                else
                expandafter@secondoftwo
                fi
                kernel@ifnextchar[#1@dblarg#1%
                protect#2%
                %
                %
                newcommandmycommand[2][dummyoptional]%
                This is taken for the optional argument: #1.\
                This is taken for the mandatory argument: #2.
                %
                makefirstmandatorytheoptionalmycommand%
                makeatother

                parindent=0ex
                parskip=medskipamount

                begindocument

                No optional argument given---verb|mycommandA|:

                mycommandA

                Optional argument "B" given---verb|mycommand[B]A|:

                mycommand[B]A

                enddocument


                enter image description here






                share|improve this answer

























                  2












                  2








                  2







                  This is an attempt to add protection as with other macros that process optional arguments:



                  %%errorcontextlines=1000
                  documentclass[a4paper]article

                  makeatletter
                  newcommandmakefirstmandatorytheoptional[1]%
                  expandafterinnermakefirstmandatorytheoptional
                  expandaftercsnamestring#1endcsname#1%
                  %
                  newcommandinnermakefirstmandatorytheoptional[2]%
                  def#2%
                  ifxprotect@typeset@protect
                  expandafter@firstoftwo
                  else
                  expandafter@secondoftwo
                  fi
                  kernel@ifnextchar[#1@dblarg#1%
                  protect#2%
                  %
                  %
                  newcommandmycommand[2][dummyoptional]%
                  This is taken for the optional argument: #1.\
                  This is taken for the mandatory argument: #2.
                  %
                  makefirstmandatorytheoptionalmycommand%
                  makeatother

                  parindent=0ex
                  parskip=medskipamount

                  begindocument

                  No optional argument given---verb|mycommandA|:

                  mycommandA

                  Optional argument "B" given---verb|mycommand[B]A|:

                  mycommand[B]A

                  enddocument


                  enter image description here






                  share|improve this answer













                  This is an attempt to add protection as with other macros that process optional arguments:



                  %%errorcontextlines=1000
                  documentclass[a4paper]article

                  makeatletter
                  newcommandmakefirstmandatorytheoptional[1]%
                  expandafterinnermakefirstmandatorytheoptional
                  expandaftercsnamestring#1endcsname#1%
                  %
                  newcommandinnermakefirstmandatorytheoptional[2]%
                  def#2%
                  ifxprotect@typeset@protect
                  expandafter@firstoftwo
                  else
                  expandafter@secondoftwo
                  fi
                  kernel@ifnextchar[#1@dblarg#1%
                  protect#2%
                  %
                  %
                  newcommandmycommand[2][dummyoptional]%
                  This is taken for the optional argument: #1.\
                  This is taken for the mandatory argument: #2.
                  %
                  makefirstmandatorytheoptionalmycommand%
                  makeatother

                  parindent=0ex
                  parskip=medskipamount

                  begindocument

                  No optional argument given---verb|mycommandA|:

                  mycommandA

                  Optional argument "B" given---verb|mycommand[B]A|:

                  mycommand[B]A

                  enddocument


                  enter image description here







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 8 at 23:58









                  Ulrich DiezUlrich Diez

                  5,495620




                  5,495620



























                      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%2f478354%2fnewcommand-using-one-parameter-as-the-default-for-the-other%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 get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

                      Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                      List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229