Is it possible to write a class file that loops over the documentbody?












2














What I would like to achieve is a repeat.cls such that I could write a .tex file



documentclass{repeat}
begin{document}
foobar
end{document}


and then the document would contain 'foobar' a certain number of times.



Is this possible? I tried to combine loop or foreach with the AtBeginDocument and AtEndDocument, but from what I know now, that approach cannot work as TeX would never really "see" the end{document}.










share|improve this question
























  • Probably not that way. You'd need a macro which would scan the text, but this macro can't "exit" the class file. You'd need a command myWeirdLoop after the documentclass. It would still mess up you document, mainly anything that contains catcode changes. What do you want to achieve exactly?
    – Phelype Oleinik
    Dec 11 '18 at 18:22






  • 1




    @PhelypeOleinik could we save the meaning of document (begin and end) and the combine with environ. Plus a macro in the preamble to specify the number of repetitions.
    – daleif
    Dec 11 '18 at 18:29






  • 1




    @daleif Ooh, with egeg's answer I understood it. Sometimes I need a drawing to understand things :P
    – Phelype Oleinik
    Dec 11 '18 at 18:38
















2














What I would like to achieve is a repeat.cls such that I could write a .tex file



documentclass{repeat}
begin{document}
foobar
end{document}


and then the document would contain 'foobar' a certain number of times.



Is this possible? I tried to combine loop or foreach with the AtBeginDocument and AtEndDocument, but from what I know now, that approach cannot work as TeX would never really "see" the end{document}.










share|improve this question
























  • Probably not that way. You'd need a macro which would scan the text, but this macro can't "exit" the class file. You'd need a command myWeirdLoop after the documentclass. It would still mess up you document, mainly anything that contains catcode changes. What do you want to achieve exactly?
    – Phelype Oleinik
    Dec 11 '18 at 18:22






  • 1




    @PhelypeOleinik could we save the meaning of document (begin and end) and the combine with environ. Plus a macro in the preamble to specify the number of repetitions.
    – daleif
    Dec 11 '18 at 18:29






  • 1




    @daleif Ooh, with egeg's answer I understood it. Sometimes I need a drawing to understand things :P
    – Phelype Oleinik
    Dec 11 '18 at 18:38














2












2








2







What I would like to achieve is a repeat.cls such that I could write a .tex file



documentclass{repeat}
begin{document}
foobar
end{document}


and then the document would contain 'foobar' a certain number of times.



Is this possible? I tried to combine loop or foreach with the AtBeginDocument and AtEndDocument, but from what I know now, that approach cannot work as TeX would never really "see" the end{document}.










share|improve this question















What I would like to achieve is a repeat.cls such that I could write a .tex file



documentclass{repeat}
begin{document}
foobar
end{document}


and then the document would contain 'foobar' a certain number of times.



Is this possible? I tried to combine loop or foreach with the AtBeginDocument and AtEndDocument, but from what I know now, that approach cannot work as TeX would never really "see" the end{document}.







tex-core documentclass-writing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 11 '18 at 18:26









Stefan Pinnow

19.4k83175




19.4k83175










asked Dec 11 '18 at 18:17









CFrei

132




132












  • Probably not that way. You'd need a macro which would scan the text, but this macro can't "exit" the class file. You'd need a command myWeirdLoop after the documentclass. It would still mess up you document, mainly anything that contains catcode changes. What do you want to achieve exactly?
    – Phelype Oleinik
    Dec 11 '18 at 18:22






  • 1




    @PhelypeOleinik could we save the meaning of document (begin and end) and the combine with environ. Plus a macro in the preamble to specify the number of repetitions.
    – daleif
    Dec 11 '18 at 18:29






  • 1




    @daleif Ooh, with egeg's answer I understood it. Sometimes I need a drawing to understand things :P
    – Phelype Oleinik
    Dec 11 '18 at 18:38


















  • Probably not that way. You'd need a macro which would scan the text, but this macro can't "exit" the class file. You'd need a command myWeirdLoop after the documentclass. It would still mess up you document, mainly anything that contains catcode changes. What do you want to achieve exactly?
    – Phelype Oleinik
    Dec 11 '18 at 18:22






  • 1




    @PhelypeOleinik could we save the meaning of document (begin and end) and the combine with environ. Plus a macro in the preamble to specify the number of repetitions.
    – daleif
    Dec 11 '18 at 18:29






  • 1




    @daleif Ooh, with egeg's answer I understood it. Sometimes I need a drawing to understand things :P
    – Phelype Oleinik
    Dec 11 '18 at 18:38
















Probably not that way. You'd need a macro which would scan the text, but this macro can't "exit" the class file. You'd need a command myWeirdLoop after the documentclass. It would still mess up you document, mainly anything that contains catcode changes. What do you want to achieve exactly?
– Phelype Oleinik
Dec 11 '18 at 18:22




Probably not that way. You'd need a macro which would scan the text, but this macro can't "exit" the class file. You'd need a command myWeirdLoop after the documentclass. It would still mess up you document, mainly anything that contains catcode changes. What do you want to achieve exactly?
– Phelype Oleinik
Dec 11 '18 at 18:22




