46

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
3

4 Answers 4

30

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.

5
  • 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 4, 2015 at 11:36
  • 7
    @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 5, 2015 at 0: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 6, 2015 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, 2015 at 1: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. Nov 2, 2016 at 9:57
20

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.

6
  • 5
    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. Oct 10, 2012 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, 2012 at 20:26
  • "...CSL files are completely useless for biblatex. " ???????? May 9, 2013 at 9: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 9, 2013 at 15:50
  • 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, 2013 at 7:00
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.

2
  • 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, 2016 at 2: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. Mar 28, 2016 at 15:59
11
+100

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

8
  • Wow! This is soo amazing
    – DG'
    Oct 13, 2021 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, 2021 at 9:01
  • @michal.h21 That's pretty cool!
    – gusbrs
    Oct 20, 2021 at 9:39
  • @michal.h21 - I hope so. This is really great news.
    – DG'
    Oct 20, 2021 at 9: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, 2021 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.