newcommand: Using one parameter as the default for the other
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
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,
newcommand{mycommand}[2][#1]{ ... }
does not work, neither does
newcommand{mycommand}[2][#2]{ ... }
Does anybody know how to do this?
macros optional-arguments
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
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,
newcommand{mycommand}[2][#1]{ ... }
does not work, neither does
newcommand{mycommand}[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,
newcommand{mycommand}[2][#1]{ ... }
does not work, neither does
newcommand{mycommand}[2][#2]{ ... }
Does anybody know how to do this?
macros optional-arguments
macros optional-arguments
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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:
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}
Great, thank you very much!
– Bodo Manthey
23 hours ago
add a comment |
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.
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
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}
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
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:
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}
Great, thank you very much!
– Bodo Manthey
23 hours ago
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:
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}
Great, thank you very much!
– Bodo Manthey
23 hours ago
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:
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}
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:
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}
answered 23 hours ago
WernerWerner
446k699871692
446k699871692
Great, thank you very much!
– Bodo Manthey
23 hours ago
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered 22 hours ago
egregegreg
725k8819183224
725k8819183224
add a comment |
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
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}
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
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}
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
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}
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}
answered 5 hours ago
Ulrich DiezUlrich Diez
5,275619
5,275619
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
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