How to add conditions in LATEX macros?












2














I would like to create custom macros with conditions.
For example, I would like to add an "if-condition" to a macro that I created to write derivatives of any order in relation to a single variable.
Here is the current code of my macro:



newcommand{df}{mathrm{d}}
newcommand{drv}[3]{dfrac{df #2^#1}{df #3^#1}}


The first argument #1 is the order of derivation, the second #2 the function that we take the derivative and the third #3 the variable of derivation.
For example,



drv{n}{f}{x}


will give



enter image description here



I would like to change this code so that it don't display the derivation order #1 if it is equal to 1. But I don't know the syntax, this is why I need your help. I want to write something like



newcommand{drv}[3]{
if #1=1
dfrac{df #2}{df #3}
else
dfrac{df ^#1 #2}{df #3^#1}
}


Thank you for your help, have a good day.










share|improve this question









New contributor




Loïc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • @ChristianHupfer Thank you. I know but I would like to create my own macros, this is just a simple example to understand the use of conditions.
    – Loïc
    37 mins ago












  • There already exist esdiff and diffcoeff packages which already can do that (and more: they consider the case of partial derivatives of any order). Why reinvent the wheel?
    – Bernard
    25 mins ago










  • @Bernard the OP said this was just a specific example of a more general question about writing macros.
    – alephzero
    21 mins ago










  • @Bernard I think the question of the OP goes more in the direction of 'How to create macros with conditions?' The derivative is just an example, so using a package will not help him in this case
    – caverac
    21 mins ago










  • @Bernard thank you for introducing me to these packages. Indeed, I was mainly looking to know how to use "if-conditions", the derivation was only one example.
    – Loïc
    18 mins ago
















2














I would like to create custom macros with conditions.
For example, I would like to add an "if-condition" to a macro that I created to write derivatives of any order in relation to a single variable.
Here is the current code of my macro:



newcommand{df}{mathrm{d}}
newcommand{drv}[3]{dfrac{df #2^#1}{df #3^#1}}


The first argument #1 is the order of derivation, the second #2 the function that we take the derivative and the third #3 the variable of derivation.
For example,



drv{n}{f}{x}


will give



enter image description here



I would like to change this code so that it don't display the derivation order #1 if it is equal to 1. But I don't know the syntax, this is why I need your help. I want to write something like



newcommand{drv}[3]{
if #1=1
dfrac{df #2}{df #3}
else
dfrac{df ^#1 #2}{df #3^#1}
}


Thank you for your help, have a good day.










share|improve this question









New contributor




Loïc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • @ChristianHupfer Thank you. I know but I would like to create my own macros, this is just a simple example to understand the use of conditions.
    – Loïc
    37 mins ago












  • There already exist esdiff and diffcoeff packages which already can do that (and more: they consider the case of partial derivatives of any order). Why reinvent the wheel?
    – Bernard
    25 mins ago










  • @Bernard the OP said this was just a specific example of a more general question about writing macros.
    – alephzero
    21 mins ago










  • @Bernard I think the question of the OP goes more in the direction of 'How to create macros with conditions?' The derivative is just an example, so using a package will not help him in this case
    – caverac
    21 mins ago










  • @Bernard thank you for introducing me to these packages. Indeed, I was mainly looking to know how to use "if-conditions", the derivation was only one example.
    – Loïc
    18 mins ago














2












2








2







I would like to create custom macros with conditions.
For example, I would like to add an "if-condition" to a macro that I created to write derivatives of any order in relation to a single variable.
Here is the current code of my macro:



newcommand{df}{mathrm{d}}
newcommand{drv}[3]{dfrac{df #2^#1}{df #3^#1}}


The first argument #1 is the order of derivation, the second #2 the function that we take the derivative and the third #3 the variable of derivation.
For example,



drv{n}{f}{x}


will give



enter image description here



I would like to change this code so that it don't display the derivation order #1 if it is equal to 1. But I don't know the syntax, this is why I need your help. I want to write something like



newcommand{drv}[3]{
if #1=1
dfrac{df #2}{df #3}
else
dfrac{df ^#1 #2}{df #3^#1}
}


Thank you for your help, have a good day.










share|improve this question









New contributor




Loïc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I would like to create custom macros with conditions.
For example, I would like to add an "if-condition" to a macro that I created to write derivatives of any order in relation to a single variable.
Here is the current code of my macro:



newcommand{df}{mathrm{d}}
newcommand{drv}[3]{dfrac{df #2^#1}{df #3^#1}}


The first argument #1 is the order of derivation, the second #2 the function that we take the derivative and the third #3 the variable of derivation.
For example,



drv{n}{f}{x}


will give



enter image description here



I would like to change this code so that it don't display the derivation order #1 if it is equal to 1. But I don't know the syntax, this is why I need your help. I want to write something like



newcommand{drv}[3]{
if #1=1
dfrac{df #2}{df #3}
else
dfrac{df ^#1 #2}{df #3^#1}
}


Thank you for your help, have a good day.







macros conditionals condition






share|improve this question









New contributor




Loïc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Loïc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 4 mins ago









Christian Hupfer

148k14193387




148k14193387






New contributor




Loïc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 43 mins ago









Loïc

135




135




New contributor




Loïc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Loïc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Loïc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • @ChristianHupfer Thank you. I know but I would like to create my own macros, this is just a simple example to understand the use of conditions.
    – Loïc
    37 mins ago












  • There already exist esdiff and diffcoeff packages which already can do that (and more: they consider the case of partial derivatives of any order). Why reinvent the wheel?
    – Bernard
    25 mins ago










  • @Bernard the OP said this was just a specific example of a more general question about writing macros.
    – alephzero
    21 mins ago










  • @Bernard I think the question of the OP goes more in the direction of 'How to create macros with conditions?' The derivative is just an example, so using a package will not help him in this case
    – caverac
    21 mins ago










  • @Bernard thank you for introducing me to these packages. Indeed, I was mainly looking to know how to use "if-conditions", the derivation was only one example.
    – Loïc
    18 mins ago


















  • @ChristianHupfer Thank you. I know but I would like to create my own macros, this is just a simple example to understand the use of conditions.
    – Loïc
    37 mins ago












  • There already exist esdiff and diffcoeff packages which already can do that (and more: they consider the case of partial derivatives of any order). Why reinvent the wheel?
    – Bernard
    25 mins ago










  • @Bernard the OP said this was just a specific example of a more general question about writing macros.
    – alephzero
    21 mins ago










  • @Bernard I think the question of the OP goes more in the direction of 'How to create macros with conditions?' The derivative is just an example, so using a package will not help him in this case
    – caverac
    21 mins ago










  • @Bernard thank you for introducing me to these packages. Indeed, I was mainly looking to know how to use "if-conditions", the derivation was only one example.
    – Loïc
    18 mins ago
















@ChristianHupfer Thank you. I know but I would like to create my own macros, this is just a simple example to understand the use of conditions.
– Loïc
37 mins ago






@ChristianHupfer Thank you. I know but I would like to create my own macros, this is just a simple example to understand the use of conditions.
– Loïc
37 mins ago














There already exist esdiff and diffcoeff packages which already can do that (and more: they consider the case of partial derivatives of any order). Why reinvent the wheel?
– Bernard
25 mins ago




There already exist esdiff and diffcoeff packages which already can do that (and more: they consider the case of partial derivatives of any order). Why reinvent the wheel?
– Bernard
25 mins ago












@Bernard the OP said this was just a specific example of a more general question about writing macros.
– alephzero
21 mins ago




@Bernard the OP said this was just a specific example of a more general question about writing macros.
– alephzero
21 mins ago












@Bernard I think the question of the OP goes more in the direction of 'How to create macros with conditions?' The derivative is just an example, so using a package will not help him in this case
– caverac
21 mins ago




@Bernard I think the question of the OP goes more in the direction of 'How to create macros with conditions?' The derivative is just an example, so using a package will not help him in this case
– caverac
21 mins ago












@Bernard thank you for introducing me to these packages. Indeed, I was mainly looking to know how to use "if-conditions", the derivation was only one example.
– Loïc
18 mins ago




@Bernard thank you for introducing me to these packages. Indeed, I was mainly looking to know how to use "if-conditions", the derivation was only one example.
– Loïc
18 mins ago










1 Answer
1






active

oldest

votes


















1














It's easy with xparse:



documentclass{article}
usepackage{amsmath}
usepackage{xparse}

newcommand{df}{mathop{}!d} % use mathrm{d} if you really prefer it

NewDocumentCommand{drv}{omm}{%
frac{dfIfValueT{#1}{^{#1}kern-scriptspace}#2}
{df#3IfValueT{#1}{^{#1}}}%
}

begin{document}

[
drv{f}{x}=f'(x),qquad
drv[n+1]{f}{x}=drv{}{x}drv[n]{f}{x}
]

end{document}


enter image description here



With o an optional argument is denoted; if it is present, IfValueT{#1} will return true and use the argument as shown.



With the given definition, df will behave as expected also in integrals, leaving a thin space before it when necessary.






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
    });


    }
    });






    Loïc is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f468107%2fhow-to-add-conditions-in-latex-macros%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    It's easy with xparse:



    documentclass{article}
    usepackage{amsmath}
    usepackage{xparse}

    newcommand{df}{mathop{}!d} % use mathrm{d} if you really prefer it

    NewDocumentCommand{drv}{omm}{%
    frac{dfIfValueT{#1}{^{#1}kern-scriptspace}#2}
    {df#3IfValueT{#1}{^{#1}}}%
    }

    begin{document}

    [
    drv{f}{x}=f'(x),qquad
    drv[n+1]{f}{x}=drv{}{x}drv[n]{f}{x}
    ]

    end{document}


    enter image description here



    With o an optional argument is denoted; if it is present, IfValueT{#1} will return true and use the argument as shown.



    With the given definition, df will behave as expected also in integrals, leaving a thin space before it when necessary.






    share|improve this answer


























      1














      It's easy with xparse:



      documentclass{article}
      usepackage{amsmath}
      usepackage{xparse}

      newcommand{df}{mathop{}!d} % use mathrm{d} if you really prefer it

      NewDocumentCommand{drv}{omm}{%
      frac{dfIfValueT{#1}{^{#1}kern-scriptspace}#2}
      {df#3IfValueT{#1}{^{#1}}}%
      }

      begin{document}

      [
      drv{f}{x}=f'(x),qquad
      drv[n+1]{f}{x}=drv{}{x}drv[n]{f}{x}
      ]

      end{document}


      enter image description here



      With o an optional argument is denoted; if it is present, IfValueT{#1} will return true and use the argument as shown.



      With the given definition, df will behave as expected also in integrals, leaving a thin space before it when necessary.






      share|improve this answer
























        1












        1








        1






        It's easy with xparse:



        documentclass{article}
        usepackage{amsmath}
        usepackage{xparse}

        newcommand{df}{mathop{}!d} % use mathrm{d} if you really prefer it

        NewDocumentCommand{drv}{omm}{%
        frac{dfIfValueT{#1}{^{#1}kern-scriptspace}#2}
        {df#3IfValueT{#1}{^{#1}}}%
        }

        begin{document}

        [
        drv{f}{x}=f'(x),qquad
        drv[n+1]{f}{x}=drv{}{x}drv[n]{f}{x}
        ]

        end{document}


        enter image description here



        With o an optional argument is denoted; if it is present, IfValueT{#1} will return true and use the argument as shown.



        With the given definition, df will behave as expected also in integrals, leaving a thin space before it when necessary.






        share|improve this answer












        It's easy with xparse:



        documentclass{article}
        usepackage{amsmath}
        usepackage{xparse}

        newcommand{df}{mathop{}!d} % use mathrm{d} if you really prefer it

        NewDocumentCommand{drv}{omm}{%
        frac{dfIfValueT{#1}{^{#1}kern-scriptspace}#2}
        {df#3IfValueT{#1}{^{#1}}}%
        }

        begin{document}

        [
        drv{f}{x}=f'(x),qquad
        drv[n+1]{f}{x}=drv{}{x}drv[n]{f}{x}
        ]

        end{document}


        enter image description here



        With o an optional argument is denoted; if it is present, IfValueT{#1} will return true and use the argument as shown.



        With the given definition, df will behave as expected also in integrals, leaving a thin space before it when necessary.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 13 mins ago









        egreg

        709k8618823165




        709k8618823165






















            Loïc is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Loïc is a new contributor. Be nice, and check out our Code of Conduct.













            Loïc is a new contributor. Be nice, and check out our Code of Conduct.












            Loïc is a new contributor. Be nice, and check out our Code of Conduct.
















            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f468107%2fhow-to-add-conditions-in-latex-macros%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

            Eastern Orthodox Church

            Zagreb

            Understanding the information contained in the Deep Space Network XML data?