Summing the values of a sequence using expl3expl3 outputting sequence of plistsexpl3 par and sequencesHow to...

Alias to source .bashrc after it's been edited?

SFDX Can query Package Installation Status, can we?

SQL Server Management Studio SSMS 18.0 General Availability release (GA) install fails

Manager is threatning to grade me poorly if I don't complete the project

Which industry am I working in? Software development or financial services?

Number of seconds in 6 weeks

What is the most remote airport from the center of the city it supposedly serves?

Accidentally deleted the "/usr/share" folder

Answer "Justification for travel support" in conference registration form

Quoting Yourself

I caught several of my students plagiarizing. Could it be my fault as a teacher?

What are the differences between credential stuffing and password spraying?

What are the spoon bit of a spoon and fork bit of a fork called?

Where can I go to avoid planes overhead?

Why is Arya visibly scared in the library in S8E3?

How can I get a job without pushing my family's income into a higher tax bracket?

My ID is expired, can I fly to the Bahamas with my passport?

Does this article imply that Turing-Computability is not the same as "effectively computable"?

Would glacier 'trees' be plausible?

Besides the up and down quark, what other quarks are present in daily matter around us?

How to reply this mail from potential PhD professor?

How do I tell my manager that his code review comment is wrong?

What happens if I start too many background jobs?

Can Ghost kill White Walkers or Wights?



Summing the values of a sequence using expl3


expl3 outputting sequence of plistsexpl3 par and sequencesHow to embed a command in an environment using Expl3?Automatic Labels with automatically generated keysDetermining the length of an expl3 sequenceMore efficient implementation using expl3Problem using a sequence in expl3Using a sequence for the x values in a functiontableNested splitting of a sequence using expl3expl3 property list values as `clist` vs token lists













1















In the code below, what should the definition of sumcounters be to make it sum the current values of the counters thm and lemma?



documentclass{book}
usepackage{amsthm}
usepackage{xparse}
newtheorem{thm}{Theorem}[chapter]
newtheorem{lemma}{Lemma}[chapter]
ExplSyntaxOn
seq_new:N g_my_counters
seq_gput_right:Nn g_my_counters { thm }
seq_gput_right:Nn g_my_counters { lemma }
% NewDocumentCommand{sumcounters}{}{< ? >}
ExplSyntaxOff
begin{document}
chapter{Some chapter}
sumcounters % should print 0

begin{thm}
A theorem.
end{thm}
sumcounters % should print 1

begin{lemma}
A lemma.
end{lemma}
sumcounters % should print 2
end{document}









