Replacing function, how to use + as delimiter
I am using a function for summing numbers like in paper and my input at arguments is like this:
showsum{123+456}
I need to replace the custom function with other from package xlop
- opadd
.
I want the same function name but there are 2 separated arguments in opadd
and I want to use {123+456}
for opadd
arguments. How to make this delimiter?
documentclass{article}
usepackage{xlop}
usepackage{amsmath}
usepackage{setspace}
newcommand{showsum}[2][c]{%
$edeforiginalplusmathcode{themathcode`+}%
begingrouplccode`~=`+ lowercase{endgroupdef~}{\mathcharoriginalplusmathcode&}%
edeforiginalminusmathcode{themathcode`-}%
begingrouplccode`~=`- lowercase{endgroupdef~}{\mathcharoriginalminusmathcode&}%
mathcode`+ "8000
mathcode`- "8000
begin{array}[#1]{@{}r@{;}r@{}}
& #2 \
hline
& thenumexpr#2relax
end{array}%
$%
}
begin{document}
Original function(don't need anymore)showsum{123+456}
\
\
Replacement needed opadd{123}{456}
end{document}
delimiters
add a comment |
I am using a function for summing numbers like in paper and my input at arguments is like this:
showsum{123+456}
I need to replace the custom function with other from package xlop
- opadd
.
I want the same function name but there are 2 separated arguments in opadd
and I want to use {123+456}
for opadd
arguments. How to make this delimiter?
documentclass{article}
usepackage{xlop}
usepackage{amsmath}
usepackage{setspace}
newcommand{showsum}[2][c]{%
$edeforiginalplusmathcode{themathcode`+}%
begingrouplccode`~=`+ lowercase{endgroupdef~}{\mathcharoriginalplusmathcode&}%
edeforiginalminusmathcode{themathcode`-}%
begingrouplccode`~=`- lowercase{endgroupdef~}{\mathcharoriginalminusmathcode&}%
mathcode`+ "8000
mathcode`- "8000
begin{array}[#1]{@{}r@{;}r@{}}
& #2 \
hline
& thenumexpr#2relax
end{array}%
$%
}
begin{document}
Original function(don't need anymore)showsum{123+456}
\
\
Replacement needed opadd{123}{456}
end{document}
delimiters
add a comment |
I am using a function for summing numbers like in paper and my input at arguments is like this:
showsum{123+456}
I need to replace the custom function with other from package xlop
- opadd
.
I want the same function name but there are 2 separated arguments in opadd
and I want to use {123+456}
for opadd
arguments. How to make this delimiter?
documentclass{article}
usepackage{xlop}
usepackage{amsmath}
usepackage{setspace}
newcommand{showsum}[2][c]{%
$edeforiginalplusmathcode{themathcode`+}%
begingrouplccode`~=`+ lowercase{endgroupdef~}{\mathcharoriginalplusmathcode&}%
edeforiginalminusmathcode{themathcode`-}%
begingrouplccode`~=`- lowercase{endgroupdef~}{\mathcharoriginalminusmathcode&}%
mathcode`+ "8000
mathcode`- "8000
begin{array}[#1]{@{}r@{;}r@{}}
& #2 \
hline
& thenumexpr#2relax
end{array}%
$%
}
begin{document}
Original function(don't need anymore)showsum{123+456}
\
\
Replacement needed opadd{123}{456}
end{document}
delimiters
I am using a function for summing numbers like in paper and my input at arguments is like this:
showsum{123+456}
I need to replace the custom function with other from package xlop
- opadd
.
I want the same function name but there are 2 separated arguments in opadd
and I want to use {123+456}
for opadd
arguments. How to make this delimiter?
documentclass{article}
usepackage{xlop}
usepackage{amsmath}
usepackage{setspace}
newcommand{showsum}[2][c]{%
$edeforiginalplusmathcode{themathcode`+}%
begingrouplccode`~=`+ lowercase{endgroupdef~}{\mathcharoriginalplusmathcode&}%
edeforiginalminusmathcode{themathcode`-}%
begingrouplccode`~=`- lowercase{endgroupdef~}{\mathcharoriginalminusmathcode&}%
mathcode`+ "8000
mathcode`- "8000
begin{array}[#1]{@{}r@{;}r@{}}
& #2 \
hline
& thenumexpr#2relax
end{array}%
$%
}
begin{document}
Original function(don't need anymore)showsum{123+456}
\
\
Replacement needed opadd{123}{456}
end{document}
delimiters
delimiters
asked Jan 6 at 13:26
Simeon SimeonovSimeon Simeonov
3356
3356
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can use a macro with delimited arguments:
documentclass{article}
usepackage{xlop}
newcommand{showsum}[1]{doshowsum#1doshowsum}
defdoshowsum#1+#2doshowsum{opadd{#1}{#2}}
begin{document}
showsum{123+456} qquad opadd{123}{456}
end{document}
You can also support subtraction:
documentclass{article}
usepackage{xparse}
usepackage{xlop}
ExplSyntaxOn
NewDocumentCommand{showsum}{m}
{
regex_match:nnTF { + } { #1 }
{
simeon_showsum:nnn { #1 } { + } { opadd }
}
{
simeon_showsum:nnn { #1 } { - } { opsub }
}
}
cs_new_protected:Nn simeon_showsum:nnn
{
tl_set:Nn l_tmpa_tl { #1 }
% remove all spaces
regex_replace_all:nnN { s } { } l_tmpa_tl
% replace <number>+<number> with opadd{<number>}{<number>} or
% replace <number>-<number> with opsub{<number>}{<number>}
regex_replace_once:nnN
{ A ([^#2]*) #2 (.*) Z }
{ c{#3}cB{1cE}cB{2cE} }
l_tmpa_tl
% deliver the result
tl_use:N l_tmpa_tl
}
ExplSyntaxOff
begin{document}
showsum{123+456} qquad showsum{456-123}
end{document}
Great! I needed subtraction aswell. I have new problem. My functions are showsum{123 + 456} with spaces between +/- and numbers. Is there any fast solution?
– Simeon Simeonov
Jan 6 at 15:01
1
@SimeonSimeonov I added removal of spaces
– egreg
Jan 6 at 15:07
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%2f468819%2freplacing-function-how-to-use-as-delimiter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use a macro with delimited arguments:
documentclass{article}
usepackage{xlop}
newcommand{showsum}[1]{doshowsum#1doshowsum}
defdoshowsum#1+#2doshowsum{opadd{#1}{#2}}
begin{document}
showsum{123+456} qquad opadd{123}{456}
end{document}
You can also support subtraction:
documentclass{article}
usepackage{xparse}
usepackage{xlop}
ExplSyntaxOn
NewDocumentCommand{showsum}{m}
{
regex_match:nnTF { + } { #1 }
{
simeon_showsum:nnn { #1 } { + } { opadd }
}
{
simeon_showsum:nnn { #1 } { - } { opsub }
}
}
cs_new_protected:Nn simeon_showsum:nnn
{
tl_set:Nn l_tmpa_tl { #1 }
% remove all spaces
regex_replace_all:nnN { s } { } l_tmpa_tl
% replace <number>+<number> with opadd{<number>}{<number>} or
% replace <number>-<number> with opsub{<number>}{<number>}
regex_replace_once:nnN
{ A ([^#2]*) #2 (.*) Z }
{ c{#3}cB{1cE}cB{2cE} }
l_tmpa_tl
% deliver the result
tl_use:N l_tmpa_tl
}
ExplSyntaxOff
begin{document}
showsum{123+456} qquad showsum{456-123}
end{document}
Great! I needed subtraction aswell. I have new problem. My functions are showsum{123 + 456} with spaces between +/- and numbers. Is there any fast solution?
– Simeon Simeonov
Jan 6 at 15:01
1
@SimeonSimeonov I added removal of spaces
– egreg
Jan 6 at 15:07
add a comment |
You can use a macro with delimited arguments:
documentclass{article}
usepackage{xlop}
newcommand{showsum}[1]{doshowsum#1doshowsum}
defdoshowsum#1+#2doshowsum{opadd{#1}{#2}}
begin{document}
showsum{123+456} qquad opadd{123}{456}
end{document}
You can also support subtraction:
documentclass{article}
usepackage{xparse}
usepackage{xlop}
ExplSyntaxOn
NewDocumentCommand{showsum}{m}
{
regex_match:nnTF { + } { #1 }
{
simeon_showsum:nnn { #1 } { + } { opadd }
}
{
simeon_showsum:nnn { #1 } { - } { opsub }
}
}
cs_new_protected:Nn simeon_showsum:nnn
{
tl_set:Nn l_tmpa_tl { #1 }
% remove all spaces
regex_replace_all:nnN { s } { } l_tmpa_tl
% replace <number>+<number> with opadd{<number>}{<number>} or
% replace <number>-<number> with opsub{<number>}{<number>}
regex_replace_once:nnN
{ A ([^#2]*) #2 (.*) Z }
{ c{#3}cB{1cE}cB{2cE} }
l_tmpa_tl
% deliver the result
tl_use:N l_tmpa_tl
}
ExplSyntaxOff
begin{document}
showsum{123+456} qquad showsum{456-123}
end{document}
Great! I needed subtraction aswell. I have new problem. My functions are showsum{123 + 456} with spaces between +/- and numbers. Is there any fast solution?
– Simeon Simeonov
Jan 6 at 15:01
1
@SimeonSimeonov I added removal of spaces
– egreg
Jan 6 at 15:07
add a comment |
You can use a macro with delimited arguments:
documentclass{article}
usepackage{xlop}
newcommand{showsum}[1]{doshowsum#1doshowsum}
defdoshowsum#1+#2doshowsum{opadd{#1}{#2}}
begin{document}
showsum{123+456} qquad opadd{123}{456}
end{document}
You can also support subtraction:
documentclass{article}
usepackage{xparse}
usepackage{xlop}
ExplSyntaxOn
NewDocumentCommand{showsum}{m}
{
regex_match:nnTF { + } { #1 }
{
simeon_showsum:nnn { #1 } { + } { opadd }
}
{
simeon_showsum:nnn { #1 } { - } { opsub }
}
}
cs_new_protected:Nn simeon_showsum:nnn
{
tl_set:Nn l_tmpa_tl { #1 }
% remove all spaces
regex_replace_all:nnN { s } { } l_tmpa_tl
% replace <number>+<number> with opadd{<number>}{<number>} or
% replace <number>-<number> with opsub{<number>}{<number>}
regex_replace_once:nnN
{ A ([^#2]*) #2 (.*) Z }
{ c{#3}cB{1cE}cB{2cE} }
l_tmpa_tl
% deliver the result
tl_use:N l_tmpa_tl
}
ExplSyntaxOff
begin{document}
showsum{123+456} qquad showsum{456-123}
end{document}
You can use a macro with delimited arguments:
documentclass{article}
usepackage{xlop}
newcommand{showsum}[1]{doshowsum#1doshowsum}
defdoshowsum#1+#2doshowsum{opadd{#1}{#2}}
begin{document}
showsum{123+456} qquad opadd{123}{456}
end{document}
You can also support subtraction:
documentclass{article}
usepackage{xparse}
usepackage{xlop}
ExplSyntaxOn
NewDocumentCommand{showsum}{m}
{
regex_match:nnTF { + } { #1 }
{
simeon_showsum:nnn { #1 } { + } { opadd }
}
{
simeon_showsum:nnn { #1 } { - } { opsub }
}
}
cs_new_protected:Nn simeon_showsum:nnn
{
tl_set:Nn l_tmpa_tl { #1 }
% remove all spaces
regex_replace_all:nnN { s } { } l_tmpa_tl
% replace <number>+<number> with opadd{<number>}{<number>} or
% replace <number>-<number> with opsub{<number>}{<number>}
regex_replace_once:nnN
{ A ([^#2]*) #2 (.*) Z }
{ c{#3}cB{1cE}cB{2cE} }
l_tmpa_tl
% deliver the result
tl_use:N l_tmpa_tl
}
ExplSyntaxOff
begin{document}
showsum{123+456} qquad showsum{456-123}
end{document}
edited Jan 6 at 15:07
answered Jan 6 at 13:41
egregegreg
712k8618913175
712k8618913175
Great! I needed subtraction aswell. I have new problem. My functions are showsum{123 + 456} with spaces between +/- and numbers. Is there any fast solution?
– Simeon Simeonov
Jan 6 at 15:01
1
@SimeonSimeonov I added removal of spaces
– egreg
Jan 6 at 15:07
add a comment |
Great! I needed subtraction aswell. I have new problem. My functions are showsum{123 + 456} with spaces between +/- and numbers. Is there any fast solution?
– Simeon Simeonov
Jan 6 at 15:01
1
@SimeonSimeonov I added removal of spaces
– egreg
Jan 6 at 15:07
Great! I needed subtraction aswell. I have new problem. My functions are showsum{123 + 456} with spaces between +/- and numbers. Is there any fast solution?
– Simeon Simeonov
Jan 6 at 15:01
Great! I needed subtraction aswell. I have new problem. My functions are showsum{123 + 456} with spaces between +/- and numbers. Is there any fast solution?
– Simeon Simeonov
Jan 6 at 15:01
1
1
@SimeonSimeonov I added removal of spaces
– egreg
Jan 6 at 15:07
@SimeonSimeonov I added removal of spaces
– egreg
Jan 6 at 15:07
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%2f468819%2freplacing-function-how-to-use-as-delimiter%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