Arguments of newcommand as variable names?
I have the following situation: I want to use the argument of newcommand to be the name of a new variable, which I define within newcommand using def.
When I try to compile it always says that my newly defined variable doesn't exist. I think the problem is the "#"-symbol of the argument within the def command.
documentclass{article}
newcounter{src}
newcommand{src}[1]{stepcounter{src}def #1 {arabic{src}} [arabic{src}]}
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}
My expected output would be:
1
2
first test: 1
second test: 2
But instead I get:
! Undefined control sequence. testone
! Undefined control sequence. testtwo
Does anyone have a solution for how to use the # for a variable name?
Thank you in advance
macros
add a comment |
I have the following situation: I want to use the argument of newcommand to be the name of a new variable, which I define within newcommand using def.
When I try to compile it always says that my newly defined variable doesn't exist. I think the problem is the "#"-symbol of the argument within the def command.
documentclass{article}
newcounter{src}
newcommand{src}[1]{stepcounter{src}def #1 {arabic{src}} [arabic{src}]}
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}
My expected output would be:
1
2
first test: 1
second test: 2
But instead I get:
! Undefined control sequence. testone
! Undefined control sequence. testtwo
Does anyone have a solution for how to use the # for a variable name?
Thank you in advance
macros
Welcome to TeX.SX Tryexpandafterdefcsname#1endcsnameinstead ofdef #1. But your counter number will be wrong anyway. It will use the current counter value, so it must be expanded, e.g.edef, for example
– Christian Hupfer
Dec 27 '16 at 22:14
Please consider to accept one of the given answers here!
– Christian Hupfer
Jan 29 '17 at 14:09
add a comment |
I have the following situation: I want to use the argument of newcommand to be the name of a new variable, which I define within newcommand using def.
When I try to compile it always says that my newly defined variable doesn't exist. I think the problem is the "#"-symbol of the argument within the def command.
documentclass{article}
newcounter{src}
newcommand{src}[1]{stepcounter{src}def #1 {arabic{src}} [arabic{src}]}
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}
My expected output would be:
1
2
first test: 1
second test: 2
But instead I get:
! Undefined control sequence. testone
! Undefined control sequence. testtwo
Does anyone have a solution for how to use the # for a variable name?
Thank you in advance
macros
I have the following situation: I want to use the argument of newcommand to be the name of a new variable, which I define within newcommand using def.
When I try to compile it always says that my newly defined variable doesn't exist. I think the problem is the "#"-symbol of the argument within the def command.
documentclass{article}
newcounter{src}
newcommand{src}[1]{stepcounter{src}def #1 {arabic{src}} [arabic{src}]}
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}
My expected output would be:
1
2
first test: 1
second test: 2
But instead I get:
! Undefined control sequence. testone
! Undefined control sequence. testtwo
Does anyone have a solution for how to use the # for a variable name?
Thank you in advance
macros
macros
edited Jan 22 at 19:41
Christian Hupfer
149k14197392
149k14197392
asked Dec 27 '16 at 22:11
PhilippPhilipp
844
844
Welcome to TeX.SX Tryexpandafterdefcsname#1endcsnameinstead ofdef #1. But your counter number will be wrong anyway. It will use the current counter value, so it must be expanded, e.g.edef, for example
– Christian Hupfer
Dec 27 '16 at 22:14
Please consider to accept one of the given answers here!
– Christian Hupfer
Jan 29 '17 at 14:09
add a comment |
Welcome to TeX.SX Tryexpandafterdefcsname#1endcsnameinstead ofdef #1. But your counter number will be wrong anyway. It will use the current counter value, so it must be expanded, e.g.edef, for example
– Christian Hupfer
Dec 27 '16 at 22:14
Please consider to accept one of the given answers here!
– Christian Hupfer
Jan 29 '17 at 14:09
Welcome to TeX.SX Try
expandafterdefcsname#1endcsname instead of def #1. But your counter number will be wrong anyway. It will use the current counter value, so it must be expanded, e.g. edef, for example– Christian Hupfer
Dec 27 '16 at 22:14
Welcome to TeX.SX Try
expandafterdefcsname#1endcsname instead of def #1. But your counter number will be wrong anyway. It will use the current counter value, so it must be expanded, e.g. edef, for example– Christian Hupfer
Dec 27 '16 at 22:14
Please consider to accept one of the given answers here!
– Christian Hupfer
Jan 29 '17 at 14:09
Please consider to accept one of the given answers here!
– Christian Hupfer
Jan 29 '17 at 14:09
add a comment |
3 Answers
3
active
oldest
votes
The construction of command sequence names can be done with
csname #1endcsname
but this is not sufficient for def, it must be prepended with expandafter, to expand the sequence name first, then use def (the same holds for edef etc.)
i.e.:
expandafterdefcsname #1endcsname{some expansion stuff}
will expand to deffoo{some expansion stuff} if #1 contains foo.
I used edef here to use the counter number at the time of definition of the macro name, otherwise arabic{src} would print the current number.
documentclass{article}
newcounter{src}
newcommand{src}[1]{stepcounter{src}expandafteredefcsname#1endcsname {arabic{src}} [arabic{src}]}
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}
This seems to work, thanks a lot! However, when executingsrcI get a big space in front of [number]. How can I get rid of this?
– Philipp
Dec 27 '16 at 22:26
Of course this will not work this way if#1has a `` in it
– Christian Hupfer
Dec 27 '16 at 22:26
@Philipp: Which big space? There is only the usual paragraph indentation
– Christian Hupfer
Dec 27 '16 at 22:29
do you know why I can't usesrcwithin theitemizeenvironment?
– Philipp
Dec 27 '16 at 22:46
begin{itemize}item src{foo} foo end{itemize}works for me
– Christian Hupfer
Dec 27 '16 at 22:51
|
show 3 more comments
Christian Hupfer has well explained what happens. You might enjoy a different approach based on expl3.
The main function used is cs_new:cpx, which is a variant of cs_new:Npn.
csstands for “control sequence”
newis self explaining
:separates the function name from the signature, that is, the list of its arguments
Nstands for a single token
pstands for “parameter text” (in the example case below nothing)
nstands for “braced argument” (the replacement text)
The variant is cs_new:cpx where c means that we expect a braced argument which will be transformed into a single symbolic token with its backslash at the beginning; x instead means that the corresponding argument will be fully expanded. We need full expansion because we want to store the current value of the counter, not the instructions to produce the value.
The other functions are int_new:N that allocates a new variable of type int (integer) and int_gincr:N that globally increments the variable.
documentclass{article}
usepackage{xparse}
ExplSyntaxOn
% allocate a new integer variable
int_new:N g_philipp_src_int
% the main command
NewDocumentCommand{src}{m}
{
% step the integer variable
int_gincr:N g_philipp_src_int
% define #1 with expansion of the replacement text
cs_new:cpx { #1 } { int_to_arabic:n { g_philipp_src_int } }
% print the current value
[int_to_arabic:n { g_philipp_src_int }]
}
ExplSyntaxOff
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}

