How to add conditions in LATEX macros?












4















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

























  • @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
    Jan 1 at 10:13













  • 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
    Jan 1 at 10:25











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

    – alephzero
    Jan 1 at 10:29











  • @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
    Jan 1 at 10:29











  • @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
    Jan 1 at 10:32
















4















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

























  • @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
    Jan 1 at 10:13













  • 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
    Jan 1 at 10:25











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

    – alephzero
    Jan 1 at 10:29











  • @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
    Jan 1 at 10:29











  • @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
    Jan 1 at 10:32














4












4








4


1






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
















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















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 10:46









Christian Hupfer

148k14193390




148k14193390










asked Jan 1 at 10:07









LoïcLoïc

307




307













  • @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
    Jan 1 at 10:13













  • 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
    Jan 1 at 10:25











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

    – alephzero
    Jan 1 at 10:29











  • @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
    Jan 1 at 10:29











  • @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
    Jan 1 at 10:32



















  • @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
    Jan 1 at 10:13













  • 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
    Jan 1 at 10:25











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

    – alephzero
    Jan 1 at 10:29











  • @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
    Jan 1 at 10:29











  • @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
    Jan 1 at 10:32

















@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
Jan 1 at 10:13







@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
Jan 1 at 10:13















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
Jan 1 at 10:25





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
Jan 1 at 10:25













@Bernard the OP said this was just a specific example of a more general question about writing macros.

– alephzero
Jan 1 at 10:29





@Bernard the OP said this was just a specific example of a more general question about writing macros.

– alephzero
Jan 1 at 10:29













@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
Jan 1 at 10:29





@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
Jan 1 at 10:29













@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
Jan 1 at 10:32





@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
Jan 1 at 10:32










1 Answer
1






active

oldest

votes


















4














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





    Thank you, this is perfect. I just do not understand the use of kern-scriptspace.

    – Loïc
    Jan 1 at 10:53













  • @Loïc: scriptspace is the amount of space inserted after a superscript and kern<length> inserts horizontal space. Thus kern-scriptspace precisely cancels the space inserted between e.g. d^{n+1} and f.

    – Circumscribe
    Jan 1 at 19:34













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









4














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





    Thank you, this is perfect. I just do not understand the use of kern-scriptspace.

    – Loïc
    Jan 1 at 10:53













  • @Loïc: scriptspace is the amount of space inserted after a superscript and kern<length> inserts horizontal space. Thus kern-scriptspace precisely cancels the space inserted between e.g. d^{n+1} and f.

    – Circumscribe
    Jan 1 at 19:34


















4














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





    Thank you, this is perfect. I just do not understand the use of kern-scriptspace.

    – Loïc
    Jan 1 at 10:53













  • @Loïc: scriptspace is the amount of space inserted after a superscript and kern<length> inserts horizontal space. Thus kern-scriptspace precisely cancels the space inserted between e.g. d^{n+1} and f.

    – Circumscribe
    Jan 1 at 19:34
















4












4








4







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 Jan 1 at 10:37









egregegreg

711k8618913174




711k8618913174








  • 1





    Thank you, this is perfect. I just do not understand the use of kern-scriptspace.

    – Loïc
    Jan 1 at 10:53













  • @Loïc: scriptspace is the amount of space inserted after a superscript and kern<length> inserts horizontal space. Thus kern-scriptspace precisely cancels the space inserted between e.g. d^{n+1} and f.

    – Circumscribe
    Jan 1 at 19:34
















  • 1





    Thank you, this is perfect. I just do not understand the use of kern-scriptspace.

    – Loïc
    Jan 1 at 10:53













  • @Loïc: scriptspace is the amount of space inserted after a superscript and kern<length> inserts horizontal space. Thus kern-scriptspace precisely cancels the space inserted between e.g. d^{n+1} and f.

    – Circumscribe
    Jan 1 at 19:34










1




1





Thank you, this is perfect. I just do not understand the use of kern-scriptspace.

– Loïc
Jan 1 at 10:53







Thank you, this is perfect. I just do not understand the use of kern-scriptspace.

– Loïc
Jan 1 at 10:53















@Loïc: scriptspace is the amount of space inserted after a superscript and kern<length> inserts horizontal space. Thus kern-scriptspace precisely cancels the space inserted between e.g. d^{n+1} and f.

– Circumscribe
Jan 1 at 19:34







@Loïc: scriptspace is the amount of space inserted after a superscript and kern<length> inserts horizontal space. Thus kern-scriptspace precisely cancels the space inserted between e.g. d^{n+1} and f.

– Circumscribe
Jan 1 at 19:34




















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

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]