52

How can I use Citation Style Language(CSL) in LaTeX Bibliography? It seems fantastic, there is 2,803 Citation Style right now in Zotero Style Repository.

The Citation Style Language (CSL) is an XML format for describing the formatting of in-text citations, notes and bibliographies. CSL offers:

  • An open format that may be used by any application
  • The ability to write compact and robust styles
  • Extensive support for style requirements
  • Automatic style localization
  • Easy distribution and updating of styles
  • A fast growing library with thousands of freely available styles
Real Dreams
  • 8,298
  • 12
  • 56
  • 78
  • 1
    never heard of such a thing, but some xsl (to generate a native language such as bibtex) wouldn't (surely) be difficult. never heard of anyone doing such a thing, though. – wasteofspace Aug 29 '12 at 10:22
  • maybe this interview is of interest for you: http://tex.blogoverflow.com/2012/08/textalk-an-interview-with-plk/ – matth May 09 '13 at 09:35
  • Related question: http://tex.stackexchange.com/questions/261023/is-it-possible-to-convert-a-citation-style-language-csl-style-file-to-a-bibtex – koppor Aug 30 '15 at 04:28

5 Answers5

31

There is in fact a tool to use CSL when compiling LaTeX documents. Pandoc accepts a --csl=<csl file> argument, and it will use the supplied CSL style to format your bibliography.

For example:

pandoc --bibliography=refs.bib --csl=mystyle.csl -o out.pdf doc.tex

Will happily generate out.pdf file via LaTeX using the bibtex file refs.bib and CSL file mystyle.csl, from source doc.tex, which presumably has a \thebibliography or some citations in there somewhere.

alyu
  • 426
  • 1
    This is a great idea for many use cases. However, Pandoc uses only a subset of LaTeX and when converting from raw TeX, the internal document model doesn't even support some basic LaTeX environments like \includegraphics. – Arthur Apr 04 '15 at 11:36
  • 8
    @Arthur sorry, but that's incorrect. Pandoc passes the LaTeX through either XeTeX or PDFTeX, and supports whatever they do as long as your output is in a format that doesn't require additional transformation by Pandoc.

    I've used \includegraphics before via Pandoc, since the Pandoc markdown format doesn't support image transforms.

    – alyu Apr 05 '15 at 00:36
  • 2
    I should have been more specific. See this issue on the Pandoc GitHub; specifying width/height is not supported which, to my mind, means \includegraphics is not fully supported. – Arthur Apr 06 '15 at 18:13
  • 4
    @Arthur, I believe that issue only applies to converting from latex to non-latex output formats. So if .pdf or .tex is your output format, then those specifications are respected. – askewchan Sep 27 '15 at 01:47
  • @Arthur You can simply use latex includegraphics in your source and pandoc will include that in the tex intermediate step, then you can choose to compile pdf or whatever, no issue. – Zelphir Kaltstahl Nov 02 '16 at 09:57
21

In one word: no.

Bruce D'Arcus, the original creator of CSL, has repeatedly said that he would like to see an implementation of CSL for LaTeX (to be more precise: he often talked about LuaLaTeX) and that such a thing wouldn't be too difficult to achieve in theory (see this and the following posts for example), but so far, no one has been interested in doing it (the post I linked to dates back to 2008!).

In my opinion, CSL for LaTeX would be extremely useful. CSL is getting more and more traction (there are about half a dozen of implementations ATM), and although it's not quite as powerful as biblatex (but what is?), it's very versatile, and, most important, truly system-agnostic.

It would be the first solution to offer bibliography styles which work equally for a LaTeX and a variety of word processors and is really able to deal with complex styles.