Isn't this a little bit over the top ? ;-)
– Christian Hupfer
Dec 27 '16 at 22:55
@ChristianHupfer Why? It's essentially the same as yours, but with noexpandaftertrickery.;-)
– egreg
Dec 27 '16 at 23:23
I know, but it might be confusing for somebody not familiar with L3 - syntax.
– Christian Hupfer
Dec 28 '16 at 8:51
add a comment |
As an other option you can use the package etoolbox. The package provides a lot of useful commands and hooks.
In your case the command csdef is useful which is defined by etoolbox. This is similar to the solution of Christian.
documentclass{article}
newcounter{src}
usepackage{etoolbox}
newcommand{src}[1]{stepcounter{src}csedef{#1}{arabic{src}}[arabic{src}]}
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}
Yes, but my solution does not need a package (see the comment of Andrew Swann below my answer ;-) (+1)
– Christian Hupfer
Jan 4 '17 at 20:26
@ChristianHupfer: Sorry. I missed this. (on the other side: too many comments ;-) )
– Marco Daniel
Jan 4 '17 at 20:33
Blame the O.P. :D
– Christian Hupfer
Jan 4 '17 at 20:36
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%2f345933%2farguments-of-newcommand-as-variable-names%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
The construction of command sequence names can be done with
csname #1endcsname
but this is not sufficient for def, it must be prepended with expandafter, to expand the sequence name first, then use def (the same holds for edef etc.)
i.e.:
expandafterdefcsname #1endcsname{some expansion stuff}
will expand to deffoo{some expansion stuff} if #1 contains foo.
I used edef here to use the counter number at the time of definition of the macro name, otherwise arabic{src} would print the current number.
documentclass{article}
newcounter{src}
newcommand{src}[1]{stepcounter{src}expandafteredefcsname#1endcsname {arabic{src}} [arabic{src}]}
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}
This seems to work, thanks a lot! However, when executingsrcI get a big space in front of [number]. How can I get rid of this?
– Philipp
Dec 27 '16 at 22:26
Of course this will not work this way if#1has a `` in it
– Christian Hupfer
Dec 27 '16 at 22:26
@Philipp: Which big space? There is only the usual paragraph indentation
– Christian Hupfer
Dec 27 '16 at 22:29
do you know why I can't usesrcwithin theitemizeenvironment?
– Philipp
Dec 27 '16 at 22:46
begin{itemize}item src{foo} foo end{itemize}works for me
– Christian Hupfer
Dec 27 '16 at 22:51
|
show 3 more comments
The construction of command sequence names can be done with
csname #1endcsname
but this is not sufficient for def, it must be prepended with expandafter, to expand the sequence name first, then use def (the same holds for edef etc.)
i.e.:
expandafterdefcsname #1endcsname{some expansion stuff}
will expand to deffoo{some expansion stuff} if #1 contains foo.
I used edef here to use the counter number at the time of definition of the macro name, otherwise arabic{src} would print the current number.
documentclass{article}
newcounter{src}
newcommand{src}[1]{stepcounter{src}expandafteredefcsname#1endcsname {arabic{src}} [arabic{src}]}
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}
This seems to work, thanks a lot! However, when executingsrcI get a big space in front of [number]. How can I get rid of this?
– Philipp
Dec 27 '16 at 22:26
Of course this will not work this way if#1has a `` in it
– Christian Hupfer
Dec 27 '16 at 22:26
@Philipp: Which big space? There is only the usual paragraph indentation
– Christian Hupfer
Dec 27 '16 at 22:29
do you know why I can't usesrcwithin theitemizeenvironment?
– Philipp
Dec 27 '16 at 22:46
begin{itemize}item src{foo} foo end{itemize}works for me
– Christian Hupfer
Dec 27 '16 at 22:51
|
show 3 more comments
The construction of command sequence names can be done with
csname #1endcsname
but this is not sufficient for def, it must be prepended with expandafter, to expand the sequence name first, then use def (the same holds for edef etc.)
i.e.:
expandafterdefcsname #1endcsname{some expansion stuff}
will expand to deffoo{some expansion stuff} if #1 contains foo.
I used edef here to use the counter number at the time of definition of the macro name, otherwise arabic{src} would print the current number.
documentclass{article}
newcounter{src}
newcommand{src}[1]{stepcounter{src}expandafteredefcsname#1endcsname {arabic{src}} [arabic{src}]}
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}
The construction of command sequence names can be done with
csname #1endcsname
but this is not sufficient for def, it must be prepended with expandafter, to expand the sequence name first, then use def (the same holds for edef etc.)
i.e.:
expandafterdefcsname #1endcsname{some expansion stuff}
will expand to deffoo{some expansion stuff} if #1 contains foo.
I used edef here to use the counter number at the time of definition of the macro name, otherwise arabic{src} would print the current number.
documentclass{article}
newcounter{src}
newcommand{src}[1]{stepcounter{src}expandafteredefcsname#1endcsname {arabic{src}} [arabic{src}]}
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}
edited Dec 27 '16 at 22:22
answered Dec 27 '16 at 22:16
Christian HupferChristian Hupfer
149k14197392
149k14197392
This seems to work, thanks a lot! However, when executingsrcI get a big space in front of [number]. How can I get rid of this?
– Philipp
Dec 27 '16 at 22:26
Of course this will not work this way if#1has a `` in it
– Christian Hupfer
Dec 27 '16 at 22:26
@Philipp: Which big space? There is only the usual paragraph indentation
– Christian Hupfer
Dec 27 '16 at 22:29
do you know why I can't usesrcwithin theitemizeenvironment?
– Philipp
Dec 27 '16 at 22:46
begin{itemize}item src{foo} foo end{itemize}works for me
– Christian Hupfer
Dec 27 '16 at 22:51
|
show 3 more comments
This seems to work, thanks a lot! However, when executingsrcI get a big space in front of [number]. How can I get rid of this?
– Philipp
Dec 27 '16 at 22:26
Of course this will not work this way if#1has a `` in it
– Christian Hupfer
Dec 27 '16 at 22:26
@Philipp: Which big space? There is only the usual paragraph indentation
– Christian Hupfer
Dec 27 '16 at 22:29
do you know why I can't usesrcwithin theitemizeenvironment?
– Philipp
Dec 27 '16 at 22:46
begin{itemize}item src{foo} foo end{itemize}works for me
– Christian Hupfer
Dec 27 '16 at 22:51
This seems to work, thanks a lot! However, when executing
src I get a big space in front of [number]. How can I get rid of this?– Philipp
Dec 27 '16 at 22:26
This seems to work, thanks a lot! However, when executing
src I get a big space in front of [number]. How can I get rid of this?– Philipp
Dec 27 '16 at 22:26
Of course this will not work this way if
#1 has a `` in it– Christian Hupfer
Dec 27 '16 at 22:26
Of course this will not work this way if
#1 has a `` in it– Christian Hupfer
Dec 27 '16 at 22:26
@Philipp: Which big space? There is only the usual paragraph indentation
– Christian Hupfer
Dec 27 '16 at 22:29
@Philipp: Which big space? There is only the usual paragraph indentation
– Christian Hupfer
Dec 27 '16 at 22:29
do you know why I can't use
src within the itemize environment?– Philipp
Dec 27 '16 at 22:46
do you know why I can't use
src within the itemize environment?– Philipp
Dec 27 '16 at 22:46
begin{itemize}item src{foo} foo end{itemize} works for me– Christian Hupfer
Dec 27 '16 at 22:51
begin{itemize}item src{foo} foo end{itemize} works for me– Christian Hupfer
Dec 27 '16 at 22:51
|
show 3 more comments
Christian Hupfer has well explained what happens. You might enjoy a different approach based on expl3.
The main function used is cs_new:cpx, which is a variant of cs_new:Npn.
csstands for “control sequence”
newis self explaining
:separates the function name from the signature, that is, the list of its arguments
Nstands for a single token
pstands for “parameter text” (in the example case below nothing)
nstands for “braced argument” (the replacement text)
The variant is cs_new:cpx where c means that we expect a braced argument which will be transformed into a single symbolic token with its backslash at the beginning; x instead means that the corresponding argument will be fully expanded. We need full expansion because we want to store the current value of the counter, not the instructions to produce the value.
The other functions are int_new:N that allocates a new variable of type int (integer) and int_gincr:N that globally increments the variable.
documentclass{article}
usepackage{xparse}
ExplSyntaxOn
% allocate a new integer variable
int_new:N g_philipp_src_int
% the main command
NewDocumentCommand{src}{m}
{
% step the integer variable
int_gincr:N g_philipp_src_int
% define #1 with expansion of the replacement text
cs_new:cpx { #1 } { int_to_arabic:n { g_philipp_src_int } }
% print the current value
[int_to_arabic:n { g_philipp_src_int }]
}
ExplSyntaxOff
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}

