Summing numbers like paper calculation
I need to make many of these. If it s possible numbers to be aligned right. How to do that?
tables macros
add a comment |
I need to make many of these. If it s possible numbers to be aligned right. How to do that?
tables macros
6
Welcome to TeX.SE. It would be helpful if you composed a fully compilable MWE includingdocumentclass
and the appropriate packages that sets up the problem. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem.
– Peter Grill
Nov 15 '18 at 7:46
1
yes, it is possible (to align to where you like to have). for example by use of anąrray or a
tabular`. but first show us, what you try so far.
– Zarko
Nov 15 '18 at 7:54
Is your question purely about typesetting, or is it also about performing the summation operations?
– Mico
Nov 15 '18 at 8:06
Duplicate? tex.stackexchange.com/questions/337840/…, tex.stackexchange.com/questions/11702/…, tex.stackexchange.com/questions/219090/…
– Steven B. Segletes
Nov 15 '18 at 10:29
add a comment |
I need to make many of these. If it s possible numbers to be aligned right. How to do that?
tables macros
I need to make many of these. If it s possible numbers to be aligned right. How to do that?
tables macros
tables macros
edited Nov 21 '18 at 10:05
Andrew Swann
78k9133333
78k9133333
asked Nov 15 '18 at 7:37
Simeon SimeonovSimeon Simeonov
4457
4457
6
Welcome to TeX.SE. It would be helpful if you composed a fully compilable MWE includingdocumentclass
and the appropriate packages that sets up the problem. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem.
– Peter Grill
Nov 15 '18 at 7:46
1
yes, it is possible (to align to where you like to have). for example by use of anąrray or a
tabular`. but first show us, what you try so far.
– Zarko
Nov 15 '18 at 7:54
Is your question purely about typesetting, or is it also about performing the summation operations?
– Mico
Nov 15 '18 at 8:06
Duplicate? tex.stackexchange.com/questions/337840/…, tex.stackexchange.com/questions/11702/…, tex.stackexchange.com/questions/219090/…
– Steven B. Segletes
Nov 15 '18 at 10:29
add a comment |
6
Welcome to TeX.SE. It would be helpful if you composed a fully compilable MWE includingdocumentclass
and the appropriate packages that sets up the problem. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem.
– Peter Grill
Nov 15 '18 at 7:46
1
yes, it is possible (to align to where you like to have). for example by use of anąrray or a
tabular`. but first show us, what you try so far.
– Zarko
Nov 15 '18 at 7:54
Is your question purely about typesetting, or is it also about performing the summation operations?
– Mico
Nov 15 '18 at 8:06
Duplicate? tex.stackexchange.com/questions/337840/…, tex.stackexchange.com/questions/11702/…, tex.stackexchange.com/questions/219090/…
– Steven B. Segletes
Nov 15 '18 at 10:29
6
6
Welcome to TeX.SE. It would be helpful if you composed a fully compilable MWE including
documentclass
and the appropriate packages that sets up the problem. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem.– Peter Grill
Nov 15 '18 at 7:46
Welcome to TeX.SE. It would be helpful if you composed a fully compilable MWE including
documentclass
and the appropriate packages that sets up the problem. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem.– Peter Grill
Nov 15 '18 at 7:46
1
1
yes, it is possible (to align to where you like to have). for example by use of an
ąrray or a
tabular`. but first show us, what you try so far.– Zarko
Nov 15 '18 at 7:54
yes, it is possible (to align to where you like to have). for example by use of an
ąrray or a
tabular`. but first show us, what you try so far.– Zarko
Nov 15 '18 at 7:54
Is your question purely about typesetting, or is it also about performing the summation operations?
– Mico
Nov 15 '18 at 8:06
Is your question purely about typesetting, or is it also about performing the summation operations?
– Mico
Nov 15 '18 at 8:06
Duplicate? tex.stackexchange.com/questions/337840/…, tex.stackexchange.com/questions/11702/…, tex.stackexchange.com/questions/219090/…
– Steven B. Segletes
Nov 15 '18 at 10:29
Duplicate? tex.stackexchange.com/questions/337840/…, tex.stackexchange.com/questions/11702/…, tex.stackexchange.com/questions/219090/…
– Steven B. Segletes
Nov 15 '18 at 10:29
add a comment |
4 Answers
4
active
oldest
votes
A no-package approach for three term or higher sums as well.
documentclassarticle
newcommandshowsum[2][c]%
$edeforiginalplusmathcodethemathcode`+%
begingrouplccode`~=`+ lowercaseendgroupdef~\&%
mathcode`+ "8000
beginarray[#1]@r@;r@
mathcharoriginalplusmathcode& #2 \
hline
& thenumexpr#2relax
endarray%
$%
begindocument
Xquad % to show the baseline
showsum12345 + 6543quad
showsum521725 + 256814quad
showsum523057 + 6743quad
showsum[t]57208+6207quad
showsum[b]57208+6207+12095quad
X
enddocument
The X are here to indicate baseline. (plagiarized from @egreg)
Variant display:
documentclassarticle
newcommandshowsum[2][c]%
$edeforiginalplusmathcodethemathcode`+%
begingrouplccode`~=`+ lowercaseendgroupdef~\mathcharoriginalplusmathcode&%
mathcode`+ "8000
beginarray[#1]@r@;r@
& #2 \
hline
=& thenumexpr#2relax
endarray%
$%
begindocument
Xquad % to show the baseline
showsum12345 + 6543quad
showsum521725 + 256814quad
showsum523057 + 6743quad
showsum[t]57208+6207+77777quad
showsum[b]57208+6207+12095+33333quad
X
enddocument
We can also sum negative integers:
documentclassarticle
newcommandshowsum[2][c]%
$edeforiginalplusmathcodethemathcode`+%
begingrouplccode`~=`+ lowercaseendgroupdef~\mathcharoriginalplusmathcode&%
edeforiginalminusmathcodethemathcode`-%
begingrouplccode`~=`- lowercaseendgroupdef~\mathcharoriginalminusmathcode&%
mathcode`+ "8000
mathcode`- "8000
beginarray[#1]@r@;r@
& #2 \
hline
=& thenumexpr#2relax
endarray%
$%
begindocument
Xquad % to show the baseline
showsum12345 - 6543quad
showsum521725 + 256814quad
showsum523057 - 6743quad
showsum[t]57208-6207+77777quad
showsum[b]57208-6207+12095-33333quad
X
enddocument
(there was a missing %
after the final $
in all three showsum
, fixed now but images not updated)
1
I have often dreamt about an extension of TeX of "mathematically active" to more general "quasi-active" even outside math mode, which would be active characters except inedef
,csname...endcsname
, ornumexpr...relax
context, like mathematically active characters are (my answer demonstrates it). Such "quasi-active" characters (keeping the same catcode) would be very useful.
– user4686
Nov 15 '18 at 10:34
add a comment |
The code below defines a macro, Summation
, that accepts a comma separated list of integers such as
Summation12345, 6543
Summation521725, 256814
Summation523057, 6743
Summation57208,6207
Summation[b]57208,6207,12095
The macro then adds the integers in a table, as in the OP. The commands above give the output:
There is an optional first argument that becomes the positioning optional argument in the tabular
environment (by default, t
is used). I haven't really checked, but it is likely ro break with large integers.
All of the integers are printed using the num
command from the siunitx package, so their formatting can be customised using siunitx. for example, by adding
sisetupgroup-separator=,,group-four-digits
the numbers will have a comma separating the thousands, millions, ... etc. so that the output becomes
The code is an exercise in using LaTeX3:
documentclassarticle
usepackagexparse
usepackagesiunitx
ExplSyntaxOn
clist_new:N l_int_clist
int_new:N g_total_int
tl_new:N g_summation_tl
NewDocumentCommandSummation Ot m
clist_set:Nn l_int_clist #2
int_zero:N g_total_int
tl_clear:N g_summation_tl
clist_map_inline:Nn l_int_clist
int_gadd:Nn g_total_int ##1
tl_gput_right:No g_summation_tl & num##1\
begintabular[#1]r@spacer
+ tl_use:N g_summation_tl cline2-2
&numint_use:N g_total_int
endtabular
ExplSyntaxOff
begindocument
Summation12345, 6543
Summation521725, 256814
Summation523057, 6743
Summation57208,6207
Summation[b]57208,6207,12095
enddocument
As noted in Latex3 inline mapping produces extra row in tabular, it is necessary to construct the table as a token list because otherwise hrule
will complain.
1
hmmm... look at tex.stackexchange.com/questions/88472/… --- you needsisetupgroup-four-digits=true
or something similar...
– Rmano
Nov 15 '18 at 9:09
@Rmano That question doesn't seem to be relevant as it is asking about having siunitx-like output using pgfmath.
– Andrew
Nov 15 '18 at 9:12
3
Yes, but look at your example: 12345 has a thousand separator but 6543 no, so the 2 and the 6 don't align. You have to forcesiunitx
to add the separator also for 4-figures numbers...
– Rmano
Nov 15 '18 at 9:15
1
@Rmano Ah, OK, thanks! I have added this.
– Andrew
Nov 15 '18 at 9:17
add a comment |
Let TeX do the calculations
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewDocumentCommandshowsumOcm
ensuremath
simeon_showsum:nn #1 #2
seq_new:N l__simeon_showsum_seq
cs_new_protected:Nn simeon_showsum:nn
seq_set_split:Nnn l__simeon_showsum_seq + #2
beginarray[#1]@r@;r@
+ & seq_use:Nn l__simeon_showsum_seq \ & \
hline
& int_eval:n #2
endarray
ExplSyntaxOff
begindocument
Xquad % to show the baseline
showsum12345 + 6543quad
showsum521725 + 256814quad
showsum523057 + 6743quad
showsum[t]57208+6207quad
showsum[b]57208+6207+12095quad
X
enddocument
Since you're using+
as the separator you could useint_eval:n #2
instead ofint_eval:n seq_use:Nn l__simeon_showsum_seq +
.
– Andrew
Nov 15 '18 at 9:32
@Andrew indeed!
– egreg
Nov 15 '18 at 10:03
add a comment |
Here's a LuaLaTeX-based solution. The macro mysum
takes two mandatory arguments -- the numbers to be summed -- and one optional argument, which determines how the array
environment should be placed vertically relative to the math baseline: centered (the default), top-aligned, or bottom-aligned. (If an optional argument is set, it must be listed first and enclosed in square brackets, per the usual LaTeX macro syntax rules.)
documentclassarticle
usepackagebooktabs % for "midrule" macro
newcommandmysum[3][c]%
beginarray[#1]@r@
#2 \ +: #3 \ midrule directluatex.sprint(#2+#3)
endarray
usepackagenewtxtext,newtxmath % optional (Times Roman text and math fonts)
begindocument
[
mysum123456543 qquad
mysum511725256814 qquad
mysum[b]5230576743 qquad
mysum[t]572086207
]
enddocument
Addendum to allow for an arbitrary number of summands rather than exactly two summands. The preceding code dealt with the case provided in the original query, which involved exactly two terms in the summation. The following solution, which is still LuaLaTeX-based, allows for an arbitrary number of summarnds. It works as follows:
The LaTeX macro
mysum
takes one optional argument (the vertical placement indicator, see above) and one mandatory argument: a string of comma-separated numbers. Whitespace is allowed inside the string. Thus,mysum12345,6543
,mysum12345, 6543
,mysum 12345 , 6543
, andmysum12345,6543
, are all equally valid -- and produce the same output, viz., the number18888
.The
mysum
macro performs the following tasks: It sets up anarray
environment, calls the Lua functionperform_summation
to perform most of the actual work, and terminates thearray
environment.The
perform_summation
Lua function begins by splitting the comma-delimited string of numbers into a Lua table, using,
as the separator. (The auxiliary function that performs the splitting was obtained from stackoverflow.)perform_summation
then iterates over the table entries to (a) compute the running subtotal of the entries and (b) print out each entry on a separate row. Finally, the Lua function prints the value of the sum of the entries.
The 3 "-
" symbols located at the left-hand and right-hand edges of the following screenshot merely serve to indicate the location of the math axis.
documentclassarticle
usepackagenewtxtext,newtxmath % optional: Times Roman text and math fonts
usepackagebooktabs % for "midrule" macro
usepackageluacode % for "luacode" environment
%% Lua-side code:
beginluacode
-- The following code is from https://stackoverflow.com/a/19263313:
function string:split( inSplitPattern )
outResults =
local theStart = 1
local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
while theSplitStart do
table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
theStart = theSplitEnd + 1
theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
end
table.insert( outResults, string.sub( self, theStart ) )
return outResults
end
function perform_summation ( s )
t = s:split(",")
sum = 0 -- initialize "sum" variable
tex.sprint ( "+\:" ) -- print the "+" symbol
for i=1,#t do
sum = sum+t[i]
tex.sprint ( t[i] .. "\\" )
end
tex.sprint ( "\midrule" .. sum )
end
endluacode
%% LaTeX-side code:
newcommandmysum[2][c]%
beginarray[#1]@r@
directluaperform_summation("#2")
endarray
begindocument
[
---quad % indicate math axis
mysum12345,6543 qquad
mysum1234567891234,9876543219877 qquad
mysum1,2,3,4 qquad
mysum[t] 57208 , 6207 , 12095 qquad
mysum[b]12345,67890
quad--- % indicate math axis
]
enddocument
1
thenumexpr#2+#3relax
will work fine with numbers having a sum not exceeding2147483647
.... (and it is not even needed to usenumexpr
, TeX non-expandable arithmetic would be fine too)
– user4686
Nov 15 '18 at 9:32
1
(count0=#2relaxadvancecount0by#3relaxthecount0relax)
– user4686
Nov 15 '18 at 9:35
2
@jfbu - But where's the fun if I can't usedirectlua
andtex.sprint
? :-)
– Mico
Nov 15 '18 at 9:37
1
I did suspect something like that and in view of the tremendous number of answers I have myself provided withxint
I can sympathize...:)
– user4686
Nov 15 '18 at 9:43
1
@jfbu When I posted my answer I assumed that it was only a matter of time before you posted a really quickxint
solution that would also work for large integers:) I didn't think of lualatex (+1)...another thing to learn:)
– Andrew
Nov 15 '18 at 9:51
|
show 2 more comments
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%2f460078%2fsumming-numbers-like-paper-calculation%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
A no-package approach for three term or higher sums as well.
documentclassarticle
newcommandshowsum[2][c]%
$edeforiginalplusmathcodethemathcode`+%
begingrouplccode`~=`+ lowercaseendgroupdef~\&%
mathcode`+ "8000
beginarray[#1]@r@;r@
mathcharoriginalplusmathcode& #2 \
hline
& thenumexpr#2relax
endarray%
$%
begindocument
Xquad % to show the baseline
showsum12345 + 6543quad
showsum521725 + 256814quad
showsum523057 + 6743quad
showsum[t]57208+6207quad
showsum[b]57208+6207+12095quad
X
enddocument
The X are here to indicate baseline. (plagiarized from @egreg)
Variant display:
documentclassarticle
newcommandshowsum[2][c]%
$edeforiginalplusmathcodethemathcode`+%
begingrouplccode`~=`+ lowercaseendgroupdef~\mathcharoriginalplusmathcode&%
mathcode`+ "8000
beginarray[#1]@r@;r@
& #2 \
hline
=& thenumexpr#2relax
endarray%
$%
begindocument
Xquad % to show the baseline
showsum12345 + 6543quad
showsum521725 + 256814quad
showsum523057 + 6743quad
showsum[t]57208+6207+77777quad
showsum[b]57208+6207+12095+33333quad
X
enddocument
We can also sum negative integers:
documentclassarticle
newcommandshowsum[2][c]%
$edeforiginalplusmathcodethemathcode`+%
begingrouplccode`~=`+ lowercaseendgroupdef~\mathcharoriginalplusmathcode&%
edeforiginalminusmathcodethemathcode`-%
begingrouplccode`~=`- lowercaseendgroupdef~\mathcharoriginalminusmathcode&%
mathcode`+ "8000
mathcode`- "8000
beginarray[#1]@r@;r@
& #2 \
hline
=& thenumexpr#2relax
endarray%
$%
begindocument
Xquad % to show the baseline
showsum12345 - 6543quad
showsum521725 + 256814quad
showsum523057 - 6743quad
showsum[t]57208-6207+77777quad
showsum[b]57208-6207+12095-33333quad
X
enddocument
(there was a missing %
after the final $
in all three showsum
, fixed now but images not updated)
1
I have often dreamt about an extension of TeX of "mathematically active" to more general "quasi-active" even outside math mode, which would be active characters except inedef
,csname...endcsname
, ornumexpr...relax
context, like mathematically active characters are (my answer demonstrates it). Such "quasi-active" characters (keeping the same catcode) would be very useful.
– user4686
Nov 15 '18 at 10:34
add a comment |
A no-package approach for three term or higher sums as well.
documentclassarticle
newcommandshowsum[2][c]%
$edeforiginalplusmathcodethemathcode`+%
begingrouplccode`~=`+ lowercaseendgroupdef~\&%
mathcode`+ "8000
beginarray[#1]@r@;r@
mathcharoriginalplusmathcode& #2 \
hline
& thenumexpr#2relax
endarray%
$%
begindocument
Xquad % to show the baseline
showsum12345 + 6543quad
showsum521725 + 256814quad
showsum523057 + 6743quad
showsum[t]57208+6207quad
showsum[b]57208+6207+12095quad
X
enddocument
The X are here to indicate baseline. (plagiarized from @egreg)
Variant display:
documentclassarticle
newcommandshowsum[2][c]%
$edeforiginalplusmathcodethemathcode`+%
begingrouplccode`~=`+ lowercaseendgroupdef~\mathcharoriginalplusmathcode&%
mathcode`+ "8000
beginarray[#1]@r@;r@
& #2 \
hline
=& thenumexpr#2relax
endarray%
$%
begindocument
Xquad % to show the baseline
showsum12345 + 6543quad
showsum521725 + 256814quad
showsum523057 + 6743quad
showsum[t]57208+6207+77777quad
showsum[b]57208+6207+12095+33333quad
X
enddocument
We can also sum negative integers:
documentclassarticle
newcommandshowsum[2][c]%
$edeforiginalplusmathcodethemathcode`+%
begingrouplccode`~=`+ lowercaseendgroupdef~\mathcharoriginalplusmathcode&%
edeforiginalminusmathcodethemathcode`-%
begingrouplccode`~=`- lowercaseendgroupdef~\mathcharoriginalminusmathcode&%
mathcode`+ "8000
mathcode`- "8000
beginarray[#1]@r@;r@
& #2 \
hline
=& thenumexpr#2relax
endarray%
$%
begindocument
Xquad % to show the baseline
showsum12345 - 6543quad
showsum521725 + 256814quad
showsum523057 - 6743quad
showsum[t]57208-6207+77777quad
showsum[b]57208-6207+12095-33333quad
X
enddocument
(there was a missing %
after the final $
in all three showsum
, fixed now but images not updated)
1
I have often dreamt about an extension of TeX of "mathematically active" to more general "quasi-active" even outside math mode, which would be active characters except inedef
,csname...endcsname
, ornumexpr...relax
context, like mathematically active characters are (my answer demonstrates it). Such "quasi-active" characters (keeping the same catcode) would be very useful.
– user4686
Nov 15 '18 at 10:34
add a comment |
A no-package approach for three term or higher sums as well.
documentclassarticle
newcommandshowsum[2][c]%
$edeforiginalplusmathcodethemathcode`+%
begingrouplccode`~=`+ lowercaseendgroupdef~\&%
mathcode`+ "8000
beginarray[#1]@r@;r@
mathcharoriginalplusmathcode& #2 \
hline
& thenumexpr#2relax
endarray%
$%
begindocument
Xquad % to show the baseline
showsum12345 + 6543quad
showsum521725 + 256814quad
showsum523057 + 6743quad
showsum[t]57208+6207quad
showsum[b]57208+6207+12095quad
X
enddocument
The X are here to indicate baseline. (plagiarized from @egreg)
Variant display:
documentclassarticle
newcommandshowsum[2][c]%
$edeforiginalplusmathcodethemathcode`+%
begingrouplccode`~=`+ lowercaseendgroupdef~\mathcharoriginalplusmathcode&%
mathcode`+ "8000
beginarray[#1]@r@;r@
& #2 \
hline
=& thenumexpr#2relax
endarray%
$%
begindocument
Xquad % to show the baseline
showsum12345 + 6543quad
showsum521725 + 256814quad
showsum523057 + 6743quad
showsum[t]57208+6207+77777quad
showsum[b]57208+6207+12095+33333quad
X
enddocument
We can also sum negative integers:
documentclassarticle
newcommandshowsum[2][c]%
$edeforiginalplusmathcodethemathcode`+%
begingrouplccode`~=`+ lowercaseendgroupdef~\mathcharoriginalplusmathcode&%
edeforiginalminusmathcodethemathcode`-%
begingrouplccode`~=`- lowercaseendgroupdef~\mathcharoriginalminusmathcode&%
mathcode`+ "8000
mathcode`- "8000
beginarray[#1]@r@;r@
& #2 \
hline
=& thenumexpr#2relax
endarray%
$%
begindocument
Xquad % to show the baseline
showsum12345 - 6543quad
showsum521725 + 256814quad
showsum523057 - 6743quad
showsum[t]57208-6207+77777quad
showsum[b]57208-6207+12095-33333quad
X
enddocument
(there was a missing %
after the final $
in all three showsum
, fixed now but images not updated)
A no-package approach for three term or higher sums as well.
documentclassarticle
newcommandshowsum[2][c]%
$edeforiginalplusmathcodethemathcode`+%
begingrouplccode`~=`+ lowercaseendgroupdef~\&%
mathcode`+ "8000
beginarray[#1]@r@;r@
mathcharoriginalplusmathcode& #2 \
hline
& thenumexpr#2relax
endarray%
$%
begindocument
Xquad % to show the baseline
showsum12345 + 6543quad
showsum521725 + 256814quad
showsum523057 + 6743quad
showsum[t]57208+6207quad
showsum[b]57208+6207+12095quad
X
enddocument
The X are here to indicate baseline. (plagiarized from @egreg)
Variant display:
documentclassarticle
newcommandshowsum[2][c]%
$edeforiginalplusmathcodethemathcode`+%
begingrouplccode`~=`+ lowercaseendgroupdef~\mathcharoriginalplusmathcode&%
mathcode`+ "8000
beginarray[#1]@r@;r@
& #2 \
hline
=& thenumexpr#2relax
endarray%
$%
begindocument
Xquad % to show the baseline
showsum12345 + 6543quad
showsum521725 + 256814quad
showsum523057 + 6743quad
showsum[t]57208+6207+77777quad
showsum[b]57208+6207+12095+33333quad
X
enddocument
We can also sum negative integers:
documentclassarticle
newcommandshowsum[2][c]%
$edeforiginalplusmathcodethemathcode`+%
begingrouplccode`~=`+ lowercaseendgroupdef~\mathcharoriginalplusmathcode&%
edeforiginalminusmathcodethemathcode`-%
begingrouplccode`~=`- lowercaseendgroupdef~\mathcharoriginalminusmathcode&%
mathcode`+ "8000
mathcode`- "8000
beginarray[#1]@r@;r@
& #2 \
hline
=& thenumexpr#2relax
endarray%
$%
begindocument
Xquad % to show the baseline
showsum12345 - 6543quad
showsum521725 + 256814quad
showsum523057 - 6743quad
showsum[t]57208-6207+77777quad
showsum[b]57208-6207+12095-33333quad
X
enddocument
(there was a missing %
after the final $
in all three showsum
, fixed now but images not updated)
edited Nov 15 '18 at 10:26
answered Nov 15 '18 at 10:13
user4686
1
I have often dreamt about an extension of TeX of "mathematically active" to more general "quasi-active" even outside math mode, which would be active characters except inedef
,csname...endcsname
, ornumexpr...relax
context, like mathematically active characters are (my answer demonstrates it). Such "quasi-active" characters (keeping the same catcode) would be very useful.
– user4686
Nov 15 '18 at 10:34
add a comment |
1
I have often dreamt about an extension of TeX of "mathematically active" to more general "quasi-active" even outside math mode, which would be active characters except inedef
,csname...endcsname
, ornumexpr...relax
context, like mathematically active characters are (my answer demonstrates it). Such "quasi-active" characters (keeping the same catcode) would be very useful.
– user4686
Nov 15 '18 at 10:34
1
1
I have often dreamt about an extension of TeX of "mathematically active" to more general "quasi-active" even outside math mode, which would be active characters except in
edef
, csname...endcsname
, or numexpr...relax
context, like mathematically active characters are (my answer demonstrates it). Such "quasi-active" characters (keeping the same catcode) would be very useful.– user4686
Nov 15 '18 at 10:34
I have often dreamt about an extension of TeX of "mathematically active" to more general "quasi-active" even outside math mode, which would be active characters except in
edef
, csname...endcsname
, or numexpr...relax
context, like mathematically active characters are (my answer demonstrates it). Such "quasi-active" characters (keeping the same catcode) would be very useful.– user4686
Nov 15 '18 at 10:34
add a comment |
The code below defines a macro, Summation
, that accepts a comma separated list of integers such as
Summation12345, 6543
Summation521725, 256814
Summation523057, 6743
Summation57208,6207
Summation[b]57208,6207,12095
The macro then adds the integers in a table, as in the OP. The commands above give the output:
There is an optional first argument that becomes the positioning optional argument in the tabular
environment (by default, t
is used). I haven't really checked, but it is likely ro break with large integers.
All of the integers are printed using the num
command from the siunitx package, so their formatting can be customised using siunitx. for example, by adding
sisetupgroup-separator=,,group-four-digits
the numbers will have a comma separating the thousands, millions, ... etc. so that the output becomes
The code is an exercise in using LaTeX3:
documentclassarticle
usepackagexparse
usepackagesiunitx
ExplSyntaxOn
clist_new:N l_int_clist
int_new:N g_total_int
tl_new:N g_summation_tl
NewDocumentCommandSummation Ot m
clist_set:Nn l_int_clist #2
int_zero:N g_total_int
tl_clear:N g_summation_tl
clist_map_inline:Nn l_int_clist
int_gadd:Nn g_total_int ##1
tl_gput_right:No g_summation_tl & num##1\
begintabular[#1]r@spacer
+ tl_use:N g_summation_tl cline2-2
&numint_use:N g_total_int
endtabular
ExplSyntaxOff
begindocument
Summation12345, 6543
Summation521725, 256814
Summation523057, 6743
Summation57208,6207
Summation[b]57208,6207,12095
enddocument
As noted in Latex3 inline mapping produces extra row in tabular, it is necessary to construct the table as a token list because otherwise hrule
will complain.
1
hmmm... look at tex.stackexchange.com/questions/88472/… --- you needsisetupgroup-four-digits=true
or something similar...
– Rmano
Nov 15 '18 at 9:09
@Rmano That question doesn't seem to be relevant as it is asking about having siunitx-like output using pgfmath.
– Andrew
Nov 15 '18 at 9:12
3
Yes, but look at your example: 12345 has a thousand separator but 6543 no, so the 2 and the 6 don't align. You have to forcesiunitx
to add the separator also for 4-figures numbers...
– Rmano
Nov 15 '18 at 9:15
1
@Rmano Ah, OK, thanks! I have added this.
– Andrew
Nov 15 '18 at 9:17
add a comment |
The code below defines a macro, Summation
, that accepts a comma separated list of integers such as
Summation12345, 6543
Summation521725, 256814
Summation523057, 6743
Summation57208,6207
Summation[b]57208,6207,12095
The macro then adds the integers in a table, as in the OP. The commands above give the output:
There is an optional first argument that becomes the positioning optional argument in the tabular
environment (by default, t
is used). I haven't really checked, but it is likely ro break with large integers.
All of the integers are printed using the num
command from the siunitx package, so their formatting can be customised using siunitx. for example, by adding
sisetupgroup-separator=,,group-four-digits
the numbers will have a comma separating the thousands, millions, ... etc. so that the output becomes
The code is an exercise in using LaTeX3:
documentclassarticle
usepackagexparse
usepackagesiunitx
ExplSyntaxOn
clist_new:N l_int_clist
int_new:N g_total_int
tl_new:N g_summation_tl
NewDocumentCommandSummation Ot m
clist_set:Nn l_int_clist #2
int_zero:N g_total_int
tl_clear:N g_summation_tl
clist_map_inline:Nn l_int_clist
int_gadd:Nn g_total_int ##1
tl_gput_right:No g_summation_tl & num##1\
begintabular[#1]r@spacer
+ tl_use:N g_summation_tl cline2-2
&numint_use:N g_total_int
endtabular
ExplSyntaxOff
begindocument
Summation12345, 6543
Summation521725, 256814
Summation523057, 6743
Summation57208,6207
Summation[b]57208,6207,12095
enddocument
As noted in Latex3 inline mapping produces extra row in tabular, it is necessary to construct the table as a token list because otherwise hrule
will complain.
1
hmmm... look at tex.stackexchange.com/questions/88472/… --- you needsisetupgroup-four-digits=true
or something similar...
– Rmano
Nov 15 '18 at 9:09
@Rmano That question doesn't seem to be relevant as it is asking about having siunitx-like output using pgfmath.
– Andrew
Nov 15 '18 at 9:12
3
Yes, but look at your example: 12345 has a thousand separator but 6543 no, so the 2 and the 6 don't align. You have to forcesiunitx
to add the separator also for 4-figures numbers...
– Rmano
Nov 15 '18 at 9:15
1
@Rmano Ah, OK, thanks! I have added this.
– Andrew
Nov 15 '18 at 9:17
add a comment |
The code below defines a macro, Summation
, that accepts a comma separated list of integers such as
Summation12345, 6543
Summation521725, 256814
Summation523057, 6743
Summation57208,6207
Summation[b]57208,6207,12095
The macro then adds the integers in a table, as in the OP. The commands above give the output:
There is an optional first argument that becomes the positioning optional argument in the tabular
environment (by default, t
is used). I haven't really checked, but it is likely ro break with large integers.
All of the integers are printed using the num
command from the siunitx package, so their formatting can be customised using siunitx. for example, by adding
sisetupgroup-separator=,,group-four-digits
the numbers will have a comma separating the thousands, millions, ... etc. so that the output becomes
The code is an exercise in using LaTeX3:
documentclassarticle
usepackagexparse
usepackagesiunitx
ExplSyntaxOn
clist_new:N l_int_clist
int_new:N g_total_int
tl_new:N g_summation_tl
NewDocumentCommandSummation Ot m
clist_set:Nn l_int_clist #2
int_zero:N g_total_int
tl_clear:N g_summation_tl
clist_map_inline:Nn l_int_clist
int_gadd:Nn g_total_int ##1
tl_gput_right:No g_summation_tl & num##1\
begintabular[#1]r@spacer
+ tl_use:N g_summation_tl cline2-2
&numint_use:N g_total_int
endtabular
ExplSyntaxOff
begindocument
Summation12345, 6543
Summation521725, 256814
Summation523057, 6743
Summation57208,6207
Summation[b]57208,6207,12095
enddocument
As noted in Latex3 inline mapping produces extra row in tabular, it is necessary to construct the table as a token list because otherwise hrule
will complain.
The code below defines a macro, Summation
, that accepts a comma separated list of integers such as
Summation12345, 6543
Summation521725, 256814
Summation523057, 6743
Summation57208,6207
Summation[b]57208,6207,12095
The macro then adds the integers in a table, as in the OP. The commands above give the output:
There is an optional first argument that becomes the positioning optional argument in the tabular
environment (by default, t
is used). I haven't really checked, but it is likely ro break with large integers.
All of the integers are printed using the num
command from the siunitx package, so their formatting can be customised using siunitx. for example, by adding
sisetupgroup-separator=,,group-four-digits
the numbers will have a comma separating the thousands, millions, ... etc. so that the output becomes
The code is an exercise in using LaTeX3:
documentclassarticle
usepackagexparse
usepackagesiunitx
ExplSyntaxOn
clist_new:N l_int_clist
int_new:N g_total_int
tl_new:N g_summation_tl
NewDocumentCommandSummation Ot m
clist_set:Nn l_int_clist #2
int_zero:N g_total_int
tl_clear:N g_summation_tl
clist_map_inline:Nn l_int_clist
int_gadd:Nn g_total_int ##1
tl_gput_right:No g_summation_tl & num##1\
begintabular[#1]r@spacer
+ tl_use:N g_summation_tl cline2-2
&numint_use:N g_total_int
endtabular
ExplSyntaxOff
begindocument
Summation12345, 6543
Summation521725, 256814
Summation523057, 6743
Summation57208,6207
Summation[b]57208,6207,12095
enddocument
As noted in Latex3 inline mapping produces extra row in tabular, it is necessary to construct the table as a token list because otherwise hrule
will complain.
edited Nov 15 '18 at 9:17
answered Nov 15 '18 at 8:53
AndrewAndrew
30.9k34482
30.9k34482
1
hmmm... look at tex.stackexchange.com/questions/88472/… --- you needsisetupgroup-four-digits=true
or something similar...
– Rmano
Nov 15 '18 at 9:09
@Rmano That question doesn't seem to be relevant as it is asking about having siunitx-like output using pgfmath.
– Andrew
Nov 15 '18 at 9:12
3
Yes, but look at your example: 12345 has a thousand separator but 6543 no, so the 2 and the 6 don't align. You have to forcesiunitx
to add the separator also for 4-figures numbers...
– Rmano
Nov 15 '18 at 9:15
1
@Rmano Ah, OK, thanks! I have added this.
– Andrew
Nov 15 '18 at 9:17
add a comment |
1
hmmm... look at tex.stackexchange.com/questions/88472/… --- you needsisetupgroup-four-digits=true
or something similar...
– Rmano
Nov 15 '18 at 9:09
@Rmano That question doesn't seem to be relevant as it is asking about having siunitx-like output using pgfmath.
– Andrew
Nov 15 '18 at 9:12
3
Yes, but look at your example: 12345 has a thousand separator but 6543 no, so the 2 and the 6 don't align. You have to forcesiunitx
to add the separator also for 4-figures numbers...
– Rmano
Nov 15 '18 at 9:15
1
@Rmano Ah, OK, thanks! I have added this.
– Andrew
Nov 15 '18 at 9:17
1
1
hmmm... look at tex.stackexchange.com/questions/88472/… --- you need
sisetupgroup-four-digits=true
or something similar...– Rmano
Nov 15 '18 at 9:09
hmmm... look at tex.stackexchange.com/questions/88472/… --- you need
sisetupgroup-four-digits=true
or something similar...– Rmano
Nov 15 '18 at 9:09
@Rmano That question doesn't seem to be relevant as it is asking about having siunitx-like output using pgfmath.
– Andrew
Nov 15 '18 at 9:12
@Rmano That question doesn't seem to be relevant as it is asking about having siunitx-like output using pgfmath.
– Andrew
Nov 15 '18 at 9:12
3
3
Yes, but look at your example: 12345 has a thousand separator but 6543 no, so the 2 and the 6 don't align. You have to force
siunitx
to add the separator also for 4-figures numbers...– Rmano
Nov 15 '18 at 9:15
Yes, but look at your example: 12345 has a thousand separator but 6543 no, so the 2 and the 6 don't align. You have to force
siunitx
to add the separator also for 4-figures numbers...– Rmano
Nov 15 '18 at 9:15
1
1
@Rmano Ah, OK, thanks! I have added this.
– Andrew
Nov 15 '18 at 9:17
@Rmano Ah, OK, thanks! I have added this.
– Andrew
Nov 15 '18 at 9:17
add a comment |
Let TeX do the calculations
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewDocumentCommandshowsumOcm
ensuremath
simeon_showsum:nn #1 #2
seq_new:N l__simeon_showsum_seq
cs_new_protected:Nn simeon_showsum:nn
seq_set_split:Nnn l__simeon_showsum_seq + #2
beginarray[#1]@r@;r@
+ & seq_use:Nn l__simeon_showsum_seq \ & \
hline
& int_eval:n #2
endarray
ExplSyntaxOff
begindocument
Xquad % to show the baseline
showsum12345 + 6543quad
showsum521725 + 256814quad
showsum523057 + 6743quad
showsum[t]57208+6207quad
showsum[b]57208+6207+12095quad
X
enddocument
Since you're using+
as the separator you could useint_eval:n #2
instead ofint_eval:n seq_use:Nn l__simeon_showsum_seq +
.
– Andrew
Nov 15 '18 at 9:32
@Andrew indeed!
– egreg
Nov 15 '18 at 10:03
add a comment |
Let TeX do the calculations
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewDocumentCommandshowsumOcm
ensuremath
simeon_showsum:nn #1 #2
seq_new:N l__simeon_showsum_seq
cs_new_protected:Nn simeon_showsum:nn
seq_set_split:Nnn l__simeon_showsum_seq + #2
beginarray[#1]@r@;r@
+ & seq_use:Nn l__simeon_showsum_seq \ & \
hline
& int_eval:n #2
endarray
ExplSyntaxOff
begindocument
Xquad % to show the baseline
showsum12345 + 6543quad
showsum521725 + 256814quad
showsum523057 + 6743quad
showsum[t]57208+6207quad
showsum[b]57208+6207+12095quad
X
enddocument
Since you're using+
as the separator you could useint_eval:n #2
instead ofint_eval:n seq_use:Nn l__simeon_showsum_seq +
.
– Andrew
Nov 15 '18 at 9:32
@Andrew indeed!
– egreg
Nov 15 '18 at 10:03
add a comment |
Let TeX do the calculations
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewDocumentCommandshowsumOcm
ensuremath
simeon_showsum:nn #1 #2
seq_new:N l__simeon_showsum_seq
cs_new_protected:Nn simeon_showsum:nn
seq_set_split:Nnn l__simeon_showsum_seq + #2
beginarray[#1]@r@;r@
+ & seq_use:Nn l__simeon_showsum_seq \ & \
hline
& int_eval:n #2
endarray
ExplSyntaxOff
begindocument
Xquad % to show the baseline
showsum12345 + 6543quad
showsum521725 + 256814quad
showsum523057 + 6743quad
showsum[t]57208+6207quad
showsum[b]57208+6207+12095quad
X
enddocument
Let TeX do the calculations
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewDocumentCommandshowsumOcm
ensuremath
simeon_showsum:nn #1 #2
seq_new:N l__simeon_showsum_seq
cs_new_protected:Nn simeon_showsum:nn
seq_set_split:Nnn l__simeon_showsum_seq + #2
beginarray[#1]@r@;r@
+ & seq_use:Nn l__simeon_showsum_seq \ & \
hline
& int_eval:n #2
endarray
ExplSyntaxOff
begindocument
Xquad % to show the baseline
showsum12345 + 6543quad
showsum521725 + 256814quad
showsum523057 + 6743quad
showsum[t]57208+6207quad
showsum[b]57208+6207+12095quad
X
enddocument
edited Nov 15 '18 at 11:47
answered Nov 15 '18 at 9:14
egregegreg
733k8919313254
733k8919313254
Since you're using+
as the separator you could useint_eval:n #2
instead ofint_eval:n seq_use:Nn l__simeon_showsum_seq +
.
– Andrew
Nov 15 '18 at 9:32
@Andrew indeed!
– egreg
Nov 15 '18 at 10:03
add a comment |
Since you're using+
as the separator you could useint_eval:n #2
instead ofint_eval:n seq_use:Nn l__simeon_showsum_seq +
.
– Andrew
Nov 15 '18 at 9:32
@Andrew indeed!
– egreg
Nov 15 '18 at 10:03
Since you're using
+
as the separator you could use int_eval:n #2
instead of int_eval:n seq_use:Nn l__simeon_showsum_seq +
.– Andrew
Nov 15 '18 at 9:32
Since you're using
+
as the separator you could use int_eval:n #2
instead of int_eval:n seq_use:Nn l__simeon_showsum_seq +
.– Andrew
Nov 15 '18 at 9:32
@Andrew indeed!
– egreg
Nov 15 '18 at 10:03
@Andrew indeed!
– egreg
Nov 15 '18 at 10:03
add a comment |
Here's a LuaLaTeX-based solution. The macro mysum
takes two mandatory arguments -- the numbers to be summed -- and one optional argument, which determines how the array
environment should be placed vertically relative to the math baseline: centered (the default), top-aligned, or bottom-aligned. (If an optional argument is set, it must be listed first and enclosed in square brackets, per the usual LaTeX macro syntax rules.)
documentclassarticle
usepackagebooktabs % for "midrule" macro
newcommandmysum[3][c]%
beginarray[#1]@r@
#2 \ +: #3 \ midrule directluatex.sprint(#2+#3)
endarray
usepackagenewtxtext,newtxmath % optional (Times Roman text and math fonts)
begindocument
[
mysum123456543 qquad
mysum511725256814 qquad
mysum[b]5230576743 qquad
mysum[t]572086207
]
enddocument
Addendum to allow for an arbitrary number of summands rather than exactly two summands. The preceding code dealt with the case provided in the original query, which involved exactly two terms in the summation. The following solution, which is still LuaLaTeX-based, allows for an arbitrary number of summarnds. It works as follows:
The LaTeX macro
mysum
takes one optional argument (the vertical placement indicator, see above) and one mandatory argument: a string of comma-separated numbers. Whitespace is allowed inside the string. Thus,mysum12345,6543
,mysum12345, 6543
,mysum 12345 , 6543
, andmysum12345,6543
, are all equally valid -- and produce the same output, viz., the number18888
.The
mysum
macro performs the following tasks: It sets up anarray
environment, calls the Lua functionperform_summation
to perform most of the actual work, and terminates thearray
environment.The
perform_summation
Lua function begins by splitting the comma-delimited string of numbers into a Lua table, using,
as the separator. (The auxiliary function that performs the splitting was obtained from stackoverflow.)perform_summation
then iterates over the table entries to (a) compute the running subtotal of the entries and (b) print out each entry on a separate row. Finally, the Lua function prints the value of the sum of the entries.
The 3 "-
" symbols located at the left-hand and right-hand edges of the following screenshot merely serve to indicate the location of the math axis.
documentclassarticle
usepackagenewtxtext,newtxmath % optional: Times Roman text and math fonts
usepackagebooktabs % for "midrule" macro
usepackageluacode % for "luacode" environment
%% Lua-side code:
beginluacode
-- The following code is from https://stackoverflow.com/a/19263313:
function string:split( inSplitPattern )
outResults =
local theStart = 1
local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
while theSplitStart do
table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
theStart = theSplitEnd + 1
theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
end
table.insert( outResults, string.sub( self, theStart ) )
return outResults
end
function perform_summation ( s )
t = s:split(",")
sum = 0 -- initialize "sum" variable
tex.sprint ( "+\:" ) -- print the "+" symbol
for i=1,#t do
sum = sum+t[i]
tex.sprint ( t[i] .. "\\" )
end
tex.sprint ( "\midrule" .. sum )
end
endluacode
%% LaTeX-side code:
newcommandmysum[2][c]%
beginarray[#1]@r@
directluaperform_summation("#2")
endarray
begindocument
[
---quad % indicate math axis
mysum12345,6543 qquad
mysum1234567891234,9876543219877 qquad
mysum1,2,3,4 qquad
mysum[t] 57208 , 6207 , 12095 qquad
mysum[b]12345,67890
quad--- % indicate math axis
]
enddocument
1
thenumexpr#2+#3relax
will work fine with numbers having a sum not exceeding2147483647
.... (and it is not even needed to usenumexpr
, TeX non-expandable arithmetic would be fine too)
– user4686
Nov 15 '18 at 9:32
1
(count0=#2relaxadvancecount0by#3relaxthecount0relax)
– user4686
Nov 15 '18 at 9:35
2
@jfbu - But where's the fun if I can't usedirectlua
andtex.sprint
? :-)
– Mico
Nov 15 '18 at 9:37
1
I did suspect something like that and in view of the tremendous number of answers I have myself provided withxint
I can sympathize...:)
– user4686
Nov 15 '18 at 9:43
1
@jfbu When I posted my answer I assumed that it was only a matter of time before you posted a really quickxint
solution that would also work for large integers:) I didn't think of lualatex (+1)...another thing to learn:)
– Andrew
Nov 15 '18 at 9:51
|
show 2 more comments
Here's a LuaLaTeX-based solution. The macro mysum
takes two mandatory arguments -- the numbers to be summed -- and one optional argument, which determines how the array
environment should be placed vertically relative to the math baseline: centered (the default), top-aligned, or bottom-aligned. (If an optional argument is set, it must be listed first and enclosed in square brackets, per the usual LaTeX macro syntax rules.)
documentclassarticle
usepackagebooktabs % for "midrule" macro
newcommandmysum[3][c]%
beginarray[#1]@r@
#2 \ +: #3 \ midrule directluatex.sprint(#2+#3)
endarray
usepackagenewtxtext,newtxmath % optional (Times Roman text and math fonts)
begindocument
[
mysum123456543 qquad
mysum511725256814 qquad
mysum[b]5230576743 qquad
mysum[t]572086207
]
enddocument
Addendum to allow for an arbitrary number of summands rather than exactly two summands. The preceding code dealt with the case provided in the original query, which involved exactly two terms in the summation. The following solution, which is still LuaLaTeX-based, allows for an arbitrary number of summarnds. It works as follows:
The LaTeX macro
mysum
takes one optional argument (the vertical placement indicator, see above) and one mandatory argument: a string of comma-separated numbers. Whitespace is allowed inside the string. Thus,mysum12345,6543
,mysum12345, 6543
,mysum 12345 , 6543
, andmysum12345,6543
, are all equally valid -- and produce the same output, viz., the number18888
.The
mysum
macro performs the following tasks: It sets up anarray
environment, calls the Lua functionperform_summation
to perform most of the actual work, and terminates thearray
environment.The
perform_summation
Lua function begins by splitting the comma-delimited string of numbers into a Lua table, using,
as the separator. (The auxiliary function that performs the splitting was obtained from stackoverflow.)perform_summation
then iterates over the table entries to (a) compute the running subtotal of the entries and (b) print out each entry on a separate row. Finally, the Lua function prints the value of the sum of the entries.
The 3 "-
" symbols located at the left-hand and right-hand edges of the following screenshot merely serve to indicate the location of the math axis.
documentclassarticle
usepackagenewtxtext,newtxmath % optional: Times Roman text and math fonts
usepackagebooktabs % for "midrule" macro
usepackageluacode % for "luacode" environment
%% Lua-side code:
beginluacode
-- The following code is from https://stackoverflow.com/a/19263313:
function string:split( inSplitPattern )
outResults =
local theStart = 1
local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
while theSplitStart do
table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
theStart = theSplitEnd + 1
theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
end
table.insert( outResults, string.sub( self, theStart ) )
return outResults
end
function perform_summation ( s )
t = s:split(",")
sum = 0 -- initialize "sum" variable
tex.sprint ( "+\:" ) -- print the "+" symbol
for i=1,#t do
sum = sum+t[i]
tex.sprint ( t[i] .. "\\" )
end
tex.sprint ( "\midrule" .. sum )
end
endluacode
%% LaTeX-side code:
newcommandmysum[2][c]%
beginarray[#1]@r@
directluaperform_summation("#2")
endarray
begindocument
[
---quad % indicate math axis
mysum12345,6543 qquad
mysum1234567891234,9876543219877 qquad
mysum1,2,3,4 qquad
mysum[t] 57208 , 6207 , 12095 qquad
mysum[b]12345,67890
quad--- % indicate math axis
]
enddocument
1
thenumexpr#2+#3relax
will work fine with numbers having a sum not exceeding2147483647
.... (and it is not even needed to usenumexpr
, TeX non-expandable arithmetic would be fine too)
– user4686
Nov 15 '18 at 9:32
1
(count0=#2relaxadvancecount0by#3relaxthecount0relax)
– user4686
Nov 15 '18 at 9:35
2
@jfbu - But where's the fun if I can't usedirectlua
andtex.sprint
? :-)
– Mico
Nov 15 '18 at 9:37
1
I did suspect something like that and in view of the tremendous number of answers I have myself provided withxint
I can sympathize...:)
– user4686
Nov 15 '18 at 9:43
1
@jfbu When I posted my answer I assumed that it was only a matter of time before you posted a really quickxint
solution that would also work for large integers:) I didn't think of lualatex (+1)...another thing to learn:)
– Andrew
Nov 15 '18 at 9:51
|
show 2 more comments
Here's a LuaLaTeX-based solution. The macro mysum
takes two mandatory arguments -- the numbers to be summed -- and one optional argument, which determines how the array
environment should be placed vertically relative to the math baseline: centered (the default), top-aligned, or bottom-aligned. (If an optional argument is set, it must be listed first and enclosed in square brackets, per the usual LaTeX macro syntax rules.)
documentclassarticle
usepackagebooktabs % for "midrule" macro
newcommandmysum[3][c]%
beginarray[#1]@r@
#2 \ +: #3 \ midrule directluatex.sprint(#2+#3)
endarray
usepackagenewtxtext,newtxmath % optional (Times Roman text and math fonts)
begindocument
[
mysum123456543 qquad
mysum511725256814 qquad
mysum[b]5230576743 qquad
mysum[t]572086207
]
enddocument
Addendum to allow for an arbitrary number of summands rather than exactly two summands. The preceding code dealt with the case provided in the original query, which involved exactly two terms in the summation. The following solution, which is still LuaLaTeX-based, allows for an arbitrary number of summarnds. It works as follows:
The LaTeX macro
mysum
takes one optional argument (the vertical placement indicator, see above) and one mandatory argument: a string of comma-separated numbers. Whitespace is allowed inside the string. Thus,mysum12345,6543
,mysum12345, 6543
,mysum 12345 , 6543
, andmysum12345,6543
, are all equally valid -- and produce the same output, viz., the number18888
.The
mysum
macro performs the following tasks: It sets up anarray
environment, calls the Lua functionperform_summation
to perform most of the actual work, and terminates thearray
environment.The
perform_summation
Lua function begins by splitting the comma-delimited string of numbers into a Lua table, using,
as the separator. (The auxiliary function that performs the splitting was obtained from stackoverflow.)perform_summation
then iterates over the table entries to (a) compute the running subtotal of the entries and (b) print out each entry on a separate row. Finally, the Lua function prints the value of the sum of the entries.
The 3 "-
" symbols located at the left-hand and right-hand edges of the following screenshot merely serve to indicate the location of the math axis.
documentclassarticle
usepackagenewtxtext,newtxmath % optional: Times Roman text and math fonts
usepackagebooktabs % for "midrule" macro
usepackageluacode % for "luacode" environment
%% Lua-side code:
beginluacode
-- The following code is from https://stackoverflow.com/a/19263313:
function string:split( inSplitPattern )
outResults =
local theStart = 1
local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
while theSplitStart do
table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
theStart = theSplitEnd + 1
theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
end
table.insert( outResults, string.sub( self, theStart ) )
return outResults
end
function perform_summation ( s )
t = s:split(",")
sum = 0 -- initialize "sum" variable
tex.sprint ( "+\:" ) -- print the "+" symbol
for i=1,#t do
sum = sum+t[i]
tex.sprint ( t[i] .. "\\" )
end
tex.sprint ( "\midrule" .. sum )
end
endluacode
%% LaTeX-side code:
newcommandmysum[2][c]%
beginarray[#1]@r@
directluaperform_summation("#2")
endarray
begindocument
[
---quad % indicate math axis
mysum12345,6543 qquad
mysum1234567891234,9876543219877 qquad
mysum1,2,3,4 qquad
mysum[t] 57208 , 6207 , 12095 qquad
mysum[b]12345,67890
quad--- % indicate math axis
]
enddocument
Here's a LuaLaTeX-based solution. The macro mysum
takes two mandatory arguments -- the numbers to be summed -- and one optional argument, which determines how the array
environment should be placed vertically relative to the math baseline: centered (the default), top-aligned, or bottom-aligned. (If an optional argument is set, it must be listed first and enclosed in square brackets, per the usual LaTeX macro syntax rules.)
documentclassarticle
usepackagebooktabs % for "midrule" macro
newcommandmysum[3][c]%
beginarray[#1]@r@
#2 \ +: #3 \ midrule directluatex.sprint(#2+#3)
endarray
usepackagenewtxtext,newtxmath % optional (Times Roman text and math fonts)
begindocument
[
mysum123456543 qquad
mysum511725256814 qquad
mysum[b]5230576743 qquad
mysum[t]572086207
]
enddocument
Addendum to allow for an arbitrary number of summands rather than exactly two summands. The preceding code dealt with the case provided in the original query, which involved exactly two terms in the summation. The following solution, which is still LuaLaTeX-based, allows for an arbitrary number of summarnds. It works as follows:
The LaTeX macro
mysum
takes one optional argument (the vertical placement indicator, see above) and one mandatory argument: a string of comma-separated numbers. Whitespace is allowed inside the string. Thus,mysum12345,6543
,mysum12345, 6543
,mysum 12345 , 6543
, andmysum12345,6543
, are all equally valid -- and produce the same output, viz., the number18888
.The
mysum
macro performs the following tasks: It sets up anarray
environment, calls the Lua functionperform_summation
to perform most of the actual work, and terminates thearray
environment.The
perform_summation
Lua function begins by splitting the comma-delimited string of numbers into a Lua table, using,
as the separator. (The auxiliary function that performs the splitting was obtained from stackoverflow.)perform_summation
then iterates over the table entries to (a) compute the running subtotal of the entries and (b) print out each entry on a separate row. Finally, the Lua function prints the value of the sum of the entries.
The 3 "-
" symbols located at the left-hand and right-hand edges of the following screenshot merely serve to indicate the location of the math axis.
documentclassarticle
usepackagenewtxtext,newtxmath % optional: Times Roman text and math fonts
usepackagebooktabs % for "midrule" macro
usepackageluacode % for "luacode" environment
%% Lua-side code:
beginluacode
-- The following code is from https://stackoverflow.com/a/19263313:
function string:split( inSplitPattern )
outResults =
local theStart = 1
local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
while theSplitStart do
table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
theStart = theSplitEnd + 1
theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
end
table.insert( outResults, string.sub( self, theStart ) )
return outResults
end
function perform_summation ( s )
t = s:split(",")
sum = 0 -- initialize "sum" variable
tex.sprint ( "+\:" ) -- print the "+" symbol
for i=1,#t do
sum = sum+t[i]
tex.sprint ( t[i] .. "\\" )
end
tex.sprint ( "\midrule" .. sum )
end
endluacode
%% LaTeX-side code:
newcommandmysum[2][c]%
beginarray[#1]@r@
directluaperform_summation("#2")
endarray
begindocument
[
---quad % indicate math axis
mysum12345,6543 qquad
mysum1234567891234,9876543219877 qquad
mysum1,2,3,4 qquad
mysum[t] 57208 , 6207 , 12095 qquad
mysum[b]12345,67890
quad--- % indicate math axis
]
enddocument
edited Nov 16 '18 at 9:49
answered Nov 15 '18 at 9:30
MicoMico
286k32390779
286k32390779
1
thenumexpr#2+#3relax
will work fine with numbers having a sum not exceeding2147483647
.... (and it is not even needed to usenumexpr
, TeX non-expandable arithmetic would be fine too)
– user4686
Nov 15 '18 at 9:32
1
(count0=#2relaxadvancecount0by#3relaxthecount0relax)
– user4686
Nov 15 '18 at 9:35
2
@jfbu - But where's the fun if I can't usedirectlua
andtex.sprint
? :-)
– Mico
Nov 15 '18 at 9:37
1
I did suspect something like that and in view of the tremendous number of answers I have myself provided withxint
I can sympathize...:)
– user4686
Nov 15 '18 at 9:43
1
@jfbu When I posted my answer I assumed that it was only a matter of time before you posted a really quickxint
solution that would also work for large integers:) I didn't think of lualatex (+1)...another thing to learn:)
– Andrew
Nov 15 '18 at 9:51
|
show 2 more comments
1
thenumexpr#2+#3relax
will work fine with numbers having a sum not exceeding2147483647
.... (and it is not even needed to usenumexpr
, TeX non-expandable arithmetic would be fine too)
– user4686
Nov 15 '18 at 9:32
1
(count0=#2relaxadvancecount0by#3relaxthecount0relax)
– user4686
Nov 15 '18 at 9:35
2
@jfbu - But where's the fun if I can't usedirectlua
andtex.sprint
? :-)
– Mico
Nov 15 '18 at 9:37
1
I did suspect something like that and in view of the tremendous number of answers I have myself provided withxint
I can sympathize...:)
– user4686
Nov 15 '18 at 9:43
1
@jfbu When I posted my answer I assumed that it was only a matter of time before you posted a really quickxint
solution that would also work for large integers:) I didn't think of lualatex (+1)...another thing to learn:)
– Andrew
Nov 15 '18 at 9:51
1
1
thenumexpr#2+#3relax
will work fine with numbers having a sum not exceeding 2147483647
.... (and it is not even needed to use numexpr
, TeX non-expandable arithmetic would be fine too)– user4686
Nov 15 '18 at 9:32
thenumexpr#2+#3relax
will work fine with numbers having a sum not exceeding 2147483647
.... (and it is not even needed to use numexpr
, TeX non-expandable arithmetic would be fine too)– user4686
Nov 15 '18 at 9:32
1
1
(count0=#2relaxadvancecount0by#3relaxthecount0relax)
– user4686
Nov 15 '18 at 9:35
(count0=#2relaxadvancecount0by#3relaxthecount0relax)
– user4686
Nov 15 '18 at 9:35
2
2
@jfbu - But where's the fun if I can't use
directlua
and tex.sprint
? :-)– Mico
Nov 15 '18 at 9:37
@jfbu - But where's the fun if I can't use
directlua
and tex.sprint
? :-)– Mico
Nov 15 '18 at 9:37
1
1
I did suspect something like that and in view of the tremendous number of answers I have myself provided with
xint
I can sympathize... :)
– user4686
Nov 15 '18 at 9:43
I did suspect something like that and in view of the tremendous number of answers I have myself provided with
xint
I can sympathize... :)
– user4686
Nov 15 '18 at 9:43
1
1
@jfbu When I posted my answer I assumed that it was only a matter of time before you posted a really quick
xint
solution that would also work for large integers:) I didn't think of lualatex (+1)...another thing to learn:)– Andrew
Nov 15 '18 at 9:51
@jfbu When I posted my answer I assumed that it was only a matter of time before you posted a really quick
xint
solution that would also work for large integers:) I didn't think of lualatex (+1)...another thing to learn:)– Andrew
Nov 15 '18 at 9:51
|
show 2 more comments
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%2f460078%2fsumming-numbers-like-paper-calculation%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
6
Welcome to TeX.SE. It would be helpful if you composed a fully compilable MWE including
documentclass
and the appropriate packages that sets up the problem. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem.– Peter Grill
Nov 15 '18 at 7:46
1
yes, it is possible (to align to where you like to have). for example by use of an
ąrray or a
tabular`. but first show us, what you try so far.– Zarko
Nov 15 '18 at 7:54
Is your question purely about typesetting, or is it also about performing the summation operations?
– Mico
Nov 15 '18 at 8:06
Duplicate? tex.stackexchange.com/questions/337840/…, tex.stackexchange.com/questions/11702/…, tex.stackexchange.com/questions/219090/…
– Steven B. Segletes
Nov 15 '18 at 10:29