How to implement full LaTeX syntax in Rmarkdown?












0















I have a two part question regarding the use of LaTeX in Rmarkdown:



1) When working in Rmarkdown (with the intent to render to PDF), is there a rule for when we should just use the double dollar signs ($$) to write something in LaTeX or when we should use the LaTeX syntax to begin and end all of our LaTeX code (e.g. documentclass{...}, begin{document}, end{document}, etc.



I believe I have read that it is okay to just use the latter option, and Rmarkdown will ignore all of the escaped latex commands if the document is rendered to anything other than PDF.



2) The reason I am asking, in this case, is that I am trying to incorporate some labelled matrix multiplication in an Rmarkdown document I am writing. Specifically, I would like to include some matrices that take the form show on this page. Here is the code:



documentclass{article}
usepackage{amsmath}

newenvironment{spmatrix}[1]
{defmysubscript{#1}mathopbgroupbegin{pmatrix}}
{end{pmatrix}egroup_{textstylemathstrutmysubscript}}

begin{document}
begin{equation}
begin{spmatrix}{A}
a & b \
c & d
end{spmatrix}
begin{spmatrix}{x}
x_1 \
x_2
end{spmatrix}
=
begin{spmatrix}{b}
b_1 \
b_2
end{spmatrix}
end{equation}
end{document}


How would one implement this code in Rmarkdown? Do you need to move the usepackage call into the YAML as suggested in other threads discussing the loading of LaTeX packages in Rmarkdown? Is the first line, documentclass{article} even needed within an Rmarkdown document?



I'm new to all of this, and thusfar, I've been able to get by using the double dollar signs to set off all my LaTeX code for simple equations and even a simple matrix here and there that I've tried to write, but for mathematical expressions that require more formatting, aligning, multi-line proofs, etc., most of the examples I've encountered are on the TEX boards written with syntax similar to the code above. I haven't been able to figure out how to implement these types of examples in Rmarkdown. Any helpful suggestions or pointers where to better understand this issue would be much appreciated!










share|improve this question





























    0















    I have a two part question regarding the use of LaTeX in Rmarkdown:



    1) When working in Rmarkdown (with the intent to render to PDF), is there a rule for when we should just use the double dollar signs ($$) to write something in LaTeX or when we should use the LaTeX syntax to begin and end all of our LaTeX code (e.g. documentclass{...}, begin{document}, end{document}, etc.



    I believe I have read that it is okay to just use the latter option, and Rmarkdown will ignore all of the escaped latex commands if the document is rendered to anything other than PDF.



    2) The reason I am asking, in this case, is that I am trying to incorporate some labelled matrix multiplication in an Rmarkdown document I am writing. Specifically, I would like to include some matrices that take the form show on this page. Here is the code:



    documentclass{article}
    usepackage{amsmath}

    newenvironment{spmatrix}[1]
    {defmysubscript{#1}mathopbgroupbegin{pmatrix}}
    {end{pmatrix}egroup_{textstylemathstrutmysubscript}}

    begin{document}
    begin{equation}
    begin{spmatrix}{A}
    a & b \
    c & d
    end{spmatrix}
    begin{spmatrix}{x}
    x_1 \
    x_2
    end{spmatrix}
    =
    begin{spmatrix}{b}
    b_1 \
    b_2
    end{spmatrix}
    end{equation}
    end{document}


    How would one implement this code in Rmarkdown? Do you need to move the usepackage call into the YAML as suggested in other threads discussing the loading of LaTeX packages in Rmarkdown? Is the first line, documentclass{article} even needed within an Rmarkdown document?



    I'm new to all of this, and thusfar, I've been able to get by using the double dollar signs to set off all my LaTeX code for simple equations and even a simple matrix here and there that I've tried to write, but for mathematical expressions that require more formatting, aligning, multi-line proofs, etc., most of the examples I've encountered are on the TEX boards written with syntax similar to the code above. I haven't been able to figure out how to implement these types of examples in Rmarkdown. Any helpful suggestions or pointers where to better understand this issue would be much appreciated!










    share|improve this question



























      0












      0








      0








      I have a two part question regarding the use of LaTeX in Rmarkdown:



      1) When working in Rmarkdown (with the intent to render to PDF), is there a rule for when we should just use the double dollar signs ($$) to write something in LaTeX or when we should use the LaTeX syntax to begin and end all of our LaTeX code (e.g. documentclass{...}, begin{document}, end{document}, etc.



      I believe I have read that it is okay to just use the latter option, and Rmarkdown will ignore all of the escaped latex commands if the document is rendered to anything other than PDF.



      2) The reason I am asking, in this case, is that I am trying to incorporate some labelled matrix multiplication in an Rmarkdown document I am writing. Specifically, I would like to include some matrices that take the form show on this page. Here is the code:



      documentclass{article}
      usepackage{amsmath}

      newenvironment{spmatrix}[1]
      {defmysubscript{#1}mathopbgroupbegin{pmatrix}}
      {end{pmatrix}egroup_{textstylemathstrutmysubscript}}

      begin{document}
      begin{equation}
      begin{spmatrix}{A}
      a & b \
      c & d
      end{spmatrix}
      begin{spmatrix}{x}
      x_1 \
      x_2
      end{spmatrix}
      =
      begin{spmatrix}{b}
      b_1 \
      b_2
      end{spmatrix}
      end{equation}
      end{document}


      How would one implement this code in Rmarkdown? Do you need to move the usepackage call into the YAML as suggested in other threads discussing the loading of LaTeX packages in Rmarkdown? Is the first line, documentclass{article} even needed within an Rmarkdown document?



      I'm new to all of this, and thusfar, I've been able to get by using the double dollar signs to set off all my LaTeX code for simple equations and even a simple matrix here and there that I've tried to write, but for mathematical expressions that require more formatting, aligning, multi-line proofs, etc., most of the examples I've encountered are on the TEX boards written with syntax similar to the code above. I haven't been able to figure out how to implement these types of examples in Rmarkdown. Any helpful suggestions or pointers where to better understand this issue would be much appreciated!










      share|improve this question
















      I have a two part question regarding the use of LaTeX in Rmarkdown:



      1) When working in Rmarkdown (with the intent to render to PDF), is there a rule for when we should just use the double dollar signs ($$) to write something in LaTeX or when we should use the LaTeX syntax to begin and end all of our LaTeX code (e.g. documentclass{...}, begin{document}, end{document}, etc.



      I believe I have read that it is okay to just use the latter option, and Rmarkdown will ignore all of the escaped latex commands if the document is rendered to anything other than PDF.



      2) The reason I am asking, in this case, is that I am trying to incorporate some labelled matrix multiplication in an Rmarkdown document I am writing. Specifically, I would like to include some matrices that take the form show on this page. Here is the code:



      documentclass{article}
      usepackage{amsmath}

      newenvironment{spmatrix}[1]
      {defmysubscript{#1}mathopbgroupbegin{pmatrix}}
      {end{pmatrix}egroup_{textstylemathstrutmysubscript}}

      begin{document}
      begin{equation}
      begin{spmatrix}{A}
      a & b \
      c & d
      end{spmatrix}
      begin{spmatrix}{x}
      x_1 \
      x_2
      end{spmatrix}
      =
      begin{spmatrix}{b}
      b_1 \
      b_2
      end{spmatrix}
      end{equation}
      end{document}


      How would one implement this code in Rmarkdown? Do you need to move the usepackage call into the YAML as suggested in other threads discussing the loading of LaTeX packages in Rmarkdown? Is the first line, documentclass{article} even needed within an Rmarkdown document?



      I'm new to all of this, and thusfar, I've been able to get by using the double dollar signs to set off all my LaTeX code for simple equations and even a simple matrix here and there that I've tried to write, but for mathematical expressions that require more formatting, aligning, multi-line proofs, etc., most of the examples I've encountered are on the TEX boards written with syntax similar to the code above. I haven't been able to figure out how to implement these types of examples in Rmarkdown. Any helpful suggestions or pointers where to better understand this issue would be much appreciated!







      latex r-markdown tex






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 20:57







      anguyen1210

















      asked Nov 24 '18 at 17:37









      anguyen1210anguyen1210

      175




      175
























          1 Answer
          1






          active

          oldest

          votes


















          1














          It is indeed possible to put most body-level LaTeX constructs into the body of your Rmd file. Other backends will ignore these constructs, but the result might look change. So from my point of view your are binding yourself to PDF output. But that might be fine in your case.



          Concerning your concrete problem:





          • amsmath is already included by the default template, which also takes care of ḑocumentclass and the document environment.

          • You need to add the environment definition into a separate tex file (in my case preamble.tex) and include that file via the YAML headers.

          • You can then use the LaTeX constructs as is in the Rmd body.


          Putting things together:



          ---
          output:
          pdf_document:
          keep_tex: yes
          includes:
          in_header: preamble.tex
          ---

          begin{equation}
          begin{spmatrix}{A}
          a & b \
          c & d
          end{spmatrix}
          begin{spmatrix}{x}
          x_1 \
          x_2
          end{spmatrix}
          =
          begin{spmatrix}{b}
          b_1 \
          b_2
          end{spmatrix}
          end{equation}





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2fstackoverflow.com%2fquestions%2f53460769%2fhow-to-implement-full-latex-syntax-in-rmarkdown%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









            1














            It is indeed possible to put most body-level LaTeX constructs into the body of your Rmd file. Other backends will ignore these constructs, but the result might look change. So from my point of view your are binding yourself to PDF output. But that might be fine in your case.



            Concerning your concrete problem:





            • amsmath is already included by the default template, which also takes care of ḑocumentclass and the document environment.

            • You need to add the environment definition into a separate tex file (in my case preamble.tex) and include that file via the YAML headers.

            • You can then use the LaTeX constructs as is in the Rmd body.


            Putting things together:



            ---
            output:
            pdf_document:
            keep_tex: yes
            includes:
            in_header: preamble.tex
            ---

            begin{equation}
            begin{spmatrix}{A}
            a & b \
            c & d
            end{spmatrix}
            begin{spmatrix}{x}
            x_1 \
            x_2
            end{spmatrix}
            =
            begin{spmatrix}{b}
            b_1 \
            b_2
            end{spmatrix}
            end{equation}





            share|improve this answer




























              1














              It is indeed possible to put most body-level LaTeX constructs into the body of your Rmd file. Other backends will ignore these constructs, but the result might look change. So from my point of view your are binding yourself to PDF output. But that might be fine in your case.



              Concerning your concrete problem:





              • amsmath is already included by the default template, which also takes care of ḑocumentclass and the document environment.

              • You need to add the environment definition into a separate tex file (in my case preamble.tex) and include that file via the YAML headers.

              • You can then use the LaTeX constructs as is in the Rmd body.


              Putting things together:



              ---
              output:
              pdf_document:
              keep_tex: yes
              includes:
              in_header: preamble.tex
              ---

              begin{equation}
              begin{spmatrix}{A}
              a & b \
              c & d
              end{spmatrix}
              begin{spmatrix}{x}
              x_1 \
              x_2
              end{spmatrix}
              =
              begin{spmatrix}{b}
              b_1 \
              b_2
              end{spmatrix}
              end{equation}





              share|improve this answer


























                1












                1








                1







                It is indeed possible to put most body-level LaTeX constructs into the body of your Rmd file. Other backends will ignore these constructs, but the result might look change. So from my point of view your are binding yourself to PDF output. But that might be fine in your case.



                Concerning your concrete problem:





                • amsmath is already included by the default template, which also takes care of ḑocumentclass and the document environment.

                • You need to add the environment definition into a separate tex file (in my case preamble.tex) and include that file via the YAML headers.

                • You can then use the LaTeX constructs as is in the Rmd body.


                Putting things together:



                ---
                output:
                pdf_document:
                keep_tex: yes
                includes:
                in_header: preamble.tex
                ---

                begin{equation}
                begin{spmatrix}{A}
                a & b \
                c & d
                end{spmatrix}
                begin{spmatrix}{x}
                x_1 \
                x_2
                end{spmatrix}
                =
                begin{spmatrix}{b}
                b_1 \
                b_2
                end{spmatrix}
                end{equation}





                share|improve this answer













                It is indeed possible to put most body-level LaTeX constructs into the body of your Rmd file. Other backends will ignore these constructs, but the result might look change. So from my point of view your are binding yourself to PDF output. But that might be fine in your case.



                Concerning your concrete problem:





                • amsmath is already included by the default template, which also takes care of ḑocumentclass and the document environment.

                • You need to add the environment definition into a separate tex file (in my case preamble.tex) and include that file via the YAML headers.

                • You can then use the LaTeX constructs as is in the Rmd body.


                Putting things together:



                ---
                output:
                pdf_document:
                keep_tex: yes
                includes:
                in_header: preamble.tex
                ---

                begin{equation}
                begin{spmatrix}{A}
                a & b \
                c & d
                end{spmatrix}
                begin{spmatrix}{x}
                x_1 \
                x_2
                end{spmatrix}
                =
                begin{spmatrix}{b}
                b_1 \
                b_2
                end{spmatrix}
                end{equation}






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 24 '18 at 22:55









                Ralf StubnerRalf Stubner

                13.9k21437




                13.9k21437






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • 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%2fstackoverflow.com%2fquestions%2f53460769%2fhow-to-implement-full-latex-syntax-in-rmarkdown%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)