%D \module
%D   [       file=publ-imp-aps,
%D        version=2015.03.22,
%D          title=APS bibliography style,
%D       subtitle=Publications,
%D         author=Alan Braslau and Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is therefore copyrighted
%D by \PRAGMA. See mreadme.pdf for details.

\startbtxrenderingdefinitions[aps]

%D Reference:
%D \startTEX
%D @Book{APS2011,
%D     title    ={Physical Review Style and Notation Guide}
%D     year     ={2011},
%D     month    ={June}
%D     edition  ={Revised},
%D     editor   ={Waldron, A and Judd, P. and Miller, V.},
%D     address  ={Ridge, NY},
%D     publisher={American Physical Society},
%D     Xpages   ={26},
%D     url      ={http://journals.aps.org/files/styleguide-pr.pdf}
%D }
%D \stopTEX

% set ALL specific APS compliant values

\definebtx
  [aps]
  [\c!default=default,
   \c!specification=aps,
   \c!otherstext={\btxspace{\it\btxlabeltext{others}}},
   \c!etallimit=10,
   \c!etaldisplay=\btxparameter\c!etallimit,
   %c!journalconversion=\v!normal,
   \c!monthconversion=\v!month,
   \c!title=\v!yes,
   \c!separator:names:2={\btxcomma},
   \c!separator:names:3={\btxcomma\btxlabeltext{and}\space},
   \c!separator:names:4={\btxspace\btxlabeltext{and}\space}]

% First, define and set list and rendering parameters

\definebtxrendering
  [aps]
  [\c!specification=aps,
   \c!sorttype=\v!default,
   \c!numbering=num]

\setupbtxlist
  [aps]
  [\c!alternative=b, % spaces
   \c!align={normal,verytolerant,stretch}]

\definebtx
  [aps:\s!list]
  [aps]
  [\c!authorconversion=normalshort]

\definebtx
  [aps:\s!list:author]
  [aps:\s!list]

\definebtx
  [aps:\s!list:editor]
  [aps:\s!list:author]

\definebtx
  [aps:\s!list:suffix]
  [aps:\s!list]

\definebtx
  [aps:\s!list:url]
  [aps:\s!list]

\definebtx
  [aps:\s!list:doi]
  [aps:\s!list]

\definebtx
  [aps:\s!list:\s!page]
  [aps:\s!list]
  [\c!separator:2={\btxcomma},
   \c!separator:3={\btxcomma\btxlabeltext{and}\space},
   \c!separator:4={\btxspace\btxlabeltext{and}\space},
   \c!left={\btxleftparenthesis},
   \c!right={\btxrightparenthesis},
   \c!command={\wordright}]

\definebtx
  [aps:\s!list:numbering]
  [aps:\s!list]
  [left={[},
   right={]}]

\definebtx
  [aps:\s!list:numbering:default]
  [aps:\s!list:numbering]

\definebtx
  [aps:\s!list:numbering:num]
  [aps:\s!list:numbering]

\definebtx
  [aps:\s!list:numbering:short]
  [aps:\s!list:numbering]

\definebtx
  [aps:\s!list:numbering:tag]
  [aps:\s!list:numbering]

\definebtx
  [aps:\s!list:numbering:index]
  [aps:\s!list:numbering]

%D In order to be able to get journals expanded (or normalized or abbreviated) you need
%D to load a list:
%D
%D \starttyping
%D \btxloadjournallist[journals.txt] % the jabref list
%D \stoptyping

% TODO

\definebtx
  [aps:\s!list:journal]
  [aps:\s!list]
  [\c!style=\v!italic]
  %command=\btxexpandedjournal] % btxabbreviatedjournal

\definebtx
  [aps:\s!list:volume]
  [aps:\s!list]
  [\c!style=\v!bold]

\definebtx
  [aps:\s!list:title]
  [aps:\s!list]
  [\c!style=\v!italic,
   \c!command=\Word]

\definebtx
  [aps:\s!list:title:article]
  [aps:\s!list:title]
  [\c!style=] % journal is set in italics

\definebtx
  [aps:\s!list:title:magazine]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:title:newspaper]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:title:periodical]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:title:standard]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:title:book]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:title:inbook]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:title:incollection]
  [aps:\s!list:title]
  [\c!style=] % booktitle is set in italics

