How to create two commands with the same name but different number of parameters
I want to define a command with the same name, but a different number of parameters.
I'm considering something like this:
documentclass{article}
usepackage{hyperref}
newcommand{quelle}[1]{textit{(Quelle: url{#1})})}
newcommand{quelle}[2]{textit{(Quelle: url{#1} Absatz: #2)})}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de, sometext}
end{document}
If I try to compile this, I get the following result:
Command quelle already defined. ...2]{textit{(Quelle: url{#1} Absatz: #2)})}
Thank you for your help!
macros
add a comment |
I want to define a command with the same name, but a different number of parameters.
I'm considering something like this:
documentclass{article}
usepackage{hyperref}
newcommand{quelle}[1]{textit{(Quelle: url{#1})})}
newcommand{quelle}[2]{textit{(Quelle: url{#1} Absatz: #2)})}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de, sometext}
end{document}
If I try to compile this, I get the following result:
Command quelle already defined. ...2]{textit{(Quelle: url{#1} Absatz: #2)})}
Thank you for your help!
macros
Traditional LaTeX does not allow macros with the same name having a different mandatory number of arguments. You can always define a macro having a certain number of arguments, but you have to check whether they are empty (i.e. not specified). Withexpl3
you could usecs_new:Nn
foo_bar:nn` orcs_new:Nn foo_bar:nnnn
for example, but those macros are not meant for user space
– Christian Hupfer
12 hours ago
add a comment |
I want to define a command with the same name, but a different number of parameters.
I'm considering something like this:
documentclass{article}
usepackage{hyperref}
newcommand{quelle}[1]{textit{(Quelle: url{#1})})}
newcommand{quelle}[2]{textit{(Quelle: url{#1} Absatz: #2)})}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de, sometext}
end{document}
If I try to compile this, I get the following result:
Command quelle already defined. ...2]{textit{(Quelle: url{#1} Absatz: #2)})}
Thank you for your help!
macros
I want to define a command with the same name, but a different number of parameters.
I'm considering something like this:
documentclass{article}
usepackage{hyperref}
newcommand{quelle}[1]{textit{(Quelle: url{#1})})}
newcommand{quelle}[2]{textit{(Quelle: url{#1} Absatz: #2)})}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de, sometext}
end{document}
If I try to compile this, I get the following result:
Command quelle already defined. ...2]{textit{(Quelle: url{#1} Absatz: #2)})}
Thank you for your help!
macros
macros
edited 7 mins ago
Agi Hammerthief
1033
1033
asked 12 hours ago
mrbelamrbela
4681312
4681312
Traditional LaTeX does not allow macros with the same name having a different mandatory number of arguments. You can always define a macro having a certain number of arguments, but you have to check whether they are empty (i.e. not specified). Withexpl3
you could usecs_new:Nn
foo_bar:nn` orcs_new:Nn foo_bar:nnnn
for example, but those macros are not meant for user space
– Christian Hupfer
12 hours ago
add a comment |
Traditional LaTeX does not allow macros with the same name having a different mandatory number of arguments. You can always define a macro having a certain number of arguments, but you have to check whether they are empty (i.e. not specified). Withexpl3
you could usecs_new:Nn
foo_bar:nn` orcs_new:Nn foo_bar:nnnn
for example, but those macros are not meant for user space
– Christian Hupfer
12 hours ago
Traditional LaTeX does not allow macros with the same name having a different mandatory number of arguments. You can always define a macro having a certain number of arguments, but you have to check whether they are empty (i.e. not specified). With
expl3
you could use cs_new:Nn
foo_bar:nn` or cs_new:Nn foo_bar:nnnn
for example, but those macros are not meant for user space– Christian Hupfer
12 hours ago
Traditional LaTeX does not allow macros with the same name having a different mandatory number of arguments. You can always define a macro having a certain number of arguments, but you have to check whether they are empty (i.e. not specified). With
expl3
you could use cs_new:Nn
foo_bar:nn` or cs_new:Nn foo_bar:nnnn
for example, but those macros are not meant for user space– Christian Hupfer
12 hours ago
add a comment |
4 Answers
4
active
oldest
votes
The only change from the requested syntax is to use a !
as the separator between the http reference and "sometext", as I figured a comma ,
was much more likely to appear as part of "sometext:.
documentclass{article}
usepackage{hyperref,listofitems}
newcommand{quelle}[1]{%
setsepchar{!}%
readlistqarg{#1}%
(textit{Quelle: url{qarg[1]}}%
ifnumlistlenqarg>1relaxtextit{ Absatz: qarg[2]}fi%
)%
}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de! sometext}
end{document}
add a comment |
I suggest an optional argument at the end, for example, with xparse
and NewDocumentCommand
.
If the optional argument is not given, quelle
works like the intended one-argument version, if it is given Absatz #2
is printed in addition.
The check, whether it has been specified can be done with IfValueT{#2}{}
.
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
%newcommand{quelle}[1]{textit{(Quelle: url{#1})})}
NewDocumentCommand{quelle}{m+o}{textit{(Quelle: url{#1}IfValueT{#2}{Absatz: #2)}}}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de}[sometext]
end{document}
add a comment |
I'd prefer a syntax with a leading optional argument, that's more in line with common LaTeX conventions.
Anyway, you can have your preferred syntax by defining a single quelle
command: TeX cannot have two meanings for the same command.
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
NewDocumentCommand{quelle}{ >{SplitArgument{1}{,}}m }{%
makequelle#1%
}
NewDocumentCommand{makequelle}{mm}{%
textit{%
(Quelle: url{#1}%
IfValueT{#2}{ Absatz: #2})%
}%
}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de, sometext}
end{document}
The argument is split at the first comma, if present, and passed as a pair of arguments to makequelle
. If no comma is present, the second argument will make the test IfValueT
false, so nothing will be printed.
The same output with a more common syntax:
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
NewDocumentCommand{quelle}{om}{%
textit{%
(Quelle: url{#2}%
IfValueT{#1}{ Absatz: #1})%
}%
}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle[sometext]{www.wikipedia.de}
end{document}
add a comment |
The following code delivers what you're after using the following setup:
Use
quelleattributes{<list>}
to define the prefixes/attribute names in a comma-separate<list>
you'll be supplying (Quelle
,Absatz
, ...).Using
etoolbox
, we process the list sequentially, setting the attribute name, followed by the attribute. A text-comparison is done with each attribute name to see whether it matchesQuelle
. Only those are set usingurl
, while the others are set as-is.
documentclass{article}
usepackage{etoolbox}
usepackage{hyperref}
newcounter{quellecnt}
makeatletter
newcommand{quelleattributes}[1]{%
setcounter{quellecnt}{0}% Restart counter
renewcommand*{do}[1]{% How to update each item
stepcounter{quellecnt}% Step counter
@namedef{quelle@thequellecnt}{##1}% Define quelle@<num> to attribute
}%
docsvlist{#1}% Process list
}
newcommand{quelle}[1]{%
setcounter{quellecnt}{0}% Restart counter
renewcommand*{do}[1]{%
stepcounter{quellecnt}% Step counter
letquelleformatrelax% Default formatting of attribute
ifnumpdfstrcmp{@nameuse{quelle@thequellecnt}}{Quelle}=0
defquelleformat{url}% Attribute should be a url
fi
@nameuse{quelle@thequellecnt}: quelleformat{##1} % Set attribute
}
(textit{%
docsvlist{#1}unskip% Process list
})%
}
makeatother
begin{document}
quelleattributes{%
Quelle,
Absatz%
}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de, sometext}
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%2f472779%2fhow-to-create-two-commands-with-the-same-name-but-different-number-of-parameters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
The only change from the requested syntax is to use a !
as the separator between the http reference and "sometext", as I figured a comma ,
was much more likely to appear as part of "sometext:.
documentclass{article}
usepackage{hyperref,listofitems}
newcommand{quelle}[1]{%
setsepchar{!}%
readlistqarg{#1}%
(textit{Quelle: url{qarg[1]}}%
ifnumlistlenqarg>1relaxtextit{ Absatz: qarg[2]}fi%
)%
}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de! sometext}
end{document}
add a comment |
The only change from the requested syntax is to use a !
as the separator between the http reference and "sometext", as I figured a comma ,
was much more likely to appear as part of "sometext:.
documentclass{article}
usepackage{hyperref,listofitems}
newcommand{quelle}[1]{%
setsepchar{!}%
readlistqarg{#1}%
(textit{Quelle: url{qarg[1]}}%
ifnumlistlenqarg>1relaxtextit{ Absatz: qarg[2]}fi%
)%
}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de! sometext}
end{document}
add a comment |
The only change from the requested syntax is to use a !
as the separator between the http reference and "sometext", as I figured a comma ,
was much more likely to appear as part of "sometext:.
documentclass{article}
usepackage{hyperref,listofitems}
newcommand{quelle}[1]{%
setsepchar{!}%
readlistqarg{#1}%
(textit{Quelle: url{qarg[1]}}%
ifnumlistlenqarg>1relaxtextit{ Absatz: qarg[2]}fi%
)%
}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de! sometext}
end{document}
The only change from the requested syntax is to use a !
as the separator between the http reference and "sometext", as I figured a comma ,
was much more likely to appear as part of "sometext:.
documentclass{article}
usepackage{hyperref,listofitems}
newcommand{quelle}[1]{%
setsepchar{!}%
readlistqarg{#1}%
(textit{Quelle: url{qarg[1]}}%
ifnumlistlenqarg>1relaxtextit{ Absatz: qarg[2]}fi%
)%
}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de! sometext}
end{document}
edited 11 hours ago
answered 12 hours ago
Steven B. SegletesSteven B. Segletes
153k9194402
153k9194402
add a comment |
add a comment |
I suggest an optional argument at the end, for example, with xparse
and NewDocumentCommand
.
If the optional argument is not given, quelle
works like the intended one-argument version, if it is given Absatz #2
is printed in addition.
The check, whether it has been specified can be done with IfValueT{#2}{}
.
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
%newcommand{quelle}[1]{textit{(Quelle: url{#1})})}
NewDocumentCommand{quelle}{m+o}{textit{(Quelle: url{#1}IfValueT{#2}{Absatz: #2)}}}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de}[sometext]
end{document}
add a comment |
I suggest an optional argument at the end, for example, with xparse
and NewDocumentCommand
.
If the optional argument is not given, quelle
works like the intended one-argument version, if it is given Absatz #2
is printed in addition.
The check, whether it has been specified can be done with IfValueT{#2}{}
.
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
%newcommand{quelle}[1]{textit{(Quelle: url{#1})})}
NewDocumentCommand{quelle}{m+o}{textit{(Quelle: url{#1}IfValueT{#2}{Absatz: #2)}}}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de}[sometext]
end{document}
add a comment |
I suggest an optional argument at the end, for example, with xparse
and NewDocumentCommand
.
If the optional argument is not given, quelle
works like the intended one-argument version, if it is given Absatz #2
is printed in addition.
The check, whether it has been specified can be done with IfValueT{#2}{}
.
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
%newcommand{quelle}[1]{textit{(Quelle: url{#1})})}
NewDocumentCommand{quelle}{m+o}{textit{(Quelle: url{#1}IfValueT{#2}{Absatz: #2)}}}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de}[sometext]
end{document}
I suggest an optional argument at the end, for example, with xparse
and NewDocumentCommand
.
If the optional argument is not given, quelle
works like the intended one-argument version, if it is given Absatz #2
is printed in addition.
The check, whether it has been specified can be done with IfValueT{#2}{}
.
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
%newcommand{quelle}[1]{textit{(Quelle: url{#1})})}
NewDocumentCommand{quelle}{m+o}{textit{(Quelle: url{#1}IfValueT{#2}{Absatz: #2)}}}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de}[sometext]
end{document}
answered 12 hours ago
Christian HupferChristian Hupfer
149k14197392
149k14197392
add a comment |
add a comment |
I'd prefer a syntax with a leading optional argument, that's more in line with common LaTeX conventions.
Anyway, you can have your preferred syntax by defining a single quelle
command: TeX cannot have two meanings for the same command.
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
NewDocumentCommand{quelle}{ >{SplitArgument{1}{,}}m }{%
makequelle#1%
}
NewDocumentCommand{makequelle}{mm}{%
textit{%
(Quelle: url{#1}%
IfValueT{#2}{ Absatz: #2})%
}%
}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de, sometext}
end{document}
The argument is split at the first comma, if present, and passed as a pair of arguments to makequelle
. If no comma is present, the second argument will make the test IfValueT
false, so nothing will be printed.
The same output with a more common syntax:
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
NewDocumentCommand{quelle}{om}{%
textit{%
(Quelle: url{#2}%
IfValueT{#1}{ Absatz: #1})%
}%
}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle[sometext]{www.wikipedia.de}
end{document}
add a comment |
I'd prefer a syntax with a leading optional argument, that's more in line with common LaTeX conventions.
Anyway, you can have your preferred syntax by defining a single quelle
command: TeX cannot have two meanings for the same command.
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
NewDocumentCommand{quelle}{ >{SplitArgument{1}{,}}m }{%
makequelle#1%
}
NewDocumentCommand{makequelle}{mm}{%
textit{%
(Quelle: url{#1}%
IfValueT{#2}{ Absatz: #2})%
}%
}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de, sometext}
end{document}
The argument is split at the first comma, if present, and passed as a pair of arguments to makequelle
. If no comma is present, the second argument will make the test IfValueT
false, so nothing will be printed.
The same output with a more common syntax:
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
NewDocumentCommand{quelle}{om}{%
textit{%
(Quelle: url{#2}%
IfValueT{#1}{ Absatz: #1})%
}%
}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle[sometext]{www.wikipedia.de}
end{document}
add a comment |
I'd prefer a syntax with a leading optional argument, that's more in line with common LaTeX conventions.
Anyway, you can have your preferred syntax by defining a single quelle
command: TeX cannot have two meanings for the same command.
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
NewDocumentCommand{quelle}{ >{SplitArgument{1}{,}}m }{%
makequelle#1%
}
NewDocumentCommand{makequelle}{mm}{%
textit{%
(Quelle: url{#1}%
IfValueT{#2}{ Absatz: #2})%
}%
}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de, sometext}
end{document}
The argument is split at the first comma, if present, and passed as a pair of arguments to makequelle
. If no comma is present, the second argument will make the test IfValueT
false, so nothing will be printed.
The same output with a more common syntax:
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
NewDocumentCommand{quelle}{om}{%
textit{%
(Quelle: url{#2}%
IfValueT{#1}{ Absatz: #1})%
}%
}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle[sometext]{www.wikipedia.de}
end{document}
I'd prefer a syntax with a leading optional argument, that's more in line with common LaTeX conventions.
Anyway, you can have your preferred syntax by defining a single quelle
command: TeX cannot have two meanings for the same command.
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
NewDocumentCommand{quelle}{ >{SplitArgument{1}{,}}m }{%
makequelle#1%
}
NewDocumentCommand{makequelle}{mm}{%
textit{%
(Quelle: url{#1}%
IfValueT{#2}{ Absatz: #2})%
}%
}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de, sometext}
end{document}
The argument is split at the first comma, if present, and passed as a pair of arguments to makequelle
. If no comma is present, the second argument will make the test IfValueT
false, so nothing will be printed.
The same output with a more common syntax:
documentclass{article}
usepackage{xparse}
usepackage{hyperref}
NewDocumentCommand{quelle}{om}{%
textit{%
(Quelle: url{#2}%
IfValueT{#1}{ Absatz: #1})%
}%
}
begin{document}
Test: quelle{www.wikipedia.de}
Test: quelle[sometext]{www.wikipedia.de}
end{document}
answered 11 hours ago
egregegreg
716k8619023191
716k8619023191
add a comment |
add a comment |
The following code delivers what you're after using the following setup:
Use
quelleattributes{<list>}
to define the prefixes/attribute names in a comma-separate<list>
you'll be supplying (Quelle
,Absatz
, ...).Using
etoolbox
, we process the list sequentially, setting the attribute name, followed by the attribute. A text-comparison is done with each attribute name to see whether it matchesQuelle
. Only those are set usingurl
, while the others are set as-is.
documentclass{article}
usepackage{etoolbox}
usepackage{hyperref}
newcounter{quellecnt}
makeatletter
newcommand{quelleattributes}[1]{%
setcounter{quellecnt}{0}% Restart counter
renewcommand*{do}[1]{% How to update each item
stepcounter{quellecnt}% Step counter
@namedef{quelle@thequellecnt}{##1}% Define quelle@<num> to attribute
}%
docsvlist{#1}% Process list
}
newcommand{quelle}[1]{%
setcounter{quellecnt}{0}% Restart counter
renewcommand*{do}[1]{%
stepcounter{quellecnt}% Step counter
letquelleformatrelax% Default formatting of attribute
ifnumpdfstrcmp{@nameuse{quelle@thequellecnt}}{Quelle}=0
defquelleformat{url}% Attribute should be a url
fi
@nameuse{quelle@thequellecnt}: quelleformat{##1} % Set attribute
}
(textit{%
docsvlist{#1}unskip% Process list
})%
}
makeatother
begin{document}
quelleattributes{%
Quelle,
Absatz%
}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de, sometext}
end{document}
add a comment |
The following code delivers what you're after using the following setup:
Use
quelleattributes{<list>}
to define the prefixes/attribute names in a comma-separate<list>
you'll be supplying (Quelle
,Absatz
, ...).Using
etoolbox
, we process the list sequentially, setting the attribute name, followed by the attribute. A text-comparison is done with each attribute name to see whether it matchesQuelle
. Only those are set usingurl
, while the others are set as-is.
documentclass{article}
usepackage{etoolbox}
usepackage{hyperref}
newcounter{quellecnt}
makeatletter
newcommand{quelleattributes}[1]{%
setcounter{quellecnt}{0}% Restart counter
renewcommand*{do}[1]{% How to update each item
stepcounter{quellecnt}% Step counter
@namedef{quelle@thequellecnt}{##1}% Define quelle@<num> to attribute
}%
docsvlist{#1}% Process list
}
newcommand{quelle}[1]{%
setcounter{quellecnt}{0}% Restart counter
renewcommand*{do}[1]{%
stepcounter{quellecnt}% Step counter
letquelleformatrelax% Default formatting of attribute
ifnumpdfstrcmp{@nameuse{quelle@thequellecnt}}{Quelle}=0
defquelleformat{url}% Attribute should be a url
fi
@nameuse{quelle@thequellecnt}: quelleformat{##1} % Set attribute
}
(textit{%
docsvlist{#1}unskip% Process list
})%
}
makeatother
begin{document}
quelleattributes{%
Quelle,
Absatz%
}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de, sometext}
end{document}
add a comment |
The following code delivers what you're after using the following setup:
Use
quelleattributes{<list>}
to define the prefixes/attribute names in a comma-separate<list>
you'll be supplying (Quelle
,Absatz
, ...).Using
etoolbox
, we process the list sequentially, setting the attribute name, followed by the attribute. A text-comparison is done with each attribute name to see whether it matchesQuelle
. Only those are set usingurl
, while the others are set as-is.
documentclass{article}
usepackage{etoolbox}
usepackage{hyperref}
newcounter{quellecnt}
makeatletter
newcommand{quelleattributes}[1]{%
setcounter{quellecnt}{0}% Restart counter
renewcommand*{do}[1]{% How to update each item
stepcounter{quellecnt}% Step counter
@namedef{quelle@thequellecnt}{##1}% Define quelle@<num> to attribute
}%
docsvlist{#1}% Process list
}
newcommand{quelle}[1]{%
setcounter{quellecnt}{0}% Restart counter
renewcommand*{do}[1]{%
stepcounter{quellecnt}% Step counter
letquelleformatrelax% Default formatting of attribute
ifnumpdfstrcmp{@nameuse{quelle@thequellecnt}}{Quelle}=0
defquelleformat{url}% Attribute should be a url
fi
@nameuse{quelle@thequellecnt}: quelleformat{##1} % Set attribute
}
(textit{%
docsvlist{#1}unskip% Process list
})%
}
makeatother
begin{document}
quelleattributes{%
Quelle,
Absatz%
}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de, sometext}
end{document}
The following code delivers what you're after using the following setup:
Use
quelleattributes{<list>}
to define the prefixes/attribute names in a comma-separate<list>
you'll be supplying (Quelle
,Absatz
, ...).Using
etoolbox
, we process the list sequentially, setting the attribute name, followed by the attribute. A text-comparison is done with each attribute name to see whether it matchesQuelle
. Only those are set usingurl
, while the others are set as-is.
documentclass{article}
usepackage{etoolbox}
usepackage{hyperref}
newcounter{quellecnt}
makeatletter
newcommand{quelleattributes}[1]{%
setcounter{quellecnt}{0}% Restart counter
renewcommand*{do}[1]{% How to update each item
stepcounter{quellecnt}% Step counter
@namedef{quelle@thequellecnt}{##1}% Define quelle@<num> to attribute
}%
docsvlist{#1}% Process list
}
newcommand{quelle}[1]{%
setcounter{quellecnt}{0}% Restart counter
renewcommand*{do}[1]{%
stepcounter{quellecnt}% Step counter
letquelleformatrelax% Default formatting of attribute
ifnumpdfstrcmp{@nameuse{quelle@thequellecnt}}{Quelle}=0
defquelleformat{url}% Attribute should be a url
fi
@nameuse{quelle@thequellecnt}: quelleformat{##1} % Set attribute
}
(textit{%
docsvlist{#1}unskip% Process list
})%
}
makeatother
begin{document}
quelleattributes{%
Quelle,
Absatz%
}
Test: quelle{www.wikipedia.de}
Test: quelle{www.wikipedia.de, sometext}
end{document}
answered 12 hours ago
WernerWerner
442k679741670
442k679741670
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%2f472779%2fhow-to-create-two-commands-with-the-same-name-but-different-number-of-parameters%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
Traditional LaTeX does not allow macros with the same name having a different mandatory number of arguments. You can always define a macro having a certain number of arguments, but you have to check whether they are empty (i.e. not specified). With
expl3
you could usecs_new:Nn
foo_bar:nn` orcs_new:Nn foo_bar:nnnn
for example, but those macros are not meant for user space– Christian Hupfer
12 hours ago