Indexing variables











up vote
4
down vote

favorite












Is it possible to create variables that can be defined using integers. I would create an example to work with but I have no idea how to start. Hopefully the snippet I have included makes sense.



% creates variables wk[1], wk[2], ... , wk[10]
foreach i in {1,2,...,10}{newcommandwk[i]{}}

% stores the value 'this is week 2' into wk[2]
wk[2]{this is week 2}

% outputs 'this is week 2'
wk[2]









share|improve this question






















  • It would help if you stated the objective of this project; e.g., what you're trying to do with the elements of the wk string vector. It would also help if you stated which TeX engine you use: pdfLaTeX, XeLaTeX, LuaLaTeX, or something else?
    – Mico
    Dec 9 at 7:26










  • Probably related: tex.stackexchange.com/questions/395752/numbers-lists-in-latex/…
    – Andrew
    Dec 9 at 12:19















up vote
4
down vote

favorite












Is it possible to create variables that can be defined using integers. I would create an example to work with but I have no idea how to start. Hopefully the snippet I have included makes sense.



% creates variables wk[1], wk[2], ... , wk[10]
foreach i in {1,2,...,10}{newcommandwk[i]{}}

% stores the value 'this is week 2' into wk[2]
wk[2]{this is week 2}

% outputs 'this is week 2'
wk[2]









share|improve this question






















  • It would help if you stated the objective of this project; e.g., what you're trying to do with the elements of the wk string vector. It would also help if you stated which TeX engine you use: pdfLaTeX, XeLaTeX, LuaLaTeX, or something else?
    – Mico
    Dec 9 at 7:26










  • Probably related: tex.stackexchange.com/questions/395752/numbers-lists-in-latex/…
    – Andrew
    Dec 9 at 12:19













up vote
4
down vote

favorite









up vote
4
down vote

favorite











Is it possible to create variables that can be defined using integers. I would create an example to work with but I have no idea how to start. Hopefully the snippet I have included makes sense.



% creates variables wk[1], wk[2], ... , wk[10]
foreach i in {1,2,...,10}{newcommandwk[i]{}}

% stores the value 'this is week 2' into wk[2]
wk[2]{this is week 2}

% outputs 'this is week 2'
wk[2]









share|improve this question













Is it possible to create variables that can be defined using integers. I would create an example to work with but I have no idea how to start. Hopefully the snippet I have included makes sense.



% creates variables wk[1], wk[2], ... , wk[10]
foreach i in {1,2,...,10}{newcommandwk[i]{}}

% stores the value 'this is week 2' into wk[2]
wk[2]{this is week 2}

% outputs 'this is week 2'
wk[2]






variable






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 9 at 6:25









Garth Fleming

37218




37218












  • It would help if you stated the objective of this project; e.g., what you're trying to do with the elements of the wk string vector. It would also help if you stated which TeX engine you use: pdfLaTeX, XeLaTeX, LuaLaTeX, or something else?
    – Mico
    Dec 9 at 7:26










  • Probably related: tex.stackexchange.com/questions/395752/numbers-lists-in-latex/…
    – Andrew
    Dec 9 at 12:19


















  • It would help if you stated the objective of this project; e.g., what you're trying to do with the elements of the wk string vector. It would also help if you stated which TeX engine you use: pdfLaTeX, XeLaTeX, LuaLaTeX, or something else?
    – Mico
    Dec 9 at 7:26










  • Probably related: tex.stackexchange.com/questions/395752/numbers-lists-in-latex/…
    – Andrew
    Dec 9 at 12:19
















It would help if you stated the objective of this project; e.g., what you're trying to do with the elements of the wk string vector. It would also help if you stated which TeX engine you use: pdfLaTeX, XeLaTeX, LuaLaTeX, or something else?
– Mico
Dec 9 at 7:26




It would help if you stated the objective of this project; e.g., what you're trying to do with the elements of the wk string vector. It would also help if you stated which TeX engine you use: pdfLaTeX, XeLaTeX, LuaLaTeX, or something else?
– Mico
Dec 9 at 7:26