Isn't this a little bit over the top ? ;-)
– Christian Hupfer
Dec 27 '16 at 22:55
@ChristianHupfer Why? It's essentially the same as yours, but with noexpandaftertrickery.;-)
– egreg
Dec 27 '16 at 23:23
I know, but it might be confusing for somebody not familiar with L3 - syntax.
– Christian Hupfer
Dec 28 '16 at 8:51
add a comment |
Christian Hupfer has well explained what happens. You might enjoy a different approach based on expl3.
The main function used is cs_new:cpx, which is a variant of cs_new:Npn.
csstands for “control sequence”
newis self explaining
:separates the function name from the signature, that is, the list of its arguments
Nstands for a single token
pstands for “parameter text” (in the example case below nothing)
nstands for “braced argument” (the replacement text)
The variant is cs_new:cpx where c means that we expect a braced argument which will be transformed into a single symbolic token with its backslash at the beginning; x instead means that the corresponding argument will be fully expanded. We need full expansion because we want to store the current value of the counter, not the instructions to produce the value.
The other functions are int_new:N that allocates a new variable of type int (integer) and int_gincr:N that globally increments the variable.
documentclass{article}
usepackage{xparse}
ExplSyntaxOn
% allocate a new integer variable
int_new:N g_philipp_src_int
% the main command
NewDocumentCommand{src}{m}
{
% step the integer variable
int_gincr:N g_philipp_src_int
% define #1 with expansion of the replacement text
cs_new:cpx { #1 } { int_to_arabic:n { g_philipp_src_int } }
% print the current value
[int_to_arabic:n { g_philipp_src_int }]
}
ExplSyntaxOff
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}