1




1




@PhelypeOleinik could we save the meaning of document (begin and end) and the combine with environ. Plus a macro in the preamble to specify the number of repetitions.
– daleif
Dec 11 '18 at 18:29




@PhelypeOleinik could we save the meaning of document (begin and end) and the combine with environ. Plus a macro in the preamble to specify the number of repetitions.
– daleif
Dec 11 '18 at 18:29




1




1




@daleif Ooh, with egeg's answer I understood it. Sometimes I need a drawing to understand things :P
– Phelype Oleinik
Dec 11 '18 at 18:38




@daleif Ooh, with egeg's answer I understood it. Sometimes I need a drawing to understand things :P
– Phelype Oleinik
Dec 11 '18 at 18:38










1 Answer
1






active

oldest

votes


















4














Proof of concept follows. Don't do it at home.



repeat.cls



ProvidesClass{repeat}
defrepetitions{5} % default
DeclareOption*{edefrepetitions{CurrentOption}}
ProcessOptionsrelax
LoadClass{article}

RequirePackage{environ,xparse}
letlatexdocumentdocument
letendlatexdocumentenddocument

ExplSyntaxOn
RenewEnviron{document}{
tl_gset_eq:NN g_tmpa_tl BODY
latexdocument
prg_replicate:nn { repetitions } { g_tmpa_tl par }
endlatexdocument
}
ExplSyntaxOff


test.tex



documentclass[20]{repeat}
begin{document}
foobar
end{document}


Output



enter image description here






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%2f464366%2fis-it-possible-to-write-a-class-file-that-loops-over-the-documentbody%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









    4














    Proof of concept follows. Don't do it at home.



    repeat.cls



    ProvidesClass{repeat}
    defrepetitions{5} % default
    DeclareOption*{edefrepetitions{CurrentOption}}
    ProcessOptionsrelax
    LoadClass{article}

    RequirePackage{environ,xparse}
    letlatexdocumentdocument
    letendlatexdocumentenddocument

    ExplSyntaxOn
    RenewEnviron{document}{
    tl_gset_eq:NN g_tmpa_tl BODY
    latexdocument
    prg_replicate:nn { repetitions } { g_tmpa_tl par }
    endlatexdocument
    }
    ExplSyntaxOff


    test.tex



    documentclass[20]{repeat}
    begin{document}
    foobar
    end{document}


    Output



    enter image description here






    share|improve this answer


























      4














      Proof of concept follows. Don't do it at home.



      repeat.cls



      ProvidesClass{repeat}
      defrepetitions{5} % default
      DeclareOption*{edefrepetitions{CurrentOption}}
      ProcessOptionsrelax
      LoadClass{article}

      RequirePackage{environ,xparse}
      letlatexdocumentdocument
      letendlatexdocumentenddocument

      ExplSyntaxOn
      RenewEnviron{document}{
      tl_gset_eq:NN g_tmpa_tl BODY
      latexdocument
      prg_replicate:nn { repetitions } { g_tmpa_tl par }
      endlatexdocument
      }
      ExplSyntaxOff


      test.tex



      documentclass[20]{repeat}
      begin{document}
      foobar
      end{document}


      Output



      enter image description here






      share|improve this answer
























        4












        4








        4






        Proof of concept follows. Don't do it at home.



        repeat.cls



        ProvidesClass{repeat}
        defrepetitions{5} % default
        DeclareOption*{edefrepetitions{CurrentOption}}
        ProcessOptionsrelax
        LoadClass{article}

        RequirePackage{environ,xparse}
        letlatexdocumentdocument
        letendlatexdocumentenddocument

        ExplSyntaxOn
        RenewEnviron{document}{
        tl_gset_eq:NN g_tmpa_tl BODY
        latexdocument
        prg_replicate:nn { repetitions } { g_tmpa_tl par }
        endlatexdocument
        }
        ExplSyntaxOff


        test.tex



        documentclass[20]{repeat}
        begin{document}
        foobar
        end{document}


        Output



        enter image description here






        share|improve this answer












        Proof of concept follows. Don't do it at home.



        repeat.cls



        ProvidesClass{repeat}
        defrepetitions{5} % default
        DeclareOption*{edefrepetitions{CurrentOption}}
        ProcessOptionsrelax
        LoadClass{article}

        RequirePackage{environ,xparse}
        letlatexdocumentdocument
        letendlatexdocumentenddocument

        ExplSyntaxOn
        RenewEnviron{document}{
        tl_gset_eq:NN g_tmpa_tl BODY
        latexdocument
        prg_replicate:nn { repetitions } { g_tmpa_tl par }
        endlatexdocument
        }
        ExplSyntaxOff


        test.tex



        documentclass[20]{repeat}
        begin{document}
        foobar
        end{document}


        Output



        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 11 '18 at 18:28









        egreg

        709k8618823165




        709k8618823165






























            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%2f464366%2fis-it-possible-to-write-a-class-file-that-loops-over-the-documentbody%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

            mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?