Pulling numbers out of a file name











up vote
5
down vote

favorite












A long time ago, I wanted to solve the problem where I would typeset my homework assignments, but copy the file "hw1.tex" to make "hw2.tex", and forget to change the title "Homework 1" at the top of the page.



So I wrote a function (that now I no longer 100% remember how it works, or maybe I got parts of it from a place like StackExchange and never did fully understand it):



usepackage{substr}
newcommand{hwnum}{BehindSubString{hw}{scantokensexpandafter{jobnamenoexpand}}}


Now I can make Homework hwnum the title. When I compile "hw1.tex", the title is "Homework 1", and when I compile "hw2.tex", the title becomes "Homework 2".



Minimal working example (assuming you first save it as a file with the appropriate name):



documentclass{article}

usepackage{substr}
newcommand{hwnum}{BehindSubString{hw}{scantokensexpandafter{jobnamenoexpand}}}

begin{document}

This is Homework hwnum.

end{document}


These days, I have to typeset more than just homework assignments, so I'd like to have this be a bit more flexible.




  1. It would be nicer, though not very important, if I didn't have to change the prefix "hw" in the command if I want to deal with filenames such as "day1.tex", "day2.tex", and so on.

  2. What I really want is to extract more than one number: for example, "Lecture 7" and "Chapter 3" (or whatever) from a filename such as "ch3lec7.tex".

  3. Putting these together, an ideal function would just be able to locate all the numbers in the filename, no matter which non-numbers they're separated by. The same function could pull out 3 and 7 whether it's given the filename "ch3lec7.tex" or "week3day7.tex".


What is a way for me to do at least #2, but also if possible #1 or #3?










share|improve this question









New contributor




