Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
662 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
|
||
|
||
|
||
|
||
This gives a quick example about how we can add citations into a markdown file with the same ease that we can in latex. | ||
I illustrate two different ways to get citation information. In the first example we look up the metadata automatically given | ||
the digital object identifier (doi) for the paper. | ||
|
||
In the second example we will read in a bibliography in bibtex format using the bibtex package, which can then be expressed in markdown immediately. | ||
|
||
Next I create an inline citation function that can take either a list of doi's or bibentry topics and format them into inline citations, while also storing this information to generate the full bibliography later. | ||
|
||
### DOI Approach | ||
|
||
The crossref function is available in the rplos package 0.0-6, (use the github copy, not in the CRAN version yet) | ||
|
||
|
||
```r | ||
require(rplos) | ||
``` | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
### Bibtex approach | ||
|
||
For convenience let's write the entry we pulled with crossref to a bibfile, just to get a simple bibfile as our starting point. | ||
|
||
|
||
```r | ||
library(bibtex) | ||
entry <- crossref("10.1111/j.1461-0248.2005.00827.x") | ||
write.bib(entry, file="example.bib") | ||
``` | ||
|
||
|
||
|
||
|
||
We simply read in any bibtex file and print the citation: | ||
|
||
|
||
``` | ||
Halpern BS, Regan HM, Possingham HP and McCarthy MA (2006). "Accounting | ||
for uncertainty in marine reserve design." _Ecology Letters_, *9*. ISSN | ||
1461-023X, <URL: http://dx.doi.org/10.1111/j.1461-0248.2005.00827.x>. | ||
``` | ||
|
||
|
||
|
||
Now this would be much more awesome if we could generate keys on write.bib and use those bibtex keys, instead of the index value, `[[1]]`, to generate the citation. | ||
|
||
## Inline citations | ||
The other thing we need is a way to print inline citations that would just use a given shortened format (e.g. author-year, or a number) and add the citation to a `works_cited` object, which we could then use to generate the full citation information at the end. | ||
|
||
|
||
|
||
```r | ||
empty <- list() | ||
class(empty) <- "bibentry" | ||
options(works_cited = empty) | ||
pcite <- function(x){ | ||
sapply(x,function(x){ | ||
if(is(x, "character")) | ||
entry <- crossref(x) | ||
else # assume it's a bibentry object already | ||
entry <- x | ||
## keep track of what we've cited so far | ||
options(works_cited = c(getOption("works_cited"), entry)) | ||
|
||
## And format the inline citation | ||
n <- length(entry$author) | ||
if(n==1) | ||
sprintf("(%s, %s)", entry$author[[1]]$family, entry$year) | ||
if(n==2) | ||
sprintf("(%s & %s, %s)", entry$author[[1]]$family, entry$author[[2]]$family, entry$year) | ||
else if(n>2) | ||
sprintf("(%s _et. al._ %s)", entry$author[[1]]$family, entry$year) | ||
}) | ||
} | ||
``` | ||
|
||
|
||
|
||
Note that the function can take a list of objects (though I'll have to clean up their formatting), and it can take either dois or the biblio list from above. It would be easy to write the textual citation formatting and number formatting too. I may add these to a simple R package for that purpose. | ||
|
||
Now we can gereate inline citations like this `(Halpern _et. al._ 2006)`. Hmm, looks like we need a way for `rinline` code to use `asis` formatting though. | ||
|
||
## Bibliography | ||
Then at the end of the document, use this command to print the bibliography generated by the use of our inline citations. | ||
|
||
|
||
```r | ||
getOption("works_cited") | ||
``` | ||
|
||
Halpern B, Regan H, Possingham H and McCarthy M (2006). "Accounting for | ||
uncertainty in marine reserve design." _Ecology Letters_, *9*. ISSN | ||
1461-023X, <URL: http://dx.doi.org/10.1111/j.1461-0248.2005.00827.x>. | ||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<!--roptions tidy=FALSE, warning=FALSE, comment=NA, message=FALSE--> | ||
<!--begin.rcode echo=FALSE | ||
#require(socialR) | ||
#render_wordpress() | ||
render_gfm() | ||
end.rcode--> | ||
|
||
This gives a quick example about how we can add citations into a markdown file with the same ease that we can in latex. | ||
I illustrate two different ways to get citation information. In the first example we look up the metadata automatically given | ||
the digital object identifier (doi) for the paper. | ||
|
||
In the second example we will read in a bibliography in bibtex format using the bibtex package, which can then be expressed in markdown immediately. | ||
|
||
Next I create an inline citation function that can take either a list of doi's or bibentry topics and format them into inline citations, while also storing this information to generate the full bibliography later. | ||
|
||
### DOI Approach | ||
|
||
The crossref function is available in the rplos package 0.0-6, (use the github copy, not in the CRAN version yet) | ||
<!--begin.rcode | ||
require(rplos) | ||
end.rcode--> | ||
|
||
<!--begin.rcode results="asis", | ||
crossref("10.1111/j.1461-0248.2005.00827.x") | ||
end.rcode--> | ||
|
||
|
||
### Bibtex approach | ||
|
||
For convenience let's write the entry we pulled with crossref to a bibfile, just to get a simple bibfile as our starting point. | ||
<!--begin.rcode | ||
library(bibtex) | ||
entry <- crossref("10.1111/j.1461-0248.2005.00827.x") | ||
write.bib(entry, file="example.bib") | ||
end.rcode--> | ||
|
||
We simply read in any bibtex file and print the citation: | ||
<!--begin.rcode r bibex, results="asis", echo=FALSE | ||
biblio <- read.bib("example.bib") | ||
biblio[[1]] | ||
end.rcode--> | ||
Now this would be much more awesome if we could generate keys on write.bib and use those bibtex keys, instead of the index value, `[[1]]`, to generate the citation. | ||
|
||
## Inline citations | ||
The other thing we need is a way to print inline citations that would just use a given shortened format (e.g. author-year, or a number) and add the citation to a `works_cited` object, which we could then use to generate the full citation information at the end. | ||
|
||
<!--begin.rcode | ||
pcite <- function(x){ | ||
if(is.null(getOption("works_cited"))){ | ||
empty <- list() | ||
class(empty) <- "bibentry" | ||
options(works_cited = empty) | ||
} | ||
sapply(x,function(x){ | ||
if(is(x, "character")) | ||
entry <- crossref(x) | ||
else # assume it's a bibentry object already | ||
entry <- x | ||
## keep track of what we've cited so far | ||
options(works_cited = c(getOption("works_cited"), entry)) | ||
## And format the inline citation | ||
n <- length(entry$author) | ||
if(n==1) | ||
sprintf("(%s, %s)", entry$author[[1]]$family, entry$year) | ||
if(n==2) | ||
sprintf("(%s & %s, %s)", entry$author[[1]]$family, entry$author[[2]]$family, entry$year) | ||
else if(n>2) | ||
sprintf("(%s _et. al._ %s)", entry$author[[1]]$family, entry$year) | ||
}) | ||
} | ||
end.rcode--> | ||
Note that the function can take a list of objects (though I'll have to clean up their formatting), and it can take either dois or the biblio list from above. It would be easy to write the textual citation formatting and number formatting too. I may add these to a simple R package for that purpose. | ||
|
||
Now we can gereate inline citations like this <!--rinline pcite("10.1111/j.1461-0248.2005.00827.x")-->. Hmm, looks like we need a way for `rinline` code to use `asis` formatting though. | ||
|
||
## Bibliography | ||
Then at the end of the document, use this command to print the bibliography generated by the use of our inline citations. | ||
<!--begin.rcode results="asis" | ||
getOption("works_cited") | ||
end.rcode--> | ||
|
||
|
||
Like many others on this list, I finding myself more and more drawn to markdown rather then tex/Rnw as my standard format (not least of which is the ease of displaying the files on github, particularly now that we have automatic image uploading). One thing I miss from latex is the citation commands. | ||
|
||
I've taken a little whack at generating parenthetical citations | ||
|
||
|
Oops, something went wrong.