How can this command be altered to work in more cases?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
So the command is as follows:
newcommandsumi1tonsumlimits_i=1^n
I want to be able to generalize this further. For example, if I type sumj3toQ then it would give me the summation from j=3 to Q. How do I make this happen without writing a command for every possible scenario?
math-mode macros
add a comment |Â
up vote
1
down vote
favorite
So the command is as follows:
newcommandsumi1tonsumlimits_i=1^n
I want to be able to generalize this further. For example, if I type sumj3toQ then it would give me the summation from j=3 to Q. How do I make this happen without writing a command for every possible scenario?
math-mode macros
3
Surely you must be getting an error message when you try to define a macro calledsumi1ton
via anewcommand
ordef
instruction: One isn't allowed to have numerals (and other non-letter characters) in a TeX macro whose name contains one or more letter characters.
– Mico
Aug 7 at 20:10
Possible related here? tex.stackexchange.com/questions/224430/…
– Sebastiano
Aug 7 at 20:14
1
@Mico you are correct. I made a mistake in this post. The actual command was sumiton
– Mohammed Shahid
Aug 7 at 20:34
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
So the command is as follows:
newcommandsumi1tonsumlimits_i=1^n
I want to be able to generalize this further. For example, if I type sumj3toQ then it would give me the summation from j=3 to Q. How do I make this happen without writing a command for every possible scenario?
math-mode macros
So the command is as follows:
newcommandsumi1tonsumlimits_i=1^n
I want to be able to generalize this further. For example, if I type sumj3toQ then it would give me the summation from j=3 to Q. How do I make this happen without writing a command for every possible scenario?
math-mode macros
edited Aug 7 at 20:01


TeXnician
20.2k52880
20.2k52880
asked Aug 7 at 19:59


Mohammed Shahid
216
216
3
Surely you must be getting an error message when you try to define a macro calledsumi1ton
via anewcommand
ordef
instruction: One isn't allowed to have numerals (and other non-letter characters) in a TeX macro whose name contains one or more letter characters.
– Mico
Aug 7 at 20:10
Possible related here? tex.stackexchange.com/questions/224430/…
– Sebastiano
Aug 7 at 20:14
1
@Mico you are correct. I made a mistake in this post. The actual command was sumiton
– Mohammed Shahid
Aug 7 at 20:34
add a comment |Â
3
Surely you must be getting an error message when you try to define a macro calledsumi1ton
via anewcommand
ordef
instruction: One isn't allowed to have numerals (and other non-letter characters) in a TeX macro whose name contains one or more letter characters.
– Mico
Aug 7 at 20:10
Possible related here? tex.stackexchange.com/questions/224430/…
– Sebastiano
Aug 7 at 20:14
1
@Mico you are correct. I made a mistake in this post. The actual command was sumiton
– Mohammed Shahid
Aug 7 at 20:34
3
3
Surely you must be getting an error message when you try to define a macro called
sumi1ton
via a newcommand
or def
instruction: One isn't allowed to have numerals (and other non-letter characters) in a TeX macro whose name contains one or more letter characters.– Mico
Aug 7 at 20:10
Surely you must be getting an error message when you try to define a macro called
sumi1ton
via a newcommand
or def
instruction: One isn't allowed to have numerals (and other non-letter characters) in a TeX macro whose name contains one or more letter characters.– Mico
Aug 7 at 20:10
Possible related here? tex.stackexchange.com/questions/224430/…
– Sebastiano
Aug 7 at 20:14
Possible related here? tex.stackexchange.com/questions/224430/…
– Sebastiano
Aug 7 at 20:14
1
1
@Mico you are correct. I made a mistake in this post. The actual command was sumiton
– Mohammed Shahid
Aug 7 at 20:34
@Mico you are correct. I made a mistake in this post. The actual command was sumiton
– Mohammed Shahid
Aug 7 at 20:34
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
6
down vote
With spaces as delimiters to make it clearer:
documentclassarticle
defsumx #1 #2 to #3sumlimits_#1=#2^#3
begindocument
[sumx i 1 to n]
[sumx j 3 to Q]
enddocument
Wonderful answer +1 surely.
– Sebastiano
Aug 7 at 20:06
Thank you. So in order to use this, I would need to include it in the preamble for every document, right? Is it possible to include this in every document without having to put it in the preamble everytime?
– Mohammed Shahid
Aug 7 at 20:12
2
@MohammedShahid You can compile an own format, but surely that would be more effort than just putting this into every document.
– TeXnician
Aug 7 at 20:14
@TeXnician So would having a document with all of the commands I need and then pasting it into the preamble for every other document be the easiest solution?
– Mohammed Shahid
Aug 7 at 20:21
2
@MohammedShahid It depends on the amount. Dependent on your technical knowledge it might be easier to write a package in the local texmf-tree and include that (if your definitions accumulate) or even create a new format, but yes, usually copy and paste is the easiest method.
– TeXnician
Aug 7 at 20:29
 |Â