\definebtx
  [aps:\s!list:title:proceedings]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:title:inproceedings]
  [aps:\s!list:title]
  [\c!style=] % booktitle is set in italics

\definebtx
  [aps:\s!list:title:conference]
  [aps:\s!list:title]
  [\c!style=] % booktitle is set in italics

\definebtx
  [aps:\s!list:title:thesis]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:title:phdthesis]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:title:mastersthesis]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:title:booklet]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:title:manual]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:title:techreport]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:title:unpublished]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:title:patent]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:title:electronic]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:title:other]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:title:misc]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:title:literal]
  [aps:\s!list:title]

\definebtx
  [aps:\s!list:type]
  [\c!command=\Word]

% Then define and set all cite parameters

\definebtx
  [aps:\s!cite]
  [aps]
  [\c!alternative=num,
   \c!authorconversion=\v!name,
   \c!compress=\v!yes,
   \c!sorttype=normal]

\definebtx
  [aps:\s!cite:name]
  [aps:\s!cite]
  [\c!authorconversion=\v!name]

\definebtx
  [aps:\s!cite:inverted]
  [aps:\s!cite]
  [\c!authorconversion=\v!invertedshort]

\definebtx
  [aps:\s!cite:invertedshort]
  [aps:\s!cite]
  [\c!authorconversion=\v!invertedshort]

\definebtx
  [aps:\s!cite:normalshort]
  [aps:\s!cite]
  [\c!authorconversion=\v!normalshort]

\definebtx
  [aps:\s!cite:normal]
  [aps:\s!cite]
  [\c!authorconversion=\v!normal]

\definebtx
  [aps:\s!cite:author]
  [aps:\s!cite]

\definebtx
  [aps:\s!cite:organization]
  [aps:\s!cite]

\definebtx
  [aps:\s!cite:authoryear]
  [aps:\s!cite:author]
  [\c!left={(},
   \c!right={)},
   \c!inbetween={\btxcomma}]

\definebtx
  [aps:\s!cite:authoryears]
  [aps:\s!cite:authoryear]
  [\c!left=,
   \c!right=,
   \c!inbetween={\btxspace}]

\definebtx
  [aps:\s!cite:authornum]
  [aps:\s!cite:author]
  [\c!left={(},
   \c!right={)}]

\definebtx
  [aps:\s!cite:author:num] % todo
  [aps:\s!cite:authornum]
  [\c!left={\btxspace[},
   \c!right={]}]

\definebtx
  [aps:\s!cite:author:year] % todo
  [aps:\s!cite:authoryear]
  [\c!left=,
   \c!right=]

\definebtx
  [aps:\s!cite:author:years] % todo
  [aps:\s!cite:authoryears]
  [\c!inbetween=,
   \c!left={\btxspace(},
   \c!right={)}]

\definebtx
  [aps:\s!cite:year]
  [aps:\s!cite]

\definebtx
  [aps:\s!cite:title]
  [aps:\s!cite]
  [\c!command={\language[\currentbtxlanguage]}, % BAH
   \c!sorttype=none,
   \c!style=\v!italic]

\definebtx
  [aps:\s!cite:booktitle]
  [aps:\s!cite:title]

\definebtx
  [aps:\s!cite:tag]
  [aps:\s!cite]
  [\c!left={[},
   \c!right={]}]

\definebtx
  [aps:\s!cite:index]
  [aps:\s!cite]
  [\c!left={[},
   \c!right={]}]

