<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/blogdown-shortcode-for-radix-like-bibtex/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.yihanwu.ca/post/blogdown-shortcode-generation-for-bibtex/"> R on YIHAN WU</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In the spirit of trying out new things in Hugo since my <a rel="nofollow" href="https://www.yihanwu.ca/post/hugo-templating-adding-enclosure-image-in-rss-feed" target="_blank">last post</a> on modifying the RSS feed for this website, I attempted to implement the new citation feature from the new <a rel="nofollow" href="https://rstudio.github.io/radix/" target="_blank"><code>radix</code></a> package by RStudio.</p>
<p>Essentially, I tried using a custom hugo shortcode to replicate the text and BibTex citation at the bottom of the page when rendered by <code>radix</code>.</p>
<p></p>
<p>Custom shortcodes should live in the <code>layouts/shortcodes</code> directory as a <code>example_shortcode.html</code> and then the HTML code can be injected into the page through a shortcode as <code>
<pre>{{&lt; example_shortcode >}}</pre>
<p>. A lot of this code comes directly from the hugo documentation on <a rel="nofollow" href="https://gohugo.io/content-management/shortcodes/" target="_blank">shortcodes</a> and the hugo community forums.</p>
<p>The entire shortcode is below:</p>
<pre><code><h6>Citation:</h6>
<p>For attribution, please cite this work as</p>
<code>
{{$.Page.Params.author}}. ({{dateFormat &quot;2006-01-02&quot; $.Page.Params.date }}). &quot;{{$.Page.Params.title}}&quot;. Retrieved from {{ $.Page.Permalink }}. 
</code>
<p>BibTex citation</p>
<code><pre>
  @misc{{{$name_string := replace $.Page.Params.author &quot; &quot; &quot;_&quot;}}{{$name_string}}{{dateFormat &quot;2006-01-02&quot; $.Page.Params.date }},
    title = {{$title_str := $.Page.Params.title}}{{printf &quot;{%s}&quot; $title_str}},
    howpublished = {{$url_str := $.Page.Permalink}}{{printf &quot;{\\url{%s}}&quot; $url_str}},
    year = {{$year_str := dateFormat &quot;2006&quot; $.Page.Params.date }}{{printf &quot;{%s}&quot; $year_str}}
  }
</pre></code>
</code></pre>
<p>The first two lines of the shortcode are simple HTML tags, and the next line <code>{{$.Page.Params.author}}. ({{dateFormat &quot;2006-01-02&quot; $.Page.Params.date }}). &quot;{{$.Page.Params.title}}&quot;. Retrieved from {{ $.Page.Permalink }}.</code> takes in the parameter values from the blog post. So author, the date, and the post title.</p>
<p>Date can be formatted by specifying the format using <code>dateFormat</code>.</p>
<p>The main difficulty came with getting the BibTex citation to show up properly. First, BibTex works best with curly brackets, but double curly brackets are used to access and run Hugo code and functions. I could not find any way of escaping the curly bracket used in BibTex formatting. <code>{{{</code> in a row would throw errors so I first used <code>printf</code> to insert the string between the curly brackets.</p>
<p>In this line of code, <code>{{$title_str := $.Page.Params.title}}{{printf &quot;{%s}&quot; $title_str}}</code>, I first assigned the parameter to a string variable and then printed the string with <code>printf</code>. But this method did not work for the first and last curly brace. The simpler soluation is to just use HTML special characters which I only realized after examining the hugo documentation site page source.</p>
<p>So really, the shortcode could be cleaner if I just used HTML special characters instead of a <code>prinf</code> string insert, but I am unfamiliar with the HTML codes that the go code is more understandable to me than trying to figure out what each HTML character actually is.</p>
<p>And here is the end result.</p>
<h6>Citation:</h6>
<p>For attribution, please cite this work as</p>
<p><code>
<pre>
Yihan Wu. (2018-12-21). &quot;Blogdown - shortcode for radix-like Bibtex&quot;. Retrieved from https://www.yihanwu.ca/post/blogdown-shortcode-generation-for-bibtex/. 
</pre>
<p></p>
<p>BibTex citation</p>
<p><code>
<pre>
  @misc{Yihan_Wu2018-12-21,
    title = {Blogdown - shortcode for radix-like Bibtex},
    howpublished = {\url{https://www.yihanwu.ca/post/blogdown-shortcode-generation-for-bibtex/}},
    year = {2018}
  }
</pre>
<p></p>
<p></p>
<p>I haven’t looked up what is the difference between a partial and a shortcode in hugo but I imagine it would not be hard to add this as a partial and have an automatic citation set up for every blog post/page.</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="https://www.yihanwu.ca/post/blogdown-shortcode-generation-for-bibtex/"> R on YIHAN WU</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 width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/C9OGrh70Yvo" height="1"></a></div></code></code></code>