Probably related: tex.stackexchange.com/questions/395752/numbers-lists-in-latex/…
– Andrew
Dec 9 at 12:19




Probably related: tex.stackexchange.com/questions/395752/numbers-lists-in-latex/…
– Andrew
Dec 9 at 12:19










3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










Variables?



(La)TeX is a macro language with macro-expansion/replacement.



You have macros, registers and parameters.



You can use macros that don't process arguments as variables.



In any case you need to be aware of how macro-expansion works and about the order in time in which single expansion-steps and assignments are carried out.



I'd just use a macro name which takes a sequence of tokens nested in curly braces for the name of a control-sequence-token and via csname..endcsname constructs/delivers that control-sequence-token while leaving things in place that possibly occur between name and the opening-curly-brace.

That control-sequence-token in turn can be (re)defined to be a macro or can be expanded, just as you like:



documentclass{article}
usepackage{pgffor}

newcommandname{}longdefname#1#{romannumeral0innername{#1}}%
newcommandinnername[2]{%
expandafterexchangeexpandafter{csname #2endcsname}{0 #1}%
}%
newcommandexchange[2]{#2#1}%

% This loop defines macros/control-word-tokens wk[1], wk[2], ... , wk[10]
% with "empty" replacement-text:
foreach i in {1,2,...,10}{%
namenewcommand*{wk[i]}{}%
% foreach opens up a local scope.
% Thus we need to turn assignments restricted to that scope into global
% assignments:
namenamegloballet{wk[i]}={wk[i]}%
} %

begin{document}

% This "stores the value 'this is week 2' into wk[2]".
% Better: This redefines the macro wk[2] to expand to the phrase "this is week 2".
namerenewcommand*{wk[2]}{this is week 2}

% As there is nothing between name and the opening brace, this just delivers the
% control-sequence-token wk[2] which in turn gets expanded as usual while that
% expansion yields 'this is week 2':
name{wk[2]}

% If eTeX-extensions are available, you can use numbernumexpr.. for calculations:
name{wk[numbernumexpr(2*3)-5+1relax]}

% this shows the definition/meaning of wk[2] on the screen/in the .log-file:
%nameshow{wk[2]}

% this prints the meaning of wk[2]
texttt{namestring{wk[2]}: namemeaning{wk[2]}}

end{document}





share|improve this answer























  • thank you, this is great. I particularly like the commenting.
    – Garth Fleming
    Dec 9 at 23:47


















up vote
2
down vote













Here's a simple version that makes use of the xparse package to define a new array-like command with a first parameter in square brackets and an optional second parameter in braces:



documentclass{article}
usepackage{xparse}
usepackage{pgffor}

newcommandmakearray[1]{%
expandafterNewDocumentCommandcsname#1endcsname{rg}{%
IfValueTF{##2}{%
expandafterdefcsname #1@##1endcsname{##2}%
}{%
csname #1@##1endcsname
}%
}
}

begin{document}
makearray{wk}

wk[1]{this is week 1}
wk[3]{this is week 3}

foreach i in {1,2,3} {wk[i]par}
end{document}


You use it like



makearray{name}


to define a new array command name. Then your can use



name[key]{value}


to set a value at position key, and



name[key]


to output the stored value at position key. If no key was set before, the result is empty (or equal to relax, to be more precise).



Thus, the output for the above example is




this is week 1

this is week 3







share|improve this answer






























    up vote
    0
    down vote













    Since you seem to want it in contexts of loops, here's an implementation that allows arithmetic expressions in the argument.



    I distinguish between the phases of setting and retrieving values. The syntax you propose disallows wk being expandable, because one would have to check whether it is used for setting a value.



    documentclass{article}
    usepackage{xparse}

    ExplSyntaxOn
    NewExpandableDocumentCommand{wk}{m}
    {
    prop_item:Nf g_garth_week_prop { int_eval:n { #1 } }
    }
    NewDocumentCommand{setwk}{mm}
    {
    prop_gput:Nfn g_garth_week_prop { int_eval:n { #1 } } { #2 }
    }

    cs_generate_variant:Nn prop_item:Nn { Nf }
    cs_generate_variant:Nn prop_gput:Nnn { Nf }

    prop_new:N g_garth_week_prop

    ExplSyntaxOff

    setwk{2}{This is week two}
    setwk{2+1}{This is week three}

    begin{document}

    wk{2}

    wk{7-2*2}

    end{document}


    enter image description here



    Here is an abstraction layer that allows to define as many arrays as you want.



    documentclass{article}
    usepackage{xparse}

    ExplSyntaxOn
    NewDocumentCommand{NewArray}{m}
    {
    prop_new:c { g_garth_#1_prop }
    cs_new_protected:cpn { set#1 } ##1 ##2
    {
    prop_gput:cfn { g_garth_#1_prop } { int_eval:n { ##1 } } { ##2 }
    }
    cs_new:cpn { #1 } ##1
    {
    prop_item:cf { g_garth_#1_prop } { int_eval:n { ##1 } }
    }
    }

    cs_generate_variant:Nn prop_item:Nn { cf }
    cs_generate_variant:Nn prop_gput:Nnn { cf }

    ExplSyntaxOff

    NewArray{wk}

    setwk{2}{This is week two}
    setwk{2+1}{This is week three}

    begin{document}

    wk{2}

    wk{7-2*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',
      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%2f463902%2findexing-variables%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted










      Variables?



      (La)TeX is a macro language with macro-expansion/replacement.



      You have macros, registers and parameters.



      You can use macros that don't process arguments as variables.



      In any case you need to be aware of how macro-expansion works and about the order in time in which single expansion-steps and assignments are carried out.



      I'd just use a macro name which takes a sequence of tokens nested in curly braces for the name of a control-sequence-token and via csname..endcsname constructs/delivers that control-sequence-token while leaving things in place that possibly occur between name and the opening-curly-brace.

      That control-sequence-token in turn can be (re)defined to be a macro or can be expanded, just as you like:



      documentclass{article}
      usepackage{pgffor}

      newcommandname{}longdefname#1#{romannumeral0innername{#1}}%
      newcommandinnername[2]{%
      expandafterexchangeexpandafter{csname #2endcsname}{0 #1}%
      }%
      newcommandexchange[2]{#2#1}%

      % This loop defines macros/control-word-tokens wk[1], wk[2], ... , wk[10]
      % with "empty" replacement-text:
      foreach i in {1,2,...,10}{%
      namenewcommand*{wk[i]}{}%
      % foreach opens up a local scope.
      % Thus we need to turn assignments restricted to that scope into global
      % assignments:
      namenamegloballet{wk[i]}={wk[i]}%
      } %

      begin{document}

      % This "stores the value 'this is week 2' into wk[2]".
      % Better: This redefines the macro wk[2] to expand to the phrase "this is week 2".
      namerenewcommand*{wk[2]}{this is week 2}

      % As there is nothing between name and the opening brace, this just delivers the
      % control-sequence-token wk[2] which in turn gets expanded as usual while that
      % expansion yields 'this is week 2':
      name{wk[2]}

      % If eTeX-extensions are available, you can use numbernumexpr.. for calculations:
      name{wk[numbernumexpr(2*3)-5+1relax]}

      % this shows the definition/meaning of wk[2] on the screen/in the .log-file:
      %nameshow{wk[2]}

      % this prints the meaning of wk[2]
      texttt{namestring{wk[2]}: namemeaning{wk[2]}}

      end{document}





      share|improve this answer























      • thank you, this is great. I particularly like the commenting.
        – Garth Fleming
        Dec 9 at 23:47















      up vote
      0
      down vote



      accepted










      Variables?



      (La)TeX is a macro language with macro-expansion/replacement.



      You have macros, registers and parameters.



      You can use macros that don't process arguments as variables.



      In any case you need to be aware of how macro-expansion works and about the order in time in which single expansion-steps and assignments are carried out.



      I'd just use a macro name which takes a sequence of tokens nested in curly braces for the name of a control-sequence-token and via csname..endcsname constructs/delivers that control-sequence-token while leaving things in place that possibly occur between name and the opening-curly-brace.

      That control-sequence-token in turn can be (re)defined to be a macro or can be expanded, just as you like:



      documentclass{article}
      usepackage{pgffor}

      newcommandname{}longdefname#1#{romannumeral0innername{#1}}%
      newcommandinnername[2]{%
      expandafterexchangeexpandafter{csname #2endcsname}{0 #1}%
      }%
      newcommandexchange[2]{#2#1}%

      % This loop defines macros/control-word-tokens wk[1], wk[2], ... , wk[10]
      % with "empty" replacement-text:
      foreach i in {1,2,...,10}{%
      namenewcommand*{wk[i]}{}%
      % foreach opens up a local scope.
      % Thus we need to turn assignments restricted to that scope into global
      % assignments:
      namenamegloballet{wk[i]}={wk[i]}%
      } %

      begin{document}

      % This "stores the value 'this is week 2' into wk[2]".
      % Better: This redefines the macro wk[2] to expand to the phrase "this is week 2".
      namerenewcommand*{wk[2]}{this is week 2}

      % As there is nothing between name and the opening brace, this just delivers the
      % control-sequence-token wk[2] which in turn gets expanded as usual while that
      % expansion yields 'this is week 2':
      name{wk[2]}

      % If eTeX-extensions are available, you can use numbernumexpr.. for calculations:
      name{wk[numbernumexpr(2*3)-5+1relax]}

      % this shows the definition/meaning of wk[2] on the screen/in the .log-file:
      %nameshow{wk[2]}

      % this prints the meaning of wk[2]
      texttt{namestring{wk[2]}: namemeaning{wk[2]}}

      end{document}





      share|improve this answer























      • thank you, this is great. I particularly like the commenting.
        – Garth Fleming
        Dec 9 at 23:47













      up vote
      0
      down vote



      accepted







      up vote
      0
      down vote



      accepted






      Variables?



      (La)TeX is a macro language with macro-expansion/replacement.



      You have macros, registers and parameters.



      You can use macros that don't process arguments as variables.



      In any case you need to be aware of how macro-expansion works and about the order in time in which single expansion-steps and assignments are carried out.



      I'd just use a macro name which takes a sequence of tokens nested in curly braces for the name of a control-sequence-token and via csname..endcsname constructs/delivers that control-sequence-token while leaving things in place that possibly occur between name and the opening-curly-brace.

      That control-sequence-token in turn can be (re)defined to be a macro or can be expanded, just as you like:



      documentclass{article}
      usepackage{pgffor}

      newcommandname{}longdefname#1#{romannumeral0innername{#1}}%
      newcommandinnername[2]{%
      expandafterexchangeexpandafter{csname #2endcsname}{0 #1}%
      }%
      newcommandexchange[2]{#2#1}%

      % This loop defines macros/control-word-tokens wk[1], wk[2], ... , wk[10]
      % with "empty" replacement-text:
      foreach i in {1,2,...,10}{%
      namenewcommand*{wk[i]}{}%
      % foreach opens up a local scope.
      % Thus we need to turn assignments restricted to that scope into global
      % assignments:
      namenamegloballet{wk[i]}={wk[i]}%
      } %

      begin{document}

      % This "stores the value 'this is week 2' into wk[2]".
      % Better: This redefines the macro wk[2] to expand to the phrase "this is week 2".
      namerenewcommand*{wk[2]}{this is week 2}

      % As there is nothing between name and the opening brace, this just delivers the
      % control-sequence-token wk[2] which in turn gets expanded as usual while that
      % expansion yields 'this is week 2':
      name{wk[2]}

      % If eTeX-extensions are available, you can use numbernumexpr.. for calculations:
      name{wk[numbernumexpr(2*3)-5+1relax]}

      % this shows the definition/meaning of wk[2] on the screen/in the .log-file:
      %nameshow{wk[2]}

      % this prints the meaning of wk[2]
      texttt{namestring{wk[2]}: namemeaning{wk[2]}}

      end{document}





      share|improve this answer














      Variables?



      (La)TeX is a macro language with macro-expansion/replacement.



      You have macros, registers and parameters.



      You can use macros that don't process arguments as variables.



      In any case you need to be aware of how macro-expansion works and about the order in time in which single expansion-steps and assignments are carried out.



      I'd just use a macro name which takes a sequence of tokens nested in curly braces for the name of a control-sequence-token and via csname..endcsname constructs/delivers that control-sequence-token while leaving things in place that possibly occur between name and the opening-curly-brace.

      That control-sequence-token in turn can be (re)defined to be a macro or can be expanded, just as you like:



      documentclass{article}
      usepackage{pgffor}

      newcommandname{}longdefname#1#{romannumeral0innername{#1}}%
      newcommandinnername[2]{%
      expandafterexchangeexpandafter{csname #2endcsname}{0 #1}%
      }%
      newcommandexchange[2]{#2#1}%

      % This loop defines macros/control-word-tokens wk[1], wk[2], ... , wk[10]
      % with "empty" replacement-text:
      foreach i in {1,2,...,10}{%
      namenewcommand*{wk[i]}{}%
      % foreach opens up a local scope.
      % Thus we need to turn assignments restricted to that scope into global
      % assignments:
      namenamegloballet{wk[i]}={wk[i]}%
      } %

      begin{document}

      % This "stores the value 'this is week 2' into wk[2]".
      % Better: This redefines the macro wk[2] to expand to the phrase "this is week 2".
      namerenewcommand*{wk[2]}{this is week 2}

      % As there is nothing between name and the opening brace, this just delivers the
      % control-sequence-token wk[2] which in turn gets expanded as usual while that
      % expansion yields 'this is week 2':
      name{wk[2]}

      % If eTeX-extensions are available, you can use numbernumexpr.. for calculations:
      name{wk[numbernumexpr(2*3)-5+1relax]}

      % this shows the definition/meaning of wk[2] on the screen/in the .log-file:
      %nameshow{wk[2]}

      % this prints the meaning of wk[2]
      texttt{namestring{wk[2]}: namemeaning{wk[2]}}

      end{document}






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Dec 9 at 13:25

























      answered Dec 9 at 13:08









      Ulrich Diez

      3,955615




      3,955615












      • thank you, this is great. I particularly like the commenting.
        – Garth Fleming
        Dec 9 at 23:47


















      • thank you, this is great. I particularly like the commenting.
        – Garth Fleming
        Dec 9 at 23:47
















      thank you, this is great. I particularly like the commenting.
      – Garth Fleming
      Dec 9 at 23:47




      thank you, this is great. I particularly like the commenting.
      – Garth Fleming
      Dec 9 at 23:47










      up vote
      2
      down vote













      Here's a simple version that makes use of the xparse package to define a new array-like command with a first parameter in square brackets and an optional second parameter in braces:



      documentclass{article}
      usepackage{xparse}
      usepackage{pgffor}

      newcommandmakearray[1]{%
      expandafterNewDocumentCommandcsname#1endcsname{rg}{%
      IfValueTF{##2}{%
      expandafterdefcsname #1@##1endcsname{##2}%
      }{%
      csname #1@##1endcsname
      }%
      }
      }

      begin{document}
      makearray{wk}

      wk[1]{this is week 1}
      wk[3]{this is week 3}

      foreach i in {1,2,3} {wk[i]par}
      end{document}


      You use it like



      makearray{name}


      to define a new array command name. Then your can use



      name[key]{value}


      to set a value at position key, and



      name[key]


      to output the stored value at position key. If no key was set before, the result is empty (or equal to relax, to be more precise).



      Thus, the output for the above example is




      this is week 1

      this is week 3







      share|improve this answer



























        up vote
        2
        down vote













        Here's a simple version that makes use of the xparse package to define a new array-like command with a first parameter in square brackets and an optional second parameter in braces:



        documentclass{article}
        usepackage{xparse}
        usepackage{pgffor}

        newcommandmakearray[1]{%
        expandafterNewDocumentCommandcsname#1endcsname{rg}{%
        IfValueTF{##2}{%
        expandafterdefcsname #1@##1endcsname{##2}%
        }{%
        csname #1@##1endcsname
        }%
        }
        }

        begin{document}
        makearray{wk}

        wk[1]{this is week 1}
        wk[3]{this is week 3}

        foreach i in {1,2,3} {wk[i]par}
        end{document}


        You use it like



        makearray{name}


        to define a new array command name. Then your can use



        name[key]{value}


        to set a value at position key, and



        name[key]


        to output the stored value at position key. If no key was set before, the result is empty (or equal to relax, to be more precise).



        Thus, the output for the above example is




        this is week 1

        this is week 3







        share|improve this answer

























          up vote
          2
          down vote










          up vote
          2
          down vote









          Here's a simple version that makes use of the xparse package to define a new array-like command with a first parameter in square brackets and an optional second parameter in braces:



          documentclass{article}
          usepackage{xparse}
          usepackage{pgffor}

          newcommandmakearray[1]{%
          expandafterNewDocumentCommandcsname#1endcsname{rg}{%
          IfValueTF{##2}{%
          expandafterdefcsname #1@##1endcsname{##2}%
          }{%
          csname #1@##1endcsname
          }%
          }
          }

          begin{document}
          makearray{wk}

          wk[1]{this is week 1}
          wk[3]{this is week 3}

          foreach i in {1,2,3} {wk[i]par}
          end{document}


          You use it like



          makearray{name}


          to define a new array command name. Then your can use



          name[key]{value}


          to set a value at position key, and



          name[key]


          to output the stored value at position key. If no key was set before, the result is empty (or equal to relax, to be more precise).



          Thus, the output for the above example is




          this is week 1

          this is week 3







          share|improve this answer














          Here's a simple version that makes use of the xparse package to define a new array-like command with a first parameter in square brackets and an optional second parameter in braces:



          documentclass{article}
          usepackage{xparse}
          usepackage{pgffor}

          newcommandmakearray[1]{%
          expandafterNewDocumentCommandcsname#1endcsname{rg}{%
          IfValueTF{##2}{%
          expandafterdefcsname #1@##1endcsname{##2}%
          }{%
          csname #1@##1endcsname
          }%
          }
          }

          begin{document}
          makearray{wk}

          wk[1]{this is week 1}
          wk[3]{this is week 3}

          foreach i in {1,2,3} {wk[i]par}
          end{document}


          You use it like



          makearray{name}


          to define a new array command name. Then your can use



          name[key]{value}


          to set a value at position key, and



          name[key]


          to output the stored value at position key. If no key was set before, the result is empty (or equal to relax, to be more precise).



          Thus, the output for the above example is




          this is week 1

          this is week 3








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 9 at 7:51

























          answered Dec 9 at 7:33









          siracusa

          4,92511228




          4,92511228






















              up vote
              0
              down vote













              Since you seem to want it in contexts of loops, here's an implementation that allows arithmetic expressions in the argument.



              I distinguish between the phases of setting and retrieving values. The syntax you propose disallows wk being expandable, because one would have to check whether it is used for setting a value.



              documentclass{article}
              usepackage{xparse}

              ExplSyntaxOn
              NewExpandableDocumentCommand{wk}{m}
              {
              prop_item:Nf g_garth_week_prop { int_eval:n { #1 } }
              }
              NewDocumentCommand{setwk}{mm}
              {
              prop_gput:Nfn g_garth_week_prop { int_eval:n { #1 } } { #2 }
              }

              cs_generate_variant:Nn prop_item:Nn { Nf }
              cs_generate_variant:Nn prop_gput:Nnn { Nf }

              prop_new:N g_garth_week_prop

              ExplSyntaxOff

              setwk{2}{This is week two}
              setwk{2+1}{This is week three}

              begin{document}

              wk{2}

              wk{7-2*2}

              end{document}


              enter image description here



              Here is an abstraction layer that allows to define as many arrays as you want.



              documentclass{article}
              usepackage{xparse}

              ExplSyntaxOn
              NewDocumentCommand{NewArray}{m}
              {
              prop_new:c { g_garth_#1_prop }
              cs_new_protected:cpn { set#1 } ##1 ##2
              {
              prop_gput:cfn { g_garth_#1_prop } { int_eval:n { ##1 } } { ##2 }
              }
              cs_new:cpn { #1 } ##1
              {
              prop_item:cf { g_garth_#1_prop } { int_eval:n { ##1 } }
              }
              }

              cs_generate_variant:Nn prop_item:Nn { cf }
              cs_generate_variant:Nn prop_gput:Nnn { cf }

              ExplSyntaxOff

              NewArray{wk}

              setwk{2}{This is week two}
              setwk{2+1}{This is week three}

              begin{document}

              wk{2}

              wk{7-2*2}

              end{document}





              share|improve this answer



























                up vote
                0
                down vote













                Since you seem to want it in contexts of loops, here's an implementation that allows arithmetic expressions in the argument.



                I distinguish between the phases of setting and retrieving values. The syntax you propose disallows wk being expandable, because one would have to check whether it is used for setting a value.



                documentclass{article}
                usepackage{xparse}

                ExplSyntaxOn
                NewExpandableDocumentCommand{wk}{m}
                {
                prop_item:Nf g_garth_week_prop { int_eval:n { #1 } }
                }
                NewDocumentCommand{setwk}{mm}
                {
                prop_gput:Nfn g_garth_week_prop { int_eval:n { #1 } } { #2 }
                }

                cs_generate_variant:Nn prop_item:Nn { Nf }
                cs_generate_variant:Nn prop_gput:Nnn { Nf }

                prop_new:N g_garth_week_prop

                ExplSyntaxOff

                setwk{2}{This is week two}
                setwk{2+1}{This is week three}

                begin{document}

                wk{2}

                wk{7-2*2}

                end{document}


                enter image description here



                Here is an abstraction layer that allows to define as many arrays as you want.



                documentclass{article}
                usepackage{xparse}

                ExplSyntaxOn
                NewDocumentCommand{NewArray}{m}
                {
                prop_new:c { g_garth_#1_prop }
                cs_new_protected:cpn { set#1 } ##1 ##2
                {
                prop_gput:cfn { g_garth_#1_prop } { int_eval:n { ##1 } } { ##2 }
                }
                cs_new:cpn { #1 } ##1
                {
                prop_item:cf { g_garth_#1_prop } { int_eval:n { ##1 } }
                }
                }

                cs_generate_variant:Nn prop_item:Nn { cf }
                cs_generate_variant:Nn prop_gput:Nnn { cf }

                ExplSyntaxOff

                NewArray{wk}

                setwk{2}{This is week two}
                setwk{2+1}{This is week three}

                begin{document}

                wk{2}

                wk{7-2*2}

                end{document}





                share|improve this answer

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Since you seem to want it in contexts of loops, here's an implementation that allows arithmetic expressions in the argument.



                  I distinguish between the phases of setting and retrieving values. The syntax you propose disallows wk being expandable, because one would have to check whether it is used for setting a value.



                  documentclass{article}
                  usepackage{xparse}

                  ExplSyntaxOn
                  NewExpandableDocumentCommand{wk}{m}
                  {
                  prop_item:Nf g_garth_week_prop { int_eval:n { #1 } }
                  }
                  NewDocumentCommand{setwk}{mm}
                  {
                  prop_gput:Nfn g_garth_week_prop { int_eval:n { #1 } } { #2 }
                  }

                  cs_generate_variant:Nn prop_item:Nn { Nf }
                  cs_generate_variant:Nn prop_gput:Nnn { Nf }

                  prop_new:N g_garth_week_prop

                  ExplSyntaxOff

                  setwk{2}{This is week two}
                  setwk{2+1}{This is week three}

                  begin{document}

                  wk{2}

                  wk{7-2*2}

                  end{document}


                  enter image description here



                  Here is an abstraction layer that allows to define as many arrays as you want.



                  documentclass{article}
                  usepackage{xparse}

                  ExplSyntaxOn
                  NewDocumentCommand{NewArray}{m}
                  {
                  prop_new:c { g_garth_#1_prop }
                  cs_new_protected:cpn { set#1 } ##1 ##2
                  {
                  prop_gput:cfn { g_garth_#1_prop } { int_eval:n { ##1 } } { ##2 }
                  }
                  cs_new:cpn { #1 } ##1
                  {
                  prop_item:cf { g_garth_#1_prop } { int_eval:n { ##1 } }
                  }
                  }

                  cs_generate_variant:Nn prop_item:Nn { cf }
                  cs_generate_variant:Nn prop_gput:Nnn { cf }

                  ExplSyntaxOff

                  NewArray{wk}

                  setwk{2}{This is week two}
                  setwk{2+1}{This is week three}

                  begin{document}

                  wk{2}

                  wk{7-2*2}

                  end{document}





                  share|improve this answer














                  Since you seem to want it in contexts of loops, here's an implementation that allows arithmetic expressions in the argument.



                  I distinguish between the phases of setting and retrieving values. The syntax you propose disallows wk being expandable, because one would have to check whether it is used for setting a value.



                  documentclass{article}
                  usepackage{xparse}

                  ExplSyntaxOn
                  NewExpandableDocumentCommand{wk}{m}
                  {
                  prop_item:Nf g_garth_week_prop { int_eval:n { #1 } }
                  }
                  NewDocumentCommand{setwk}{mm}
                  {
                  prop_gput:Nfn g_garth_week_prop { int_eval:n { #1 } } { #2 }
                  }

                  cs_generate_variant:Nn prop_item:Nn { Nf }
                  cs_generate_variant:Nn prop_gput:Nnn { Nf }

                  prop_new:N g_garth_week_prop

                  ExplSyntaxOff

                  setwk{2}{This is week two}
                  setwk{2+1}{This is week three}

                  begin{document}

                  wk{2}

                  wk{7-2*2}

                  end{document}


                  enter image description here



                  Here is an abstraction layer that allows to define as many arrays as you want.



                  documentclass{article}
                  usepackage{xparse}

                  ExplSyntaxOn
                  NewDocumentCommand{NewArray}{m}
                  {
                  prop_new:c { g_garth_#1_prop }
                  cs_new_protected:cpn { set#1 } ##1 ##2
                  {
                  prop_gput:cfn { g_garth_#1_prop } { int_eval:n { ##1 } } { ##2 }
                  }
                  cs_new:cpn { #1 } ##1
                  {
                  prop_item:cf { g_garth_#1_prop } { int_eval:n { ##1 } }
                  }
                  }

                  cs_generate_variant:Nn prop_item:Nn { cf }
                  cs_generate_variant:Nn prop_gput:Nnn { cf }

                  ExplSyntaxOff

                  NewArray{wk}

                  setwk{2}{This is week two}
                  setwk{2+1}{This is week three}

                  begin{document}

                  wk{2}

                  wk{7-2*2}

                  end{document}






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 9 at 10:57

























                  answered Dec 9 at 10:32









                  egreg

                  704k8618763155




                  704k8618763155






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f463902%2findexing-variables%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

                      Morgemoulin

                      Scott Moir

                      Souastre