cboettig / knitcitations

:package: Generate citations for knitr markdown and html files
http://carlboettiger.info
Other
220 stars 28 forks source link

Month as text in bibtex #76

Open dmenne opened 9 years ago

dmenne commented 9 years ago

Using biber/biblatex, it is valid to use a string for month, even if various versions are around (December, dec, {December}, {Dec}). For one example, see

http://tex.stackexchange.com/questions/70455/bibtex-month-format

where some possible mappings are indicated in egreg's answer.

read.bibtex (knitcitations 1.0.5) chokes on these string and wants a number. I know I can suppress the warnings, but I might miss more important ones when doing so.

@Article{anton,
  Title                    = {Relativity is relative},
  Author                   = {Einstein},
  Journal                  = {Journal},
  Year                     = {1988},
  Month                    = {Dec} 
}

1: All formats failed to parse. No formats found. 2: In ProcessDate(bib[["year"]], bib[["month"]]) : Failed to parse month: Dec. Ignoring and using year only.

mwmclean commented 9 years ago

What version of RefManageR do you have installed? I cannot reproduce this with the development version of RefManageR on GitHub. knitcitations::read.bibtex is just a call to RefManageR::ReadBib.

dmenne commented 9 years ago

Sorry, my first edit misunderstood your question. RefManageR is 0.8.45, Windows, R 3.1.3

mwmclean commented 9 years ago

I'm not following how that is relevant to your first post or my reply. I just ran your new example and all I get is warnings for packages that don't have a date field to begin with. I have also confirmed that the first example you provided with Month = {Dec} is parsed fine by the version of RefManageR available on CRAN.

library(knitcitations)
tfile <- tempfile(fileext = ".bib")
writeLines("@Article{anton,
  Title                    = {Relativity is relative},
  Author                   = {Einstein},
  Journal                  = {Journal},
  Year                     = {1988},
  Month                    = {Dec},
}", tfile)
read.bibtex(tfile)
## [1] Einstein. "Relativity is relative". In: _Journal_ (Dec. 1988).
sessionInfo()
## R Under development (unstable) (2015-02-26 r67906)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 7 x64 (build 7601) Service Pack 1

## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    

## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     

## other attached packages:
## [1] knitcitations_1.0.5 RefManageR_0.8.45  

## loaded via a namespace (and not attached):
##  [1] compiler_3.2.0    httr_0.6.1        plyr_1.8.1        tools_3.2.0      
##  [5] RCurl_1.95-4.5    memoise_0.1       Rcpp_0.11.4       lubridate_1.3.3  
##  [9] RJSONIO_1.3-0     digest_0.6.3      stringr_0.6.2     bibtex_0.4.0.9000
## [13] bitops_1.0-6      XML_3.98-1.1    
dmenne commented 9 years ago

Your pointer is correct, it is a local problem. So we should move this to mwmclean, but since he is here .-).

LC_COLLATE=German_Germany.1252;LC_CTYPE=German_Germany.1252;LC_MONETARY=German_Germany.1252;LC_NUMERIC=C;LC_TIME=German_Germany.1252"

When I replace "Dec" by Dez or Dezember, ReadBib/RefManageR works Ok. I assume that not too many users have non-english locales in bib files , so it would be better to have English as the default.

I did not see a locale in ReadBib, did I miss it? What's the best workaround?

mwmclean commented 9 years ago

RefManageR uses lubridate::parse_date_time to parse date fields. parse_date_time has an argument locale which defaults to Sys.getlocale("LC_TIME"), so if you use Sys.setlocale("LC_TIME", biblocale) before calling read.bibtex or ReadBib, where biblocale is an appropriate string for your system and the date fields in your .bib file, things should work fine.

dmenne commented 9 years ago
library("RefManageR")
Sys.setlocale("LC_TIME", "English_United States.1252")
ReadBib("menne.bib")

Thanks, works, but would require resetting the locale and try() wrapping after ReadBib, otherwise other things will go wrong. My suggestion would be to handle set/reset internally, and using English as the default for months/time in bib files.

mwmclean commented 9 years ago

Why do you need try() and not just another call to Sys.setlocale right after calling ReadBib? This all definitely seems worth noting in the ReadBib help page. I guess I'll add an LC_TIME option to BibOptions() though I don't really see what's so bad about asking the user to call Sys.setlocale themselves like they should be used to doing when they need to work with locales different from their default. It is not obvious to me that it is a good idea for RefManageR to automatically override whatever the user has set for Sys.getlocale("LC_TIME") to whatever English is for their OS.

dmenne commented 9 years ago

Why "try": because ReadBib may fail, and I always wrap everything in a 'try' when global parameters are changed. If you do not want to put it into ReadBib code, documentation should be ok, but the same should be in knitcitation's documentation.