\definebtx
  [aps:\s!cite:page]
  [aps:\s!cite]
  [\c!left=,
   \c!right=,
   \c!separator:2={\btxcomma}, % :0 and :1 - between items of a list
   \c!separator:3={\btxcomma\btxlabeltext{and}\space},
   \c!separator:4={\btxspace\btxlabeltext{and}\space}]

\definebtx
  [aps:\s!cite:pages]
  [aps:\s!cite:page]

\definebtx
  [aps:\s!cite:keywords]
  [aps:\s!cite]
  [\c!left={(},
   \c!right={)}]

\definebtx
  [aps:\s!cite:short]
  [aps:\s!cite]
  [\c!left={[},
   \c!right={]}]

\definebtx
  [aps:\s!cite:category]
  [aps:\s!cite]

\definebtx
  [aps:\s!cite:url]
  [aps:\s!cite]
  [\c!left={(},
   \c!right={)}]

\definebtx
  [aps:\s!cite:doi]
  [aps:\s!cite:url]

\definebtx
  [aps:\s!cite:num]
  [aps:\s!cite]
  [\c!left={[},
   \c!right={]},
  %\c!left=,   % TODO: PRB uses superscript references...
  %\c!right=,  % and after punctuation, PRA, C, D, E, and L are before!
  %\c!command={\high},
   \c!separator:2={\btxcommabreak},
   \c!separator:3=\btxparameter{\c!separator:2},
   \c!separator:4=\btxparameter{\c!separator:2}]

\definebtx
  [aps:\s!cite:default]
  [aps:\s!cite:num]

\definebtx
  [aps:\s!cite:textnum]
  [aps:\s!cite:num]
  [\c!left={Ref.\nbsp},
   \c!command=,
   \c!separator:2={\btxcomma},
   \c!separator:3={\btxspace\btxlabeltext{and}\space},
   \c!separator:4={\btxspace\btxlabeltext{and}\space}]

\definebtx
  [aps:\s!cite:entry]
  [aps:\s!cite]
  [\c!left=,
   \c!right=,
   \c!inbetween={\btxspace},
   \c!separator:2={\btxsemicolon},
   \c!separator:3=\btxparameter{\c!separator:2},
   \c!separator:4=\btxparameter{\c!separator:2}]

%D Sometimes we have verbose injections in an entry and these can be language
%D dependent, so we use labels.
%D
%D Because we want to mix rendering (in the manual) we need a namespace in label
%D texts:

\setupbtxlabeltext
  [en]
  [aps:number={no.},
   aps:edition={ed.},
   aps:Editor={Ed.},
   aps:Editors={Eds.},
   aps:Volume={Vol.},
   aps:Volumes={Vols.},
   aps:supplement={Suppl.},           % Supplement (not used?)
   aps:inpress={in press},
   aps:tobe={to be published},
   aps:unpublished={unpublished}]

% Check this (google translate!!):

\setupbtxlabeltext
  [nl]
  [aps:number={nr.},
   aps:edition={ed.}, % editie
   aps:Editor=Editor, % Ed./Eds.
   aps:Editors=Editors,
   aps:Volume={Vol.},
   aps:Volumes={Vols.},
   aps:supplement=Supplement,
   aps:inpress={in press}, % CHECK THESE!
   aps:tobe={worden gepubliceerd},
   aps:unpublished={onuitgegeven}]

\setupbtxlabeltext
  [fr]
  [aps:number={n\high{o}},
   aps:edition={édition},
   aps:Editor=Éditeur,
   aps:Editors=Éditeurs,
   aps:Volume=Volume,
   aps:Volumes=Volumes,
   aps:supplement=Supplément,
   aps:inpress={sous impression},
   aps:tobe={à paraître},
   aps:unpublished={inédit}]          % pour un livre

