3 min read

Blog posts with academic styles

Goal: I want to write academic style blog posts with: citations, cross-reference of tables and figures, and I want to manage figure path by myself. The default setting of blogdown can handle citations and cross-references pretty well thanks to Yihui’s awesome work on bookdown and blogdown packages, but the figures are nested too deep. I just want to put all figures under static/figures.

After a bit of digging, I managed to do this. The main trick is to add a knitr setup chunk to the Rmd file, and then parse it with blogdown::render_page(), based on Yihui’s set up. If a post does not have any figures, it will pass the first step and go directly with blogdown::render_page(). I did not look through all functions available from the blogdown package. But I am sure there must be a better way to do this. Anyway, I get what I want at this moment.


Citations

For citations, put the bibtex file in the same folder as the post, and then add bibliography: ref.bib in the yaml. You can even define the citation styles via csl: url_of_csl_file in the yaml.1 Thousands of csl files are available at Github CSL repository. Go and find one you like and paste the url in the yaml.

Testing paragraph: Invasion of non-native species, one of the most widespread and harmful consequences of global change, is causing worldwide ecosystem degradation and economic loss (Vilà et al., 2011; Simberloff et al., 2013).

Math equations

Here is inline equations \(a^2 + b^2 = c^2\); and display equations:

\[f(x)=\frac{1}{\sigma\sqrt{2\pi}}e^{-\frac{(x-\mu)^{2}}{2\sigma^{2}}}\]

R code chunk

summary(cars)
##      speed           dist    
##  Min.   : 4.0   Min.   :  2  
##  1st Qu.:12.0   1st Qu.: 26  
##  Median :15.0   Median : 36  
##  Mean   :15.4   Mean   : 43  
##  3rd Qu.:19.0   3rd Qu.: 56  
##  Max.   :25.0   Max.   :120

Including plots and cross-refer it back

You can also embed plots and cross-refer it with \@ref(fig:figure-label), for example Figure 1:

plot(pressure)
Here is figure caption.

Figure 1: Here is figure caption.

Table and cross-reference

You can also print tables and cross-refer it with \@ref(tab:table-label), for example Table 1:

knitr::kable(head(pressure), caption = "Table legend.")
Table 1: Table legend.
temperature pressure
0 0.0002
20 0.0012
40 0.0060
60 0.0300
80 0.0900
100 0.2700

That’s it.

Any suggestions to improve this workflow? Comment below or send me a pull request. Thanks.

References

Simberloff, D., Martin, J.-L., Genovesi, P., Maris, V., Wardle, D.A., Aronson, J., Courchamp, F., Galil, B., García-Berthou, E., Pascal, M. & others (2013) Impacts of biological invasions: What’s what and the way forward. Trends in ecology & evolution, 28, 58–66.

Vilà, M., Espinar, J.L., Hejda, M., Hulme, P.E., Jarošík, V., Maron, J.L., Pergl, J., Schaffner, U., Sun, Y. & Pyšek, P. (2011) Ecological impacts of invasive alien plants: A meta-analysis of their effects on species, communities and ecosystems. Ecology letters, 14, 702–708.


  1. A local file in the same folder will work too.