newcommand: Using one parameter as the default for the other












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
    23 hours ago











  • 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
    23 hours ago


















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
    23 hours ago











  • 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
    23 hours ago
















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 16 hours ago







Bodo Manthey

















asked 23 hours ago









Bodo MantheyBodo Manthey

1588




1588













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

    – marmot
    23 hours ago











  • 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
    23 hours ago





















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

    – marmot
    23 hours ago











  • 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
    23 hours ago



















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

– marmot
23 hours ago





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

– marmot
23 hours ago













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
23 hours ago







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
23 hours ago












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
    23 hours ago



















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
        23 hours ago
















      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
        23 hours ago














      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 23 hours ago









      WernerWerner

      446k699871692




      446k699871692













      • Great, thank you very much!

        – Bodo Manthey
        23 hours ago



















      • Great, thank you very much!

        – Bodo Manthey
        23 hours ago

















      Great, thank you very much!

      – Bodo Manthey
      23 hours ago





      Great, thank you very much!

      – Bodo Manthey
      23 hours ago











      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 22 hours ago









          egregegreg

          725k8819183224




          725k8819183224























              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 5 hours ago









                  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

                      How to label and detect the document text images

                      Vallis Paradisi

                      Tabula Rosettana