newcommand: Using one parameter as the default for the otherDefining a command for a minted...

Why do phishing e-mails use faked e-mail addresses instead of the real one?

What is Tony Stark injecting into himself in Iron Man 3?

Is there a logarithm base for which the logarithm becomes an identity function?

Are small insurances worth it?

Trocar background-image com delay via jQuery

Computation logic of Partway in TikZ

How can I portion out frozen cookie dough?

Should we avoid writing fiction about historical events without extensive research?

Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?

How do I increase the number of TTY consoles?

Why restrict private health insurance?

What is better: yes / no radio, or simple checkbox?

How exactly does an Ethernet collision happen in the cable, since nodes use different circuits for Tx and Rx?

I am the person who abides by rules, but breaks the rules. Who am I?

What would be the most expensive material to an intergalactic society?

Why is there an extra space when I type "ls" on the Desktop?

Would those living in a "perfect society" not understand satire

Cycles on the torus

Under what conditions can the right to remain silent be revoked in the USA?

Are all players supposed to be able to see each others' character sheets?

Traveling to heavily polluted city, what practical measures can I take to minimize impact?

Converting from "matrix" data into "coordinate" data

Use Mercury as quenching liquid for swords?

How to educate team mate to take screenshots for bugs with out unwanted stuff



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 newmacro{a}{b}[c]{d}[e][f] and newmacro{a}{b}[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













7















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,



newcommand{mycommand}[2][#1]{ ... }


does not work, neither does



newcommand{mycommand}[2][#2]{ ... }


Does anybody know how to do this?










share|improve this question

























  • newcommand{mycommand}[2][default]{...}?

    – marmot
    yesterday











  • 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
    yesterday


















7















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,



newcommand{mycommand}[2][#1]{ ... }


does not work, neither does



newcommand{mycommand}[2][#2]{ ... }


Does anybody know how to do this?










share|improve this question

























  • newcommand{mycommand}[2][default]{...}?

    – marmot
    yesterday











  • 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
    yesterday
















7












7








7


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,



newcommand{mycommand}[2][#1]{ ... }


does not work, neither does



newcommand{mycommand}[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,



newcommand{mycommand}[2][#1]{ ... }


does not work, neither does



newcommand{mycommand}[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 yesterday







Bodo Manthey

















asked yesterday









Bodo MantheyBodo Manthey

1588




1588













  • newcommand{mycommand}[2][default]{...}?

    – marmot
    yesterday











  • 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
    yesterday





















  • newcommand{mycommand}[2][default]{...}?

    – marmot
    yesterday











  • 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
    yesterday



















newcommand{mycommand}[2][default]{...}?

– marmot
yesterday





newcommand{mycommand}[2][default]{...}?

– marmot
yesterday













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
yesterday







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
yesterday












3 Answers
3






active

oldest

votes


















8














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



documentclass{article}

usepackage{xparse}

newcommand{printthis}[2]{%
Optional: #1; Mandatory: #2%
}

NewDocumentCommand{mycommand}{o m}{%
IfValueTF{#1}
{printthis{#1}{#2}}% mycommand[..]{...}
{printthis{#2}{#2}}% mycommand{...}
}

begin{document}

mycommand{first}

mycommand[first]{second}

end{document}




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:



documentclass{article}

newcommand{printthis}[2][]{%
Optional: #1; Mandatory: #2%
}

makeatletter
newcommand{mycommand}{%
@dblargprintthis
}
makeatother

begin{document}

mycommand{first}

mycommand[first]{second}

end{document}





share|improve this answer
























  • Great, thank you very much!

    – Bodo Manthey
    yesterday



















8














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



documentclass{article}
usepackage{xparse}

NewDocumentCommand{foo}{O{#2}m}{%
Optional=``#1'', mandatory=``#2''par
}
NewDocumentCommand{oof}{mO{#1}}{%
Mandatory=``#1'', optional=``#2''par
}

begin{document}

foo{x}

foo[y]{x}

oof{x}

oof{x}[y]

end{document}


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































    0














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



    %%errorcontextlines=1000
    documentclass[a4paper]{article}

    makeatletter
    newcommandmakefirstmandatorytheoptional[1]{%
    expandafterinnermakefirstmandatorytheoptional
    expandafter{csnamestring#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.
    }%
    makefirstmandatorytheoptional{mycommand}%
    makeatother

    parindent=0ex
    parskip=medskipamount

    begin{document}

    No optional argument given---verb|mycommand{A}|:

    mycommand{A}

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

    mycommand[B]{A}

    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%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









      8














      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



      documentclass{article}

      usepackage{xparse}

      newcommand{printthis}[2]{%
      Optional: #1; Mandatory: #2%
      }

      NewDocumentCommand{mycommand}{o m}{%
      IfValueTF{#1}
      {printthis{#1}{#2}}% mycommand[..]{...}
      {printthis{#2}{#2}}% mycommand{...}
      }

      begin{document}

      mycommand{first}

      mycommand[first]{second}

      end{document}




      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:



      documentclass{article}

      newcommand{printthis}[2][]{%
      Optional: #1; Mandatory: #2%
      }

      makeatletter
      newcommand{mycommand}{%
      @dblargprintthis
      }
      makeatother

      begin{document}

      mycommand{first}

      mycommand[first]{second}

      end{document}





      share|improve this answer
























      • Great, thank you very much!

        – Bodo Manthey
        yesterday
















      8














      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



      documentclass{article}

      usepackage{xparse}

      newcommand{printthis}[2]{%
      Optional: #1; Mandatory: #2%
      }

      NewDocumentCommand{mycommand}{o m}{%
      IfValueTF{#1}
      {printthis{#1}{#2}}% mycommand[..]{...}
      {printthis{#2}{#2}}% mycommand{...}
      }

      begin{document}

      mycommand{first}

      mycommand[first]{second}

      end{document}




      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:



      documentclass{article}

      newcommand{printthis}[2][]{%
      Optional: #1; Mandatory: #2%
      }

      makeatletter
      newcommand{mycommand}{%
      @dblargprintthis
      }
      makeatother

      begin{document}

      mycommand{first}

      mycommand[first]{second}

      end{document}





      share|improve this answer
























      • Great, thank you very much!

        – Bodo Manthey
        yesterday














      8












      8








      8







      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



      documentclass{article}

      usepackage{xparse}

      newcommand{printthis}[2]{%
      Optional: #1; Mandatory: #2%
      }

      NewDocumentCommand{mycommand}{o m}{%
      IfValueTF{#1}
      {printthis{#1}{#2}}% mycommand[..]{...}
      {printthis{#2}{#2}}% mycommand{...}
      }

      begin{document}

      mycommand{first}

      mycommand[first]{second}

      end{document}




      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:



      documentclass{article}

      newcommand{printthis}[2][]{%
      Optional: #1; Mandatory: #2%
      }

      makeatletter
      newcommand{mycommand}{%
      @dblargprintthis
      }
      makeatother

      begin{document}

      mycommand{first}

      mycommand[first]{second}

      end{document}





      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



      documentclass{article}

      usepackage{xparse}

      newcommand{printthis}[2]{%
      Optional: #1; Mandatory: #2%
      }

      NewDocumentCommand{mycommand}{o m}{%
      IfValueTF{#1}
      {printthis{#1}{#2}}% mycommand[..]{...}
      {printthis{#2}{#2}}% mycommand{...}
      }

      begin{document}

      mycommand{first}

      mycommand[first]{second}

      end{document}




      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:



      documentclass{article}

      newcommand{printthis}[2][]{%
      Optional: #1; Mandatory: #2%
      }

      makeatletter
      newcommand{mycommand}{%
      @dblargprintthis
      }
      makeatother

      begin{document}

      mycommand{first}

      mycommand[first]{second}

      end{document}






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered yesterday









      WernerWerner

      447k699891693




      447k699891693













      • Great, thank you very much!

        – Bodo Manthey
        yesterday



















      • Great, thank you very much!

        – Bodo Manthey
        yesterday

















      Great, thank you very much!

      – Bodo Manthey
      yesterday





      Great, thank you very much!

      – Bodo Manthey
      yesterday











      8














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



      documentclass{article}
      usepackage{xparse}

      NewDocumentCommand{foo}{O{#2}m}{%
      Optional=``#1'', mandatory=``#2''par
      }
      NewDocumentCommand{oof}{mO{#1}}{%
      Mandatory=``#1'', optional=``#2''par
      }

      begin{document}

      foo{x}

      foo[y]{x}

      oof{x}

      oof{x}[y]

      end{document}


      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




























        8














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



        documentclass{article}
        usepackage{xparse}

        NewDocumentCommand{foo}{O{#2}m}{%
        Optional=``#1'', mandatory=``#2''par
        }
        NewDocumentCommand{oof}{mO{#1}}{%
        Mandatory=``#1'', optional=``#2''par
        }

        begin{document}

        foo{x}

        foo[y]{x}

        oof{x}

        oof{x}[y]

        end{document}


        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


























          8












          8








          8







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



          documentclass{article}
          usepackage{xparse}

          NewDocumentCommand{foo}{O{#2}m}{%
          Optional=``#1'', mandatory=``#2''par
          }
          NewDocumentCommand{oof}{mO{#1}}{%
          Mandatory=``#1'', optional=``#2''par
          }

          begin{document}

          foo{x}

          foo[y]{x}

          oof{x}

          oof{x}[y]

          end{document}


          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:



          documentclass{article}
          usepackage{xparse}

          NewDocumentCommand{foo}{O{#2}m}{%
          Optional=``#1'', mandatory=``#2''par
          }
          NewDocumentCommand{oof}{mO{#1}}{%
          Mandatory=``#1'', optional=``#2''par
          }

          begin{document}

          foo{x}

          foo[y]{x}

          oof{x}

          oof{x}[y]

          end{document}


          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 yesterday









          egregegreg

          725k8819193224




          725k8819193224























              0














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



              %%errorcontextlines=1000
              documentclass[a4paper]{article}

              makeatletter
              newcommandmakefirstmandatorytheoptional[1]{%
              expandafterinnermakefirstmandatorytheoptional
              expandafter{csnamestring#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.
              }%
              makefirstmandatorytheoptional{mycommand}%
              makeatother

              parindent=0ex
              parskip=medskipamount

              begin{document}

              No optional argument given---verb|mycommand{A}|:

              mycommand{A}

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

              mycommand[B]{A}

              end{document}


              enter image description here






              share|improve this answer




























                0














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



                %%errorcontextlines=1000
                documentclass[a4paper]{article}

                makeatletter
                newcommandmakefirstmandatorytheoptional[1]{%
                expandafterinnermakefirstmandatorytheoptional
                expandafter{csnamestring#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.
                }%
                makefirstmandatorytheoptional{mycommand}%
                makeatother

                parindent=0ex
                parskip=medskipamount

                begin{document}

                No optional argument given---verb|mycommand{A}|:

                mycommand{A}

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

                mycommand[B]{A}

                end{document}


                enter image description here






                share|improve this answer


























                  0












                  0








                  0







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



                  %%errorcontextlines=1000
                  documentclass[a4paper]{article}

                  makeatletter
                  newcommandmakefirstmandatorytheoptional[1]{%
                  expandafterinnermakefirstmandatorytheoptional
                  expandafter{csnamestring#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.
                  }%
                  makefirstmandatorytheoptional{mycommand}%
                  makeatother

                  parindent=0ex
                  parskip=medskipamount

                  begin{document}

                  No optional argument given---verb|mycommand{A}|:

                  mycommand{A}

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

                  mycommand[B]{A}

                  end{document}


                  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
                  expandafter{csnamestring#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.
                  }%
                  makefirstmandatorytheoptional{mycommand}%
                  makeatother

                  parindent=0ex
                  parskip=medskipamount

                  begin{document}

                  No optional argument given---verb|mycommand{A}|:

                  mycommand{A}

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

                  mycommand[B]{A}

                  end{document}


                  enter image description here







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered yesterday









                  Ulrich DiezUlrich Diez

                  5,275619




                  5,275619






























                      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

                      Magento 2 - Add success message with knockout Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Success / Error message on ajax request$.widget is not a function when loading a homepage after add custom jQuery on custom themeHow can bind jQuery to current document in Magento 2 When template load by ajaxRedirect page using plugin in Magento 2Magento 2 - Update quantity and totals of cart page without page reload?Magento 2: Quote data not loaded on knockout checkoutMagento 2 : I need to change add to cart success message after adding product into cart through pluginMagento 2.2.5 How to add additional products to cart from new checkout step?Magento 2 Add error/success message with knockoutCan't validate Post Code on checkout page

                      Fil:Tokke komm.svg

                      Where did Arya get these scars? Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Favourite questions and answers from the 1st quarter of 2019Why did Arya refuse to end it?Has the pronunciation of Arya Stark's name changed?Has Arya forgiven people?Why did Arya Stark lose her vision?Why can Arya still use the faces?Has the Narrow Sea become narrower?Does Arya Stark know how to make poisons outside of the House of Black and White?Why did Nymeria leave Arya?Why did Arya not kill the Lannister soldiers she encountered in the Riverlands?What is the current canonical age of Sansa, Bran and Arya Stark?