13

I'm using bookdown to create an HTML gitbook from R markdown files (i.e. .Rmd), with the default option of split_bib = TRUE resulting in a bibliography at the end of each chapter, as well as a complete bibliography at the end of the book.

The end-of-book bibliography is in alphabetical order, but the end-of-chapter bibliographies are not. (Here's an example).

How can I arrange all reference lists alphabetically?

Martin Smith
  • 3,687
  • 1
  • 24
  • 51

1 Answers1

3

$(function(){
    var elems = $('#refs').children('div').remove();
    elems.sort(function (a, b) {
        return b.id > a.id ? -1 : 1;
    });
    $('#refs').append(elems);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="refs" class="references">
  <div id="ref-Goloboff2016">
    <p>Goloboff, P. A., and S. A. Catalano. 2016: TNT version 1.5, including a full implementation of phylogenetic morphometrics. Cladistics 32:221–238.</p>
  </div>
  <div id="ref-Goloboff1999">
    <p>Goloboff, P. 1999: Analyzing large data sets in reasonable times: solutions for composite optima. Cladistics 15:415–428.</p>
  </div>
  <div id="ref-Nixon1999">
    <p>Nixon, K. C. 1999: The Parsimony Ratchet, a new method for rapid parsimony analysis. Cladistics 15:407–414.</p>
  </div>
  <div id="ref-Goloboff1997">
    <p>Goloboff, P. A. 1997: Self-weighted optimization: tree searches and character state reconstructions under implied transformation costs. Cladistics 13:225–245.</p>
  </div>
</div>

My solution sorts by the id of the divs within #refs. You didn't specify whether you wanted to sort by surname ascending and year descending, which would necessitate something more complicated.

Yvonne Aburrow
  • 2,602
  • 1
  • 17
  • 47
  • A clever answer, for sure! Not marking as accepted just yet, as it feels like an _a priori_ solution ought also to exist without relying on javascript? – Martin Smith Apr 17 '18 at 07:28
  • 1
    you should be able to use a more precise CSS selector to only sort the references you wanted to sort - or you could use the `:not` pseudo selector https://developer.mozilla.org/en-US/docs/Web/CSS/:not – Yvonne Aburrow Apr 17 '18 at 09:17
  • [Restored accidentally deleted comment:] I've had a stab at implementing this (using the includes:after_body bookdown option) and an unfortunate side-effect is that it jumbles the order on the references page, as these references are contained in divs that lack an ID element. Is there an elegant way to disable the script on the references page, short of pasting it manually into all other pages? – Martin Smith Apr 17 '18 at 09:21
  • 1
    I ended up modifying the elems.sort function to read `return $(b).children('p').html() > $(a).children('p').html() ? -1 : 1;`, and including the script in an html file linked in the `bookdown::gitbook: includes: after_body` directive of _output.yml. This then sorts the refs on each page and in the bibliography, alphabetically. – Martin Smith Apr 19 '18 at 06:45