A command challenge
Here's a little challenge. The goal is to create a command that takes is two or three arguments without using square brackets for optional arguments. The first and second argument are separated by a comma and a space, while the second and the third (if present) are separated by a dash. This means that both of the following inputs should be acceptable
mycommand{arg_1, arg_2}
and
mycommand{arg_1, arg_2-arg_3}
The definition of what mycommand
does with the two/three arguments is irrelevant to the problem.
macros expl3 xparse
add a comment |
Here's a little challenge. The goal is to create a command that takes is two or three arguments without using square brackets for optional arguments. The first and second argument are separated by a comma and a space, while the second and the third (if present) are separated by a dash. This means that both of the following inputs should be acceptable
mycommand{arg_1, arg_2}
and
mycommand{arg_1, arg_2-arg_3}
The definition of what mycommand
does with the two/three arguments is irrelevant to the problem.
macros expl3 xparse
1
That's one argument from a TeX point of view ... I guess you want a parser for that one document-level argument.
– Joseph Wright♦
4 hours ago
Trydefmycommand#1,space#2-#3{...}
.
– John Kormylo
48 mins ago
add a comment |
Here's a little challenge. The goal is to create a command that takes is two or three arguments without using square brackets for optional arguments. The first and second argument are separated by a comma and a space, while the second and the third (if present) are separated by a dash. This means that both of the following inputs should be acceptable
mycommand{arg_1, arg_2}
and
mycommand{arg_1, arg_2-arg_3}
The definition of what mycommand
does with the two/three arguments is irrelevant to the problem.
macros expl3 xparse
Here's a little challenge. The goal is to create a command that takes is two or three arguments without using square brackets for optional arguments. The first and second argument are separated by a comma and a space, while the second and the third (if present) are separated by a dash. This means that both of the following inputs should be acceptable
mycommand{arg_1, arg_2}
and
mycommand{arg_1, arg_2-arg_3}
The definition of what mycommand
does with the two/three arguments is irrelevant to the problem.
macros expl3 xparse
macros expl3 xparse
asked 4 hours ago
noibenoibe
935
935
1
That's one argument from a TeX point of view ... I guess you want a parser for that one document-level argument.
– Joseph Wright♦
4 hours ago
Trydefmycommand#1,space#2-#3{...}
.
– John Kormylo
48 mins ago
add a comment |
1
That's one argument from a TeX point of view ... I guess you want a parser for that one document-level argument.
– Joseph Wright♦
4 hours ago
Trydefmycommand#1,space#2-#3{...}
.
– John Kormylo
48 mins ago
1
1
That's one argument from a TeX point of view ... I guess you want a parser for that one document-level argument.
– Joseph Wright♦
4 hours ago
That's one argument from a TeX point of view ... I guess you want a parser for that one document-level argument.
– Joseph Wright♦
4 hours ago
Try
defmycommand#1,space#2-#3{...}
.– John Kormylo
48 mins ago
Try
defmycommand#1,space#2-#3{...}
.– John Kormylo
48 mins ago
add a comment |
3 Answers
3
active
oldest
votes
You can do it with xparse
:
documentclass{article}
usepackage{xparse}
% split at the comma
NewDocumentCommand{mycommand}{ >{SplitArgument{1}{,}}m }{%
mycommandA#1%
}
% do something with #1 and split the second part at the hyphen
NewDocumentCommand{mycommandA}{ m >{SplitArgument{1}{-}}m }{%
Main is #1%
IfNoValueTF{#2}{.}{; mycommandB#2}%
}
% do something with the second part
NewDocumentCommand{mycommandB}{mm}{%
Secondary is #1IfValueT{#2}{ plus #2}.%
}
begin{document}
mycommand{arg1}
mycommand{arg1, arg2}
mycommand{arg1, arg2-arg3}
end{document}
An advantage: it's irrelevant if you forget the space after the comma.
add a comment |
documentclass{article}
defmycommand#1{xmycommand#1--relax}
defxmycommand#1,#2-#3-#4relax{[#1][#2][#3]}
begin{document}
$mycommand{arg_1, arg_2}$
$mycommand{arg_1, arg_2-arg3}$
end{document}
6
Even without electricity. I'm impressed!
– marmot
4 hours ago
How about adding some mortar to the bricks!?
– Werner
4 hours ago
@Werner where I come from we prefer this style: derbyshireuk.net/derbyshire_drystonewalls.html
– David Carlisle
4 hours ago
@DavidCarlisle: It seems drafty... :D
– Werner
4 hours ago
2
@noibe oh no a disaster! I don't mind not getting the tick (I have more than enough points) but seeing points go to egreg is so depressing:)
– David Carlisle
3 hours ago
|
show 2 more comments
A listofitems
approach.
documentclass{article}
usepackage{listofitems}
newcommandmycommand[1]{setsepchar{,/-}readlist*myargs{#1}%
noindent Arg 1 is myargs[1]\
Arg 2 is myargs[2,1]\
Arg 3 is ifnumlistlenmyargs[2]=1relax Absentelsemyargs[2,2]fi%
}
begin{document}
mycommand{arg1, arg2}
and
mycommand{arg1, arg2-arg3}
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%2f473965%2fa-command-challenge%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 do it with xparse
:
documentclass{article}
usepackage{xparse}
% split at the comma
NewDocumentCommand{mycommand}{ >{SplitArgument{1}{,}}m }{%
mycommandA#1%
}
% do something with #1 and split the second part at the hyphen
NewDocumentCommand{mycommandA}{ m >{SplitArgument{1}{-}}m }{%
Main is #1%
IfNoValueTF{#2}{.}{; mycommandB#2}%
}
% do something with the second part
NewDocumentCommand{mycommandB}{mm}{%
Secondary is #1IfValueT{#2}{ plus #2}.%
}
begin{document}
mycommand{arg1}
mycommand{arg1, arg2}
mycommand{arg1, arg2-arg3}
end{document}
An advantage: it's irrelevant if you forget the space after the comma.
add a comment |
You can do it with xparse
:
documentclass{article}
usepackage{xparse}
% split at the comma
NewDocumentCommand{mycommand}{ >{SplitArgument{1}{,}}m }{%
mycommandA#1%
}
% do something with #1 and split the second part at the hyphen
NewDocumentCommand{mycommandA}{ m >{SplitArgument{1}{-}}m }{%
Main is #1%
IfNoValueTF{#2}{.}{; mycommandB#2}%
}
% do something with the second part
NewDocumentCommand{mycommandB}{mm}{%
Secondary is #1IfValueT{#2}{ plus #2}.%
}
begin{document}
mycommand{arg1}
mycommand{arg1, arg2}
mycommand{arg1, arg2-arg3}
end{document}
An advantage: it's irrelevant if you forget the space after the comma.
add a comment |
You can do it with xparse
:
documentclass{article}
usepackage{xparse}
% split at the comma
NewDocumentCommand{mycommand}{ >{SplitArgument{1}{,}}m }{%
mycommandA#1%
}
% do something with #1 and split the second part at the hyphen
NewDocumentCommand{mycommandA}{ m >{SplitArgument{1}{-}}m }{%
Main is #1%
IfNoValueTF{#2}{.}{; mycommandB#2}%
}
% do something with the second part
NewDocumentCommand{mycommandB}{mm}{%
Secondary is #1IfValueT{#2}{ plus #2}.%
}
begin{document}
mycommand{arg1}
mycommand{arg1, arg2}
mycommand{arg1, arg2-arg3}
end{document}
An advantage: it's irrelevant if you forget the space after the comma.
You can do it with xparse
:
documentclass{article}
usepackage{xparse}
% split at the comma
NewDocumentCommand{mycommand}{ >{SplitArgument{1}{,}}m }{%
mycommandA#1%
}
% do something with #1 and split the second part at the hyphen
NewDocumentCommand{mycommandA}{ m >{SplitArgument{1}{-}}m }{%
Main is #1%
IfNoValueTF{#2}{.}{; mycommandB#2}%
}
% do something with the second part
NewDocumentCommand{mycommandB}{mm}{%
Secondary is #1IfValueT{#2}{ plus #2}.%
}
begin{document}
mycommand{arg1}
mycommand{arg1, arg2}
mycommand{arg1, arg2-arg3}
end{document}
An advantage: it's irrelevant if you forget the space after the comma.
answered 4 hours ago
egregegreg
718k8719043200
718k8719043200
add a comment |
add a comment |
documentclass{article}
defmycommand#1{xmycommand#1--relax}
defxmycommand#1,#2-#3-#4relax{[#1][#2][#3]}
begin{document}
$mycommand{arg_1, arg_2}$
$mycommand{arg_1, arg_2-arg3}$
end{document}
6
Even without electricity. I'm impressed!
– marmot
4 hours ago
How about adding some mortar to the bricks!?
– Werner
4 hours ago
@Werner where I come from we prefer this style: derbyshireuk.net/derbyshire_drystonewalls.html
– David Carlisle
4 hours ago
@DavidCarlisle: It seems drafty... :D
– Werner
4 hours ago
2
@noibe oh no a disaster! I don't mind not getting the tick (I have more than enough points) but seeing points go to egreg is so depressing:)
– David Carlisle
3 hours ago
|
show 2 more comments
documentclass{article}
defmycommand#1{xmycommand#1--relax}
defxmycommand#1,#2-#3-#4relax{[#1][#2][#3]}
begin{document}
$mycommand{arg_1, arg_2}$
$mycommand{arg_1, arg_2-arg3}$
end{document}
6
Even without electricity. I'm impressed!
– marmot
4 hours ago
How about adding some mortar to the bricks!?
– Werner
4 hours ago
@Werner where I come from we prefer this style: derbyshireuk.net/derbyshire_drystonewalls.html
– David Carlisle
4 hours ago
@DavidCarlisle: It seems drafty... :D
– Werner
4 hours ago
2
@noibe oh no a disaster! I don't mind not getting the tick (I have more than enough points) but seeing points go to egreg is so depressing:)
– David Carlisle
3 hours ago
|
show 2 more comments
documentclass{article}
defmycommand#1{xmycommand#1--relax}
defxmycommand#1,#2-#3-#4relax{[#1][#2][#3]}
begin{document}
$mycommand{arg_1, arg_2}$
$mycommand{arg_1, arg_2-arg3}$
end{document}
documentclass{article}
defmycommand#1{xmycommand#1--relax}
defxmycommand#1,#2-#3-#4relax{[#1][#2][#3]}
begin{document}
$mycommand{arg_1, arg_2}$
$mycommand{arg_1, arg_2-arg3}$
end{document}
answered 4 hours ago
David CarlisleDavid Carlisle
489k4111321880
489k4111321880
6
Even without electricity. I'm impressed!
– marmot
4 hours ago
How about adding some mortar to the bricks!?
– Werner
4 hours ago
@Werner where I come from we prefer this style: derbyshireuk.net/derbyshire_drystonewalls.html
– David Carlisle
4 hours ago
@DavidCarlisle: It seems drafty... :D
– Werner
4 hours ago
2
@noibe oh no a disaster! I don't mind not getting the tick (I have more than enough points) but seeing points go to egreg is so depressing:)
– David Carlisle
3 hours ago
|
show 2 more comments
6
Even without electricity. I'm impressed!
– marmot
4 hours ago
How about adding some mortar to the bricks!?
– Werner
4 hours ago
@Werner where I come from we prefer this style: derbyshireuk.net/derbyshire_drystonewalls.html
– David Carlisle
4 hours ago
@DavidCarlisle: It seems drafty... :D
– Werner
4 hours ago
2
@noibe oh no a disaster! I don't mind not getting the tick (I have more than enough points) but seeing points go to egreg is so depressing:)
– David Carlisle
3 hours ago
6
6
Even without electricity. I'm impressed!
– marmot
4 hours ago
Even without electricity. I'm impressed!
– marmot
4 hours ago
How about adding some mortar to the bricks!?
– Werner
4 hours ago
How about adding some mortar to the bricks!?
– Werner
4 hours ago
@Werner where I come from we prefer this style: derbyshireuk.net/derbyshire_drystonewalls.html
– David Carlisle
4 hours ago
@Werner where I come from we prefer this style: derbyshireuk.net/derbyshire_drystonewalls.html
– David Carlisle
4 hours ago
@DavidCarlisle: It seems drafty... :D
– Werner
4 hours ago
@DavidCarlisle: It seems drafty... :D
– Werner
4 hours ago
2
2
@noibe oh no a disaster! I don't mind not getting the tick (I have more than enough points) but seeing points go to egreg is so depressing:)
– David Carlisle
3 hours ago
@noibe oh no a disaster! I don't mind not getting the tick (I have more than enough points) but seeing points go to egreg is so depressing:)
– David Carlisle
3 hours ago
|
show 2 more comments
A listofitems
approach.
documentclass{article}
usepackage{listofitems}
newcommandmycommand[1]{setsepchar{,/-}readlist*myargs{#1}%
noindent Arg 1 is myargs[1]\
Arg 2 is myargs[2,1]\
Arg 3 is ifnumlistlenmyargs[2]=1relax Absentelsemyargs[2,2]fi%
}
begin{document}
mycommand{arg1, arg2}
and
mycommand{arg1, arg2-arg3}
end{document}
add a comment |
A listofitems
approach.
documentclass{article}
usepackage{listofitems}
newcommandmycommand[1]{setsepchar{,/-}readlist*myargs{#1}%
noindent Arg 1 is myargs[1]\
Arg 2 is myargs[2,1]\
Arg 3 is ifnumlistlenmyargs[2]=1relax Absentelsemyargs[2,2]fi%
}
begin{document}
mycommand{arg1, arg2}
and
mycommand{arg1, arg2-arg3}
end{document}
add a comment |
A listofitems
approach.
documentclass{article}
usepackage{listofitems}
newcommandmycommand[1]{setsepchar{,/-}readlist*myargs{#1}%
noindent Arg 1 is myargs[1]\
Arg 2 is myargs[2,1]\
Arg 3 is ifnumlistlenmyargs[2]=1relax Absentelsemyargs[2,2]fi%
}
begin{document}
mycommand{arg1, arg2}
and
mycommand{arg1, arg2-arg3}
end{document}
A listofitems
approach.
documentclass{article}
usepackage{listofitems}
newcommandmycommand[1]{setsepchar{,/-}readlist*myargs{#1}%
noindent Arg 1 is myargs[1]\
Arg 2 is myargs[2,1]\
Arg 3 is ifnumlistlenmyargs[2]=1relax Absentelsemyargs[2,2]fi%
}
begin{document}
mycommand{arg1, arg2}
and
mycommand{arg1, arg2-arg3}
end{document}
answered 3 hours ago
Steven B. SegletesSteven B. Segletes
154k9198405
154k9198405
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%2f473965%2fa-command-challenge%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
1
That's one argument from a TeX point of view ... I guess you want a parser for that one document-level argument.
– Joseph Wright♦
4 hours ago
Try
defmycommand#1,space#2-#3{...}
.– John Kormylo
48 mins ago