<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/writing-academic-articles-using-r-markdown-and-latex/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r.prevos.net/r-markdown-and-latex/"> The Devil is in the Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>One of my favourite activities in R is using Markdown to create business reports. Most of my work I export to MS Word to communicate analytical results with my colleagues. For my academic work and eBooks, I prefer LaTeX to produce great typography. This article explains how to write academic articles and essays combining R Markdown and LaTeX. The article is formatted in accordance with the APA (American Psychological Association) requirements.</p>
<p>To illustrate the principles of using R Markdown and LaTeX, I recycled an essay about problems with body image that I wrote for a psychology course many years ago. You can find the completed paper and all necessary files on my <a rel="nofollow" href="https://github.com/pprevos/r.prevos.net/tree/master/Misc/BodyImage" target="_blank">GitHub repository</a>.</p>
<p><a rel="nofollow" href="http://r.prevos.net/?attachment_id=1022" target="_blank"><img data-orig-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/09/openbook_1000x647.jpg?fit=996%2C384" data-orig-size="996,384" data-medium-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/09/openbook_1000x647.jpg?fit=300%2C116" data-attachment-id="1022" data-image-meta="{" data-image-description="" src="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/09/openbook_1000x647.jpg?resize=584%2C225" data-permalink="http://r.prevos.net/r-markdown-and-latex/openbook_1000x647/" data-large-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/09/openbook_1000x647.jpg?fit=584%2C225" alt="R Markdown and LaTeX using the APA journal template" data-image-title="openbook_1000x647" sizes="(max-width: 584px) 100vw, 584px" data-comments-opened="1" class="wp-image-1022" data-recalc-dims="1"></a></p>
<h2>Body Image</h2>
<p>Body image describes the way we feel about the shape of our body. The literature on this topic demonstrates that many people, especially young women, struggle with their body image. A negative body image has been strongly associated with eating disorders. Psychologists measure body image using a special scale, shown in the image below.</p>
<p>My paper measures the current and ideal body shape of the subject and the body shape of the most attractive other sex. The results confirm previous research which found that body dissatisfaction for females is significantly higher than for men. The research also found a mild positive correlation between age and ideal body shape for women and between age and the female body shape found most attractive by men. You can read the full paper on my <a rel="nofollow" href="https://prevos.net/humanities/psychology/body-image/" target="_blank">personal website</a>.</p>
<div class="wp-caption"><a rel="nofollow" href="http://r.prevos.net/?attachment_id=851" target="_blank"><img data-orig-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/07/BodyScale.png?fit=850%2C566" data-orig-size="850,566" data-medium-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/07/BodyScale.png?fit=300%2C200" data-attachment-id="851" data-image-meta="{" data-image-description="" src="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/07/BodyScale.png?resize=584%2C389" data-permalink="http://r.prevos.net/r-markdown-and-latex/bodyscale/" data-large-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/07/BodyScale.png?fit=584%2C389" alt="Body shape measurement scale." data-image-title="BodyScale" sizes="(max-width: 584px) 100vw, 584px" data-comments-opened="1" class="wp-image-851" data-recalc-dims="1"></a>
<p class="wp-caption-text">Body shape measurement scale.</p>
</div>
<h2>R Markdown and LaTeX</h2>
<p>The R Markdown file for this essay uses the Sweave package to integrate R code with LaTeX. The first two code chunks create a table to summarise the respondents using the <a rel="nofollow" href="https://cran.r-project.org/web/packages/xtable/index.html" target="_blank">xtable</a> package. This package creates LaTeX or HTML tables from data generated by R code.</p>
<p>The first lines of the code read and prepare the data, while the second set of lines creates a table in LaTeX code. The code chunk uses <code>results=tex</code> to ensure the output is interpreted as LaTeX. This approach is used in most of the other chunks. The image is created within the document and saved as a pdf file and back integrated into the document as an image with appropriate label and caption.</p>
<pre>
&lt;echo=FALSE, results=tex>>=
body &lt;- read.csv(&quot;body_image.csv&quot;)
# Respondent characteristics
body$Cohort &lt;- cut(body$Age, c(0, 15, 30, 50, 99), 
                   labels = c(&quot;&lt;16&quot;, &quot;16--30&quot;, &quot;31--50&quot;, &quot;>50&quot;))
body$Date &lt;- as.Date(body$Date)
body$Current_Ideal &lt;- body$Current - body$Ideal

library(xtable)
respondents &lt;- addmargins(table(body$Gender, body$Cohort))
xtable(respondents, caption = &quot;Age profile of survey participants&quot;, 
                    label = &quot;gender-age&quot;, digits = 0)
@
</pre>
<h2>Configuration</h2>
<p>I created this file in R Studio, using the <a rel="nofollow" href="https://support.rstudio.com/hc/en-us/articles/200552056-Using-Sweave-and-knitr" target="_blank">Sweave and </a>knitr functionality. To knit the R Markdown file for this paper you will need to install the <a rel="nofollow" href="https://ctan.org/pkg/apa6" target="_blank">apa6</a> and <a rel="nofollow" href="https://ctan.org/pkg/ccicons?lang=en" target="_blank">ccicons</a> packages in your LaTeX distribution. The apa6 package provides macros to format papers in accordance with the requirements American Psychological Association.</p>
<p>The post <a rel="nofollow" href="http://r.prevos.net/r-markdown-and-latex/" target="_blank">Writing academic articles using R Markdown and LaTeX</a> appeared first on <a rel="nofollow" href="http://r.prevos.net/" target="_blank">The Devil is in the Data</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r.prevos.net/r-markdown-and-latex/"> The Devil is in the Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/uSeJEPH5sD8" height="1"></a></div>