ge-ne/bibtool

Sorting records by new entry types Online, Thesis, Software (BibLaTeX)?

Closed this issue · 4 comments

bibtoolrsc

% print.line.length        = 1000000
print.align.key          = 0
print.indent             = 2
print.newline            = 0
print.use.tab            = off
suppress.initial.newline = on
symbol.type              = lower
pass.comments            = on

% Manage keys manually:
preserve.keys            = on
preserve.key.case        = on

% BibLaTeX additions from
new.entry.type{Online}
new.entry.type{Thesis}
new.entry.type{Software}

% Sort records
sort                     = on
sort.macros              = {off}
sort.format              = { %s($type) %s(year) %s(month) {%N(author) # %N(editor)} }

Example bib file:

❯ cat demo.bib
@Software{Singular:2018,
  author        = {Decker, Wolfram and Greuel, Gert-Martin and Pfister, Gerhard and Sch\"onemann, Hans},
  title         = {{\sc Singular} {4-1-1} --- {A} computer algebra system for polynomial computations},
  year          = {2018},
  howpublished  = {\url{http://www.singular.uni-kl.de}},
  url           = {http://www.singular.uni-kl.de/}
}
@Software{cgal,
  author        = {The CGAL Project},
  title         = {The Computational Geometry Algorithms Library {(CGAL)}},
  url           = {https://www.cgal.org/},
}
@Online{DB_COD:2018,
  author        = {Grazulis, S., Chateigner, D., Downs, R. T., Yokochi, A. T., Quiros, M., Lutterotti, L., Manakova, E., Butkus, J., Moeck, P. AND Le Bail},
  title         = {Crystallography Open Database (COD)},
  date          = {2003/},
  url           = {http://www.crystallography.net/cod/},
  urldate       = {2018-05-05},
  annote        = {395.186 entries in the COD}
}
@Article{Bernal:1960,
  author        = {Bernal, J. D.},
  title         = {{Geometry of the Structure of Monatomic Liquids}},
  doi           = {10.1038/185068a0},
  issn          = {0028-0836},
  journal       = {Nature},
  month         = {jan},
  number        = {4706},
  pages         = {68--70},
  volume        = {185},
  year          = {1960}
}
❯ bibtool -v demo.bib
--- BibTool: Trying /Users/kiryph/.bibtoolrsc
--- BibTool: Set resource print.align.key = 0
--- BibTool: Set resource print.indent = 2
--- BibTool: Set resource print.newline = 0
--- BibTool: Set resource print.use.tab = off
--- BibTool: Set resource suppress.initial.newline = on
--- BibTool: Set resource symbol.type = lower
--- BibTool: Set resource pass.comments = on
--- BibTool: Set resource preserve.keys = on
--- BibTool: Set resource preserve.key.case = on
--- BibTool: Set resource new.entry.type = Online
--- BibTool: Set resource new.entry.type = Thesis
--- BibTool: Set resource new.entry.type = Software
--- BibTool: Set resource sort = on
--- BibTool: Set resource sort.macros = off
--- BibTool: Set resource sort.format =  %s($type) %N(year) month {%N(author) # %N(editor)}
--- BibTool: Trying ./demo.bib
--- BibTool: Reading demo.bib
++++--- BibTool: Done with demo.bib
@Article{Bernal:1960,
  author        = {Bernal, J. D.},
  title         = {{Geometry of the Structure of Monatomic Liquids}},
  doi           = {10.1038/185068a0},
  issn          = {0028-0836},
  journal       = {Nature},
  month         = {jan},
  number        = {4706},
  pages         = {68--70},
  url           = {http://www.nature.com/doifinder/10.1038/185068a0},
  volume        = {185},
  year          = {1960}
}
@Software{Singular:2018,
  author        = {Decker, Wolfram and Greuel, Gert-Martin and Pfister,
                  Gerhard and Sch\"onemann, Hans},
  title         = {{\sc Singular} {4-1-1} --- {A} computer algebra system for
                  polynomial computations},
  year          = {2018},
  howpublished  = {\url{http://www.singular.uni-kl.de}},
  url           = {http://www.singular.uni-kl.de/}
}
@Software{cgal,
  author        = {The CGAL Project},
  title         = {The Computational Geometry Algorithms Library {(CGAL)}},
  url           = {https://www.cgal.org/}
}
@Online{DB_COD:2018,
  author        = {Grazulis, S., Chateigner, D., Downs, R. T., Yokochi, A.
                  T., Quiros, M., Lutterotti, L., Manakova, E., Butkus, J.,
                  Moeck, P. AND Le Bail},
  title         = {Crystallography Open Database (COD)},
  date          = {2003/},
  url           = {http://www.crystallography.net/cod/},
  urldate       = {2018-05-05},
  annote        = {395.186 entries in the COD}
}

IMHO it looks like bibtool cannot sort the new entry types. They are treated as the same type and sorted by the other criteria.

@ge-ne Any chance you could have a look into this older issue as well. Is this more complicated than I imagine?

ge-ne commented

sort.format = { %s($type) %s(year) %s(month) {%N(author) # %N(editor)} }

Whenever a format specifier fails -- because a field is not found or other reasons -- then backtracking takes place and an alternative is sought.
In the example this happens for the fields year and month which are not present in the new types.Thus backtracking occurs until the whole format fails. Finally the default key is used as final alternative. For those entries the key determines the sorting order. This can be seen when using the format to generate new keys.
A construction like {%s(year)#} and {%s(month)#} solve this problem.
The same behaviour can show up when neither author nor editor are present. The solution applies he same trick.

Thanks for the explanation and solution. Very much appreciated.

I have read section A.7 in the documentation again. I think a few places could be improved. For example, the sentence

If everything fails the sort key is empty.

should be

If everything fails, the sort key is empty and the default sort key is used which is the reference key.

This is already mentioned at the beginning of this section:

The sort key is by default the reference key.

Also I would add something like

Whenever a format specifier fails -- because a field is not found or other reasons -- then backtracking takes place and an alternative is sought. Backtracking occurs until the whole format fails and the default sort key is used which is the reference key.
For example, when you want to sort chronologically by year and month as the first criteria and as secondary criteria by author or editor name, you should write the expression as

sort.format = { {%s(year) # } {%s(month) # } {%N(author) # %N(editor) # } }

This avoids that the whole sort format fails if any of year and month or both author and editor are not present.

For debugging purposes, I tried to see the sort key with

% Sort records
sort                     = on
sort.macros              = {off}
sort.format              = { %s($type) {%s(year)#} {%s(month)#} {%N(author) # %N(editor) #} }

add.field {sort="%s($sortkey)"}

However, the new field 'sort' is empty? Did I miss something?

Finally, I want to take here the opportunity to thank you for all your recent comments, commits and in general for bibtool. Many open source projects are abondaned after period of time but often I encounter issues which I would hope could be addressed.

ge-ne commented

However, the new field 'sort' is empty? Did I miss something?

The field rewriting (add.field) is performed before key creation. This is necessary to make sure that fields referenced in sort format can be provided before they are used. Thus the sort key is empty when you add it...