Gitbook chapter bibliography not in alphabetical order
Asked Answered
C

1

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?

Cutlor answered 20/3, 2018 at 17:50 Comment(2)
Is your project's repository online? Can you point me to it?Incommunicative
github.com/ms609/HyFriedrick
V
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.

Violent answered 16/4, 2018 at 14:14 Comment(4)
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?Cutlor
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 developer.mozilla.org/en-US/docs/Web/CSS/:notViolent
[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?Cutlor
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.Cutlor

© 2022 - 2024 — McMap. All rights reserved.