\setupbtxlabeltext
  [de]
  [aps:number={nr.},
   aps:edition=Auf\/lage,
   aps:Editor=Herausgeber, % Hrsg./Hg.
   aps:Editors=Herausgeber,
   aps:Volume=Band,        % Bd.
   aps:Volumes={Bände},
   aps:supplement={Beilage},          % Supplement
   aps:inpress={in der Presse}, % CHECK THESE!
   aps:tobe={veröffentlicht werden},
   aps:unpublished={unveröffentlicht}]

% thanks: Andrea Valle

\setupbtxlabeltext
  [it]
  [aps:number={nº},
   aps:edition={ed.}, % edizione
   aps:Editor={A cura di},
   aps:Editors={A cura di},
   aps:Volume={Vol.},  % Volume
   aps:Volumes={Vol.}, % Volumi
   aps:supplement={Supplemento},
   aps:inpress={in press}, % CHECK THESE!
   aps:tobe={da pubblicare},
   aps:unpublished={inedito}]

\setupbtxlabeltext
  [es]
  [aps:number={nº},
   aps:edition={ed.}, % edición
   aps:Editor=Editor, % Ed./Eds.
   aps:Editors=Editores,
   aps:Volume={Vol.},   % Volumen
   aps:Volumes={Vols.}, % Volúmenes
   aps:supplement=Suplemento,
   aps:inpress={en prensa}, % CHECK THESE!
   aps:tobe={que se publicará},
   aps:unpublished={inédito}]

\setupbtxlabeltext
  [sv]
  [aps:number={nr.},
   aps:edition={Utgåva},
   aps:Editor=Redaktör,
   aps:Editors=Redaktörer,
   aps:Volume=Band,
   aps:Volumes=Band,
   aps:supplement=Bilaga,
   aps:inpress={under tryckning},
   aps:tobe={att publiceras},
   aps:unpublished={opublicerat}]


% cite setups

\startsetups btx:aps:nd
    \doifelse {\currentbtxcategory} {article} {
        \btxlabeltext{aps:tobe}
    } {
        \doifelse {\currentbtxcategory} {book} {
            \btxlabeltext{aps:inpress}
        } {
            \btxlabeltext{aps:unpublished}
        }
    }
\stopsetups

\startsetups btx:aps:cite:author:year
    \texdefinition{\s!btx:\s!cite:concat}
    \ifx\currentbtxfirst\empty
        \fastsetup{btx:aps:nd}
    \else
        \texdefinition {\s!btx:\s!cite:inject} {
            \btxcitereference
            \currentbtxfirst
        }
        \ifx\currentbtxsecond\empty \else
            \btxparameter\c!inbetween
            \texdefinition {\s!btx:\s!cite:inject} {
                \currentbtxsecond
            }
        \fi
        \btxflushsuffix
    \fi
\stopsetups

\startsetups btx:aps:cite:author:years
    \fastsetup{btx:aps:cite:author:year}
\stopsetups

% used in publ-imp-page.mkvi

\startsetups [btx:aps:list:page-or-pages]
    \ifx\currentbtxlastpage\empty
        \btxlabeltext{p}
    \else
        \btxlabeltext{pp}
    \fi
    \btxnbsp
\stopsetups

%D Instead of texdefinitions without arguments, we could have used setups but in my
%D editor (hh, scite) the commands stand out better. It also saves an additional
%D component in the name (e.g. common:) because commands and setups have a different
%D namespace, so similar calls don't clash. Performance of definitions is somewhat
%D better.

%D We use "texdefinitions" (with eventual arguments) for helpers that are used
%D in the rendering "setups" defined for each category below.

%D Note that \btxdoif... and \btxflush rely on the definitions in
%D publ-imp-aps.lua: fields that are not listed as required nor optional are
%D IGNORED. We also make heavy use of the notion of sets - comma-separated lists
%D of alternative fields to be used in hierarchal order. For example:
%D author = { "author", "editor", "publisher", "title" }, will return the
%D author field if it exists; if not, the editor field will be returned, if it
%D exists; if not, the publisher field will be returned, if it exists; if not,
%D the title field will be returned, it it exists; if not, nothing will be
%D returned. In lua syntax, it can be understood as
%D author or editor or publisher or title or ""