Misha Lavrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    5
    down vote

    favorite












    A long time ago, I wanted to solve the problem where I would typeset my homework assignments, but copy the file "hw1.tex" to make "hw2.tex", and forget to change the title "Homework 1" at the top of the page.



    So I wrote a function (that now I no longer 100% remember how it works, or maybe I got parts of it from a place like StackExchange and never did fully understand it):



    usepackage{substr}
    newcommand{hwnum}{BehindSubString{hw}{scantokensexpandafter{jobnamenoexpand}}}


    Now I can make Homework hwnum the title. When I compile "hw1.tex", the title is "Homework 1", and when I compile "hw2.tex", the title becomes "Homework 2".



    Minimal working example (assuming you first save it as a file with the appropriate name):



    documentclass{article}

    usepackage{substr}
    newcommand{hwnum}{BehindSubString{hw}{scantokensexpandafter{jobnamenoexpand}}}

    begin{document}

    This is Homework hwnum.

    end{document}


    These days, I have to typeset more than just homework assignments, so I'd like to have this be a bit more flexible.




    1. It would be nicer, though not very important, if I didn't have to change the prefix "hw" in the command if I want to deal with filenames such as "day1.tex", "day2.tex", and so on.

    2. What I really want is to extract more than one number: for example, "Lecture 7" and "Chapter 3" (or whatever) from a filename such as "ch3lec7.tex".

    3. Putting these together, an ideal function would just be able to locate all the numbers in the filename, no matter which non-numbers they're separated by. The same function could pull out 3 and 7 whether it's given the filename "ch3lec7.tex" or "week3day7.tex".


    What is a way for me to do at least #2, but also if possible #1 or #3?










    share|improve this question









    New contributor




    Misha Lavrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      A long time ago, I wanted to solve the problem where I would typeset my homework assignments, but copy the file "hw1.tex" to make "hw2.tex", and forget to change the title "Homework 1" at the top of the page.



      So I wrote a function (that now I no longer 100% remember how it works, or maybe I got parts of it from a place like StackExchange and never did fully understand it):



      usepackage{substr}
      newcommand{hwnum}{BehindSubString{hw}{scantokensexpandafter{jobnamenoexpand}}}


      Now I can make Homework hwnum the title. When I compile "hw1.tex", the title is "Homework 1", and when I compile "hw2.tex", the title becomes "Homework 2".



      Minimal working example (assuming you first save it as a file with the appropriate name):



      documentclass{article}

      usepackage{substr}
      newcommand{hwnum}{BehindSubString{hw}{scantokensexpandafter{jobnamenoexpand}}}

      begin{document}

      This is Homework hwnum.

      end{document}


      These days, I have to typeset more than just homework assignments, so I'd like to have this be a bit more flexible.




      1. It would be nicer, though not very important, if I didn't have to change the prefix "hw" in the command if I want to deal with filenames such as "day1.tex", "day2.tex", and so on.

      2. What I really want is to extract more than one number: for example, "Lecture 7" and "Chapter 3" (or whatever) from a filename such as "ch3lec7.tex".

      3. Putting these together, an ideal function would just be able to locate all the numbers in the filename, no matter which non-numbers they're separated by. The same function could pull out 3 and 7 whether it's given the filename "ch3lec7.tex" or "week3day7.tex".


      What is a way for me to do at least #2, but also if possible #1 or #3?










      share|improve this question









      New contributor




      Misha Lavrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      A long time ago, I wanted to solve the problem where I would typeset my homework assignments, but copy the file "hw1.tex" to make "hw2.tex", and forget to change the title "Homework 1" at the top of the page.



      So I wrote a function (that now I no longer 100% remember how it works, or maybe I got parts of it from a place like StackExchange and never did fully understand it):



      usepackage{substr}
      newcommand{hwnum}{BehindSubString{hw}{scantokensexpandafter{jobnamenoexpand}}}


      Now I can make Homework hwnum the title. When I compile "hw1.tex", the title is "Homework 1", and when I compile "hw2.tex", the title becomes "Homework 2".



      Minimal working example (assuming you first save it as a file with the appropriate name):



      documentclass{article}

      usepackage{substr}
      newcommand{hwnum}{BehindSubString{hw}{scantokensexpandafter{jobnamenoexpand}}}

      begin{document}

      This is Homework hwnum.

      end{document}


      These days, I have to typeset more than just homework assignments, so I'd like to have this be a bit more flexible.




      1. It would be nicer, though not very important, if I didn't have to change the prefix "hw" in the command if I want to deal with filenames such as "day1.tex", "day2.tex", and so on.

      2. What I really want is to extract more than one number: for example, "Lecture 7" and "Chapter 3" (or whatever) from a filename such as "ch3lec7.tex".

      3. Putting these together, an ideal function would just be able to locate all the numbers in the filename, no matter which non-numbers they're separated by. The same function could pull out 3 and 7 whether it's given the filename "ch3lec7.tex" or "week3day7.tex".


      What is a way for me to do at least #2, but also if possible #1 or #3?







      strings jobname






      share|improve this question









      New contributor




      Misha Lavrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Misha Lavrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 1 hour ago





















      New contributor




      Misha Lavrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 4 hours ago









      Misha Lavrov

      1264




      1264




      New contributor




      Misha Lavrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Misha Lavrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Misha Lavrov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote













          The code below uses regular expressions from LaTeX3 to extract all of the numbers in the filename and then makes them available as misha{1}, misha{2}, .... There is no error checking so, for example, if you have misha{100} in your document then this command will fail silently, doing nothing.



          If you save the code below as the file ch3lec7.tex then run it you will get the output:



          enter image description here



          Here is the code:



          documentclass{article}

          usepackage{expl3}
          ExplSyntaxOn
          cs_generate_variant:Nn regex_extract_all:nnN {nVN}
          seq_new:N l_misha_seq
          regex_extract_all:nVN {d+} c_sys_jobname_str l_misha_seq
          newcommandmisha[1]{seq_item:Nn l_misha_seq {#1}}
          ExplSyntaxOff

          begin{document}

          Chapter misha{1}, lecture misha{2}.

          end{document}


          The work is all done by the command regex_extract_all:nVN, which puts all of the numbers in jobname into an internal LaTeX3 sequence. (As egreg pointed out, LaTeX3 stores the filename in the string constant c_sys_jobname_str.) The command misha{k} prints the kth element of this sequence.






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


            }
            });






            Misha Lavrov is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f464025%2fpulling-numbers-out-of-a-file-name%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
            4
            down vote













            The code below uses regular expressions from LaTeX3 to extract all of the numbers in the filename and then makes them available as misha{1}, misha{2}, .... There is no error checking so, for example, if you have misha{100} in your document then this command will fail silently, doing nothing.



            If you save the code below as the file ch3lec7.tex then run it you will get the output:



            enter image description here



            Here is the code:



            documentclass{article}

            usepackage{expl3}
            ExplSyntaxOn
            cs_generate_variant:Nn regex_extract_all:nnN {nVN}
            seq_new:N l_misha_seq
            regex_extract_all:nVN {d+} c_sys_jobname_str l_misha_seq
            newcommandmisha[1]{seq_item:Nn l_misha_seq {#1}}
            ExplSyntaxOff

            begin{document}

            Chapter misha{1}, lecture misha{2}.

            end{document}


            The work is all done by the command regex_extract_all:nVN, which puts all of the numbers in jobname into an internal LaTeX3 sequence. (As egreg pointed out, LaTeX3 stores the filename in the string constant c_sys_jobname_str.) The command misha{k} prints the kth element of this sequence.






            share|improve this answer



























              up vote
              4
              down vote













              The code below uses regular expressions from LaTeX3 to extract all of the numbers in the filename and then makes them available as misha{1}, misha{2}, .... There is no error checking so, for example, if you have misha{100} in your document then this command will fail silently, doing nothing.



              If you save the code below as the file ch3lec7.tex then run it you will get the output:



              enter image description here



              Here is the code:



              documentclass{article}

              usepackage{expl3}
              ExplSyntaxOn
              cs_generate_variant:Nn regex_extract_all:nnN {nVN}
              seq_new:N l_misha_seq
              regex_extract_all:nVN {d+} c_sys_jobname_str l_misha_seq
              newcommandmisha[1]{seq_item:Nn l_misha_seq {#1}}
              ExplSyntaxOff

              begin{document}

              Chapter misha{1}, lecture misha{2}.

              end{document}


              The work is all done by the command regex_extract_all:nVN, which puts all of the numbers in jobname into an internal LaTeX3 sequence. (As egreg pointed out, LaTeX3 stores the filename in the string constant c_sys_jobname_str.) The command misha{k} prints the kth element of this sequence.






              share|improve this answer

























                up vote
                4
                down vote










                up vote
                4
                down vote









                The code below uses regular expressions from LaTeX3 to extract all of the numbers in the filename and then makes them available as misha{1}, misha{2}, .... There is no error checking so, for example, if you have misha{100} in your document then this command will fail silently, doing nothing.



                If you save the code below as the file ch3lec7.tex then run it you will get the output:



                enter image description here



                Here is the code:



                documentclass{article}

                usepackage{expl3}
                ExplSyntaxOn
                cs_generate_variant:Nn regex_extract_all:nnN {nVN}
                seq_new:N l_misha_seq
                regex_extract_all:nVN {d+} c_sys_jobname_str l_misha_seq
                newcommandmisha[1]{seq_item:Nn l_misha_seq {#1}}
                ExplSyntaxOff

                begin{document}

                Chapter misha{1}, lecture misha{2}.

                end{document}


                The work is all done by the command regex_extract_all:nVN, which puts all of the numbers in jobname into an internal LaTeX3 sequence. (As egreg pointed out, LaTeX3 stores the filename in the string constant c_sys_jobname_str.) The command misha{k} prints the kth element of this sequence.






                share|improve this answer














                The code below uses regular expressions from LaTeX3 to extract all of the numbers in the filename and then makes them available as misha{1}, misha{2}, .... There is no error checking so, for example, if you have misha{100} in your document then this command will fail silently, doing nothing.



                If you save the code below as the file ch3lec7.tex then run it you will get the output:



                enter image description here



                Here is the code:



                documentclass{article}

                usepackage{expl3}
                ExplSyntaxOn
                cs_generate_variant:Nn regex_extract_all:nnN {nVN}
                seq_new:N l_misha_seq
                regex_extract_all:nVN {d+} c_sys_jobname_str l_misha_seq
                newcommandmisha[1]{seq_item:Nn l_misha_seq {#1}}
                ExplSyntaxOff

                begin{document}

                Chapter misha{1}, lecture misha{2}.

                end{document}


                The work is all done by the command regex_extract_all:nVN, which puts all of the numbers in jobname into an internal LaTeX3 sequence. (As egreg pointed out, LaTeX3 stores the filename in the string constant c_sys_jobname_str.) The command misha{k} prints the kth element of this sequence.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 31 mins ago

























                answered 1 hour ago









                Andrew

                30.2k34380




                30.2k34380






















                    Misha Lavrov is a new contributor. Be nice, and check out our Code of Conduct.










                    draft saved

                    draft discarded


















                    Misha Lavrov is a new contributor. Be nice, and check out our Code of Conduct.













                    Misha Lavrov is a new contributor. Be nice, and check out our Code of Conduct.












                    Misha Lavrov is a new contributor. Be nice, and check out our Code of Conduct.
















                    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%2f464025%2fpulling-numbers-out-of-a-file-name%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

                    What visual should I use to simply compare current year value vs last year in Power BI desktop

                    How to ignore python UserWarning in pytest?

                    Alexandru Averescu