From mboxrd@z Thu Jan 1 00:00:00 1970 From: Matt Price Subject: org-ref: conditionally add url to formatted citation Date: Fri, 30 Aug 2019 15:12:54 -0400 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="0000000000006d625705915a69f0" Return-path: Received: from eggs.gnu.org ([2001:470:142:3::10]:42647) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i3mKc-0001zY-AN for emacs-orgmode@gnu.org; Fri, 30 Aug 2019 15:13:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i3mKY-0003eS-Uo for emacs-orgmode@gnu.org; Fri, 30 Aug 2019 15:13:09 -0400 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]:40740) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i3mKY-0003dp-Lw for emacs-orgmode@gnu.org; Fri, 30 Aug 2019 15:13:06 -0400 Received: by mail-pl1-x629.google.com with SMTP id h3so3764834pls.7 for ; Fri, 30 Aug 2019 12:13:06 -0700 (PDT) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: John Kitchin Cc: Org Mode --0000000000006d625705915a69f0 Content-Type: text/plain; charset="UTF-8" On Sat, Jan 7, 2017 at 8:23 AM John Kitchin wrote: > > > >> > >> 2. If you click on a citation link, you can select an action to copy the > >> formatted entry for that key. > > > > > > I had trouble with this. I constructed a minimal emacs config: > > > > #+BEGIN_SRC emacs-lisp > > ;;; elpa interface > > (setq package-archives ()) > > (add-to-list 'package-archives '("marmalade" . " > > http://marmalade-repo.org/packages/")) > > (add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/ > ")) > > (add-to-list 'package-archives '("org" . "http://orgmode.org/elpa/") t) > > (add-to-list 'package-archives '("melpa" . " > > http://melpa.milkbox.net/packages/") t) > > ;; basic initialization, (require) non-ELPA packages, etc. > > (package-initialize) > > > > (require 'cl) > > (require 'org) > > (require 'helm-config) > > (require 'helm) > > (require 'helm-bibtex) > > > > ;; see org-ref for use of these variables > > (setq org-ref-bibliography-notes "~/Bibliography/notes.org" > > org-ref-default-bibliography '("~/Bibliography/Bibliography.bib") > > org-ref-pdf-directory "~/Bibliography/files/") > > > > (setq bibtex-completion-bibliography "~/Bibliography/Bibliography.bib" > > bibtex-completion-library-path "~/Bibliography/files" > > ) > > > > ;; org-define-error > > ;; for whatever reason, org wasn't finding this > > (defun org-define-error (name message) > > "Define NAME as a new error signal. > > MESSAGE is a string that will be output to the echo area if such > > an error is signaled without being caught by a `condition-case'. > > Implements `define-error' for older emacsen." > > (if (fboundp 'define-error) (define-error name message) > > (put name 'error-conditions > > (copy-sequence (cons name (get 'error 'error-conditions)))))) > > > > (setq org-ref-formatted-citation-backend "org") > > (setq org-ref-completion-library 'org-ref-helm-bibtex) > > > > (require 'org-ref) > > > > #+END_SRC > > It is pretty weird about org-define-error. It is in org-compat.el. I am > using org-9. > > > The action proceeds without errors, but nothing seems to be copied ot the > > clipboard. > > > >> > >> 3. On a bibtex entry, you can use the org-ref-bibtex-hydra/body to copy > >> a formatted string of the entry. > >> > > this works great. Obviously I need to learn how to use hydra... > > I go back and forth on liking ivy/hydra vs helm. > > >> You would want to have these settings with a new org-ref: > >> (setq org-ref-formatted-citation-backend "org") > >> > >> (setf (cdr (assoc "book" (cdr (assoc "org" > >> org-ref-formatted-citation-formats)))) > >> "${author} ${year}. /${title}/, ${address}:${publisher}. [[${url}]]") > >> > >> Then you can insert the citation and get this (there is no address for > >> the publisher in my entry). With helm bibtex as the backend, you would > >> type C-c ], select an entry, and press f8 (or tab and select the insert > >> formatted action). In the ivy backend, you type M-o f. Both of them > >> support multiple selections. > > > > Yeah, this is awesome, thank you. it's F7 though, not F8. > > could be, the key is related to the number/order of actions in helm-bibtex. > >> > >> > >> > >> That entry type isn't defined in the formats (there should be a default > >> entry, but it isn't formatted the way you want), but you could add it > > like this: > >> > >> > >> (push '("misc" . "${author} ${year}. /${title}/, > > [[${howpublished}]]")(cdr (assoc "org" > >> org-ref-formatted-citation-formats))) > >> > > > > got it now. This is great, thanks. Moving forward now that you've > solved > > my last problem... for properly formatted citations, is latex the only > > fully supported publication type? What about, say, pandoc md or odt? > > > > Thank you thank you thank you! > > org-ref has some support to export citations in latex, html, org, > md/pandoc and text. I do not know how good it is, but the framework for > it mostly exists. > > > I rediscovered this thread recently and found it very helpful. Hope the necroposting isn't annoying to others. Using a current org-ref, I was able to use these hints to make my life quite a bit easier when writing syllabi. I'm just inserting formatted citations for course texts; this is a bit different from most uses of org-ref. I want the text to be as readable as possible, and to include a link to the resource if it exists online, or to ignore the url field if it doesn't exist. I did this by lightly modifying ~org-ref-formatted-citation-formats~ and then modifying the function org-ref uses to insert formatted citations: ----------- (defun org-ref-format-bibtex-entry (entry) "Return a formatted citation for the bibtex entry at point. Formats are from `org-ref-formatted-citation-formats'. The variable `org-ref-formatted-citation-backend' determines the set of format strings used." (save-excursion (bibtex-beginning-of-entry) (let* ((formats (cdr (assoc org-ref-formatted-citation-backend org-ref-formatted-citation-formats))) (format-string) (ref)) (if (null entry) "!!! No entry found !!!" (setq format-string (cdr (or (assoc (downcase (bibtex-completion-get-value "=type=" entry)) formats) (assoc nil formats)))) (if (and (string-equal org-ref-formatted-citation-backend "org" ) (bibtex-completion-get-value "url" entry)) (setq format-string (replace-regexp-in-string "${title}" "[[${url}][${title}]]" format-string))) (setq ref (s-format format-string 'bibtex-completion-apa-get-value entry)) (replace-regexp-in-string "\\([.?!]\\)\\." "\\1" ref))))) --------------- The relevant section is the "if" near hte end of the function: (if (and (string-equal org-ref-formatted-citation-backend "org" ) (bibtex-completion-get-value "url" entry)) (setq format-string (replace-regexp-in-string "${title}" "[[${url}][${title}]]" format-string))) I thought it might possibly be useful for others -- like, it might be nice to use when composing emails etc -- so I figured I'd post it here. And thanks again esp to John for this tool! --0000000000006d625705915a69f0 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Sat, Jan 7, 2017 at 8:23 AM John K= itchin <jkitchin@andrew.cmu.e= du> wrote:
http://marmalade-repo.org/packages/"))
> (add-to-list 'package-archives '("gnu" . "h= ttp://elpa.gnu.org/packages/"))
> (add-to-list 'package-archives '("org" . "http:/= /orgmode.org/elpa/") t)
> (add-to-list 'package-archives '("melpa" . " > http://melpa.milkbox.net/packages/") t)
> ;; basic initialization, (require) non-ELPA packages, etc.
> (package-initialize)
>
> (require 'cl)
> (require 'org)
> (require 'helm-config)
> (require 'helm)
> (require 'helm-bibtex)
>
> ;; see org-ref for use of these variables
> (setq org-ref-bibliography-notes "~/Bibliography/notes.org"
>=C2=A0 =C2=A0 =C2=A0 =C2=A0org-ref-default-bibliography '("~/B= ibliography/Bibliography.bib")
>=C2=A0 =C2=A0 =C2=A0 =C2=A0org-ref-pdf-directory "~/Bibliography/f= iles/")
>
> (setq bibtex-completion-bibliography "~/Bibliography/Bibliography= .bib"
>=C2=A0 =C2=A0 =C2=A0 =C2=A0bibtex-completion-library-path "~/Bibli= ography/files"
>=C2=A0 =C2=A0 =C2=A0 =C2=A0)
>
> ;; org-define-error
> ;; for whatever reason, org wasn't finding this
> (defun org-define-error (name message)
>=C2=A0 =C2=A0"Define NAME as a new error signal.
> MESSAGE is a string that will be output to the echo area if such
> an error is signaled without being caught by a `condition-case'. > Implements `define-error' for older emacsen."
>=C2=A0 =C2=A0(if (fboundp 'define-error) (define-error name message= )
>=C2=A0 =C2=A0 =C2=A0(put name 'error-conditions
>=C2=A0 =C2=A0 =C2=A0 (copy-sequence (cons name (get 'error 'err= or-conditions))))))
>
> (setq org-ref-formatted-citation-backend "org")
> (setq org-ref-completion-library 'org-ref-helm-bibtex)
>
> (require 'org-ref)
>
> #+END_SRC

It is pretty weird about org-define-error. It is in org-compat.el. I am
using org-9.

> The action proceeds without errors, but nothing seems to be copied ot = the
> clipboard.
>
>>
>> 3. On a bibtex entry, you can use the org-ref-bibtex-hydra/body to= copy
>> a formatted string of the entry.
>>
> this works great.=C2=A0 Obviously I need to learn how to use hydra...<= br>
I go back and forth on liking ivy/hydra vs helm.

>> You would want to have these settings with a new org-ref:
>> (setq org-ref-formatted-citation-backend "org")
>>
>> (setf (cdr (assoc "book" (cdr (assoc "org"
>> org-ref-formatted-citation-formats))))
>>=C2=A0 "${author} ${year}. /${title}/, ${address}:${publisher}= . [[${url}]]")
>>
>> Then you can insert the citation and get this (there is no address= for
>> the publisher in my entry). With helm bibtex as the backend, you w= ould
>> type C-c ], select an entry, and press f8 (or tab and select the i= nsert
>> formatted action). In the ivy backend, you type M-o f. Both of the= m
>> support multiple selections.
>
> Yeah, this is awesome, thank you.=C2=A0 it's F7 though, not F8.
could be, the key is related to the number/order of actions in helm-bibtex.=
>>
>>
>>
>> That entry type isn't defined in the formats (there should be = a default
>> entry, but it isn't formatted the way you want), but you could= add it
> like this:
>>
>>
>> (push '("misc" . "${author} ${year}. /${title}/= ,
> [[${howpublished}]]")(cdr (assoc "org"
>> org-ref-formatted-citation-formats)))
>>
>
> got it now.=C2=A0 This is great, thanks.=C2=A0 Moving forward now that= you've solved
> my last problem... for properly formatted citations, is latex the only=
> fully supported publication type? What about, say, pandoc md or odt? >
> Thank you thank you thank you!

org-ref has some support to export citations in latex, html, org,
md/pandoc and text. I do not know how good it is, but the framework for
it mostly exists.



I rediscovered this thread recently an= d found it very helpful.=C2=A0 Hope the necroposting isn't annoying to = others.

Using a current org-ref, I was able to=C2= =A0 use these hints to make my life quite a bit easier when writing syllabi= .=C2=A0 I'm just inserting formatted citations for course texts; this i= s a bit different from most uses of org-ref. I want the text to be as reada= ble as possible, and to include a link to the resource if it exists online,= or to ignore the url field if it doesn't exist.=C2=A0 I did this by li= ghtly modifying ~org-ref-formatted-citation-formats~ and then modifying the= function org-ref uses to insert formatted citations:

-----------
(defun org-ref-format-bibtex-entry (entry)=C2=A0 "Return a formatted citation for the bibtex entry at point.Formats are from `org-ref-formatted-citation-formats'. The
variabl= e `org-ref-formatted-citation-backend' determines the set
of format = strings used."
=C2=A0 (save-excursion
=C2=A0 =C2=A0 (bibtex-begi= nning-of-entry)
=C2=A0 =C2=A0 (let* ((formats (cdr (assoc org-ref-format= ted-citation-backend =C2=A0org-ref-formatted-citation-formats)))
=C2= =A0 (format-string)
=C2=A0 (ref))
=C2=A0 =C2=A0 =C2=A0 (if (null en= try)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "!!! No entry found !!!&quo= t;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq format-string (cdr (or (assoc (down= case (bibtex-completion-get-value "=3Dtype=3D" entry)) formats)=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(assoc nil forma= ts))))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (if (and (string-equal org-ref-format= ted-citation-backend "org" )
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(bibtex-completion-get-value "url"= entry))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq format-string=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (replace-r= egexp-in-string "${title}" "[[${url}][${title}]]" forma= t-string)))

=C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq ref (s-format format-s= tring 'bibtex-completion-apa-get-value entry))
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 (replace-regexp-in-string "\\([.?!]\\)\\." "\\1"= ref)))))
---------------

The releva= nt section is the "if" near hte end of the function:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (if (and (string-equal org-ref-for= matted-citation-backend "org" )
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(bibtex-completion-get-value "url&qu= ot; entry))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq format-strin= g
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (replac= e-regexp-in-string "${title}" "[[${url}][${title}]]" fo= rmat-string)))

I thought it might possibly be usef= ul for others -- like, it might be nice to use when composing emails etc --= so I figured I'd post it here.=C2=A0 And thanks again esp to John for = this tool!

--0000000000006d625705915a69f0--