Isn't this a little bit over the top ? ;-)
– Christian Hupfer
Dec 27 '16 at 22:55
@ChristianHupfer Why? It's essentially the same as yours, but with noexpandaftertrickery.;-)
– egreg
Dec 27 '16 at 23:23
I know, but it might be confusing for somebody not familiar with L3 - syntax.
– Christian Hupfer
Dec 28 '16 at 8:51
add a comment |
Christian Hupfer has well explained what happens. You might enjoy a different approach based on expl3.
The main function used is cs_new:cpx, which is a variant of cs_new:Npn.
csstands for “control sequence”
newis self explaining
:separates the function name from the signature, that is, the list of its arguments
Nstands for a single token
pstands for “parameter text” (in the example case below nothing)
nstands for “braced argument” (the replacement text)
The variant is cs_new:cpx where c means that we expect a braced argument which will be transformed into a single symbolic token with its backslash at the beginning; x instead means that the corresponding argument will be fully expanded. We need full expansion because we want to store the current value of the counter, not the instructions to produce the value.
The other functions are int_new:N that allocates a new variable of type int (integer) and int_gincr:N that globally increments the variable.
documentclass{article}
usepackage{xparse}
ExplSyntaxOn
% allocate a new integer variable
int_new:N g_philipp_src_int
% the main command
NewDocumentCommand{src}{m}
{
% step the integer variable
int_gincr:N g_philipp_src_int
% define #1 with expansion of the replacement text
cs_new:cpx { #1 } { int_to_arabic:n { g_philipp_src_int } }
% print the current value
[int_to_arabic:n { g_philipp_src_int }]
}
ExplSyntaxOff
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}