Simifilm
  • 3,168
  • 23
  • 29
  • 6
    What you mean from: "although it's not quite as powerful as biblatex ...", I think they are not comparable, indeed BibLaTeX may use CSL files to style its output. – Real Dreams Oct 10 '12 at 16:28
  • I mean, among other things, that biblatex has a more exhaustive data model, allows (in combination with biber) for more complex sorting and offers more fine-grained typographic control. And no, CSL files are completely useless for biblatex. – Simifilm Oct 10 '12 at 20:26
  • "...CSL files are completely useless for biblatex. " ???????? – Real Dreams May 09 '13 at 09:24
  • 1
    Yes, they are. CSL files need a CSL processor which biblatex is not, which, as I wrote in my answer, doesn't exist (yet) for LaTeX. – Simifilm May 09 '13 at 15:50
  • @Simifilm biber would be such a processor. Given its active development, it'd be a prime candidate for use. – Sean Allred Nov 21 '13 at 02:47
  • 2
    Turning biber into a CSL processor would mean rewriting it from scratch. biker was designed for handling biblatex's styles, there's nothing in the program which helps you with CSL at the moment. Adding a CSL processor to it, doesn't really make a lot of sense anyway, you could just write a new program. – Simifilm Nov 21 '13 at 07:00
15

Edit:

citeproc-lua now contains it's own LaTeX package, it added support for BibTeX files and it uses the normal LaTeX citation commands. It is available on CTAN, so you can install it using tlmgr.

See also the instructions on how to run the example:

\documentclass{article}

\usepackage{citation-style-language} \cslsetup{style = apa} \addbibresource{example.bib}

\begin{document}

Foo \cite{ITEM-1} bar \cite{ITEM-1, ITEM-2} baz.

\printbibliography

\end{document}

Result:

enter image description here


Original answer:

There is now a full Lua version of Citeproc. It doesn't contain TeX interface yet, but simple package for LuaLaTeX that uses it can look like this citeproc.sty:

\ProvidesPackage{citeproc}
\RequirePackage{luacode}

% Basic initialization of Citeproc.lua

\begin{luacode*} require("lualibs")

-- global object for functions defined in this CSL = {}

local dom = require("luaxml-domobject") local formats = require("citeproc.citeproc-formats")

local CiteProc = require("citeproc")

local function read_file(path) local file = io.open(path, "r") if not file then return nil end local content = file:read("*a") file:close() return content end

function CSL.load_style(filename) CSL.style = read_file(filename) end

function CSL.load_json(filename) CSL.bib = CSL.bib or {} local items = utilities.json.tolua(read_file(filename)) if not items then return nil, "JSON file cannot be loaded: " .. filename end for _, item in ipairs(items) do CSL.bib[item["id"]] = item end end

function CSL.load_lang(lang) CSL.lang= lang end

function CSL.load_style(style) CSL.style = read_file(style) end

function make_citeproc_sys(bib) local bib = bib local citeproc_sys = { retrieveLocale = function (self, lang) local locale_name_format = CSL.locale_name_format or "locales-%s.xml" local filename = string.format(locale_name_format, lang) local content = read_file(filename) if content then return dom.parse(content) else return nil end end, retrieveItem = function (self, id) return bib[id] end } return citeproc_sys end

function CSL.init() CSL.bib = CSL.bib or {} local citeproc_sys = make_citeproc_sys(CSL.bib) CSL.citeproc = CiteProc:new(citeproc_sys, CSL.style) CSL.citeproc.formatter = formats.latex end