show 2 more comments
up vote
1
down vote
Are that enough weird use cases? The default behaviour for empty arguments could be changed though.
I've added the special case that you use only two arguments with a to
in between. In that case it is assumed you did mean sumx#10#2
, you can change the default start (0 in the above example) at the lines marked with % CHANGE DEFAULT START HERE
.
EDIT: Allowing and parsing =
contained in #1
as if both #1
and #2
were given.
EDIT: Also allow an optional =
in between #1
and #2
.
documentclassarticle
makeatletter
newcommandsumx%>>>
%
@ifnextcharbgroup
sumx@braces@a% if sumx is followed by
sumx@spaces@a%
%<<<
newcommandsumx@ifempty[1]%>>>
%
ifrelaxdetokenize#1relax
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
%<<<
newcommandsumx@if@equal@contained@a[1]%>>>
%
sumx@if@equal@contained@b#1=end
%<<<
newcommandsumx@if@equal@contained@b%>>>
defsumx@if@equal@contained@b#1=#2end
%
sumx@ifempty#2@secondoftwo@firstoftwo%
%<<<
newcommandsumx@braces@a[1]%>>>
%
sumx@after@a#1%
%<<<
newcommandsumx@spaces@a%>>>
defsumx@spaces@a #1
%
sumx@after@a#1%
%<<<
newcommandsumx@after@a[1]%>>>
%
sumx@if@equal@contained@a#1
sumx@after@b#1
%
@ifnextchart
sumx@to@a@after@a#1
%
@ifnextchar=
sumx@equal@after@a#1
sumx@after@after@a#1
%
%
%<<<
newcommandsumx@equal@after@a%>>>
defsumx@equal@after@a #1=%
%
sumx@after@after@a#1%
%<<<
newcommandsumx@after@after@a[1]%>>>
%
@ifnextcharbgroup
sumx@braces@b#1
sumx@spaces@b#1%
%<<<
newcommandsumx@to@a@after@a%>>>
defsumx@to@a@after@a #1t%
%
@ifnextcharo
sumx@to@b@after@a#1
sumx@braces@b#1t%
%<<<
newcommandsumx@to@b@after@a%>>>
defsumx@to@b@after@a #1o%
%
sumx@to@c@after@a#10% CHANGE DEFAULT START HERE
%<<<
newcommandsumx@to@c@after@a[2]%>>>
%
@ifnextcharbgroup
sumx@braces@c#1#2
sumx@spaces@c#1#2%
%<<<
newcommandsumx@braces@b[2]%>>>
%
sumx@after@b#1#2%
%<<<
newcommandsumx@spaces@b%>>>
defsumx@spaces@b #1#2
%
sumx@after@b#1#2%
%<<<
newcommandsumx@after@b[2]%>>>
%
@ifnextcharbgroup
sumx@braces@c#1#2
%
@ifnextchart
sumx@to@a#1#2
sumx@spaces@c#1#2%
%
%<<<
newcommandsumx@braces@c[3]%>>>
%
sumx@output#1#2#3%
%<<<
newcommandsumx@spaces@c%>>>
defsumx@spaces@c #1#2#3
%
sumx@output#1#2#3%
%<<<
newcommandsumx@to@a%>>>
defsumx@to@a #1#2t%
%
@ifnextcharo
sumx@to@b#1#2
sumx@output#1#2t%
%<<<
newcommandsumx@to@b%>>>
defsumx@to@b #1#2o%
%
@ifnextcharbgroup
sumx@braces@c#1#2
sumx@spaces@c#1#2%
%<<<
newcommandsumx@output[3]%>>>
%
sum
sumx@ifempty#1#2#3% if all arguments are empty
% do nothing more
%
limits
sumx@ifempty#1#2% if both 1 and 2 are empty
% do nothing here
%
_%
sumx@ifempty#1
#2
%
sumx@ifempty#2
#1
#1=#2%
%
%
%
sumx@ifempty#3^#3
%<<<
makeatother
begindocument
$
sumxij1030
sumxij10to30
sumx ij 10 to 30
sumx ij 10 30
sumx ij 10 30
sumx ij 10 to 30
sumx ij 10 30
sumx ij 10 30
sumx ij 10 30$par
$
sumx i 1 t %no o follows the t so it is the third argument
sumx i 1 t o fail %spaces between "t" and "o" are parsed as "to" :(
sumx110
sumxi
sumxi10
sumx i t a
sumx i to a
sumx i t o a
sumx i to a % if you really need t and o as two arguments group one or both
sumx i=1 to a
sumx i = 1 to a
$
enddocument
The %>>>
and %<<<
are just the fold marks of my VIM, you can ignore them.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
With spaces as delimiters to make it clearer:
documentclassarticle
defsumx #1 #2 to #3sumlimits_#1=#2^#3
begindocument
[sumx i 1 to n]
[sumx j 3 to Q]
enddocument
Wonderful answer +1 surely.
– Sebastiano
Aug 7 at 20:06
Thank you. So in order to use this, I would need to include it in the preamble for every document, right? Is it possible to include this in every document without having to put it in the preamble everytime?
– Mohammed Shahid
Aug 7 at 20:12
2
@MohammedShahid You can compile an own format, but surely that would be more effort than just putting this into every document.
– TeXnician
Aug 7 at 20:14
@TeXnician So would having a document with all of the commands I need and then pasting it into the preamble for every other document be the easiest solution?
– Mohammed Shahid
Aug 7 at 20:21
2
@MohammedShahid It depends on the amount. Dependent on your technical knowledge it might be easier to write a package in the local texmf-tree and include that (if your definitions accumulate) or even create a new format, but yes, usually copy and paste is the easiest method.
– TeXnician
Aug 7 at 20:29
 |Â
show 2 more comments
up vote
6
down vote
With spaces as delimiters to make it clearer:
documentclassarticle
defsumx #1 #2 to #3sumlimits_#1=#2^#3
begindocument
[sumx i 1 to n]
[sumx j 3 to Q]
enddocument
Wonderful answer +1 surely.
– Sebastiano
Aug 7 at 20:06
Thank you. So in order to use this, I would need to include it in the preamble for every document, right? Is it possible to include this in every document without having to put it in the preamble everytime?
– Mohammed Shahid
Aug 7 at 20:12
2
@MohammedShahid You can compile an own format, but surely that would be more effort than just putting this into every document.
– TeXnician
Aug 7 at 20:14
@TeXnician So would having a document with all of the commands I need and then pasting it into the preamble for every other document be the easiest solution?
– Mohammed Shahid
Aug 7 at 20:21
2
@MohammedShahid It depends on the amount. Dependent on your technical knowledge it might be easier to write a package in the local texmf-tree and include that (if your definitions accumulate) or even create a new format, but yes, usually copy and paste is the easiest method.
– TeXnician
Aug 7 at 20:29
 |Â
show 2 more comments
up vote
6
down vote
up vote
6
down vote
With spaces as delimiters to make it clearer:
documentclassarticle
defsumx #1 #2 to #3sumlimits_#1=#2^#3
begindocument
[sumx i 1 to n]
[sumx j 3 to Q]
enddocument
With spaces as delimiters to make it clearer:
documentclassarticle
defsumx #1 #2 to #3sumlimits_#1=#2^#3
begindocument
[sumx i 1 to n]
[sumx j 3 to Q]
enddocument
answered Aug 7 at 20:04


TeXnician
20.2k52880
20.2k52880
Wonderful answer +1 surely.
– Sebastiano
Aug 7 at 20:06
Thank you. So in order to use this, I would need to include it in the preamble for every document, right? Is it possible to include this in every document without having to put it in the preamble everytime?
– Mohammed Shahid
Aug 7 at 20:12
2
@MohammedShahid You can compile an own format, but surely that would be more effort than just putting this into every document.
– TeXnician
Aug 7 at 20:14
@TeXnician So would having a document with all of the commands I need and then pasting it into the preamble for every other document be the easiest solution?
– Mohammed Shahid
Aug 7 at 20:21
2
@MohammedShahid It depends on the amount. Dependent on your technical knowledge it might be easier to write a package in the local texmf-tree and include that (if your definitions accumulate) or even create a new format, but yes, usually copy and paste is the easiest method.
– TeXnician
Aug 7 at 20:29
 |Â
show 2 more comments
Wonderful answer +1 surely.
– Sebastiano
Aug 7 at 20:06
Thank you. So in order to use this, I would need to include it in the preamble for every document, right? Is it possible to include this in every document without having to put it in the preamble everytime?
– Mohammed Shahid
Aug 7 at 20:12
2
@MohammedShahid You can compile an own format, but surely that would be more effort than just putting this into every document.
– TeXnician
Aug 7 at 20:14
@TeXnician So would having a document with all of the commands I need and then pasting it into the preamble for every other document be the easiest solution?
– Mohammed Shahid
Aug 7 at 20:21
2
@MohammedShahid It depends on the amount. Dependent on your technical knowledge it might be easier to write a package in the local texmf-tree and include that (if your definitions accumulate) or even create a new format, but yes, usually copy and paste is the easiest method.
– TeXnician
Aug 7 at 20:29
Wonderful answer +1 surely.
– Sebastiano
Aug 7 at 20:06
Wonderful answer +1 surely.
– Sebastiano
Aug 7 at 20:06
Thank you. So in order to use this, I would need to include it in the preamble for every document, right? Is it possible to include this in every document without having to put it in the preamble everytime?
– Mohammed Shahid
Aug 7 at 20:12
Thank you. So in order to use this, I would need to include it in the preamble for every document, right? Is it possible to include this in every document without having to put it in the preamble everytime?
– Mohammed Shahid
Aug 7 at 20:12
2
2
@MohammedShahid You can compile an own format, but surely that would be more effort than just putting this into every document.
– TeXnician
Aug 7 at 20:14
@MohammedShahid You can compile an own format, but surely that would be more effort than just putting this into every document.
– TeXnician
Aug 7 at 20:14
@TeXnician So would having a document with all of the commands I need and then pasting it into the preamble for every other document be the easiest solution?
– Mohammed Shahid
Aug 7 at 20:21
@TeXnician So would having a document with all of the commands I need and then pasting it into the preamble for every other document be the easiest solution?
– Mohammed Shahid
Aug 7 at 20:21
2
2
@MohammedShahid It depends on the amount. Dependent on your technical knowledge it might be easier to write a package in the local texmf-tree and include that (if your definitions accumulate) or even create a new format, but yes, usually copy and paste is the easiest method.
– TeXnician
Aug 7 at 20:29
@MohammedShahid It depends on the amount. Dependent on your technical knowledge it might be easier to write a package in the local texmf-tree and include that (if your definitions accumulate) or even create a new format, but yes, usually copy and paste is the easiest method.
– TeXnician
Aug 7 at 20:29
 |Â
show 2 more comments
up vote
1
down vote
Are that enough weird use cases? The default behaviour for empty arguments could be changed though.
I've added the special case that you use only two arguments with a to
in between. In that case it is assumed you did mean sumx#10#2
, you can change the default start (0 in the above example) at the lines marked with % CHANGE DEFAULT START HERE
.
EDIT: Allowing and parsing =
contained in #1
as if both #1
and #2
were given.
EDIT: Also allow an optional =
in between #1
and #2
.
documentclassarticle
makeatletter
newcommandsumx%>>>
%
@ifnextcharbgroup
sumx@braces@a% if sumx is followed by
sumx@spaces@a%
%<<<
newcommandsumx@ifempty[1]%>>>
%
ifrelaxdetokenize#1relax
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
%<<<
newcommandsumx@if@equal@contained@a[1]%>>>
%
sumx@if@equal@contained@b#1=end
%<<<
newcommandsumx@if@equal@contained@b%>>>
defsumx@if@equal@contained@b#1=#2end
%
sumx@ifempty#2@secondoftwo@firstoftwo%
%<<<
newcommandsumx@braces@a[1]%>>>
%
sumx@after@a#1%
%<<<
newcommandsumx@spaces@a%>>>
defsumx@spaces@a #1
%
sumx@after@a#1%
%<<<
newcommandsumx@after@a[1]%>>>
%
sumx@if@equal@contained@a#1
sumx@after@b#1
%
@ifnextchart
sumx@to@a@after@a#1
%
@ifnextchar=
sumx@equal@after@a#1
sumx@after@after@a#1
%
%
%<<<
newcommandsumx@equal@after@a%>>>
defsumx@equal@after@a #1=%
%
sumx@after@after@a#1%
%<<<
newcommandsumx@after@after@a[1]%>>>
%
@ifnextcharbgroup
sumx@braces@b#1
sumx@spaces@b#1%
%<<<
newcommandsumx@to@a@after@a%>>>
defsumx@to@a@after@a #1t%
%
@ifnextcharo
sumx@to@b@after@a#1
sumx@braces@b#1t%
%<<<
newcommandsumx@to@b@after@a%>>>
defsumx@to@b@after@a #1o%
%
sumx@to@c@after@a#10% CHANGE DEFAULT START HERE
%<<<
newcommandsumx@to@c@after@a[2]%>>>
%
@ifnextcharbgroup
sumx@braces@c#1#2
sumx@spaces@c#1#2%
%<<<
newcommandsumx@braces@b[2]%>>>
%
sumx@after@b#1#2%
%<<<
newcommandsumx@spaces@b%>>>
defsumx@spaces@b #1#2
%
sumx@after@b#1#2%
%<<<
newcommandsumx@after@b[2]%>>>
%
@ifnextcharbgroup
sumx@braces@c#1#2
%
@ifnextchart
sumx@to@a#1#2
sumx@spaces@c#1#2%
%
%<<<
newcommandsumx@braces@c[3]%>>>
%
sumx@output#1#2#3%
%<<<
newcommandsumx@spaces@c%>>>
defsumx@spaces@c #1#2#3
%
sumx@output#1#2#3%
%<<<
newcommandsumx@to@a%>>>
defsumx@to@a #1#2t%
%
@ifnextcharo
sumx@to@b#1#2
sumx@output#1#2t%
%<<<
newcommandsumx@to@b%>>>
defsumx@to@b #1#2o%
%
@ifnextcharbgroup
sumx@braces@c#1#2
sumx@spaces@c#1#2%
%<<<
newcommandsumx@output[3]%>>>
%
sum
sumx@ifempty#1#2#3% if all arguments are empty
% do nothing more
%
limits
sumx@ifempty#1#2% if both 1 and 2 are empty
% do nothing here
%
_%
sumx@ifempty#1
#2
%
sumx@ifempty#2
#1
#1=#2%
%
%
%
sumx@ifempty#3^#3
%<<<
makeatother
begindocument
$
sumxij1030
sumxij10to30
sumx ij 10 to 30
sumx ij 10 30
sumx ij 10 30
sumx ij 10 to 30
sumx ij 10 30
sumx ij 10 30
sumx ij 10 30$par
$
sumx i 1 t %no o follows the t so it is the third argument
sumx i 1 t o fail %spaces between "t" and "o" are parsed as "to" :(
sumx110
sumxi
sumxi10
sumx i t a
sumx i to a
sumx i t o a
sumx i to a % if you really need t and o as two arguments group one or both
sumx i=1 to a
sumx i = 1 to a
$
enddocument
The %>>>
and %<<<
are just the fold marks of my VIM, you can ignore them.
add a comment |Â
up vote
1
down vote
Are that enough weird use cases? The default behaviour for empty arguments could be changed though.
I've added the special case that you use only two arguments with a to
in between. In that case it is assumed you did mean sumx#10#2
, you can change the default start (0 in the above example) at the lines marked with % CHANGE DEFAULT START HERE
.
EDIT: Allowing and parsing =
contained in #1
as if both #1
and #2
were given.
EDIT: Also allow an optional =
in between #1
and #2
.
documentclassarticle
makeatletter
newcommandsumx%>>>
%
@ifnextcharbgroup
sumx@braces@a% if sumx is followed by
sumx@spaces@a%
%<<<
newcommandsumx@ifempty[1]%>>>
%
ifrelaxdetokenize#1relax
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
%<<<
newcommandsumx@if@equal@contained@a[1]%>>>
%
sumx@if@equal@contained@b#1=end
%<<<
newcommandsumx@if@equal@contained@b%>>>
defsumx@if@equal@contained@b#1=#2end
%
sumx@ifempty#2@secondoftwo@firstoftwo%
%<<<
newcommandsumx@braces@a[1]%>>>
%
sumx@after@a#1%
%<<<
newcommandsumx@spaces@a%>>>
defsumx@spaces@a #1
%
sumx@after@a#1%
%<<<
newcommandsumx@after@a[1]%>>>
%
sumx@if@equal@contained@a#1
sumx@after@b#1
%
@ifnextchart
sumx@to@a@after@a#1
%
@ifnextchar=
sumx@equal@after@a#1
sumx@after@after@a#1
%
%
%<<<
newcommandsumx@equal@after@a%>>>
defsumx@equal@after@a #1=%
%
sumx@after@after@a#1%
%<<<
newcommandsumx@after@after@a[1]%>>>
%
@ifnextcharbgroup
sumx@braces@b#1
sumx@spaces@b#1%
%<<<
newcommandsumx@to@a@after@a%>>>
defsumx@to@a@after@a #1t%
%
@ifnextcharo
sumx@to@b@after@a#1
sumx@braces@b#1t%
%<<<
newcommandsumx@to@b@after@a%>>>
defsumx@to@b@after@a #1o%
%
sumx@to@c@after@a#10% CHANGE DEFAULT START HERE
%<<<
newcommandsumx@to@c@after@a[2]%>>>
%
@ifnextcharbgroup
sumx@braces@c#1#2
sumx@spaces@c#1#2%
%<<<
newcommandsumx@braces@b[2]%>>>
%
sumx@after@b#1#2%
%<<<
newcommandsumx@spaces@b%>>>
defsumx@spaces@b #1#2
%
sumx@after@b#1#2%
%<<<
newcommandsumx@after@b[2]%>>>
%
@ifnextcharbgroup
sumx@braces@c#1#2
%
@ifnextchart
sumx@to@a#1#2
sumx@spaces@c#1#2%
%
%<<<
newcommandsumx@braces@c[3]%>>>
%
sumx@output#1#2#3%
%<<<
newcommandsumx@spaces@c%>>>
defsumx@spaces@c #1#2#3
%
sumx@output#1#2#3%
%<<<
newcommandsumx@to@a%>>>
defsumx@to@a #1#2t%
%
@ifnextcharo
sumx@to@b#1#2
sumx@output#1#2t%
%<<<
newcommandsumx@to@b%>>>
defsumx@to@b #1#2o%
%
@ifnextcharbgroup
sumx@braces@c#1#2
sumx@spaces@c#1#2%
%<<<
newcommandsumx@output[3]%>>>
%
sum
sumx@ifempty#1#2#3% if all arguments are empty
% do nothing more
%
limits
sumx@ifempty#1#2% if both 1 and 2 are empty
% do nothing here
%
_%
sumx@ifempty#1
#2
%
sumx@ifempty#2
#1
#1=#2%
%
%
%
sumx@ifempty#3^#3
%<<<
makeatother
begindocument
$
sumxij1030
sumxij10to30
sumx ij 10 to 30
sumx ij 10 30
sumx ij 10 30
sumx ij 10 to 30
sumx ij 10 30
sumx ij 10 30
sumx ij 10 30$par
$
sumx i 1 t %no o follows the t so it is the third argument
sumx i 1 t o fail %spaces between "t" and "o" are parsed as "to" :(
sumx110
sumxi
sumxi10
sumx i t a
sumx i to a
sumx i t o a
sumx i to a % if you really need t and o as two arguments group one or both
sumx i=1 to a
sumx i = 1 to a
$
enddocument
The %>>>
and %<<<
are just the fold marks of my VIM, you can ignore them.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Are that enough weird use cases? The default behaviour for empty arguments could be changed though.
I've added the special case that you use only two arguments with a to
in between. In that case it is assumed you did mean sumx#10#2
, you can change the default start (0 in the above example) at the lines marked with % CHANGE DEFAULT START HERE
.
EDIT: Allowing and parsing =
contained in #1
as if both #1
and #2
were given.
EDIT: Also allow an optional =
in between #1
and #2
.
documentclassarticle
makeatletter
newcommandsumx%>>>
%
@ifnextcharbgroup
sumx@braces@a% if sumx is followed by
sumx@spaces@a%
%<<<
newcommandsumx@ifempty[1]%>>>
%
ifrelaxdetokenize#1relax
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
%<<<
newcommandsumx@if@equal@contained@a[1]%>>>
%
sumx@if@equal@contained@b#1=end
%<<<
newcommandsumx@if@equal@contained@b%>>>
defsumx@if@equal@contained@b#1=#2end
%
sumx@ifempty#2@secondoftwo@firstoftwo%
%<<<
newcommandsumx@braces@a[1]%>>>
%
sumx@after@a#1%
%<<<
newcommandsumx@spaces@a%>>>
defsumx@spaces@a #1
%
sumx@after@a#1%
%<<<
newcommandsumx@after@a[1]%>>>
%
sumx@if@equal@contained@a#1
sumx@after@b#1
%
@ifnextchart
sumx@to@a@after@a#1
%
@ifnextchar=
sumx@equal@after@a#1
sumx@after@after@a#1
%
%
%<<<
newcommandsumx@equal@after@a%>>>
defsumx@equal@after@a #1=%
%
sumx@after@after@a#1%
%<<<
newcommandsumx@after@after@a[1]%>>>
%
@ifnextcharbgroup
sumx@braces@b#1
sumx@spaces@b#1%
%<<<
newcommandsumx@to@a@after@a%>>>
defsumx@to@a@after@a #1t%
%
@ifnextcharo
sumx@to@b@after@a#1
sumx@braces@b#1t%
%<<<
newcommandsumx@to@b@after@a%>>>
defsumx@to@b@after@a #1o%
%
sumx@to@c@after@a#10% CHANGE DEFAULT START HERE
%<<<
newcommandsumx@to@c@after@a[2]%>>>
%
@ifnextcharbgroup
sumx@braces@c#1#2
sumx@spaces@c#1#2%
%<<<
newcommandsumx@braces@b[2]%>>>
%
sumx@after@b#1#2%
%<<<
newcommandsumx@spaces@b%>>>
defsumx@spaces@b #1#2
%
sumx@after@b#1#2%
%<<<
newcommandsumx@after@b[2]%>>>
%
@ifnextcharbgroup
sumx@braces@c#1#2
%
@ifnextchart
sumx@to@a#1#2
sumx@spaces@c#1#2%
%
%<<<
newcommandsumx@braces@c[3]%>>>
%
sumx@output#1#2#3%
%<<<
newcommandsumx@spaces@c%>>>
defsumx@spaces@c #1#2#3
%
sumx@output#1#2#3%
%<<<
newcommandsumx@to@a%>>>
defsumx@to@a #1#2t%
%
@ifnextcharo
sumx@to@b#1#2
sumx@output#1#2t%
%<<<
newcommandsumx@to@b%>>>
defsumx@to@b #1#2o%
%
@ifnextcharbgroup
sumx@braces@c#1#2
sumx@spaces@c#1#2%
%<<<
newcommandsumx@output[3]%>>>
%
sum
sumx@ifempty#1#2#3% if all arguments are empty
% do nothing more
%
limits
sumx@ifempty#1#2% if both 1 and 2 are empty
% do nothing here
%
_%
sumx@ifempty#1
#2
%
sumx@ifempty#2
#1
#1=#2%
%
%
%
sumx@ifempty#3^#3
%<<<
makeatother
begindocument
$
sumxij1030
sumxij10to30
sumx ij 10 to 30
sumx ij 10 30
sumx ij 10 30
sumx ij 10 to 30
sumx ij 10 30
sumx ij 10 30
sumx ij 10 30$par
$
sumx i 1 t %no o follows the t so it is the third argument
sumx i 1 t o fail %spaces between "t" and "o" are parsed as "to" :(
sumx110
sumxi
sumxi10
sumx i t a
sumx i to a
sumx i t o a
sumx i to a % if you really need t and o as two arguments group one or both
sumx i=1 to a
sumx i = 1 to a
$
enddocument
The %>>>
and %<<<
are just the fold marks of my VIM, you can ignore them.
Are that enough weird use cases? The default behaviour for empty arguments could be changed though.
I've added the special case that you use only two arguments with a to
in between. In that case it is assumed you did mean sumx#10#2
, you can change the default start (0 in the above example) at the lines marked with % CHANGE DEFAULT START HERE
.
EDIT: Allowing and parsing =
contained in #1
as if both #1
and #2
were given.
EDIT: Also allow an optional =
in between #1
and #2
.
documentclassarticle
makeatletter
newcommandsumx%>>>
%
@ifnextcharbgroup
sumx@braces@a% if sumx is followed by
sumx@spaces@a%
%<<<
newcommandsumx@ifempty[1]%>>>
%
ifrelaxdetokenize#1relax
expandafter@firstoftwo
else
expandafter@secondoftwo
fi
%<<<
newcommandsumx@if@equal@contained@a[1]%>>>
%
sumx@if@equal@contained@b#1=end
%<<<
newcommandsumx@if@equal@contained@b%>>>
defsumx@if@equal@contained@b#1=#2end
%
sumx@ifempty#2@secondoftwo@firstoftwo%
%<<<
newcommandsumx@braces@a[1]%>>>
%
sumx@after@a#1%
%<<<
newcommandsumx@spaces@a%>>>
defsumx@spaces@a #1
%
sumx@after@a#1%
%<<<
newcommandsumx@after@a[1]%>>>
%
sumx@if@equal@contained@a#1
sumx@after@b#1
%
@ifnextchart
sumx@to@a@after@a#1
%
@ifnextchar=
sumx@equal@after@a#1
sumx@after@after@a#1
%
%
%<<<
newcommandsumx@equal@after@a%>>>
defsumx@equal@after@a #1=%
%
sumx@after@after@a#1%
%<<<
newcommandsumx@after@after@a[1]%>>>
%
@ifnextcharbgroup
sumx@braces@b#1
sumx@spaces@b#1%
%<<<
newcommandsumx@to@a@after@a%>>>
defsumx@to@a@after@a #1t%
%
@ifnextcharo
sumx@to@b@after@a#1
sumx@braces@b#1t%
%<<<
newcommandsumx@to@b@after@a%>>>
defsumx@to@b@after@a #1o%
%
sumx@to@c@after@a#10% CHANGE DEFAULT START HERE
%<<<
newcommandsumx@to@c@after@a[2]%>>>
%
@ifnextcharbgroup
sumx@braces@c#1#2
sumx@spaces@c#1#2%
%<<<
newcommandsumx@braces@b[2]%>>>
%
sumx@after@b#1#2%
%<<<
newcommandsumx@spaces@b%>>>
defsumx@spaces@b #1#2
%
sumx@after@b#1#2%
%<<<
newcommandsumx@after@b[2]%>>>
%
@ifnextcharbgroup
sumx@braces@c#1#2
%
@ifnextchart
sumx@to@a#1#2
sumx@spaces@c#1#2%
%
%<<<
newcommandsumx@braces@c[3]%>>>
%
sumx@output#1#2#3%
%<<<
newcommandsumx@spaces@c%>>>
defsumx@spaces@c #1#2#3
%
sumx@output#1#2#3%
%<<<
newcommandsumx@to@a%>>>
defsumx@to@a #1#2t%
%
@ifnextcharo
sumx@to@b#1#2
sumx@output#1#2t%
%<<<
newcommandsumx@to@b%>>>
defsumx@to@b #1#2o%
%
@ifnextcharbgroup
sumx@braces@c#1#2
sumx@spaces@c#1#2%
%<<<
newcommandsumx@output[3]%>>>
%
sum
sumx@ifempty#1#2#3% if all arguments are empty
% do nothing more
%
limits
sumx@ifempty#1#2% if both 1 and 2 are empty
% do nothing here
%
_%
sumx@ifempty#1
#2
%
sumx@ifempty#2
#1
#1=#2%
%
%
%
sumx@ifempty#3^#3
%<<<
makeatother
begindocument
$
sumxij1030
sumxij10to30
sumx ij 10 to 30
sumx ij 10 30
sumx ij 10 30
sumx ij 10 to 30
sumx ij 10 30
sumx ij 10 30
sumx ij 10 30$par
$
sumx i 1 t %no o follows the t so it is the third argument
sumx i 1 t o fail %spaces between "t" and "o" are parsed as "to" :(
sumx110
sumxi
sumxi10
sumx i t a
sumx i to a
sumx i t o a
sumx i to a % if you really need t and o as two arguments group one or both
sumx i=1 to a
sumx i = 1 to a
$
enddocument
The %>>>
and %<<<
are just the fold marks of my VIM, you can ignore them.
edited Aug 8 at 1:55
answered Aug 8 at 1:25


Skillmon
17.1k11534
17.1k11534
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f445033%2fhow-can-this-command-be-altered-to-work-in-more-cases%23new-answer', 'question_page');
);
Post as a guest
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
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
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
3
Surely you must be getting an error message when you try to define a macro called
sumi1ton
via anewcommand
ordef
instruction: One isn't allowed to have numerals (and other non-letter characters) in a TeX macro whose name contains one or more letter characters.– Mico
Aug 7 at 20:10
Possible related here? tex.stackexchange.com/questions/224430/…
– Sebastiano
Aug 7 at 20:14
1
@Mico you are correct. I made a mistake in this post. The actual command was sumiton
– Mohammed Shahid
Aug 7 at 20:34