<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Matteo Delucchi on Medium]]></title>
        <description><![CDATA[Stories by Matteo Delucchi on Medium]]></description>
        <link>https://medium.com/@delucmat?source=rss-3d3091edc72e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*QThdO91NK4VbRXvLZ79ecA.png</url>
            <title>Stories by Matteo Delucchi on Medium</title>
            <link>https://medium.com/@delucmat?source=rss-3d3091edc72e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 21:00:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@delucmat/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Specify LaTeX Parameters in R Markdown]]></title>
            <link>https://medium.com/@delucmat/specify-latex-parameters-in-r-markdown-9804467caaf?source=rss-3d3091edc72e------2</link>
            <guid isPermaLink="false">https://medium.com/p/9804467caaf</guid>
            <category><![CDATA[latex]]></category>
            <category><![CDATA[pandoc]]></category>
            <category><![CDATA[r-markdown]]></category>
            <category><![CDATA[mathjax]]></category>
            <dc:creator><![CDATA[Matteo Delucchi]]></dc:creator>
            <pubDate>Fri, 09 Oct 2020 17:00:12 GMT</pubDate>
            <atom:updated>2020-10-10T06:45:52.412Z</atom:updated>
            <content:encoded><![CDATA[<h4>R Markdown on Github Pages | Set LaTeX Parameters in bookdown</h4><h4>How to set LaTeX parameters in R Markdown. From simple to complex document structures — an example with bookdown.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*EYVaezlI40cHIyk_" /><figcaption>Photo by <a href="https://unsplash.com/@foxfox?utm_source=medium&amp;utm_medium=referral">Natalia Y</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I started my Bayesian Network Compendium as a classical LaTeX document. It’s purpose is not only to focus on theory but also show practical examples which I demonstrate in the programming language R. This brought me to switch from a vanilla LaTeX document to R Markdown.</p><p>Let me start with a very brief comparison of the markup languages LaTeX, Markdown and R Markdown to help you better understand how they play in concert. Then I will guide you on the implementation of user specific LaTeX settings in R Markdown documents.</p><p>At the end you will:</p><ul><li>have an overview over the landscape of some popular markup languages.</li><li>understand how R Markdown teams up with LaTeX to render math expressions.</li><li>be able to implement your own LaTeX settings in R Markdown and specifically in the R Package bookdown.</li></ul><p>I wrote this article with the intention to share my knowledge which might save other people’s time and nerves. I’m by far not an expert and I would love to hear if this article was helpful for you!</p><h3>LaTeX vs. Markdown vs. R Markdown</h3><p>It is not difficult to fill books about <a href="https://en.wikipedia.org/wiki/LaTeX">LaTeX</a>. But I try to keep it to one sentence for now: LaTeX is well known for its tidy typesetting (i.a. of mathematical formulas) by taking advantage of the <a href="https://en.wikipedia.org/wiki/TeX">TeX typesetting system</a> and is very popular especially in the academic world.</p><p>Markdown is a <a href="https://en.wikipedia.org/wiki/Lightweight_markup_language">lightweight markup language</a> which is already easy to read in its raw form and can be converted to for example .html documents. Today there exist many different flavours and syntaxes. For example, <a href="https://faq.whatsapp.com/general/chats/how-to-format-your-messages">Whatsapp released its own Markup syntax</a> in 2016 to provide basic formatting options to messages.</p><p><a href="https://bookdown.org/yihui/rmarkdown/">R Markdown </a>is a file format combining executable R code chunks with the easy syntax of a Markdown document. This combination allows us to easily present our analyses reproducibly in beautiful and interactive documents. R Markdown is based on the minimalistic assumption that it is sufficient to have only a limited number of Markdown elements available to create great documents.</p><blockquote>“Wealth consists not in having great possessions, but in having few wants.”<br>― <a href="https://bookdown.org/yihui/rmarkdown/index.html"><strong>Epictetus</strong></a></blockquote><p>Among those features that are available in R Markdown there is the possibility to <a href="https://bookdown.org/yihui/rmarkdown/markdown-syntax.html#math-expressions">format math equations with LaTeX</a>.</p><p>As an example I will use the problem that I faced while working on the Bayesian Network Compendium as bookdown project: To display the <a href="https://en.wikipedia.org/wiki/Conditional_independence">symbol for conditional independence</a> was quite easy in LaTeX.</p><pre>\perp\!\!\!\perp </pre><figure><img alt="Symbol for conditional independence." src="https://cdn-images-1.medium.com/max/116/1*l-eMQ2V6Q29sZs0unzohoA.png" /><figcaption>Symbol for conditional independence.</figcaption></figure><p>To reduce typos and increase code readability I defined a new LaTeX command.</p><h3>Define a new command in LaTeX</h3><p>I was looking for a way to abbreviate this complex syntax in a short way. The conditional independence symbol can be defined in<a href="https://tex.stackexchange.com/questions/3631/is-there-a-standard-symbol-for-conditional-independence"> LaTeX in multiple ways</a>. Just for completeness, beside \newcommand there are different ways on how to define a new command in LaTeX (for example using the raw TeX command <a href="https://tex.stackexchange.com/a/658/164936">\def</a>). I decided on this one:</p><pre>\newcommand{\ci}{\perp\!\!\!\perp}</pre><h3>LaTeX \newcommand in R Markdown</h3><p>Defining a new LaTeX command in a R Markdown document is also quite straight forward.</p><ol><li>Define the command as you would in LaTeX.</li><li>Put it below the YAML header at the beginning of your R Markdown file.</li><li>Call the new command within $…$ to let R Markdown know that this command is defined in the LaTeX environment.</li></ol><pre>---<br>title: Title<br>author: Author<br>date: &quot;10/10/2020&quot;<br>output: html_document<br>---<br><br>```{r setup, include=FALSE}<br>knitr::opts_chunk$set(echo = TRUE)<br>```<br><br><strong>\newcommand{\ci}{\perp\!\!\!\perp}</strong><strong><br></strong><br>## R Markdown<br><br>This is an R Markdown document. Inline math equations are put in between single dollar signs $(A <strong>\ci</strong> B)$. Math equation blocks are framed with double dollar signs</pre><pre>$$\begin{equation}<br>  (X <strong>\ci</strong> Y) | Z \iff Pr(A \cap B |C) = Pr(A|C)Pr(B|C)<br>\end{equation}$$</pre><p>More details about LaTeX macros in R Markdown can be found in the <a href="https://rmarkdown.rstudio.com/authoring_pandoc_markdown.html%23raw-tex#LaTeX_macros">Pandoc Markdown manual</a>.</p><p>Alternatively you can use a <strong><em>separate file with your LaTeX specific definitions.</em></strong> This is particularly helpful if you require specific LaTeX packages within the math environment of R Markdown or you encounter issues because you use a special output format.</p><ol><li>Create a new file called preamble.tex</li><li>Add the \usepackage and \newcommand calls from the <a href="https://www.overleaf.com/learn/latex/How_to_Write_a_Thesis_in_LaTeX_(Part_1):_Basic_Structure#The_preamble">preamble part of your LaTeX document</a> to this file. This could then look like this:</li></ol><pre>\usepackage{booktabs}<br>\usepackage{amsthm}<br>\newcommand{\ci}{\perp\!\!\!\perp}</pre><ol><li>Link the file preamble.tex with your R Markdown. Tell the LaTeX engine of R Markdown to run this file before the main body. You can do that in the YAML header of the R Markdown file:</li></ol><pre>---<br>title: Title<br>author: Author<br>date: &quot;10/10/2020&quot;<br><strong>output:<br>  html_document</strong><strong>:<br>    includes:<br>      in_header: preamble.tex</strong><br>---<br><br>```{r setup, include=FALSE}<br>knitr::opts_chunk$set(echo = TRUE)<br>```</pre><pre>## R Markdown<br><br>This is an R Markdown document. Inline math equations are put in between single dollar signs $(A \ci B)$. Math equation blocks are framed with double dollar signs</pre><pre>$$\begin{equation}<br>  (X \ci Y) | Z \iff Pr(A \cap B |C) = Pr(A|C)Pr(B|C)<br>\end{equation}$$</pre><p>Pretty neat, isn’t it? Well, there’s one drawback though. The LaTeX syntax provided through preamble.tex is not portable to other output formats. In other words, if you render your bookdown to .html or ePUBS, your packages are not loaded and no new command is defined. Why this is and how to handle other output formats is explained in the next section.</p><h3>LaTeX \newcommand in bookdown</h3><p>The R package bookdown can render the output in different formats such as .html, .pdf or ePUBS. Output format specific options are provided in the _output.yml file in the root project directory.</p><p>With bookdown::gitbook you’ll end with a separate .html file for each segment¹ specified with split_by: . For example with split_by: chapter+number you’ll get for each chapter a separate .html file whereas with split_by: section+number you end with separate .html files for each section. For .html output, bookdown renders LaTeX math equations with the MathJax library. MathJax is not able to detect the LaTeX macros specified in the preamble.tex for each .html file. The solution here is to create a file named mathjax_header.html in the root of the project directory with the content from preamble.tex (don’t delete it! We need it later again). Remember to reformat it to a <a href="https://bookdown.org/yihui/bookdown/yaml-options.html">html snippet</a>. For my example above mathjax_header.html would look like this:</p><pre>&lt;span class=&quot;math inline&quot;&gt;<br>\(\newcommand{\ci}{\perp\!\!\!\perp}\)<br>&lt;/span&gt;</pre><p>We now need to tell bookdown to use mathjax_header.html when it renders to .html. This is done in bookdown’s _output.yml</p><pre>bookdown::gitbook: <br>   css: style.css <br>   split_by: chapter+number <br>   includes: <br>      in_header: mathjax_header.html</pre><p>In the bookdown::pdf_book section of the _output.yml file you specify the settings for rendering to pdf. That’s where preamble.tex comes back in the game:</p><pre>bookdown::pdf_book: <br>   includes:<br>      in_header: preamble.tex</pre><h3>Conclusion</h3><p>You learned how to implement your own LaTeX command in a classic R Markdown document and also in the more complex bookdown framework.</p><p>Additionally I explained you the basic concept of how R Markdown and bookdown compile LaTeX math expressions.</p><p>If you’re looking for more detailed information, check out the links in the text.</p><p>Read more about my bookdown project in this previous article:</p><p><a href="https://medium.com/@delucmat/how-to-publish-bookdown-projects-with-github-actions-on-github-pages-6e6aecc7331e">How to publish bookdown projects with GitHub Actions on GitHub Pages</a></p><p>¹ Cave: In bookdown::gitbook the argument split_by is different to bookdown::tufte_html_book</p><pre>bookdown::gitbook:<br>   split_by: <strong>chapter</strong>+number</pre><pre><br>bookdown::tufte_html_book: <br>   split_by: <strong>section</strong>+number</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9804467caaf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to publish bookdown projects with GitHub Actions on GitHub Pages]]></title>
            <link>https://medium.com/@delucmat/how-to-publish-bookdown-projects-with-github-actions-on-github-pages-6e6aecc7331e?source=rss-3d3091edc72e------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e6aecc7331e</guid>
            <category><![CDATA[knowledge]]></category>
            <category><![CDATA[r-markdown]]></category>
            <category><![CDATA[github-pages]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[github-actions]]></category>
            <dc:creator><![CDATA[Matteo Delucchi]]></dc:creator>
            <pubDate>Tue, 29 Sep 2020 22:00:54 GMT</pubDate>
            <atom:updated>2020-10-09T17:01:38.194Z</atom:updated>
            <content:encoded><![CDATA[<h4>R markdown on GitHub Pages | automated with GitHub Actions</h4><h4>A 10 step guide to a completely automated and pure <a href="https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions">GitHub workflow</a> for hosting your <a href="https://bookdown.org/yihui/bookdown/">bookdown</a> project on <a href="https://pages.github.com/">GitHub Pages</a>. No <a href="https://travis-ci.org/">Travis</a>. No <a href="https://jekyllrb.com/">Jekyll</a>. No <a href="https://gohugo.io/">Hugo</a>. Not local.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Sfn1evCH539H1aRW" /><figcaption>Photo by <a href="https://unsplash.com/@kdghantous?utm_source=medium&amp;utm_medium=referral">Karim Ghantous</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>After writing the first parts of my Bayesian Network Compendium in a classical LaTeX document, I was looking for the optimal solution to receive continuous feedback on the manuscript and make it available to a wide audience while still working it.</p><p><a href="https://www.overleaf.com/">Overleaf</a> is a great tool for reviewing LaTeX documents. You share a link to the document with people you’d like to contribute — either by writing or commenting. This process is great if you publish after completion of the manuscript as it is customary in peer-reviewed scientific journal articles.</p><p>However, I aimed for an open source and continuous publishing method, where every one can contribute by giving feedback, ask for a specific topic to be covered or fix mistakes by still ensuring a peer-review quality. Similar to wikipedia but less public and more specific. Additionally, running R code directly in the document would be great. That was the point when I came across the R-package <a href="https://bookdown.org/yihui/bookdown/">bookdown</a> which could meet basically all my requirements.</p><blockquote>bookdown could meet all my requirements.</blockquote><h3>The Concept of bookdown</h3><p>With bookdown you can write your manuscript in R-Markdown (<a href="https://bookdown.org/yihui/blogdown/output-format.html">.Rmd vs .md</a>) and publish it as a website online. It’s straight forward to use even if your only little familiar with R and Rstudio (whereas the latter being no precondition though). Bookdown basically creates for each chapter a separate .Rmd file, compiles them to .html and wraps it in a nice theme. You can also compile it to .epubs and .pdf but I won’t go into that in this article.</p><p>The .html files (which were compiled from the .Rmd files) are all stored within the _book directory which basically serves as a static website.</p><blockquote>There are multiple ways to publish your book or manuscript online with bookdown.</blockquote><p>To publish it on your <em>own webserver</em>, you just copy-paste the _book directory.</p><p>Bookdown ships with the command bookdown::<strong>publish_book</strong>() to directly publish on the <a href="https://rstudio.com/products/connect/"><em>R Studio Connect</em></a><em> server</em> where you first have to register.</p><p>To host on <em>GitHub Pages</em> the <a href="https://bookdown.org/yihui/bookdown/github.html">recommended way</a> to connect with bookdown is via <a href="https://jekyllrb.com/">Jekyll</a> (a static website builder) or by pushing the locally rendered .html files on a branch named gh-pages and tell GitHub to publish from this branch. This manual process can be automatized in a script.</p><p>Alternatively, the fully “Githubian-way” is using <a href="https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions">GitHub Action</a> instead of Jekyll. The concept is easy: After pushing your uncompiled bookdown project (no _book directory, no .html files) to your GitHub project repository, you create a second branch gh-pages . Then you add a GitHub Action workflow to the master branch and… voilà, there’s your first bookdown publication rendered and published by GitHub only.</p><p>Let me guide you through the process step by step.</p><ol><li>We install the R package bookdown</li><li>We configure our bookdown project with GIT and connect to GitHub</li><li>We setup a Github Action workflow for rendering on GitHub and deployment to GitHub Pages.</li></ol><p>Jump to step 3 if you already have your bookdown project on Github.</p><h3>I Install bookdown</h3><p>I assume you have a running and updated <a href="https://cran.r-project.org/doc/manuals/r-release/R-admin.html">R</a> and <a href="https://rstudio.com/products/rstudio/download/#download">RStudio</a> installation.</p><ol><li>Download the GitHub<a href="https://github.com/rstudio/bookdown-demo"> Repository</a> as a zip file and unzip it in your new project directory</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/553d4ffb525f4d8ff1120d43e2a46909/href">https://medium.com/media/553d4ffb525f4d8ff1120d43e2a46909/href</a></iframe><p>2. Install the bookdown R package</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/490e19ccf574f4c17ff2b1a350d0935a/href">https://medium.com/media/490e19ccf574f4c17ff2b1a350d0935a/href</a></iframe><p>3. Open the R Project from the demo repository in R Studio.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c32c3da185e79de18b24a9424a9e2fb9/href">https://medium.com/media/c32c3da185e79de18b24a9424a9e2fb9/href</a></iframe><p>4. Open within R Studio the file index.Rmd and render the demo bookdown</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cee6009c273d7b3d3f630d07e359a6db/href">https://medium.com/media/cee6009c273d7b3d3f630d07e359a6db/href</a></iframe><p>You should now see in the viewer pane of R Studio the rendered demo page. Check the <a href="https://bookdown.org/yihui/bookdown/">official manual</a> for more detailed information.</p><h3>II Setup your bookdown Project with GIT &amp; GitHub</h3><p>5. Let’s first clean up our project directory and initialize a new GIT repository</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7322a6675489b922bb6cdc01dd82ef43/href">https://medium.com/media/7322a6675489b922bb6cdc01dd82ef43/href</a></iframe><p>6. Create an empty GitHub Repository testbookdown-repo on Github.com</p><p>7. Connect your local GIT repository to the fresh repository on GitHub and push your master branch</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dbc6d86d19e2262908f76ae090a191c1/href">https://medium.com/media/dbc6d86d19e2262908f76ae090a191c1/href</a></iframe><h3>III Create a Github Action workflow</h3><h4>Start here, if you already have your bookdown project on GitHub but keep an eye on step 5!</h4><p>8. Create a local empty gh-pages branch and push to testbookdown-repo on GitHub.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/930738794142bdd079e59199742da340/href">https://medium.com/media/930738794142bdd079e59199742da340/href</a></iframe><p>9. Create GitHub secrets GH_PAT and EMAIL. GH_PAT is a personal access token with repository access. EMAIL is the mail address you use to login to GitHub.</p><ul><li>GH_PAT: Go to the <a href="https://github.com/settings/tokens">developer settings of your </a>GitHub<a href="https://github.com/settings/tokens"> account</a> and create a new token. Copy the code you’ll receive and go to your repository’s secrets in the tab “settings” (<a href="https://github.com/matteodelucchi/bncompendium/settings/secrets">https://github.com/&lt;username&gt;/testbookdown-repo/settings/secret</a>s) and create a new secret named GH_PAT and paste the code of the token.</li><li>EMAIL: Go to your repository’s secrets in the tab “settings” (<a href="https://github.com/matteodelucchi/bncompendium/settings/secrets">https://github.com/&lt;username&gt;/testbookdown-repo/settings/secret</a>s) and create a new secret named EMAIL and enter your mail address that you use to login to GitHub.</li></ul><p>10. Go back to your R Studio session (master branch) and add with this command</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea3fd1e890aae90931c449e18845671e/href">https://medium.com/media/ea3fd1e890aae90931c449e18845671e/href</a></iframe><p>automatically the GitHub Workflow shown below in the required directory: .github/workflow/deploy_bookdown.yml</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93a436e8b76f1980459d34a411ccc2cf/href">https://medium.com/media/93a436e8b76f1980459d34a411ccc2cf/href</a></iframe><p>The workflow basically consists of two parts. First it sets up a container with an R installation set for compiling bookdown projects. After rendering the book it stores the _book/ directory which is in the second part deployed to GitHub Pages.</p><p>GitHub usually automatically chooses the branch gh-pages with an index.html file present to publish on GitHub Pages. This can be changed in the repository settings. That’s also where you find the link to your GitHub Page.</p><h3>Continuous Review Process with GitHub</h3><p>Check out my next article on how our team makes use of GitHub to continuously extend, review and integrate changes in the text while always having the latest version online. I’ll also show how to get all out of bookdown and make full use of LaTeX.</p><p><a href="https://medium.com/@delucmat/specify-latex-parameters-in-r-markdown-9804467caaf">Specify LaTeX Parameters in R Markdown</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e6aecc7331e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>