Christian Hupfer has well explained what happens. You might enjoy a different approach based on expl3.
The main function used is cs_new:cpx, which is a variant of cs_new:Npn.
csstands for “control sequence”
newis self explaining
:separates the function name from the signature, that is, the list of its arguments
Nstands for a single token
pstands for “parameter text” (in the example case below nothing)
nstands for “braced argument” (the replacement text)
The variant is cs_new:cpx where c means that we expect a braced argument which will be transformed into a single symbolic token with its backslash at the beginning; x instead means that the corresponding argument will be fully expanded. We need full expansion because we want to store the current value of the counter, not the instructions to produce the value.
The other functions are int_new:N that allocates a new variable of type int (integer) and int_gincr:N that globally increments the variable.
documentclass{article}
usepackage{xparse}
ExplSyntaxOn
% allocate a new integer variable
int_new:N g_philipp_src_int
% the main command
NewDocumentCommand{src}{m}
{
% step the integer variable
int_gincr:N g_philipp_src_int
% define #1 with expansion of the replacement text
cs_new:cpx { #1 } { int_to_arabic:n { g_philipp_src_int } }
% print the current value
[int_to_arabic:n { g_philipp_src_int }]
}
ExplSyntaxOff
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}

answered Dec 27 '16 at 22:45
egregegreg
716k8619033189
716k8619033189
Isn't this a little bit over the top ? ;-)
– Christian Hupfer
Dec 27 '16 at 22:55
@ChristianHupfer Why? It's essentially the same as yours, but with noexpandaftertrickery.;-)
– egreg
Dec 27 '16 at 23:23
I know, but it might be confusing for somebody not familiar with L3 - syntax.
– Christian Hupfer
Dec 28 '16 at 8:51
add a comment |
Isn't this a little bit over the top ? ;-)
– Christian Hupfer
Dec 27 '16 at 22:55
@ChristianHupfer Why? It's essentially the same as yours, but with noexpandaftertrickery.;-)
– egreg
Dec 27 '16 at 23:23
I know, but it might be confusing for somebody not familiar with L3 - syntax.
– Christian Hupfer
Dec 28 '16 at 8:51
Isn't this a little bit over the top ? ;-)
– Christian Hupfer
Dec 27 '16 at 22:55
Isn't this a little bit over the top ? ;-)
– Christian Hupfer
Dec 27 '16 at 22:55
@ChristianHupfer Why? It's essentially the same as yours, but with no
expandafter trickery. ;-)– egreg
Dec 27 '16 at 23:23
@ChristianHupfer Why? It's essentially the same as yours, but with no
expandafter trickery. ;-)– egreg
Dec 27 '16 at 23:23
I know, but it might be confusing for somebody not familiar with L3 - syntax.
– Christian Hupfer
Dec 28 '16 at 8:51
I know, but it might be confusing for somebody not familiar with L3 - syntax.
– Christian Hupfer
Dec 28 '16 at 8:51
add a comment |
As an other option you can use the package etoolbox. The package provides a lot of useful commands and hooks.
In your case the command csdef is useful which is defined by etoolbox. This is similar to the solution of Christian.
documentclass{article}
newcounter{src}
usepackage{etoolbox}
newcommand{src}[1]{stepcounter{src}csedef{#1}{arabic{src}}[arabic{src}]}
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}
Yes, but my solution does not need a package (see the comment of Andrew Swann below my answer ;-) (+1)
– Christian Hupfer
Jan 4 '17 at 20:26
@ChristianHupfer: Sorry. I missed this. (on the other side: too many comments ;-) )
– Marco Daniel
Jan 4 '17 at 20:33
Blame the O.P. :D
– Christian Hupfer
Jan 4 '17 at 20:36
add a comment |
As an other option you can use the package etoolbox. The package provides a lot of useful commands and hooks.
In your case the command csdef is useful which is defined by etoolbox. This is similar to the solution of Christian.
documentclass{article}
newcounter{src}
usepackage{etoolbox}
newcommand{src}[1]{stepcounter{src}csedef{#1}{arabic{src}}[arabic{src}]}
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}
Yes, but my solution does not need a package (see the comment of Andrew Swann below my answer ;-) (+1)
– Christian Hupfer
Jan 4 '17 at 20:26
@ChristianHupfer: Sorry. I missed this. (on the other side: too many comments ;-) )
– Marco Daniel
Jan 4 '17 at 20:33
Blame the O.P. :D
– Christian Hupfer
Jan 4 '17 at 20:36
add a comment |
As an other option you can use the package etoolbox. The package provides a lot of useful commands and hooks.
In your case the command csdef is useful which is defined by etoolbox. This is similar to the solution of Christian.
documentclass{article}
newcounter{src}
usepackage{etoolbox}
newcommand{src}[1]{stepcounter{src}csedef{#1}{arabic{src}}[arabic{src}]}
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}
As an other option you can use the package etoolbox. The package provides a lot of useful commands and hooks.
In your case the command csdef is useful which is defined by etoolbox. This is similar to the solution of Christian.
documentclass{article}
newcounter{src}
usepackage{etoolbox}
newcommand{src}[1]{stepcounter{src}csedef{#1}{arabic{src}}[arabic{src}]}
begin{document}
src{testone}
src{testtwo}
first test: testone
second test: testtwo
end{document}
edited Apr 13 '17 at 12:35
Community♦
1
1
answered Jan 4 '17 at 20:09
Marco DanielMarco Daniel
78k13221387
78k13221387
Yes, but my solution does not need a package (see the comment of Andrew Swann below my answer ;-) (+1)
– Christian Hupfer
Jan 4 '17 at 20:26
@ChristianHupfer: Sorry. I missed this. (on the other side: too many comments ;-) )
– Marco Daniel
Jan 4 '17 at 20:33
Blame the O.P. :D
– Christian Hupfer
Jan 4 '17 at 20:36
add a comment |
Yes, but my solution does not need a package (see the comment of Andrew Swann below my answer ;-) (+1)
– Christian Hupfer
Jan 4 '17 at 20:26
@ChristianHupfer: Sorry. I missed this. (on the other side: too many comments ;-) )
– Marco Daniel
Jan 4 '17 at 20:33
Blame the O.P. :D
– Christian Hupfer
Jan 4 '17 at 20:36
Yes, but my solution does not need a package (see the comment of Andrew Swann below my answer ;-) (+1)
– Christian Hupfer
Jan 4 '17 at 20:26
Yes, but my solution does not need a package (see the comment of Andrew Swann below my answer ;-) (+1)
– Christian Hupfer
Jan 4 '17 at 20:26
@ChristianHupfer: Sorry. I missed this. (on the other side: too many comments ;-) )
– Marco Daniel
Jan 4 '17 at 20:33
@ChristianHupfer: Sorry. I missed this. (on the other side: too many comments ;-) )
– Marco Daniel
Jan 4 '17 at 20:33
Blame the O.P. :D
– Christian Hupfer
Jan 4 '17 at 20:36
Blame the O.P. :D
– Christian Hupfer
Jan 4 '17 at 20:36
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%2f345933%2farguments-of-newcommand-as-variable-names%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
Welcome to TeX.SX Try
expandafterdefcsname#1endcsnameinstead ofdef #1. But your counter number will be wrong anyway. It will use the current counter value, so it must be expanded, e.g.edef, for example– Christian Hupfer
Dec 27 '16 at 22:14
Please consider to accept one of the given answers here!
– Christian Hupfer
Jan 29 '17 at 14:09