newcommand: Using one parameter as the default for the otherDefining a command for a minted environmentOptional argument for newcommand?Switch on number of arguments given to a macroEnvironment with coordinate-like argumentsDefining a macro with three optional arguments in the form newmacroab[c]d[e][f] and newmacroab[c]d*[f]Set the default value of argument as parameter in newcommandUsing the compulsory argument for the optional argument?How to pass optional arguments to command?Second optional argument for newcommand to be the same of the first in case it is not specifiedLuaTeX: Call a Lua function with two optional arguments
Java - What do constructor type arguments mean when placed *before* the type?
Lifted its hind leg on or lifted its hind leg towards?
What do you call the infoboxes with text and sometimes images on the side of a page we find in textbooks?
Could solar power be utilized and substitute coal in the 19th century?
What is Sitecore Managed Cloud?
Teaching indefinite integrals that require special-casing
What will be the benefits of Brexit?
Can I create an upright 7-foot × 5-foot wall with the Minor Illusion spell?
Is it legal to discriminate due to the medicine used to treat a medical condition?
word describing multiple paths to the same abstract outcome
How can I raise concerns with a new DM about XP splitting?
How to interpret the phrase "t’en a fait voir à toi"?
Books on the History of math research at European universities
How will losing mobility of one hand affect my career as a programmer?
How to check participants in at events?
The One-Electron Universe postulate is true - what simple change can I make to change the whole universe?
Is there an Impartial Brexit Deal comparison site?
I2C signal and power over long range (10meter cable)
Visiting the UK as unmarried couple
How do ultrasonic sensors differentiate between transmitted and received signals?
Hostile work environment after whistle-blowing on coworker and our boss. What do I do?
Installing PowerShell on 32-bit Kali OS fails
Are taller landing gear bad for aircraft, particulary large airliners?
What is the term when two people sing in harmony, but they aren't singing the same notes?
newcommand: Using one parameter as the default for the other
Defining a command for a minted environmentOptional argument for newcommand?Switch on number of arguments given to a macroEnvironment with coordinate-like argumentsDefining a macro with three optional arguments in the form newmacroab[c]d[e][f] and newmacroab[c]d*[f]Set the default value of argument as parameter in newcommandUsing the compulsory argument for the optional argument?How to pass optional arguments to command?Second optional argument for newcommand to be the same of the first in case it is not specifiedLuaTeX: Call a Lua function with two optional arguments
I would like to define a macro that takes two arguments, one of which is optional. If it is not given, then it should take the other one as default value.
Unfortunately,
newcommandmycommand[2][#1] ...
does not work, neither does
newcommandmycommand[2][#2] ...
Does anybody know how to do this?
macros optional-arguments
add a comment |
I would like to define a macro that takes two arguments, one of which is optional. If it is not given, then it should take the other one as default value.
Unfortunately,
newcommandmycommand[2][#1] ...
does not work, neither does
newcommandmycommand[2][#2] ...
Does anybody know how to do this?
macros optional-arguments
newcommandmycommand[2][default]...?
– marmot
Mar 8 at 6:40
The problem is that putting #1 or #2 as default value does not work. Using an arbitrary fixed default value works just fine, but is not what I want.
– Bodo Manthey
Mar 8 at 6:41
add a comment |
I would like to define a macro that takes two arguments, one of which is optional. If it is not given, then it should take the other one as default value.
Unfortunately,
newcommandmycommand[2][#1] ...
does not work, neither does
newcommandmycommand[2][#2] ...
Does anybody know how to do this?
macros optional-arguments
I would like to define a macro that takes two arguments, one of which is optional. If it is not given, then it should take the other one as default value.
Unfortunately,
newcommandmycommand[2][#1] ...
does not work, neither does
newcommandmycommand[2][#2] ...
Does anybody know how to do this?
macros optional-arguments
macros optional-arguments
edited Mar 8 at 13:45
Bodo Manthey
asked Mar 8 at 6:38
Bodo MantheyBodo Manthey
1689
1689
newcommandmycommand[2][default]...?
– marmot
Mar 8 at 6:40
The problem is that putting #1 or #2 as default value does not work. Using an arbitrary fixed default value works just fine, but is not what I want.
– Bodo Manthey
Mar 8 at 6:41
add a comment |
newcommandmycommand[2][default]...?
– marmot
Mar 8 at 6:40
The problem is that putting #1 or #2 as default value does not work. Using an arbitrary fixed default value works just fine, but is not what I want.
– Bodo Manthey
Mar 8 at 6:41
newcommandmycommand[2][default]...?– marmot
Mar 8 at 6:40
newcommandmycommand[2][default]...?– marmot
Mar 8 at 6:40
The problem is that putting #1 or #2 as default value does not work. Using an arbitrary fixed default value works just fine, but is not what I want.
– Bodo Manthey
Mar 8 at 6:41
The problem is that putting #1 or #2 as default value does not work. Using an arbitrary fixed default value works just fine, but is not what I want.
– Bodo Manthey
Mar 8 at 6:41
add a comment |
3 Answers
3
active
oldest
votes
Good news: you can do it very easily with xparse:
documentclassarticle
usepackagexparse
NewDocumentCommandfooO#2m%
Optional=``#1'', mandatory=``#2''par
NewDocumentCommandoofmO#1%
Mandatory=``#1'', optional=``#2''par
begindocument
foox
foo[y]x
oofx
oofx[y]
enddocument
The argument specifier O... takes as argument what to substitute as default when the argument itself doesn't appear at call time. This can well be a parameter token referring to another argument.

add a comment |
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:

documentclassarticle
usepackagexparse
newcommandprintthis[2]%
Optional: #1; Mandatory: #2%
NewDocumentCommandmycommando m%
IfValueTF#1
printthis#1#2% mycommand[..]...
printthis#2#2% mycommand...
begindocument
mycommandfirst
mycommand[first]second
enddocument
A slightly different version of this stems from the use of caption, where you can supply an optional argument for the LoT/LoF, but if you don't, the mandatory arguments is sent instead (similarly for sectional units with optional arguments destined for the ToC). This uses the kernel's @dblarg:
documentclassarticle
newcommandprintthis[2][]%
Optional: #1; Mandatory: #2%
makeatletter
newcommandmycommand%
@dblargprintthis
makeatother
begindocument
mycommandfirst
mycommand[first]second
enddocument
Great, thank you very much!
– Bodo Manthey
Mar 8 at 6:53
add a comment |
This is an attempt to add protection as with other macros that process optional arguments:
%%errorcontextlines=1000
documentclass[a4paper]article
makeatletter
newcommandmakefirstmandatorytheoptional[1]%
expandafterinnermakefirstmandatorytheoptional
expandaftercsnamestring#1endcsname#1%
%
newcommandinnermakefirstmandatorytheoptional[2]%
def#2%
ifxprotect@typeset@protect
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
kernel@ifnextchar[#1@dblarg#1%
protect#2%
%
%
newcommandmycommand[2][dummyoptional]%
This is taken for the optional argument: #1.\
This is taken for the mandatory argument: #2.
%
makefirstmandatorytheoptionalmycommand%
makeatother
parindent=0ex
parskip=medskipamount
begindocument
No optional argument given---verb|mycommandA|:
mycommandA
Optional argument "B" given---verb|mycommand[B]A|:
mycommand[B]A
enddocument

add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Good news: you can do it very easily with xparse:
documentclassarticle
usepackagexparse
NewDocumentCommandfooO#2m%
Optional=``#1'', mandatory=``#2''par
NewDocumentCommandoofmO#1%
Mandatory=``#1'', optional=``#2''par
begindocument
foox
foo[y]x
oofx
oofx[y]
enddocument
The argument specifier O... takes as argument what to substitute as default when the argument itself doesn't appear at call time. This can well be a parameter token referring to another argument.

add a comment |
Good news: you can do it very easily with xparse:
documentclassarticle
usepackagexparse
NewDocumentCommandfooO#2m%
Optional=``#1'', mandatory=``#2''par
NewDocumentCommandoofmO#1%
Mandatory=``#1'', optional=``#2''par
begindocument
foox
foo[y]x
oofx
oofx[y]
enddocument
The argument specifier O... takes as argument what to substitute as default when the argument itself doesn't appear at call time. This can well be a parameter token referring to another argument.

add a comment |
Good news: you can do it very easily with xparse:
documentclassarticle
usepackagexparse
NewDocumentCommandfooO#2m%
Optional=``#1'', mandatory=``#2''par
NewDocumentCommandoofmO#1%
Mandatory=``#1'', optional=``#2''par
begindocument
foox
foo[y]x
oofx
oofx[y]
enddocument
The argument specifier O... takes as argument what to substitute as default when the argument itself doesn't appear at call time. This can well be a parameter token referring to another argument.

Good news: you can do it very easily with xparse:
documentclassarticle
usepackagexparse
NewDocumentCommandfooO#2m%
Optional=``#1'', mandatory=``#2''par
NewDocumentCommandoofmO#1%
Mandatory=``#1'', optional=``#2''par
begindocument
foox
foo[y]x
oofx
oofx[y]
enddocument
The argument specifier O... takes as argument what to substitute as default when the argument itself doesn't appear at call time. This can well be a parameter token referring to another argument.

answered Mar 8 at 7:24
egregegreg
729k8819263235
729k8819263235
add a comment |
add a comment |
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:

documentclassarticle
usepackagexparse
newcommandprintthis[2]%
Optional: #1; Mandatory: #2%
NewDocumentCommandmycommando m%
IfValueTF#1
printthis#1#2% mycommand[..]...
printthis#2#2% mycommand...
begindocument
mycommandfirst
mycommand[first]second
enddocument
A slightly different version of this stems from the use of caption, where you can supply an optional argument for the LoT/LoF, but if you don't, the mandatory arguments is sent instead (similarly for sectional units with optional arguments destined for the ToC). This uses the kernel's @dblarg:
documentclassarticle
newcommandprintthis[2][]%
Optional: #1; Mandatory: #2%
makeatletter
newcommandmycommand%
@dblargprintthis
makeatother
begindocument
mycommandfirst
mycommand[first]second
enddocument
Great, thank you very much!
– Bodo Manthey
Mar 8 at 6:53
add a comment |
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:

documentclassarticle
usepackagexparse
newcommandprintthis[2]%
Optional: #1; Mandatory: #2%
NewDocumentCommandmycommando m%
IfValueTF#1
printthis#1#2% mycommand[..]...
printthis#2#2% mycommand...
begindocument
mycommandfirst
mycommand[first]second
enddocument
A slightly different version of this stems from the use of caption, where you can supply an optional argument for the LoT/LoF, but if you don't, the mandatory arguments is sent instead (similarly for sectional units with optional arguments destined for the ToC). This uses the kernel's @dblarg:
documentclassarticle
newcommandprintthis[2][]%
Optional: #1; Mandatory: #2%
makeatletter
newcommandmycommand%
@dblargprintthis
makeatother
begindocument
mycommandfirst
mycommand[first]second
enddocument
Great, thank you very much!
– Bodo Manthey
Mar 8 at 6:53
add a comment |
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:

documentclassarticle
usepackagexparse
newcommandprintthis[2]%
Optional: #1; Mandatory: #2%
NewDocumentCommandmycommando m%
IfValueTF#1
printthis#1#2% mycommand[..]...
printthis#2#2% mycommand...
begindocument
mycommandfirst
mycommand[first]second
enddocument
A slightly different version of this stems from the use of caption, where you can supply an optional argument for the LoT/LoF, but if you don't, the mandatory arguments is sent instead (similarly for sectional units with optional arguments destined for the ToC). This uses the kernel's @dblarg:
documentclassarticle
newcommandprintthis[2][]%
Optional: #1; Mandatory: #2%
makeatletter
newcommandmycommand%
@dblargprintthis
makeatother
begindocument
mycommandfirst
mycommand[first]second
enddocument
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:

documentclassarticle
usepackagexparse
newcommandprintthis[2]%
Optional: #1; Mandatory: #2%
NewDocumentCommandmycommando m%
IfValueTF#1
printthis#1#2% mycommand[..]...
printthis#2#2% mycommand...
begindocument
mycommandfirst
mycommand[first]second
enddocument
A slightly different version of this stems from the use of caption, where you can supply an optional argument for the LoT/LoF, but if you don't, the mandatory arguments is sent instead (similarly for sectional units with optional arguments destined for the ToC). This uses the kernel's @dblarg:
documentclassarticle
newcommandprintthis[2][]%
Optional: #1; Mandatory: #2%
makeatletter
newcommandmycommand%
@dblargprintthis
makeatother
begindocument
mycommandfirst
mycommand[first]second
enddocument
answered Mar 8 at 6:47
WernerWerner
448k719931699
448k719931699
Great, thank you very much!
– Bodo Manthey
Mar 8 at 6:53
add a comment |
Great, thank you very much!
– Bodo Manthey
Mar 8 at 6:53
Great, thank you very much!
– Bodo Manthey
Mar 8 at 6:53
Great, thank you very much!
– Bodo Manthey
Mar 8 at 6:53
add a comment |
This is an attempt to add protection as with other macros that process optional arguments:
%%errorcontextlines=1000
documentclass[a4paper]article
makeatletter
newcommandmakefirstmandatorytheoptional[1]%
expandafterinnermakefirstmandatorytheoptional
expandaftercsnamestring#1endcsname#1%
%
newcommandinnermakefirstmandatorytheoptional[2]%
def#2%
ifxprotect@typeset@protect
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
kernel@ifnextchar[#1@dblarg#1%
protect#2%
%
%
newcommandmycommand[2][dummyoptional]%
This is taken for the optional argument: #1.\
This is taken for the mandatory argument: #2.
%
makefirstmandatorytheoptionalmycommand%
makeatother
parindent=0ex
parskip=medskipamount
begindocument
No optional argument given---verb|mycommandA|:
mycommandA
Optional argument "B" given---verb|mycommand[B]A|:
mycommand[B]A
enddocument

add a comment |
This is an attempt to add protection as with other macros that process optional arguments:
%%errorcontextlines=1000
documentclass[a4paper]article
makeatletter
newcommandmakefirstmandatorytheoptional[1]%
expandafterinnermakefirstmandatorytheoptional
expandaftercsnamestring#1endcsname#1%
%
newcommandinnermakefirstmandatorytheoptional[2]%
def#2%
ifxprotect@typeset@protect
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
kernel@ifnextchar[#1@dblarg#1%
protect#2%
%
%
newcommandmycommand[2][dummyoptional]%
This is taken for the optional argument: #1.\
This is taken for the mandatory argument: #2.
%
makefirstmandatorytheoptionalmycommand%
makeatother
parindent=0ex
parskip=medskipamount
begindocument
No optional argument given---verb|mycommandA|:
mycommandA
Optional argument "B" given---verb|mycommand[B]A|:
mycommand[B]A
enddocument

add a comment |
This is an attempt to add protection as with other macros that process optional arguments:
%%errorcontextlines=1000
documentclass[a4paper]article
makeatletter
newcommandmakefirstmandatorytheoptional[1]%
expandafterinnermakefirstmandatorytheoptional
expandaftercsnamestring#1endcsname#1%
%
newcommandinnermakefirstmandatorytheoptional[2]%
def#2%
ifxprotect@typeset@protect
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
kernel@ifnextchar[#1@dblarg#1%
protect#2%
%
%
newcommandmycommand[2][dummyoptional]%
This is taken for the optional argument: #1.\
This is taken for the mandatory argument: #2.
%
makefirstmandatorytheoptionalmycommand%
makeatother
parindent=0ex
parskip=medskipamount
begindocument
No optional argument given---verb|mycommandA|:
mycommandA
Optional argument "B" given---verb|mycommand[B]A|:
mycommand[B]A
enddocument

This is an attempt to add protection as with other macros that process optional arguments:
%%errorcontextlines=1000
documentclass[a4paper]article
makeatletter
newcommandmakefirstmandatorytheoptional[1]%
expandafterinnermakefirstmandatorytheoptional
expandaftercsnamestring#1endcsname#1%
%
newcommandinnermakefirstmandatorytheoptional[2]%
def#2%
ifxprotect@typeset@protect
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
kernel@ifnextchar[#1@dblarg#1%
protect#2%
%
%
newcommandmycommand[2][dummyoptional]%
This is taken for the optional argument: #1.\
This is taken for the mandatory argument: #2.
%
makefirstmandatorytheoptionalmycommand%
makeatother
parindent=0ex
parskip=medskipamount
begindocument
No optional argument given---verb|mycommandA|:
mycommandA
Optional argument "B" given---verb|mycommand[B]A|:
mycommand[B]A
enddocument

answered Mar 8 at 23:58
Ulrich DiezUlrich Diez
5,495620
5,495620
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
newcommandmycommand[2][default]...?– marmot
Mar 8 at 6:40
The problem is that putting #1 or #2 as default value does not work. Using an arbitrary fixed default value works just fine, but is not what I want.
– Bodo Manthey
Mar 8 at 6:41