\starttexdefinition unexpanded btx:aps:composed-title #title
    \btxstartstyleandcolor [aps:\s!list:title:\currentbtxcategory]
        \begingroup
            \language[\currentbtxlanguage]
            \btxusecommand [aps:\s!list:title:\currentbtxcategory] {
                \btxflush{#title}
                \btxdoif {sub#title} {
                    \btxcolon
                    \btxflush{sub#title}
                }
            }
        \endgroup
    \btxstopstyleandcolor
\stoptexdefinition

\starttexdefinition unexpanded btx:aps:title
    \btxdoif {title} {
        % we make the title active, opening file
        \btxdoifelse {file} {
            \texdefinition{btx:format:inject}
                {url(file:\btxflush{file})}
                {
                    \texdefinition{btx:aps:composed-title}{title}
                }
        } {
            \texdefinition{btx:aps:composed-title}{title}
        }
        \btxcomma
    }
\stoptexdefinition

\starttexdefinition unexpanded btx:aps:optional-title
    \doif{\btxparameter{\c!title}}\v!yes {
        \texdefinition {btx:aps:title}
    }
\stoptexdefinition

\starttexdefinition unexpanded btx:aps:year
    \btxdoifelse {year} {
        \btxflush{year}
    } {
        \fastsetup{btx:aps:nd}
    }
\stoptexdefinition

% #author may be author(set) or editor

\starttexdefinition unexpanded btx:aps:author-or-editor #author
    \btxdoif {#author} {
        \btxflush{#author}
        \doifelse {\btxfoundname{#author}} {editor} {
            \btxleftparenthesis
            \btxsingularorplural {editor} {
                \btxlabeltext{aps:Editor}
            } {
                \btxlabeltext{aps:Editors}
            }
            \btxrightparenthesis
        } {
            \btxdoif {collaboration} {
                \btxleftparenthesis
                \btxflush{collaboration}
                \btxrightparenthesis
            }
        }
    }
\stoptexdefinition

\starttexdefinition unexpanded btx:aps:author
    \btxflush{author}
    \btxcomma
\stoptexdefinition

\starttexdefinition unexpanded btx:aps:organization-if-not-author
    \btxdoif {organization} {
        \doifnot {\btxfoundname{author}} {organization} {
            \btxspace
            \btxflush{organization}
            \btxcomma
        }
    }
\stoptexdefinition

\starttexdefinition unexpanded btx:aps:editor-in
    \btxdoif {booktitle} {
        \btxlabeltext{In}
        \doifnot {\btxfoundname{author}} {editor} {
            \btxspace
            \texdefinition{btx:aps:author-or-editor} {editor}
        }
        \btxspace
        \texdefinition{btx:aps:composed-title}{booktitle}
        \btxcomma
    }
\stoptexdefinition

\starttexdefinition unexpanded btx:aps:editionset
    \doif {\currentbtxcategory} {techreport} {
        \btxdoifelse {type} {
            \btxusecommand [\currentbtx:type] {
                \btxflush{type}
            }
        } {
            \btxlabeltext{technicalreport}
        }
        \btxcomma
    }
    \btxdoif {volume} {
        \btxoneorrange {volume} {
            \btxlabeltext{aps:Volume}
        } {
            \btxlabeltext{aps:Volumes}
        }
        \btxspace
        \btxflush{volume}
        \btxcomma
    }
    \btxdoif {number} {
        \btxlabeltext{aps:number}
        \btxspace
        \btxflush{number}
        \btxcomma
    }
    \btxdoif {edition} {
        \btxflush{edition}
        \btxspace
        \btxlabeltext{aps:edition}
        \btxcomma
    }
    \btxdoif {pages} {
        \btxoneorrange {pages} {
            \btxlabeltext{p}.
        } {
            \btxlabeltext{pp}.
        }
        \btxnbsp
        \btxflush{pages}
        \btxcomma
    }
\stoptexdefinition

\starttexdefinition unexpanded btx:aps:journal-volume-year
    \btxdoif {journal} {
        \btxspace
        \btxstartstyleandcolor [aps:\s!list:journal]
            % expandedjournal abbreviatedjournal
            \btxflush{expandedjournal -> journal}
        \btxstopstyleandcolor
        \btxdoifelse {volume} {
            \btxspace
            \btxstartstyleandcolor [aps:\s!list:volume]
                \btxflush{volume}
            \btxstopstyleandcolor
            \btxdoif {number} {
                \removeunwantedspaces(
                \btxflush{number}
                \btxrightparenthesiscomma
            }
        } {
            \btxdoif {number} {
                \btxspace
                \btxflush{number}
                \btxcomma
            }
        }
        \btxdoif {pages} {
            \btxspace
            \btxflush{pages}
        }
    }
    \btxleftparenthesis
    \texdefinition{btx:aps:year}
    \btxrightparenthesis
\stoptexdefinition

\starttexdefinition unexpanded btx:aps:publisher-wherefrom-year
    \removeunwantedspaces
    \removepunctuation
    \btxleftparenthesis
    \btxflush{publisher}
    \btxdoifelse {address} {
        \btxdoif {publisher} {
            \btxcomma
        }
        \btxflush{address}
        \btxdoif {country} {
            \btxcomma
            \btxflush{country}
        }
        \btxcomma
    } {
        \btxdoif {publisher} {
            \btxcomma
        }
    }
    \texdefinition{btx:aps:year}
    \btxrightparenthesis
\stoptexdefinition

\starttexdefinition unexpanded btx:aps:note
    \btxperiod
    \btxdoif {note} {
        \btxleftparenthesis
        \btxflush{note}
        \btxrightparenthesis
    }
\stoptexdefinition

\starttexdefinition unexpanded btx:aps:doi-url #text
    \ifconditional\btxinteractive
        \btxdoifelse {doi} {
            \goto {#text} [url(http://dx.doi.org/\btxflush{doi})]
        } {
            \btxdoifelse {url} {
                \goto {#text} [url(\btxflush{url})]
            } {
                #text
            }
        }
    \else
        #text
    \fi
\stoptexdefinition

% Then setups, by category

% An article from a journal
% Required fields: author or editor or title, journal, (year).
% Optional fields: volume, number, pages, type, doi, url, note.
% Note that bibtex (and tools) do not include editor (e.g. special issue or section)

\startsetups btx:aps:list:article
    \texdefinition{btx:aps:author}
    \texdefinition{btx:aps:optional-title}
    \texdefinition{btx:aps:doi-url} {\texdefinition{btx:aps:journal-volume-year}}
    \texdefinition{btx:aps:note}
\stopsetups

% An article from a magazine.
% Required fields: author or title, journal, (year).
% Optional fields: number, pages, type, month, day, doi, url, note.

\startsetups btx:aps:list:magazine
    \fastsetup{btx:aps:list:article}
\stopsetups

% An article from a newspaper.
% Required fields: author or title, journal, (year).
% Optional fields: number, pages, type, month, day, doi, url, note.

\startsetups btx:aps:list:newspaper
    \fastsetup{btx:aps:list:article}
\stopsetups

% A complete issue of a periodical, such as a special issue of a journal.
% Required fields: title, year
% Optional fields: editor, publisher, subtitle, series, volume, number, month, organization, doi, url, issn, note

% needs to be tuned...
\startsetups btx:aps:list:periodical
    \fastsetup{btx:aps:list:article}
\stopsetups

% National and international standards issued by a standards body
% Required fields: author, institution, or organization, year, title
% Optional fields: subtitle, doi, url, note

\startsetups btx:aps:list:standard
    \texdefinition{btx:aps:author}
    \texdefinition{btx:aps:title}
    \texdefinition{btx:aps:note}
\stopsetups
% year?

% A book with an explicit publisher.
% Required fields: author or editor or publisher, title, (year).
% Optional fields: volume or number, series, address, edition, month, day, note.
% APS? ignores: month, day

% todo: series?

\startsetups btx:aps:list:book
    \texdefinition{btx:aps:author}
    \texdefinition{btx:aps:title}
    \texdefinition{btx:aps:editionset}
    \texdefinition{btx:aps:doi-url} {\texdefinition{btx:aps:publisher-wherefrom-year}}
    \texdefinition{btx:aps:note}
\stopsetups

% There is some debate about how inbook should differ from incollection

% A part of a book, which may be a chapter (or section or whatever) and/or a range of pages.
% (note that inbook is handled differently by bibtex and biblatex)
% Required fields: author or editor, title, chapter and/or pages, publisher, year.
% Optional fields: volume or number, series, type, address, edition, month, note.
% We add optional: booktitle.
% APS? ignores: chapter, month

\startsetups btx:aps:list:inbook
    \texdefinition{btx:aps:author}
    \texdefinition{btx:aps:title}
    \texdefinition{btx:aps:editor-in}
    \texdefinition{btx:aps:editionset}
    \texdefinition{btx:aps:doi-url} {\texdefinition{btx:aps:publisher-wherefrom-year}}
    \texdefinition{btx:aps:note}
\stopsetups
% chapter?

% A part of a book having its own title.
% Required fields: author, title, booktitle, publisher, year.
% Optional fields: editor, volume or number, series, type, chapter, pages, address, edition, month, note.
% APS? ignores: chapter, month

\startsetups btx:aps:list:incollection
    \fastsetup{btx:aps:list:inbook}
\stopsetups

% The proceedings of a conference.
% Required fields: title, year.
% Optional fields: editor, volume or number, series, address, month, organization, publisher, note.
% todo: series?

\startsetups btx:aps:list:proceedings
    \fastsetup{btx:aps:list:book}
\stopsetups

% An article in a conference proceedings.
% Required fields: author, title, booktitle, year.
% Optional fields: editor, volume or number, series, pages, address, month, organization, publisher, note.

\startsetups btx:aps:list:inproceedings
    \texdefinition{btx:aps:author}
    \texdefinition{btx:aps:title}
    \texdefinition{btx:aps:editor-in}
    \texdefinition{btx:aps:editionset}
   %\texdefination{btx:aps:organization-if-not-author}% first check publ-imp-aps.lua
    \btxdoif {organization} {
        \btxspace
        \btxflush{organization}
        \btxcomma
    }
    \texdefinition{btx:aps:doi-url} {\texdefinition{btx:aps:publisher-wherefrom-year}}
    \texdefinition{btx:aps:note}
\stopsetups

\startsetups btx:aps:list:conference
    \fastsetup{btx:aps:list:inproceedings}
\stopsetups

% A thesis.
% Required fields: author, title, school, year.
% Optional fields: type, address, month, note.

\startsetups btx:aps:list:thesis
    \texdefinition{btx:aps:author}
    \texdefinition{btx:aps:title}
    \btxleftparenthesis
    \btxdoifelse {type} {
        \btxusecommand [aps:list:type] {
            \btxflush{type}
        }
    } {
        \btxlabeltext{\currentbtxcategory}
    }
    \btxrightparenthesis
    \btxdoif {school} {
        \btxperiod
        \btxflush{school}
    }
    \btxdoif {address} {
        \btxdoifelse {school} {
            \btxcomma
        } {
            \btxperiod
        }
        \btxflush{address}
        \btxdoif {country} {
            \btxcomma
            \btxflush{country}
        }
    }
    \btxleftparenthesis
    \texdefinition{btx:aps:year}
    \btxrightparenthesis
    \texdefinition{btx:aps:note}
\stopsetups

\startsetups btx:aps:list:phdthesis
    \fastsetup{btx:aps:list:thesis}
\stopsetups

\startsetups btx:aps:list:mastersthesis
    \fastsetup{btx:aps:list:thesis}
\stopsetups

% A work that is printed and bound, but without a named publisher or sponsoring institution.
% Required field: title.
% Optional fields: author, howpublished, address, month, year, note.

\startsetups btx:aps:list:booklet
    \fastsetup{btx:aps:list:book}
\stopsetups

% Technical documentation.
% Required field: title.
% Optional fields: author, organization, address, edition, month, year, note.

\startsetups btx:aps:list:manual
    \fastsetup{btx:aps:list:book}
\stopsetups

% A report published by a school or other institution, usually numbered within a series.
% Required fields: author, title, institution, year.
% Optional fields: type, number, address, month, note.

\startsetups btx:aps:list:techreport
    \fastsetup{btx:aps:list:book}
\stopsetups

% A document having an author and title, but not formally published.
% Required fields: author, title, note.
% Optional fields: month, year.

\startsetups btx:aps:list:unpublished
    \fastsetup{btx:aps:list:book}
\stopsetups

% A patent. Note that this category was not defined with BIBTEX. Below from JabRef:
% Required fields: nationality, number, year, yearfiled
% Optional fields: author, title, assignee, address, type, number, day, dayfiled, month, monthfiled, note, url
% Also optional: publisher

% todo: yearfiled, monthfiled, dayfiled

\startsetups btx:aps:list:patent
    \texdefinition{btx:aps:author}
    \texdefinition{btx:aps:title}
    \begingroup
        \it
        \btxdoif {nationality} {
            \btxspace
            \btxflush{nationality}
        }
        \btxspace
        \btxlabeltext{patent}
        \btxdoif {number} {
            \btxspace
            \btxlabeltext{aps:number}
            \btxspace
            \btxflush{number}
        }
        \btxperiod
        \italiccorrection
    \endgroup
    \texdefinition{btx:aps:doi-url} {\texdefinition{btx:aps:publisher-wherefrom-year}}
    \texdefinition{btx:aps:note}
\stopsetups

% Electronic. Note that this category was not defined with BIBTEX. Below from JabRef:
% Required fields: title
% Optional fields: address, author, howpublished, month, note, organization, url, year, doi
% Also optional: type

% Like Misc below but includes organization.

\startsetups btx:aps:list:electronic
    \texdefinition{btx:aps:author}
    \texdefinition{btx:aps:title}
    \texdefinition{btx:aps:organization-if-not-author}
    \btxdoif {howpublished} {
        \texdefinition{btx:aps:doi-url} {\btxspace\btxflush{howpublished}}
    }
    \btxleftparenthesis
    \texdefinition{btx:aps:year}
    \btxrightparenthesis
    \texdefinition{btx:aps:note}
\stopsetups

% Other. Note that this category was not defined with BIBTEX. Below from JabRef:
% Required fields: author or title, year
% Optional fields: note, doi, url

\startsetups btx:aps:list:other
    \fastsetup{btx:aps:list:book}
\stopsetups

% Use this type when nothing else fits.
% Required fields: none.
% Optional fields: author, title, howpublished, month, year, note.

\startsetups btx:aps:list:misc
    \fastsetup{btx:aps:list:electronic}
    % Note that organization is an "ignored" field.
\stopsetups

% If all else fails to match:

\startsetups btx:aps:list:literal
    %\btxleftparenthesis
    \removeunwantedspaces(
    \btxflush{tag}
    \btxrightparenthesis
    \btxdoif {text} {
        \btxflush{text}
    }
\stopsetups

\stopbtxrenderingdefinitions
