%
% Copyright (c) 2021-2024 Zeping Lee
% Released under the MIT License.
% Repository: https://github.com/zepinglee/citeproc-lua
%

\documentclass{l3doc}

\usepackage{mathpazo}
\usepackage{helvet}
\usepackage{listings}

\NewDocumentCommand\opt{m}{\texttt{#1}}
\NewDocumentCommand\field{m}{\texttt{#1}}
\NewDocumentCommand\entrytype{m}{\texttt{#1}}

\lstnewenvironment{LaTeXdemo}{
  \lstset{
    basicstyle = \ttfamily\small,
    basewidth  = 0.51em,
    frame      = single,
    gobble     = 2,
    language   = [LaTeX]TeX,
  }
}{}

\lstnewenvironment{bash}{
  \lstset{
    basicstyle = \ttfamily\small,
    basewidth  = 0.51em,
    % gobble     = 2,
    language   = bash,
  }
}{}

% \hypersetup{hidelinks}
% \urlstyle{same}

\begin{document}

\title{%
  Bibliography formatting with \pkg{citation-style-language}
}

\author{%
  Zeping Lee%
  \thanks{%
    E-mail:
    \href{mailto:zepinglee@gmail.com}
      {zepinglee@gmail.com}%
  }%
}

\date{2024-07-31 v0.6.0}

\maketitle

% \begin{abstract}
%   Foo
% \end{abstract}

\begin{documentation}

\section{Introduction}

The Citation Style Language\footnote{\url{https://citationstyles.org/}} (CSL)
is an XML-based language that defines the formats of citations and bibliography.
There are currently thousands of styles in CSL including the most widely used
APA, Chicago, Vancouver, etc.
The \pkg{citation-style-language} package is aimed to provide another reference formatting method
for LaTeX that utilizes the CSL styles.
It contains a citation processor implemented in pure Lua (\pkg{citeproc-lua})
which reads bibliographic metadata and performs sorting and formatting on both
citations and bibliography according to the selected CSL style.
A LaTeX package (\file{citation-style-language.sty}) is provided to communicate with the processor.

Note that this project is in early development stage and some features of CSL
are not implemented yet.
Comments, suggestions, and bug reports are welcome.


\section{Installation}

This package is available from TeX Live 2022 or later versions.
For most users, the easiest way is to install it via |tlmgr|.
If you want to install the GitHub develop version of this package,
you may follow the steps below.

The \pkg{citation-style-language} requires the following packages:
\pkg{filehook}, \pkg{l3kernel}, \pkg{l3packages}, \pkg{lua-uca}, \pkg{lualibs},
\pkg{luatex}, \pkg{luaxml}, and \pkg{url}.
\pkg{l3build} is also required for actually performing the installation.
Make sure they are already installed in the TeX distribution.

\begin{bash}
  git clone https://github.com/zepinglee/citeproc-lua  # Clone the repository
  cd citeproc-lua
  git submodule update --init --remote                 # Fetch submodules
  l3build install
\end{bash}

These commands install the package files to |TEXMFHOME| which is usually
|~/texmf| on Linux or |~/Library/texmf| on macOS.
Besides, the |citeproc-lua| executable needs to be copied to some directory in
the |PATH| environmental variable so that it can be called directly in the shell.
For example provided |~/bin| is in |PATH|:

\begin{bash}
  cp citeproc/citeproc-lua.lua "~/bin/citeproc-lua"
\end{bash}

To uninstall the package from |TEXMFHOME|, just run |l3build uninstall|.

\section{Getting started}

An example of using \pkg{citation-style-language} package is as follows.

\begin{LaTeXdemo}
  \documentclass{...}
  \usepackage[style = apa]{citation-style-language}
  \addbibresource{bibfile.json}
  \begin{document}
  \cite{...}
  ...
  \printbibliography
  \end{document}
\end{LaTeXdemo}

The procedure to compile the document is different across engines.

\paragraph{LuaTeX}

The CSL processor is written in Lua and it can be run directly in LuaTeX
without the need of running external programs.
For LuaTeX, the compiling procedure is simply running \file{latex} twice,
which is the same as documents with cross references.

\paragraph{Other engines}

For engines other than LuaTeX, the \file{citeproc-lua} executable is required
to run on the \file{.aux} file to generate the citations and bibliography.
The general procedure is similar to the traditional BibTeX workflow.
\begin{enumerate}
  \item Run \file{latex} on \file{example.tex}.
  \item Run \file{citeproc-lua} on \file{example.aux}.
    The engine reads the \file{.csl} style, CSL locale files, and
    \file{.bib} database and then writes the processed citations and
      bibliography to \file{example.bbl}.
  \item Run \file{latex} on \file{example.tex}.
    The \file{.bbl} file is loaded and all the citations and bibliography
    are printed.
\end{enumerate}



\section{Package setup}

\begin{function}{\cslsetup}
  \begin{syntax}
    \cs{cslsetup}\marg{options}
  \end{syntax}
\end{function}

Package options may be set when the package is loaded or at any later stage
with the \cs{cslsetup} command.
These two methods are equivalent.
\begin{LaTeXdemo}
  \usepackage[style = apa]{citation-style-langugage}
  % OR
  \usepackage{citation-style-langugage}
  \cslsetup{style = apa}
\end{LaTeXdemo}

\DescribeOption{style}
The \opt{style=}\meta{style-id} option selects the style file
\meta{style-id}\file{.csl} for both citations and bibliography.
The implemented CSL style files are available in the official GitHub
repository\footnote{\url{https://github.com/citation-style-language/styles}}
as well as the Zotero style
repository\footnote{\url{https://www.zotero.org/styles}}.
The user may search and download the \file{.csl} file to the working directory.
The following styles are distributed within the package and
each of them can be directly loaded without downloading.

\begin{description}
  \item[\opt{american-chemical-society}] American Chemical Society
  \item[\opt{american-medical-association}] American Medical Association 11th edition
  \item[\opt{american-political-science-association}] American Political Science Association
  \item[\opt{american-sociological-association}] American Sociological Association 6th edition
  \item[\opt{apa}] American Psychological Association 7th edition
  \item[\opt{chicago-author-date}] Chicago Manual of Style 17th edition (author-date)
  \item[\opt{chicago-fullnote-bibliography}] Chicago Manual of Style 17th edition (full note)
  \item[\opt{chicago-note-bibliography}] Chicago Manual of Style 17th edition (note)
  \item[\opt{elsevier-harvard}] Elsevier - Harvard (with titles)
  \item[\opt{harvard-cite-them-right}] Cite Them Right 11th edition - Harvard
  \item[\opt{ieee}] IEEE
  \item[\opt{modern-humanities-research-association}] Modern Humanities Research Association 3rd edition (note with bibliography)
  \item[\opt{modern-language-association}] Modern Language Association 9th edition
  \item[\opt{nature}] Nature
  \item[\opt{vancouver}] Vancouver
\end{description}

\DescribeOption{locale}
The \opt{locale} option receives an ISO 639-1 two-letter language code
(e.g.,  ``\opt{en}'', ``\opt{zh}''), optionally with a two-letter locale code
(e.g., ``\opt{de-DE}'', ``\opt{de-AT}'').
This option affects sorting of the entries and the output of dates, numbers,
and terms (e.g., ``et al.'').
It may also be set \opt{auto} (default) and the \opt{default-locale} attribute in
the CSL style file will be used.
The locale falls back to ``\opt{en}'' (English) if the attribute is not set.
When \pkg{babel} package is loaded, the selected main language is implicitly set
as the \opt{locale} for \pkg{citation-style-language}.

\DescribeOption{ref-section}
This option automatically starts a new reference section at the given document
heading such as a chapter or a section. This is equivalent to the
\cs{newrefsection} command. The following choices are available:

\begin{description}
  \item[\opt{none}] Disable this feature (default).
  \item[\opt{part}] Start a reference section at every \cs{part} command.
  \item[\opt{chapter}] Start a reference section at every \cs{chapter} command.
  \item[\opt{chapter+}] Start a reference section at every \cs{chapter} and
    every higher level of sectioning, i.e. \cs{part}.
  \item[\opt{section}] Start a reference section at every \cs{section} command.
  \item[\opt{section+}] Start a reference section at every \cs{section} and
    every higher level of sectioning, i.e. \cs{part} and \cs{chapter}.
  \item[\opt{subsection}] Start a reference section at every \cs{subsection} command.
  \item[\opt{subsection+}] Start a reference section at every \cs{subsection} and every higher level of sectioning, i.e. \cs{part}, \cs{chapter} and \cs{section}.
\end{description}

\DescribeOption{bib-font}
Usually, the list of references is printed in the same font style and size as
the main text.
The \opt{bib-font} option is used to set different formats in the
\env{thebibliography} environment.
It may override the \opt{line-spacing} attribute configured in the CSL style.
For example, to force double-spacing in the bibliography:
\begin{LaTeXdemo}
  \cslsetup{bib-font = \linespread{2}\selectfont}
\end{LaTeXdemo}

\DescribeOption{bib-item-sep}
The vertical space between entries in the bibliography is configured in the
CSL style.
It can be overridden by this \opt{bib-item-sep} option.
It is recommended to set \opt{bib-item-sep} to a stretchable glue rather than
a fixed length to help reducing page breaks in the middle of an entry.
\begin{LaTeXdemo}
  \cslsetup{bib-item-sep = 8 pt plus 4 pt minus 2 pt}
\end{LaTeXdemo}

\DescribeOption{bib-hang}
The \opt{bib-hang} option sets the hanging indentation length which is
usually used for author-date style references.
By default, it is 1 em (with respect to the \opt{bib-font} size if set).

\DescribeOption{bib-par-indent}
The paragraph indentation of references if they are in normal paragraph style
rather than labelled or hanging indented.
By default, it is the \cs{parindent} at when printed out.


\begin{function}{\addbibresource}
  \begin{syntax}
    \cs{addbibresource}\marg{resource}
  \end{syntax}
\end{function}

The \cs{addbibresource} command loads bibliographic data from \meta{resource}
file. CSL-JSON is the preferred format especially when the data is exported
from a reference manager like Zotero.  Other formats like CSL-YAML and
Bib(La)TeX (for compatibility) are also supported but they need to be converted
to CSL-JSON for internal processing. Note that the files must be specified with
their extension names and the content should be encoded in UTF-8.
\begin{LaTeXdemo}
  \addbibresource{file1.json}
  \addbibresource{file2.yaml, file3.bib}
\end{LaTeXdemo}


\section{Citation commands}

\begin{function}{\cite}
  \begin{syntax}
    \cs{cite}\oarg{options}\marg{keys}
  \end{syntax}
\end{function}

The citation command is similar to the one in standard LaTeX except that the
\meta{options} is in key-value style.
\DescribeOption{prefix}
\DescribeOption{suffix}
\DescribeOption{page}
\DescribeOption{figure}
The \meta{options} can be \opt{prefix}, \opt{suffix} or one of locators like
\opt{page} or \opt{figure}.
The full list of supported locators is detailed in Table~\ref{tab:locators}.
An example is as follows.
\begin{LaTeXdemo}
  \cite[prefix = {See }, page = 42]{ITEM-1}
\end{LaTeXdemo}

\begin{table}
  \centering
  \caption{The locators supported in CSL v1.0.2.}
  \label{tab:locators}
  \begin{tabular}{lll}
    \toprule
    \opt{act}       & \opt{folio}     & \opt{section}    \\
    \opt{appendix}  & \opt{issue}     & \opt{sub-verbo}  \\
    \opt{article}   & \opt{line}      & \opt{supplement} \\
    \opt{book}      & \opt{note}      & \opt{table}      \\
    \opt{canon}     & \opt{opus}      & \opt{timestamp}  \\
    \opt{chapter}   & \opt{page}      & \opt{title}      \\
    \opt{column}    & \opt{paragraph} & \opt{verse}      \\
    \opt{elocation} & \opt{part}      & \opt{version}    \\
    \opt{equation}  & \opt{rule}      & \opt{volume}     \\
    \opt{figure}    & \opt{scene}     &                  \\
    \bottomrule
  \end{tabular}
\end{table}

The traditional form \cs{cite}\oarg{prenote}\oarg{postnote}\marg{keys}
introduced in \pkg{natbib} and \pkg{biblatex} is also supported but not
recommended.
If only one optional argument is provided, it is treated as \meta{postnote}.
The \meta{postnote} is used as a page locator if it consists of only digits.

\begin{function}{\parencite,\citep}
  \begin{syntax}
    \cs{parencite}\oarg{options}\marg{keys}
  \end{syntax}
\end{function}

The \cs{parencite} and \cs{citep} command are aliases of \cs{cite}.
They are added for compatibility with \pkg{biblatex} and \pkg{natbib} packages.
If the citation format defined in the CSL style does not have affixes,
these commands in \pkg{citation-style-language} do not enclose the output with
brackets, which is different from other packages.

\begin{function}{\textcite,\citet}
  \begin{syntax}
    \cs{textcite}\oarg{options}\marg{keys}
  \end{syntax}
\end{function}

These commands proceduce narrative in-text citation where the author name is
part of the running text followed by the year in parentheses.
These commands only work with author-date styles.

\DescribeOption{infix}
An extra option \opt{infix} can be given to specify the text inserted between
then author and year parts. For example, “Kesey’s early work (1962)” can be
produced by |\textcite[infix={'s early work}]{ITEM-1}|.
By default the infix is a space.

\begin{function}{\footcite}
  \begin{syntax}
    \cs{footcite}\oarg{options}\marg{keys}
  \end{syntax}
\end{function}

This command is similar to \cs{cite}. It is for compatibility with
\pkg{biblatex}.

\begin{function}{\cites}
  \begin{syntax}
    \cs{cites}\oarg{options}\marg{key}...[options]\marg{key}
  \end{syntax}
\end{function}

The \cs{cites} accepts multiple cite items in a single citation.
This command scans greedily for arguments and a following bracket may be
mistakenly recognized as a delimiter.
To prevent this, an explicit \cs{relax} command is required to terminate the
scanning process. The following example illustrates its usage.

\begin{LaTeXdemo}
  \cites[prefix = {See }, page = 6]{key1}[section = 2.3]{key2}\relax [Text]
\end{LaTeXdemo}

\begin{function}{\citeauthor}
  \begin{syntax}
    \cs{citeauthor}\marg{key}
  \end{syntax}
\end{function}

This command prints the author name.
If the orginal citation does not contain the author name (e.g., a numeric
style), an optional |<intext>| element can be suppplied as a sibling to the
|<citation>| and |<bibliography>| elements in the CSL style (see
\href{https://citeproc-js.readthedocs.io/en/latest/running.html#citation-flags-with-processcitationcluster}{citeproc-js's documentation} for details).

\begin{function}{\citeyearpar}
  \begin{syntax}
    \cs{citeyearpar}\oarg{options}\marg{keys}
  \end{syntax}
\end{function}

This command suppresses the author names in the citation.
It is equivalent to \cs{cite} with \opt{suppress-author} option enabled.

\begin{function}{\fullcite}
  \begin{syntax}
    \cs{fullcite}\oarg{options}\marg{keys}
  \end{syntax}
\end{function}

This command prints a full citation similar to the bibliographic entry.
The contents are genreated from the |<bibliography>| element in the CSL style
rather than the |<citation>| element.

\begin{function}{\nocite}
  \begin{syntax}
    \cs{nocite}\marg{keys}
  \end{syntax}
\end{function}

This command produces no output but makes the entries included in the
bibliography, which is the same in standard \LaTeX.
If the special key |*| is given (\cs{notecite\{*\}}), all the entries in the
database are included.


\section{Bibliography commands}

\begin{function}{\printbibliography}
  \begin{syntax}
    \cs{printbibliography}
    \cs{printbibliography}\oarg{options}
  \end{syntax}
\end{function}

This command prints the reference list.
It also accepts an optional argument in square brackets which is a list of
key-value options. The following options are available.

\begin{variable}{heading}
  \begin{syntax}
    heading = \meta{name}
  \end{syntax}
\end{variable}

This option selects the section heading style defined with \cs{defbibheading}.
The default heading for \cs{printbibliography} is \opt{bibliography}.

\begin{variable}{title}
  \begin{syntax}
    title = \meta{text}
  \end{syntax}
\end{variable}

This option sets the title in the heading.

\begin{variable}{label}
  \begin{syntax}
    label = \meta{label}
  \end{syntax}
\end{variable}

This option is equivalent to \cs{label}\marg{label} after the heading so that
it can be \cs{ref}ed from other parts in the document.

\begin{variable}{prenote, postnote}
  \begin{syntax}
    prenote = \meta{name}
    postnote = \meta{name}
  \end{syntax}
\end{variable}

\begin{variable}{type}
  \begin{syntax}
    type = \meta{entrytype}
  \end{syntax}
\end{variable}

Print only the entries of the given \meta{entrytype}.
Note that the \meta{entrytype} should be the name of a CSL entry type rather
than BibTeX entry type (e.g., use |article-journal| rather than |article|).

\begin{variable}{nottype}
  \begin{syntax}
    nottype = \meta{entrytype}
  \end{syntax}
\end{variable}

Print only the entries that are not \meta{entrycategory}.
This option may be used multiple times.

\begin{variable}{keyword}
  \begin{syntax}
    keyword = \meta{keyword}
  \end{syntax}
\end{variable}

Print only the entries whose \field{keyword} field includes the given
\meta{keyword}. This option may be used multiple times.

\begin{variable}{notkeyword}
  \begin{syntax}
    notkeyword = \meta{keyword}
  \end{syntax}
\end{variable}

Print only the entries whose \field{keyword} field does not include the given
\meta{keyword}. This option may be used multiple times.

\begin{variable}{category}
  \begin{syntax}
    category = \meta{category}
  \end{syntax}
\end{variable}

Print only the entries assigned to the given category.
This option may be used multiple times.


\begin{variable}{notcategory}
  \begin{syntax}
    notcategory = \meta{category}
  \end{syntax}
\end{variable}

Print only the entries assigned to the given category.
This option may be used multiple times.


\subsection{Bibliography Sections}

The \env{refsection} environment is used in the document body to mark a
reference section.
This environment is useful if you want separate, independent
bibliographies and bibliography lists in each chapter, section, or any other
part of a document.
Within a reference section, all cited works are assigned labels which are local
to the environment. Technically, reference sections are completely independent
from document divisions such as \cs{chapter} and \cs{section} even though they
will most likely be used per chapter or section.
See the refsection package option in § 3.1.2.1 for a way to automate this.
Also see § 3.14.3 for usage examples.

\begin{function}{\begin{refsection}, \end{refsection}}
  \begin{syntax}
    \cs{begin}\{refsection\}
    \cs{begin}\{refsection\}[style = \meta{style-id}, bib-resource = <datafile>, ...]
  \end{syntax}
\end{function}

All citations given outside a \env{refsection} environment are assigned to
the global section.
If \cs{printbibliography} is used within a refsection, it will automatically
select the current section.
Note that \env{refsection} environments may not be nested.
Beginning a new reference section automatically ends the active reference context (see § 3.8.10).
The optional argument is a list of key-value configurations. That following options are available.


\DescribeOption{style}
The \opt{style} option specifies the CSL style used in this reference section.
If this is not given, the global style will be used by default which is defined
via package options or \cs{cslsetup}.

\DescribeOption{bib-resource}
The bibliographic data files are specified with this option.
In case of multiple data files, remember to enclose the file with curly brackets
so that the key-values are correctly parsed
(e.g., \texttt{bib-resource = \{foo.json, bar.bib\}}).
By default the global bib resources are used if no \opt{bib-resource} is given.

\DescribeOption{locale}
This option sets the locale used in this reference section.
By default the global locale is used.


\begin{function}{\newrefsection, \endrefsection}
  \begin{syntax}
    \cs{newrefsection}[style = \meta{style-id}, bib-resource = <datafile>, ...]
    \cs{endrefsection}
  \end{syntax}
\end{function}

The \cs{newrefsection} command is similar to the \env{refsection} environment
except that it is a standalone command rather than an environment.
It automatically ends the previous reference section (if any) and immediately
starts a new one.
The \cs{endrefsection} can terminate the current reference section and restore
to the global section.


\subsection{Bibliography Headings}

\begin{function}{\defbibheading}
  \begin{syntax}
    \cs{defbibheading}\marg{name}\oarg{title}\marg{code}
  \end{syntax}
\end{function}

This command defines a bibliographic heading which can be used with the
\opt{bibheading} option of \cs{printbibliography}. The \meta{name} is an
identifier assigned to the defined style.
If a \opt{title} option is given in the \cs{printbibliography}, it is passed
to the \opt{code} as |#1|. Otherwise the value \meta{title} in the optional
argument is used by default. The following is the definition of the default
heading when used in a \cls{book} class.

\begin{LaTeXdemo}
  \defbibheading{bibliography}[\bibname]{%
    \chapter*{#1}%
    \markboth{\MakeUppercase{#1}}{\MakeUppercase{#1}}%
  }
\end{LaTeXdemo}


The following are predefined headings which can be used directly.

\begin{description}
  \item[\opt{bibliography}] The default heading used by \cs{printbibliography}.
  \item[\opt{subbibliography}] Similar to \opt{bibliography} but one sectioning level lower.
  \item[\opt{bibintoc}] Similar to \opt{bibliography} but adds an entry to the table of contents.
  \item[\opt{subbibintoc}] Similar to \opt{subbibliography} but adds an entry to the table of contents.
  \item[\opt{bibnumbered}] Similar to \opt{bibliography} but uses \cs{chapter}
    or \cs{section} to create a numbered heading which is also added to the table of contents.
  \item[\opt{subbibnumbered}] Similar to \opt{subbibliography} but uses \cs{chapter}
    or \cs{section} to create a numbered heading which is also added to the table of contents.
  \item[\opt{none}] An empty heading.
  % \item[\opt{biblist}]
  % \item[\opt{biblistintoc}]
  % \item[\opt{biblistnumbered}]
\end{description}



% \markdownInput{bib-csl-mapping.md}


\section{Compatibility with other packages}

\paragraph{\pkg{babel}}

The main language set by \pkg{babel} is used as the locale for \pkg{citation-style-language}.
In general, \pkg{babel} is supposed to be loaded before \pkg{citation-style-language}.

\paragraph{\pkg{beamer}}

Most of this package's features work fine with \pkg{beamer} except the
\opt{backref} option. At the moment this can be addressed with \pkg{hyperref}'s
\opt{backref} option.


\paragraph{\pkg{hyperref}}

When \pkg{hyperref} is loaded, the DOIs, PMIDs, and PMCIDs are correctly
rendered as hyperlinks.

\paragraph{Incompatible packages}

The following packages are not compatible with \pkg{citation-style-language}.
An error will be triggered if any of them is loaded together with \pkg{citation-style-language}.
\begin{itemize}
  \item \pkg{babelbib}
  \item \pkg{backref}
  \item \pkg{biblatex}
  \item \pkg{bibtopic}
  \item \pkg{bibunits}
  \item \pkg{chapterbib}
  \item \pkg{cite}
  \item \pkg{citeref}
  \item \pkg{inlinebib}
  \item \pkg{jurabib}
  \item \pkg{mcite}
  \item \pkg{mciteplus}
  \item \pkg{multibib}
  \item \pkg{natbib}
  \item \pkg{splitbib}
\end{itemize}

\end{documentation}

\end{document}