share|improve this question



























    1















    In the code below, what should the definition of sumcounters be to make it sum the current values of the counters thm and lemma?



    documentclass{book}
    usepackage{amsthm}
    usepackage{xparse}
    newtheorem{thm}{Theorem}[chapter]
    newtheorem{lemma}{Lemma}[chapter]
    ExplSyntaxOn
    seq_new:N g_my_counters
    seq_gput_right:Nn g_my_counters { thm }
    seq_gput_right:Nn g_my_counters { lemma }
    % NewDocumentCommand{sumcounters}{}{< ? >}
    ExplSyntaxOff
    begin{document}
    chapter{Some chapter}
    sumcounters % should print 0

    begin{thm}
    A theorem.
    end{thm}
    sumcounters % should print 1

    begin{lemma}
    A lemma.
    end{lemma}
    sumcounters % should print 2
    end{document}









    share|improve this question

























      1












      1








      1








      In the code below, what should the definition of sumcounters be to make it sum the current values of the counters thm and lemma?



      documentclass{book}
      usepackage{amsthm}
      usepackage{xparse}
      newtheorem{thm}{Theorem}[chapter]
      newtheorem{lemma}{Lemma}[chapter]
      ExplSyntaxOn
      seq_new:N g_my_counters
      seq_gput_right:Nn g_my_counters { thm }
      seq_gput_right:Nn g_my_counters { lemma }
      % NewDocumentCommand{sumcounters}{}{< ? >}
      ExplSyntaxOff
      begin{document}
      chapter{Some chapter}
      sumcounters % should print 0

      begin{thm}
      A theorem.
      end{thm}
      sumcounters % should print 1

      begin{lemma}
      A lemma.
      end{lemma}
      sumcounters % should print 2
      end{document}









      share|improve this question














      In the code below, what should the definition of sumcounters be to make it sum the current values of the counters thm and lemma?



      documentclass{book}
      usepackage{amsthm}
      usepackage{xparse}
      newtheorem{thm}{Theorem}[chapter]
      newtheorem{lemma}{Lemma}[chapter]
      ExplSyntaxOn
      seq_new:N g_my_counters
      seq_gput_right:Nn g_my_counters { thm }
      seq_gput_right:Nn g_my_counters { lemma }
      % NewDocumentCommand{sumcounters}{}{< ? >}
      ExplSyntaxOff
      begin{document}
      chapter{Some chapter}
      sumcounters % should print 0

      begin{thm}
      A theorem.
      end{thm}
      sumcounters % should print 1

      begin{lemma}
      A lemma.
      end{lemma}
      sumcounters % should print 2
      end{document}






      expl3 xparse






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 4 hours ago









      noibenoibe

      670113




      670113






















          2 Answers
          2






          active

          oldest

          votes


















          1














          You could probably do this with a temporary counter, such as l_tmpa_int, but the code below defines a new counter l_counter_sum_int and then the sumcounters macro uses seq_map_inline:Nn to add the current values of the counters in g_my_counters, after which it prints the result. The output is the expected:



          enter image description here



          Here is the full code:



          documentclass{book}
          usepackage{amsthm}
          usepackage{xparse}
          newtheorem{thm}{Theorem}[chapter]
          newtheorem{lemma}{Lemma}[chapter]
          ExplSyntaxOn
          seq_new:N g_my_counters
          seq_gput_right:Nn g_my_counters { thm }
          seq_gput_right:Nn g_my_counters { lemma }
          int_new:N l_counter_sum_int% local counter for adding counter values
          NewDocumentCommandsumcounters{}{
          int_zero:N l_counter_sum_int% set l_counter_sum_int to 0
          seq_map_inline:Nn g_my_counters {% add counters in g_my_counters
          int_add:Nn l_counter_sum_int {value{##1}}
          }
          int_use:N l_counter_sum_int% print the result
          }
          ExplSyntaxOff
          begin{document}
          chapter{Some chapter}
          sumcounters % should print 0

          begin{thm}
          A theorem.
          end{thm}
          sumcounters % should print 1

          begin{lemma}
          A lemma.
          end{lemma}
          sumcounters % should print 2
          end{document}





          share|improve this answer
























          • Does it make a difference if I put int_new:N l_counter_sum_int inside the definition of sumcounters?

            – noibe
            3 hours ago











          • @noibe Yes, it makes a difference: the code above will give an error because the second and subsequent calls of sumcounters will try to define a define a counter that already exists. Alternatively, as I suggested, you can instead use l_tmpa_int in sumcounters, in which case you can drop the int_new:N command completely. (I just checked and this works.)

            – Andrew
            3 hours ago













          • If instead of directly using sumcounters I type ifnumsumcounters=0 yes else no fi I get some unexpected results, while I'de expect a yes and two no's. What am I doing wrong?

            – noibe
            3 hours ago











          • @noibe As Henri says, this is an expansion issue. I didn't know you would want to use sumcounters this way. I don't see an easy way to make my code work, so Henri's expandable solution is the way to go.

            – Andrew
            1 hour ago



















          2














          Instead of performing an assignment, you can also calculate the sum fully-expandably. This has the advantage that you can use it in conditionals, such as



          ifnumsumcounters=0 ... fi


          I also want to remind you of the expl3 convention to use Hungarian notation for variables, i.e. a variable should carry in its name the data type it holds, usually as a suffix.



          documentclass{book}
          usepackage{amsthm}
          usepackage{xparse}
          newtheorem{thm}{Theorem}[chapter]
          newtheorem{lemma}{Lemma}[chapter]

          ExplSyntaxOn

          seq_new:N g_my_counters_seq
          seq_gput_right:Nn g_my_counters_seq { thm }
          seq_gput_right:Nn g_my_counters_seq { lemma }

          cs_new:Npn my_plus_value:n #1
          {
          + (value{#1})
          }

          NewExpandableDocumentCommand sumcounters { }
          {
          int_eval:n
          {
          ( 0 seq_map_function:NN g_my_counters_seq my_plus_value:n )
          }
          }

          ExplSyntaxOff

          begin{document}
          chapter{Some chapter}
          sumcounters % should print 0

          begin{thm}
          A theorem.
          end{thm}
          sumcounters % should print 1

          begin{lemma}
          A lemma.
          end{lemma}
          sumcounters % should print 2
          end{document}





          share|improve this answer
























            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
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f488517%2fsumming-the-values-of-a-sequence-using-expl3%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You could probably do this with a temporary counter, such as l_tmpa_int, but the code below defines a new counter l_counter_sum_int and then the sumcounters macro uses seq_map_inline:Nn to add the current values of the counters in g_my_counters, after which it prints the result. The output is the expected:



            enter image description here



            Here is the full code:



            documentclass{book}
            usepackage{amsthm}
            usepackage{xparse}
            newtheorem{thm}{Theorem}[chapter]
            newtheorem{lemma}{Lemma}[chapter]
            ExplSyntaxOn
            seq_new:N g_my_counters
            seq_gput_right:Nn g_my_counters { thm }
            seq_gput_right:Nn g_my_counters { lemma }
            int_new:N l_counter_sum_int% local counter for adding counter values
            NewDocumentCommandsumcounters{}{
            int_zero:N l_counter_sum_int% set l_counter_sum_int to 0
            seq_map_inline:Nn g_my_counters {% add counters in g_my_counters
            int_add:Nn l_counter_sum_int {value{##1}}
            }
            int_use:N l_counter_sum_int% print the result
            }
            ExplSyntaxOff
            begin{document}
            chapter{Some chapter}
            sumcounters % should print 0

            begin{thm}
            A theorem.
            end{thm}
            sumcounters % should print 1

            begin{lemma}
            A lemma.
            end{lemma}
            sumcounters % should print 2
            end{document}





            share|improve this answer
























            • Does it make a difference if I put int_new:N l_counter_sum_int inside the definition of sumcounters?

              – noibe
              3 hours ago











            • @noibe Yes, it makes a difference: the code above will give an error because the second and subsequent calls of sumcounters will try to define a define a counter that already exists. Alternatively, as I suggested, you can instead use l_tmpa_int in sumcounters, in which case you can drop the int_new:N command completely. (I just checked and this works.)

              – Andrew
              3 hours ago













            • If instead of directly using sumcounters I type ifnumsumcounters=0 yes else no fi I get some unexpected results, while I'de expect a yes and two no's. What am I doing wrong?

              – noibe
              3 hours ago











            • @noibe As Henri says, this is an expansion issue. I didn't know you would want to use sumcounters this way. I don't see an easy way to make my code work, so Henri's expandable solution is the way to go.

              – Andrew
              1 hour ago
















            1














            You could probably do this with a temporary counter, such as l_tmpa_int, but the code below defines a new counter l_counter_sum_int and then the sumcounters macro uses seq_map_inline:Nn to add the current values of the counters in g_my_counters, after which it prints the result. The output is the expected:



            enter image description here



            Here is the full code:



            documentclass{book}
            usepackage{amsthm}
            usepackage{xparse}
            newtheorem{thm}{Theorem}[chapter]
            newtheorem{lemma}{Lemma}[chapter]
            ExplSyntaxOn
            seq_new:N g_my_counters
            seq_gput_right:Nn g_my_counters { thm }
            seq_gput_right:Nn g_my_counters { lemma }
            int_new:N l_counter_sum_int% local counter for adding counter values
            NewDocumentCommandsumcounters{}{
            int_zero:N l_counter_sum_int% set l_counter_sum_int to 0
            seq_map_inline:Nn g_my_counters {% add counters in g_my_counters
            int_add:Nn l_counter_sum_int {value{##1}}
            }
            int_use:N l_counter_sum_int% print the result
            }
            ExplSyntaxOff
            begin{document}
            chapter{Some chapter}
            sumcounters % should print 0

            begin{thm}
            A theorem.
            end{thm}
            sumcounters % should print 1

            begin{lemma}
            A lemma.
            end{lemma}
            sumcounters % should print 2
            end{document}





            share|improve this answer
























            • Does it make a difference if I put int_new:N l_counter_sum_int inside the definition of sumcounters?

              – noibe
              3 hours ago











            • @noibe Yes, it makes a difference: the code above will give an error because the second and subsequent calls of sumcounters will try to define a define a counter that already exists. Alternatively, as I suggested, you can instead use l_tmpa_int in sumcounters, in which case you can drop the int_new:N command completely. (I just checked and this works.)

              – Andrew
              3 hours ago













            • If instead of directly using sumcounters I type ifnumsumcounters=0 yes else no fi I get some unexpected results, while I'de expect a yes and two no's. What am I doing wrong?

              – noibe
              3 hours ago











            • @noibe As Henri says, this is an expansion issue. I didn't know you would want to use sumcounters this way. I don't see an easy way to make my code work, so Henri's expandable solution is the way to go.

              – Andrew
              1 hour ago














            1












            1








            1







            You could probably do this with a temporary counter, such as l_tmpa_int, but the code below defines a new counter l_counter_sum_int and then the sumcounters macro uses seq_map_inline:Nn to add the current values of the counters in g_my_counters, after which it prints the result. The output is the expected:



            enter image description here



            Here is the full code:



            documentclass{book}
            usepackage{amsthm}
            usepackage{xparse}
            newtheorem{thm}{Theorem}[chapter]
            newtheorem{lemma}{Lemma}[chapter]
            ExplSyntaxOn
            seq_new:N g_my_counters
            seq_gput_right:Nn g_my_counters { thm }
            seq_gput_right:Nn g_my_counters { lemma }
            int_new:N l_counter_sum_int% local counter for adding counter values
            NewDocumentCommandsumcounters{}{
            int_zero:N l_counter_sum_int% set l_counter_sum_int to 0
            seq_map_inline:Nn g_my_counters {% add counters in g_my_counters
            int_add:Nn l_counter_sum_int {value{##1}}
            }
            int_use:N l_counter_sum_int% print the result
            }
            ExplSyntaxOff
            begin{document}
            chapter{Some chapter}
            sumcounters % should print 0

            begin{thm}
            A theorem.
            end{thm}
            sumcounters % should print 1

            begin{lemma}
            A lemma.
            end{lemma}
            sumcounters % should print 2
            end{document}





            share|improve this answer













            You could probably do this with a temporary counter, such as l_tmpa_int, but the code below defines a new counter l_counter_sum_int and then the sumcounters macro uses seq_map_inline:Nn to add the current values of the counters in g_my_counters, after which it prints the result. The output is the expected:



            enter image description here



            Here is the full code:



            documentclass{book}
            usepackage{amsthm}
            usepackage{xparse}
            newtheorem{thm}{Theorem}[chapter]
            newtheorem{lemma}{Lemma}[chapter]
            ExplSyntaxOn
            seq_new:N g_my_counters
            seq_gput_right:Nn g_my_counters { thm }
            seq_gput_right:Nn g_my_counters { lemma }
            int_new:N l_counter_sum_int% local counter for adding counter values
            NewDocumentCommandsumcounters{}{
            int_zero:N l_counter_sum_int% set l_counter_sum_int to 0
            seq_map_inline:Nn g_my_counters {% add counters in g_my_counters
            int_add:Nn l_counter_sum_int {value{##1}}
            }
            int_use:N l_counter_sum_int% print the result
            }
            ExplSyntaxOff
            begin{document}
            chapter{Some chapter}
            sumcounters % should print 0

            begin{thm}
            A theorem.
            end{thm}
            sumcounters % should print 1

            begin{lemma}
            A lemma.
            end{lemma}
            sumcounters % should print 2
            end{document}






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 3 hours ago









            AndrewAndrew

            31.8k34583




            31.8k34583













            • Does it make a difference if I put int_new:N l_counter_sum_int inside the definition of sumcounters?

              – noibe
              3 hours ago











            • @noibe Yes, it makes a difference: the code above will give an error because the second and subsequent calls of sumcounters will try to define a define a counter that already exists. Alternatively, as I suggested, you can instead use l_tmpa_int in sumcounters, in which case you can drop the int_new:N command completely. (I just checked and this works.)

              – Andrew
              3 hours ago













            • If instead of directly using sumcounters I type ifnumsumcounters=0 yes else no fi I get some unexpected results, while I'de expect a yes and two no's. What am I doing wrong?

              – noibe
              3 hours ago











            • @noibe As Henri says, this is an expansion issue. I didn't know you would want to use sumcounters this way. I don't see an easy way to make my code work, so Henri's expandable solution is the way to go.

              – Andrew
              1 hour ago



















            • Does it make a difference if I put int_new:N l_counter_sum_int inside the definition of sumcounters?

              – noibe
              3 hours ago











            • @noibe Yes, it makes a difference: the code above will give an error because the second and subsequent calls of sumcounters will try to define a define a counter that already exists. Alternatively, as I suggested, you can instead use l_tmpa_int in sumcounters, in which case you can drop the int_new:N command completely. (I just checked and this works.)

              – Andrew
              3 hours ago













            • If instead of directly using sumcounters I type ifnumsumcounters=0 yes else no fi I get some unexpected results, while I'de expect a yes and two no's. What am I doing wrong?

              – noibe
              3 hours ago











            • @noibe As Henri says, this is an expansion issue. I didn't know you would want to use sumcounters this way. I don't see an easy way to make my code work, so Henri's expandable solution is the way to go.

              – Andrew
              1 hour ago

















            Does it make a difference if I put int_new:N l_counter_sum_int inside the definition of sumcounters?

            – noibe
            3 hours ago





            Does it make a difference if I put int_new:N l_counter_sum_int inside the definition of sumcounters?

            – noibe
            3 hours ago













            @noibe Yes, it makes a difference: the code above will give an error because the second and subsequent calls of sumcounters will try to define a define a counter that already exists. Alternatively, as I suggested, you can instead use l_tmpa_int in sumcounters, in which case you can drop the int_new:N command completely. (I just checked and this works.)

            – Andrew
            3 hours ago







            @noibe Yes, it makes a difference: the code above will give an error because the second and subsequent calls of sumcounters will try to define a define a counter that already exists. Alternatively, as I suggested, you can instead use l_tmpa_int in sumcounters, in which case you can drop the int_new:N command completely. (I just checked and this works.)

            – Andrew
            3 hours ago















            If instead of directly using sumcounters I type ifnumsumcounters=0 yes else no fi I get some unexpected results, while I'de expect a yes and two no's. What am I doing wrong?

            – noibe
            3 hours ago





            If instead of directly using sumcounters I type ifnumsumcounters=0 yes else no fi I get some unexpected results, while I'de expect a yes and two no's. What am I doing wrong?

            – noibe
            3 hours ago













            @noibe As Henri says, this is an expansion issue. I didn't know you would want to use sumcounters this way. I don't see an easy way to make my code work, so Henri's expandable solution is the way to go.

            – Andrew
            1 hour ago





            @noibe As Henri says, this is an expansion issue. I didn't know you would want to use sumcounters this way. I don't see an easy way to make my code work, so Henri's expandable solution is the way to go.

            – Andrew
            1 hour ago











            2














            Instead of performing an assignment, you can also calculate the sum fully-expandably. This has the advantage that you can use it in conditionals, such as



            ifnumsumcounters=0 ... fi


            I also want to remind you of the expl3 convention to use Hungarian notation for variables, i.e. a variable should carry in its name the data type it holds, usually as a suffix.



            documentclass{book}
            usepackage{amsthm}
            usepackage{xparse}
            newtheorem{thm}{Theorem}[chapter]
            newtheorem{lemma}{Lemma}[chapter]

            ExplSyntaxOn

            seq_new:N g_my_counters_seq
            seq_gput_right:Nn g_my_counters_seq { thm }
            seq_gput_right:Nn g_my_counters_seq { lemma }

            cs_new:Npn my_plus_value:n #1
            {
            + (value{#1})
            }

            NewExpandableDocumentCommand sumcounters { }
            {
            int_eval:n
            {
            ( 0 seq_map_function:NN g_my_counters_seq my_plus_value:n )
            }
            }

            ExplSyntaxOff

            begin{document}
            chapter{Some chapter}
            sumcounters % should print 0

            begin{thm}
            A theorem.
            end{thm}
            sumcounters % should print 1

            begin{lemma}
            A lemma.
            end{lemma}
            sumcounters % should print 2
            end{document}





            share|improve this answer




























              2














              Instead of performing an assignment, you can also calculate the sum fully-expandably. This has the advantage that you can use it in conditionals, such as



              ifnumsumcounters=0 ... fi


              I also want to remind you of the expl3 convention to use Hungarian notation for variables, i.e. a variable should carry in its name the data type it holds, usually as a suffix.



              documentclass{book}
              usepackage{amsthm}
              usepackage{xparse}
              newtheorem{thm}{Theorem}[chapter]
              newtheorem{lemma}{Lemma}[chapter]

              ExplSyntaxOn

              seq_new:N g_my_counters_seq
              seq_gput_right:Nn g_my_counters_seq { thm }
              seq_gput_right:Nn g_my_counters_seq { lemma }

              cs_new:Npn my_plus_value:n #1
              {
              + (value{#1})
              }

              NewExpandableDocumentCommand sumcounters { }
              {
              int_eval:n
              {
              ( 0 seq_map_function:NN g_my_counters_seq my_plus_value:n )
              }
              }

              ExplSyntaxOff

              begin{document}
              chapter{Some chapter}
              sumcounters % should print 0

              begin{thm}
              A theorem.
              end{thm}
              sumcounters % should print 1

              begin{lemma}
              A lemma.
              end{lemma}
              sumcounters % should print 2
              end{document}





              share|improve this answer


























                2












                2








                2







                Instead of performing an assignment, you can also calculate the sum fully-expandably. This has the advantage that you can use it in conditionals, such as



                ifnumsumcounters=0 ... fi


                I also want to remind you of the expl3 convention to use Hungarian notation for variables, i.e. a variable should carry in its name the data type it holds, usually as a suffix.



                documentclass{book}
                usepackage{amsthm}
                usepackage{xparse}
                newtheorem{thm}{Theorem}[chapter]
                newtheorem{lemma}{Lemma}[chapter]

                ExplSyntaxOn

                seq_new:N g_my_counters_seq
                seq_gput_right:Nn g_my_counters_seq { thm }
                seq_gput_right:Nn g_my_counters_seq { lemma }

                cs_new:Npn my_plus_value:n #1
                {
                + (value{#1})
                }

                NewExpandableDocumentCommand sumcounters { }
                {
                int_eval:n
                {
                ( 0 seq_map_function:NN g_my_counters_seq my_plus_value:n )
                }
                }

                ExplSyntaxOff

                begin{document}
                chapter{Some chapter}
                sumcounters % should print 0

                begin{thm}
                A theorem.
                end{thm}
                sumcounters % should print 1

                begin{lemma}
                A lemma.
                end{lemma}
                sumcounters % should print 2
                end{document}





                share|improve this answer













                Instead of performing an assignment, you can also calculate the sum fully-expandably. This has the advantage that you can use it in conditionals, such as



                ifnumsumcounters=0 ... fi


                I also want to remind you of the expl3 convention to use Hungarian notation for variables, i.e. a variable should carry in its name the data type it holds, usually as a suffix.



                documentclass{book}
                usepackage{amsthm}
                usepackage{xparse}
                newtheorem{thm}{Theorem}[chapter]
                newtheorem{lemma}{Lemma}[chapter]

                ExplSyntaxOn

                seq_new:N g_my_counters_seq
                seq_gput_right:Nn g_my_counters_seq { thm }
                seq_gput_right:Nn g_my_counters_seq { lemma }

                cs_new:Npn my_plus_value:n #1
                {
                + (value{#1})
                }

                NewExpandableDocumentCommand sumcounters { }
                {
                int_eval:n
                {
                ( 0 seq_map_function:NN g_my_counters_seq my_plus_value:n )
                }
                }

                ExplSyntaxOff

                begin{document}
                chapter{Some chapter}
                sumcounters % should print 0

                begin{thm}
                A theorem.
                end{thm}
                sumcounters % should print 1

                begin{lemma}
                A lemma.
                end{lemma}
                sumcounters % should print 2
                end{document}






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 hours ago









                Henri MenkeHenri Menke

                78.2k8171285




                78.2k8171285






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f488517%2fsumming-the-values-of-a-sequence-using-expl3%23new-answer', 'question_page');
                    }
                    );

                    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







                    Popular posts from this blog

                    Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                    Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                    Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...