Werden wir Helden für einen Tag

Home | About | Archive

Advent of emacs #15: How I do citation in emacs

Posted on Dec 15, 2022 by Chung-hong Chan

Now I have my \(\mathrm{B{\scriptstyle{IB}} \! T\!_{\displaystyle E} \! X}\) file (day 13) and a markdown writing mode (day 14). Now I need to be scholar and cite all the papers by the big shots in my field. How do I actually cite references in a paper?

I think I need to say a bit the mechanism. But this is a minimal example.

title: "My great paper"
csl: "/home/chainsawriot/dev/dotfiles/apa.csl"
bibliography: "/home/chainsawriot/dev/dotfiles/bib.bib"
### Moral Foundation Dictionary

The Moral Foundation Dictionary [MFD, @graham:2009:l] is a dictionary based on the moral foundation theory proposed by the same group of authors [e.g. @haidt2012righteous]. 
Under that theory, there are five fundamental moral values: care/harm, fairness/cheating, ingroup loyalty/betrayal, authority/subversion, and purity/degradation. Similarly, 
the MFD classified words into these five axes with positive (virtue) and negative (vice) categories. Therefore, 10 categories of words are available. The original development 
of the dictionary was based on an expert evaluation of the words [@graham:2009:l]. As a validation, @graham:2009:l demonstrated the difference in word usage in religious texts between 
liberals and conservatives. The dictionary was subsequently used to analyse news text [@clifford:2013:hwd; @fulgoni2016empirical] to quantify the moral rhetoric of news text. Some 
studies billed the moral rhetoric of text as *moral sentiment* [e.g. @Dainas2015TheMF; @garten2016morality]. It is worth mentioning that the original developers adjusted the frequency
of sentiment words by the total number of words in a piece of text [@graham:2009:l], but this is not always practised [e.g. @Dainas2015TheMF]. In this study, we use the unadjusted
version of the MFD score. In total, 10 scores were calculated using this dictionary: MF Harm+ (Care), MF Harm -, MF Fairness +, MF Fairness - (cheating), MF Ingroup + (loyalty),
MF Ingroup - (betrayal), MF Authority +, MF Authority - (subversion), MF Purity +, and MF Purity - (degradation).

# References

And this is how the rendered PDF looks like. If I want it to 100% compatible with APA, I use papaja.

The general idea is to write the cite key with the ampersand. If I want it to be inside a pair parentheses, the cite keys are in a pair of square brackets. If I don’t want it to be inside a pair of paraentheses, don’t put the cite keys inside square brackets. There are other ways, but let’s assume there are only two variations. I could actually do it manually. Open the \(\mathrm{B{\scriptstyle{IB}} \! T\!_{\displaystyle E} \! X}\) file, search for papers, copy and paste some cite keys… But it can get tedious pretty quickly.

Completion engine, again

Again, I use a completion engine to make the process less tedious. As I use Helm, the natural choice is helm-bibtex by Titus von der Malsburg et al. The idea is to let Helm index my \(\mathrm{B{\scriptstyle{IB}} \! T\!_{\displaystyle E} \! X}\) file so that I can search for papers really quickly. For example, I can quickly search for all papers by Rauchfleisch.

Quick access solves one tiny part of the question. But how to actually cite papers in my markdown documents?

Search and cite

Suppose I want to cite all the papers by Hase in my library at exactly this spot.

The idea is to search for the papers I want to cite; if I only need to cite one, then just select one and hit Return. If I need to cite more than one, I press C-SPC (SPC = Space) and select multiple. You might find that it has the square brackets. But I don’t need that, I can just delete the brackets myself.


Actually, the out-of-the-box experience wasn’t as smooth as this. I actually have the following modifications.

Make the default be “Insert Citation”

For whatever reasons, hitting Return in helm-bibtex isn’t the cite action by default. The cite action needs to be selected by hitting Tab (fire up the helm contextual menu) and select the “Insert Citation” action from the menu. Of course, you can also press the F3 (by default). It is super inefficient. The modification is to make “Insert Citation” the default (1). Also, even for “Insert Citation”, there are actually three different ways: Org-mode, \(\LaTeX\), and markdown. I made markdown the default (2). Don’t ask me the format! (3)

(use-package helm-bibtex
    (autoload 'helm-bibtex "helm-bibtex" "" t)
    (setq bibtex-completion-bibliography '("~/dev/dotfiles/bib.bib"))
    (setq bibtex-completion-cite-prompt-for-optional-arguments nil) ;;3
    (setq bibtex-completion-format-citation-functions
	  '((org-mode      . bibtex-completion-format-citation-org-link-to-PDF)
	    (latex-mode    . bibtex-completion-format-citation-cite)
	    (markdown-mode . bibtex-completion-format-citation-pandoc-citeproc)
	    (default       . bibtex-completion-format-citation-pandoc-citeproc))) ;;2

    ;; make bibtex-completion-insert-citation the default action

    (helm-delete-action-from-source "Insert citation" helm-source-bibtex) ;;1
    (helm-add-action-to-source "Insert citation" 'helm-bibtex-insert-citation helm-source-bibtex 0) ;;1
    (global-set-key (kbd "C-c x") 'helm-bibtex)

Highlight markdown citations

By default, markdown-mode doesn’t recognize markdown citations. I think it’s clearer to highlight all the citations and make them distinct from the main text.

(defvar markdown-mode-keywords nil)
(setq markdown-mode-keywords '(("@[^] ;\\.]+" . font-lock-keyword-face)))
(font-lock-add-keywords 'markdown-mode  markdown-mode-keywords)

Moving on

With this setup, I can write any paper and bring a lot of bread and butter on the table. I enjoy writing papers with this setup and I enjoy writing markdown in general. But there are some interesting collaborators who resist markdown and prefer \(\LaTeX\) instead. Tomorrow, I will talk about the specific way these collaborators like to write \(\LaTeX\) documents and how I work with them in emacs.

Powered by Jekyll and profdr theme, a fork of true minimal theme