How to define two labels inside an environment to compute the number of pages the environment spans?











up vote
1
down vote

favorite












I would like to count the number of pages that a certain environment (named myenv) spans.
In principle, I set two labels, one in the beginning and one in the end of the environment.
These labels should be unique for every call of the environment, which is the part that
still fails (.tex:19: Missing endcsname inserted):



documentclass{scrartcl}
usepackage{blindtext}

% adapted from https://tex.stackexchange.com/questions/38139/how-can-i-calculate-the-difference-of-2-counters-pageref
usepackage{refcount}
newcommand{pageRange}[2]{%
numbernumexprgetpagerefnumber{#2}-getpagerefnumber{#1}+1relax}

newcounter{howOftenCalled}% counts how often the environment 'myenv' was called (to create unique labels)
setcounter{howOftenCalled}{0}
newenvironment{myenv}[1]{
stepcounter{howOftenCalled}% increase counter
label{startPage:ref{howOftenCalled}}% define the first label
{LARGE The following text covers pageRange{startPage:ref{howOftenCalled}}{endPage:ref{howOftenCalled}} pages:}\% compute the number of pages between the first and the second label
blindtext[#1]
}{label{endPage:ref{howOftenCalled}}clearpage}% defined the second label

begin{document}
begin{myenv}{8}
end{myenv}
clearpage
begin{myenv}{15}
end{myenv}
end{document}









share|improve this question


























    up vote
    1
    down vote

    favorite












    I would like to count the number of pages that a certain environment (named myenv) spans.
    In principle, I set two labels, one in the beginning and one in the end of the environment.
    These labels should be unique for every call of the environment, which is the part that
    still fails (.tex:19: Missing endcsname inserted):



    documentclass{scrartcl}
    usepackage{blindtext}

    % adapted from https://tex.stackexchange.com/questions/38139/how-can-i-calculate-the-difference-of-2-counters-pageref
    usepackage{refcount}
    newcommand{pageRange}[2]{%
    numbernumexprgetpagerefnumber{#2}-getpagerefnumber{#1}+1relax}

    newcounter{howOftenCalled}% counts how often the environment 'myenv' was called (to create unique labels)
    setcounter{howOftenCalled}{0}
    newenvironment{myenv}[1]{
    stepcounter{howOftenCalled}% increase counter
    label{startPage:ref{howOftenCalled}}% define the first label
    {LARGE The following text covers pageRange{startPage:ref{howOftenCalled}}{endPage:ref{howOftenCalled}} pages:}\% compute the number of pages between the first and the second label
    blindtext[#1]
    }{label{endPage:ref{howOftenCalled}}clearpage}% defined the second label

    begin{document}
    begin{myenv}{8}
    end{myenv}
    clearpage
    begin{myenv}{15}
    end{myenv}
    end{document}









    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I would like to count the number of pages that a certain environment (named myenv) spans.
      In principle, I set two labels, one in the beginning and one in the end of the environment.
      These labels should be unique for every call of the environment, which is the part that
      still fails (.tex:19: Missing endcsname inserted):



      documentclass{scrartcl}
      usepackage{blindtext}

      % adapted from https://tex.stackexchange.com/questions/38139/how-can-i-calculate-the-difference-of-2-counters-pageref
      usepackage{refcount}
      newcommand{pageRange}[2]{%
      numbernumexprgetpagerefnumber{#2}-getpagerefnumber{#1}+1relax}

      newcounter{howOftenCalled}% counts how often the environment 'myenv' was called (to create unique labels)
      setcounter{howOftenCalled}{0}
      newenvironment{myenv}[1]{
      stepcounter{howOftenCalled}% increase counter
      label{startPage:ref{howOftenCalled}}% define the first label
      {LARGE The following text covers pageRange{startPage:ref{howOftenCalled}}{endPage:ref{howOftenCalled}} pages:}\% compute the number of pages between the first and the second label
      blindtext[#1]
      }{label{endPage:ref{howOftenCalled}}clearpage}% defined the second label

      begin{document}
      begin{myenv}{8}
      end{myenv}
      clearpage
      begin{myenv}{15}
      end{myenv}
      end{document}









      share|improve this question













      I would like to count the number of pages that a certain environment (named myenv) spans.
      In principle, I set two labels, one in the beginning and one in the end of the environment.
      These labels should be unique for every call of the environment, which is the part that
      still fails (.tex:19: Missing endcsname inserted):



      documentclass{scrartcl}
      usepackage{blindtext}

      % adapted from https://tex.stackexchange.com/questions/38139/how-can-i-calculate-the-difference-of-2-counters-pageref
      usepackage{refcount}
      newcommand{pageRange}[2]{%
      numbernumexprgetpagerefnumber{#2}-getpagerefnumber{#1}+1relax}

      newcounter{howOftenCalled}% counts how often the environment 'myenv' was called (to create unique labels)
      setcounter{howOftenCalled}{0}
      newenvironment{myenv}[1]{
      stepcounter{howOftenCalled}% increase counter
      label{startPage:ref{howOftenCalled}}% define the first label
      {LARGE The following text covers pageRange{startPage:ref{howOftenCalled}}{endPage:ref{howOftenCalled}} pages:}\% compute the number of pages between the first and the second label
      blindtext[#1]
      }{label{endPage:ref{howOftenCalled}}clearpage}% defined the second label

      begin{document}
      begin{myenv}{8}
      end{myenv}
      clearpage
      begin{myenv}{15}
      end{myenv}
      end{document}






      environments counters labels






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      Marius Hofert

      1,99512445




      1,99512445






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Your code works except that you cannot retrieve the value of your counter with ref{howOftenCalled}. If you use thehowOftenCalled} instead, everything works as expected.



          documentclass{scrartcl}
          usepackage{blindtext}

          % adapted from https://tex.stackexchange.com/questions/38139/how-can-i-calculate-the-difference-of-2-counters-pageref
          usepackage{refcount}
          newcommand{pageRange}[2]{%
          numbernumexprgetpagerefnumber{#2}-getpagerefnumber{#1}+1relax}

          newcounter{howOftenCalled}% counts how often the environment 'myenv' was called (to create unique labels)
          setcounter{howOftenCalled}{0}
          newenvironment{myenv}[1]{%
          stepcounter{howOftenCalled}% increase counter
          label{startPage:thehowOftenCalled}% define the first label
          {LARGE The following text covers
          pageRange{startPage:thehowOftenCalled}{endPage:thehowOftenCalled} pages:}\% compute the number of pages between the first and the second label
          blindtext[#1]
          }{label{endPage:thehowOftenCalled}clearpage}% defined the second label

          begin{document}
          begin{myenv}{8}
          end{myenv}
          clearpage
          begin{myenv}{15}
          end{myenv}
          end{document}


          top of p. 1:



          enter image description here



          top of p. 3:



          enter image description here






          share|improve this answer





















          • Note that you may have to remove the aux file if you just copy this code into your old file that caused the problem.
            – marmot
            31 mins ago











          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',
          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%2f461363%2fhow-to-define-two-labels-inside-an-environment-to-compute-the-number-of-pages-th%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          Your code works except that you cannot retrieve the value of your counter with ref{howOftenCalled}. If you use thehowOftenCalled} instead, everything works as expected.



          documentclass{scrartcl}
          usepackage{blindtext}

          % adapted from https://tex.stackexchange.com/questions/38139/how-can-i-calculate-the-difference-of-2-counters-pageref
          usepackage{refcount}
          newcommand{pageRange}[2]{%
          numbernumexprgetpagerefnumber{#2}-getpagerefnumber{#1}+1relax}

          newcounter{howOftenCalled}% counts how often the environment 'myenv' was called (to create unique labels)
          setcounter{howOftenCalled}{0}
          newenvironment{myenv}[1]{%
          stepcounter{howOftenCalled}% increase counter
          label{startPage:thehowOftenCalled}% define the first label
          {LARGE The following text covers
          pageRange{startPage:thehowOftenCalled}{endPage:thehowOftenCalled} pages:}\% compute the number of pages between the first and the second label
          blindtext[#1]
          }{label{endPage:thehowOftenCalled}clearpage}% defined the second label

          begin{document}
          begin{myenv}{8}
          end{myenv}
          clearpage
          begin{myenv}{15}
          end{myenv}
          end{document}


          top of p. 1:



          enter image description here



          top of p. 3:



          enter image description here






          share|improve this answer





















          • Note that you may have to remove the aux file if you just copy this code into your old file that caused the problem.
            – marmot
            31 mins ago















          up vote
          0
          down vote













          Your code works except that you cannot retrieve the value of your counter with ref{howOftenCalled}. If you use thehowOftenCalled} instead, everything works as expected.



          documentclass{scrartcl}
          usepackage{blindtext}

          % adapted from https://tex.stackexchange.com/questions/38139/how-can-i-calculate-the-difference-of-2-counters-pageref
          usepackage{refcount}
          newcommand{pageRange}[2]{%
          numbernumexprgetpagerefnumber{#2}-getpagerefnumber{#1}+1relax}

          newcounter{howOftenCalled}% counts how often the environment 'myenv' was called (to create unique labels)
          setcounter{howOftenCalled}{0}
          newenvironment{myenv}[1]{%
          stepcounter{howOftenCalled}% increase counter
          label{startPage:thehowOftenCalled}% define the first label
          {LARGE The following text covers
          pageRange{startPage:thehowOftenCalled}{endPage:thehowOftenCalled} pages:}\% compute the number of pages between the first and the second label
          blindtext[#1]
          }{label{endPage:thehowOftenCalled}clearpage}% defined the second label

          begin{document}
          begin{myenv}{8}
          end{myenv}
          clearpage
          begin{myenv}{15}
          end{myenv}
          end{document}


          top of p. 1:



          enter image description here



          top of p. 3:



          enter image description here






          share|improve this answer





















          • Note that you may have to remove the aux file if you just copy this code into your old file that caused the problem.
            – marmot
            31 mins ago













          up vote
          0
          down vote










          up vote
          0
          down vote









          Your code works except that you cannot retrieve the value of your counter with ref{howOftenCalled}. If you use thehowOftenCalled} instead, everything works as expected.



          documentclass{scrartcl}
          usepackage{blindtext}

          % adapted from https://tex.stackexchange.com/questions/38139/how-can-i-calculate-the-difference-of-2-counters-pageref
          usepackage{refcount}
          newcommand{pageRange}[2]{%
          numbernumexprgetpagerefnumber{#2}-getpagerefnumber{#1}+1relax}

          newcounter{howOftenCalled}% counts how often the environment 'myenv' was called (to create unique labels)
          setcounter{howOftenCalled}{0}
          newenvironment{myenv}[1]{%
          stepcounter{howOftenCalled}% increase counter
          label{startPage:thehowOftenCalled}% define the first label
          {LARGE The following text covers
          pageRange{startPage:thehowOftenCalled}{endPage:thehowOftenCalled} pages:}\% compute the number of pages between the first and the second label
          blindtext[#1]
          }{label{endPage:thehowOftenCalled}clearpage}% defined the second label

          begin{document}
          begin{myenv}{8}
          end{myenv}
          clearpage
          begin{myenv}{15}
          end{myenv}
          end{document}


          top of p. 1:



          enter image description here



          top of p. 3:



          enter image description here






          share|improve this answer












          Your code works except that you cannot retrieve the value of your counter with ref{howOftenCalled}. If you use thehowOftenCalled} instead, everything works as expected.



          documentclass{scrartcl}
          usepackage{blindtext}

          % adapted from https://tex.stackexchange.com/questions/38139/how-can-i-calculate-the-difference-of-2-counters-pageref
          usepackage{refcount}
          newcommand{pageRange}[2]{%
          numbernumexprgetpagerefnumber{#2}-getpagerefnumber{#1}+1relax}

          newcounter{howOftenCalled}% counts how often the environment 'myenv' was called (to create unique labels)
          setcounter{howOftenCalled}{0}
          newenvironment{myenv}[1]{%
          stepcounter{howOftenCalled}% increase counter
          label{startPage:thehowOftenCalled}% define the first label
          {LARGE The following text covers
          pageRange{startPage:thehowOftenCalled}{endPage:thehowOftenCalled} pages:}\% compute the number of pages between the first and the second label
          blindtext[#1]
          }{label{endPage:thehowOftenCalled}clearpage}% defined the second label

          begin{document}
          begin{myenv}{8}
          end{myenv}
          clearpage
          begin{myenv}{15}
          end{myenv}
          end{document}


          top of p. 1:



          enter image description here



          top of p. 3:



          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          marmot

          77.1k487162




          77.1k487162












          • Note that you may have to remove the aux file if you just copy this code into your old file that caused the problem.
            – marmot
            31 mins ago


















          • Note that you may have to remove the aux file if you just copy this code into your old file that caused the problem.
            – marmot
            31 mins ago
















          Note that you may have to remove the aux file if you just copy this code into your old file that caused the problem.
          – marmot
          31 mins ago




          Note that you may have to remove the aux file if you just copy this code into your old file that caused the problem.
          – marmot
          31 mins ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f461363%2fhow-to-define-two-labels-inside-an-environment-to-compute-the-number-of-pages-th%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

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)