function CSL.cite(citation) local cite_items = {} for item in string.gmatch(citation, "([^,]+)") do cite_items[#cite_items+1] = {id = item} end local result = CSL.citeproc:makeCitationCluster(cite_items) tex.print(result) end

function CSL.bibliography() local params, result = CSL.citeproc:makeBibliography() tex.print("\begin{thebibliography}{}") for _,bibitem in pairs(result) do bibitem = bibitem:gsub("bibitem%[.-%]","bibitem") tex.print(bibitem) end tex.print("\end{thebibliography}") end

\end{luacode*}

\newcommand\cslstyle[1]{% \luaexec{CSL.load_style("#1")} }

\newcommand\csljson[1]{% \luaexec{CSL.load_json("#1")} }

\newcommand\cslinit{% \luaexec{CSL.init()} }

\newcommand\cslcite[1]{% \luaexec{CSL.cite("\luaescapestring{#1}")} }

\newcommand\cslbibliography{\luaexec{CSL.bibliography()}}

% initialize citeproc \AtBeginDocument{% \cslinit% }

\endinput

It doesn't support BibTeX files, it needs JSON at the moment. To run the following example, download bib.json, simple.csl, and locales-en-US.xml from the Citeproc-lua example directory.

The sample.tex file:

\documentclass{article}
\usepackage{citeproc}
\cslstyle{simple.csl}
\csljson{bib.json}
\begin{document}
hello world \cslcite{ITEM-1,ITEM-2}

\cslbibliography \end{document}

You can compile it using LuaLaTeX. This is the result:

enter image description here

michal.h21
  • 50,697
  • Wow! This is soo amazing – DG' Oct 13 '21 at 13:23
  • 1
    @DG' the Citeproc-lua now contains its own LaTeX package, it is much better than my version. It also supports BibTeX files now. The development is really fast, I think it will be usable soon. – michal.h21 Oct 20 '21 at 09:01
  • @michal.h21 That's pretty cool! – gusbrs Oct 20 '21 at 09:39
  • @michal.h21 - I hope so. This is really great news. – DG' Oct 20 '21 at 09:47
  • @michal.h21 Is the LaTeX package supposed to work with any style other than plain.csl which is provided? I tried several other styles which validate correctly, but they only produced error messages and gibberish. – Simifilm Dec 15 '21 at 09:22
  • @Simifilm I think it should support most styles, but it is still in active development and doesn't support 100 % of CSL features. I think it would be best to post a new issue to the Github Repo. – michal.h21 Dec 15 '21 at 09:52
  • Could anyone fill me in on the process of this one here? I have an Uni-intern CSL stlye and want to make it run in LaTex on BibTex... – poebelchen May 03 '22 at 17:58
  • @poebelchen - citation-style-language replaces BibTEX. How to run it depends on your latex engine, which is described in the package docs. If you have any issues, then please ask a new questions – DG' May 03 '22 at 20:23
11

I've created a Guile Scheme program that can parse a .aux file and produce a .bbl via the Juris-M or Zotero reference manager along with the Better BibTeX for Zotero plugin, modified to provide a bbl format output from its "schomd" interface.

See: https://github.com/KarlHegbloom/zotero-texmacs-integration

It is incomplete. The script in the top directory will run and produce a .bbl as long as you have Juris-M or Zotero and the version of Better BibTeX for Zotero installed that supports it. I've used it to produce a bibliography in Bluebook format (US legal writing). Eventually I will have support in TeXmacs similar to what's in the OpenOffice pluginfor Zotero, so it will handle formatting the in-document citations as well as the bibliography.

  • This looks very interesting. Will it handle citations too? (As you know surely , the rules for creating and referencing shorthands for subsequent in legal articles can be cumbersome to do correctly and automatically.) – jon Mar 28 '16 at 02:29
  • Not quite yet. I'm too busy preparing documents right now to stop and finish. After I'm done with this lawsuit and I have a little more time for it I plan to finish the support. I've decided that what I will do is clone the Open Office plug-in that's written in JavaScript inside Juris-M and Zotero, modify it to talk to code that I will write inside of texmacs. – Karl M Hegbloom Mar 28 '16 at 15:59
1

If you write your LaTeX document through RMarkdown, you have the ability to set a csl parameter in the header of your file (ie the YAML header). It's conveniently straight-forward, here's an example YAML header for an RMarkdown:

---
output: 
  pdf_document
title: 'Title of document'
author:
  name: "My Name"
bibliography: references.bib
csl: biblio_style.csl

Text [@reference].

References

arara
  • 776
  • Note that RMarkdown uses Pandoc to compile, so it is basically the same as the accepted answer above but then with the command line arguments given as a YAML header instead. – Marijn Apr 06 '23 at 13:41
  • @Marijn The pandoc command in the accepted answer messed up my PDF formatting. RMarkdown took care of all my issues so there's definitely utility in using it – arara Apr 06 '23 at 14:53