%% ---------------------------------------------------------------
%% biblatex-chem --- A set of biblatex implementations of
%%   chemistry-related bibliography styles
%% Maintained by Joseph Wright
%% E-mail: joseph.wright@morningstar2.co.uk
%% Released under the LaTeX Project Public License v1.3c or later
%% See http://www.latex-project.org/lppl.txt
%% ---------------------------------------------------------------
%%

\ProvidesFile{chem-angew.bbx}[2019/12/03 v1.1y biblatex bibliography style]

% Load the standard style to avoid copy-pasting unnecessary material
\RequireBibliographyStyle{numeric-comp}

% New options
\newtoggle{bbx:articletitle}
\newtoggle{bbx:chaptertitle}
\newtoggle{bbx:pageranges}
\DeclareBibliographyOption{articletitle}[true]{%
  \settoggle{bbx:articletitle}{#1}%
}
\DeclareBibliographyOption{chaptertitle}[true]{%
  \settoggle{bbx:chaptertitle}{#1}%
}
\DeclareBibliographyOption{pageranges}[true]{%
  \settoggle{bbx:pageranges}{#1}%
}

\DeclareBibliographyOption{biblabel}{%
  \ifstrequal{#1}{plain}
    {\DeclareFieldFormat{labelnumberwidth}{##1}}
    {%
      \ifstrequal{#1}{parens}
        {\DeclareFieldFormat{labelnumberwidth}{\mkbibparens{##1}}}%
        {%
          \ifstrequal{#1}{dot}
            {\DeclareFieldFormat{labelnumberwidth}{##1\adddot\midsentence}}
            {\DeclareFieldFormat{labelnumberwidth}{\mkbibbrackets{##1}}}%
        }%
    }%
}

% Alter settings that carry through from biblatex
\ExecuteBibliographyOptions
  {
    date         = year  ,
    doi          = false ,
    eprint       = false ,
    giveninits   = true  ,
    isbn         = false ,
    maxnames     = 999   ,
    maxcitenames = 2     ,
    pageranges   = true  ,
    url          = false
  }

% Modify the name format
\@ifpackageloaded{biblatex_legacy}
  {
    \DeclareNameFormat{default}{%
      \usebibmacro{name:first-last}{#1}{#4}{#5}{#7}%
      \usebibmacro{name:andothers}%
    }
  }
  {
    \DeclareNameFormat{default}{%
      \nameparts{#1}%
      \usebibmacro{name:given-family}
        {\namepartfamily}
        {\namepartgiveni}
        {\namepartprefix}
        {\namepartsuffix}%
      \usebibmacro{name:andothers}%
    }
  }

% Custom field formats
\DeclareFieldFormat[inproceedings]{booktitle}{#1}
\DeclareFieldFormat{date}{\mkbibbold{#1}}
\DeclareFieldFormat{doi}{%
  DOI\space
  \ifhyperref
    {\href{https://doi.org/#1}{\nolinkurl{#1}}}
    {\nolinkurl{#1}}%
}
\DeclareFieldFormat{journaltitle}{\mkbibitalic{#1}\isdot}
\DeclareFieldFormat{labelnumberwidth}{\mkbibbrackets{#1}}
\DeclareFieldFormat[article]{pages}{%
  \iftoggle{bbx:pageranges}{#1}{\mkfirstpage{#1}}%
}
\DeclareFieldFormat{removecomma}{\mkbibnocomma{#1}}
\DeclareFieldFormat{title}{„#1“}
\DeclareFieldFormat[patent]{type}{%
  \ifbibstring{#1}
    {\mkbibitalic{\bibstring{#1}}}
    {\mkbibitalic{#1}}%
}
\DeclareFieldFormat{url}{\url{#1}}
\DeclareFieldFormat*{volume}{%
  \mkbibitalic{\bibstring{volume}~#1}%
}
\DeclareFieldFormat[article]{volume}{\mkbibitalic{#1}}

% Simple modifications to punctuation, etc.
\renewcommand*{\begrelateddelim}{\addsemicolon\space}%
\renewcommand*{\finalnamedelim}{%
  \ifcurrentname{labelname}
    {%
      \ifnumgreater{\value{liststop}}{2}
        {\finalandcomma}
        {}%
      \addspace
      \bibstring{and}%
      \space
    }
    {\multinamedelim}%
}
\renewcommand*{\intitlepunct}{\addspace}
\ifdef{\mkbibnamesuffix}
  {\renewcommand*{\mkbibnamesuffix}}
  {\renewcommand*{\mkbibnameaffix}}%
    [1]{\addcomma\space#1}
\renewcommand*{\newunitpunct}{\addcomma\addspace}

% Bibliography strings
\DefineBibliographyStrings{english}{%
  chapter  = Chapter      ,
  editor   = Ed\adddot    ,
  editors  = Eds\adddot   ,
  patentus = US Pat\adddot,
  volume   = Vol\adddot
}

% Bibliography macros
\renewbibmacro*{byeditor+others}{%
  \ifnameundef{editor}
    {}
    {%
      \printtext[parens]{%
        \usebibmacro{editorstrg}%
        \setunit{\addcolon\addspace}%
        \printnames[byeditor]{editor}%
        \clearname{editor}%
        \newunit
      }%
    }%
  \usebibmacro{byeditorx}%
  \usebibmacro{bytranslator+others}%
}

\renewbibmacro*{byholder}{%
  \ifnameundef{holder}
    {}
    {\printtext[parens]{\printnames{holder}}}%
}

\renewbibmacro*{date}{%
  \iffieldundef{year}
    {}
    {\printtext[date]{\printdate}}%
}

\renewbibmacro*{event+venue+date}{%
  \printfield{eventtitle}%
  \ifboolexpr{
    test {\iffieldundef{venue}}
    and
    test {\iffieldundef{eventyear}}
  }
    {}
    {%
      \printfield{venue}%
      \newunit
      \printeventdate
      \newunit
    }%
}

\renewbibmacro*{institution+location+date}{%
  \printlist{institution}%
  \newunit
  \printlist{location}%
  \newunit
  \usebibmacro{date}%
  \newunit
}

\renewbibmacro*{journal}{%
  \iffieldundef{journaltitle}
    {}
    {%
      \printtext[journaltitle]{%
        \printfield[removecomma]{journaltitle}%
        \setunit{\subtitlepunct}%
        \printfield[removecomma]{journalsubtitle}%
      }%
    }%
}

\renewbibmacro*{journal+issuetitle}{%
  \usebibmacro{journal}%
  \setunit*{\addspace}%
  \iffieldundef{series}
    {}
    {\newunit
     \printfield{series}%
     \setunit{\addspace}}%
  \usebibmacro{date}%
  \newunit
  \printfield{volume}%
  \newunit
}

\renewbibmacro*{maintitle}{%
  \ifboolexpr{
    test {\iffieldundef{maintitle}}
    and
    test {\iffieldundef{mainsubtitle}}
  }
    {}
    {%
      \printtext[maintitle]{%
        \printfield[titlecase]{maintitle}%
        \setunit{\subtitlepunct}%
        \printfield[parens]{mainsubtitle}%
      }%
      \newunit
    }%
  \printfield{maintitleaddon}%
}

\renewbibmacro*{maintitle+booktitle}{%
  \iffieldundef{maintitle}
    {}
    {%
      \usebibmacro{maintitle}%
      \newunit\newblock
    }%
  \usebibmacro{booktitle}%
  \newunit
  \iffieldundef{volume}
    {}
	{%
	  \iffieldundef{series}
	    {%
	      \printfield{volume}%
          \printfield{part}%
        }
        {}%
    }%
  \newunit
}

\renewbibmacro*{maintitle+title}{%
  \iffieldsequal{maintitle}{title}
    {
      \clearfield{maintitle}%
       \clearfield{mainsubtitle}%
       \clearfield{maintitleaddon}%
    }
    {%
      \iffieldundef{maintitle}
        {}
        {%
          \usebibmacro{maintitle}%
          \newunit
        }%
    }%
  \usebibmacro{title}%
  \newunit
  \iffieldundef{volume}
    {}
	{%
	  \iffieldundef{series}
	    {%
	      \printfield{volume}%
          \printfield{part}%
        }
        {}%
    }%
  \newunit
  \printfield{edition}%
  \newunit
}

\renewbibmacro*{note+pages}{%
  \printfield{note}%
  \setunit{\bibpagespunct}%
  \printfield{pages}%
  \iffieldundef{pages}
    {%
      \printfield{doi}%
      \clearfield{doi}%
    }%
    {}%
}

\renewbibmacro*{publisher+location+date}{%
  \printlist{publisher}%
  \newunit
  \printlist{location}%
  \newunit
  \usebibmacro{date}%
  \newunit
}

\newbibmacro*{related:translatedas}[1]{%
  \entrydata{#1}{%
    \usebibmacro{journal+issuetitle}%
    \newunit
    \usebibmacro{byeditor+others}%
    \newunit
    \usebibmacro{note+pages}%
    \newunit\newblock
    \iftoggle{bbx:isbn}
      {\printfield{issn}}
      {}%
    \newunit\newblock
    \usebibmacro{doi+eprint+url}%
  }%
}

% Specialised functions for manipulating data, where the biblatex
% kernel does not provide a suitable function.
\newcommand*\mkbibnocomma[1]{%
  \in@{,}{#1}%
  \ifin@
    \expandafter\bbx@no@comma
  \else
    \expandafter\@firstofone
  \fi
    {#1}%
}
\newcommand*\bbx@no@comma[1]{%
  \bbx@no@comma@aux#1,\@nil,%
}
\newcommand\bbx@no@comma@aux{}
\def\bbx@no@comma@aux#1,{%
  \ifx\@nil#1\else #1\expandafter\bbx@no@comma@aux\fi
}

% New bibliography drivers, using the required order of fields. These
% are mainly copied from standard.bbx then modified.
\DeclareBibliographyDriver{article}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/translator+others}%
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{title}%
  \newunit
  \usebibmacro{byauthor}%
  \newunit\newblock
  \usebibmacro{bytranslator+others}%
  \newunit\newblock
  \printfield{version}%
  \newunit\newblock
  \usebibmacro{journal+issuetitle}%
  \newunit
  \usebibmacro{byeditor+others}%
  \newunit
  \usebibmacro{note+pages}%
  \newunit\newblock
  \iftoggle{bbx:isbn}
    {\printfield{issn}}
    {}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}
  \newunit\newblock
  \usebibmacro{addendum+pubstate}%
  \setunit{\bibpagerefpunct}\newblock
  \usebibmacro{pageref}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{finentry}%
}

\DeclareBibliographyDriver{book}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/translator+others}%
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{maintitle+title}%
  \newunit
  \usebibmacro{byauthor}%
  \newunit\newblock
  \usebibmacro{byeditor+others}%
  \newunit\newblock
  \newunit
  \printfield{volumes}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{publisher+location+date}%
  \newunit\newblock
  \usebibmacro{chapter+pages}%
  \newunit
  \printfield{pagetotal}%
  \newunit\newblock
  \printfield{isbn}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \usebibmacro{addendum+pubstate}%
  \setunit{\bibpagerefpunct}\newblock
  \usebibmacro{pageref}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{finentry}%
}

\DeclareBibliographyDriver{collection}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{maintitle+title}%
  \newunit
  \usebibmacro{byeditor+others}%
  \newunit\newblock
  \printfield{volumes}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{publisher+location+date}%
  \newunit\newblock
  \usebibmacro{chapter+pages}%
  \newunit
  \printfield{pagetotal}%
  \newunit\newblock
  \printfield{isbn}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \usebibmacro{addendum+pubstate}%
  \setunit{\bibpagerefpunct}\newblock
  \usebibmacro{pageref}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{finentry}%
}

\DeclareBibliographyDriver{inbook}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/translator+others}%
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{byauthor}%
  \newunit
  \usebibmacro{title}%
  \setunit{\addspace}%
  \usebibmacro{in:}%
  \usebibmacro{bybookauthor}%
  \setunit*{\newunitpunct}\newblock
  \usebibmacro{maintitle+booktitle}%
  \newunit\newblock
  \usebibmacro{byeditor+others}%
  \newunit\newblock
  \printfield{volumes}%
  \newunit\newblock
  \usebibmacro{series+number}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{publisher+location+date}%
  \newunit\newblock
  \usebibmacro{chapter+pages}%
  \newunit\newblock
  \printfield{isbn}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \usebibmacro{addendum+pubstate}%
  \setunit{\bibpagerefpunct}\newblock
  \usebibmacro{pageref}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{finentry}%
}

\DeclareBibliographyDriver{incollection}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/translator+others}%
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{byauthor}%
  \newunit
  \usebibmacro{title}%
  \setunit{\addspace}%
  \usebibmacro{in:}%
  \usebibmacro{maintitle+booktitle}%
  \newunit\newblock
  \usebibmacro{byeditor+others}%
  \newunit\newblock
  \printfield{volumes}%
  \newunit\newblock
  \usebibmacro{series+number}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{publisher+location+date}%
  \newunit\newblock
  \usebibmacro{chapter+pages}%
  \newunit\newblock
  \printfield{isbn}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \usebibmacro{addendum+pubstate}%
  \setunit{\bibpagerefpunct}\newblock
  \usebibmacro{pageref}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{finentry}%
}

\DeclareBibliographyDriver{inproceedings}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/translator+others}%
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{byauthor}%
  \setunit{\addspace}%
  \usebibmacro{in:}%
  \usebibmacro{maintitle+booktitle}%
  \newunit\newblock
  \usebibmacro{event+venue+date}%
  \newunit\newblock
  \usebibmacro{byeditor+others}%
  \newunit\newblock
  \printfield{volumes}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \printlist{organization}%
  \newunit
  \usebibmacro{publisher+location+date}%
  \newunit\newblock
  \usebibmacro{chapter+pages}%
  \newunit\newblock
  \printfield{isbn}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \usebibmacro{addendum+pubstate}%
  \setunit{\bibpagerefpunct}\newblock
  \usebibmacro{pageref}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{finentry}%
}

\DeclareBibliographyDriver{manual}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author/editor}%
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{title}%
  \newunit
  \usebibmacro{byauthor}%
  \newunit\newblock
  \usebibmacro{byeditor}%
  \newunit\newblock
  \usebibmacro{series+number}%
  \newunit\newblock
  \printfield{type}%
  \newunit
  \printfield{version}%
  \newunit
  \printfield{note}%
  \newunit\newblock
  \printlist{organization}%
  \newunit
  \usebibmacro{publisher+location+date}%
  \newunit\newblock
  \usebibmacro{chapter+pages}%
  \newunit
  \printfield{pagetotal}%
  \newunit\newblock
  \printfield{isbn}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \usebibmacro{addendum+pubstate}%
  \setunit{\bibpagerefpunct}\newblock
  \usebibmacro{pageref}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{finentry}%
}

\DeclareBibliographyDriver{patent}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author}%
  \setunit*{\addspace}%
  \usebibmacro{byholder}%
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{byauthor}%
  \newunit\newblock
  \printfield{type}%
  \newunit
  \printfield{number}%
  \iflistundef{location}
    {}
    {\setunit*{\addspace}%
     \printtext[parens]{%
       \printlist[][-\value{listtotal}]{location}}}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \usebibmacro{date}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \usebibmacro{addendum+pubstate}%
  \setunit{\bibpagerefpunct}\newblock
  \usebibmacro{pageref}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{finentry}%
}

\DeclareBibliographyDriver{thesis}{%
  \usebibmacro{bibindex}%
  \usebibmacro{begentry}%
  \usebibmacro{author}%
  \setunit{\labelnamepunct}\newblock
  \usebibmacro{title}%
  \newunit
  \usebibmacro{byauthor}%
  \newunit\newblock
  \printfield{note}%
  \newunit\newblock
  \printfield{type}%
  \newunit
  \usebibmacro{institution+location+date}%
  \newunit\newblock
  \usebibmacro{chapter+pages}%
  \newunit
  \printfield{pagetotal}%
  \newunit\newblock
  \printfield{isbn}%
  \newunit\newblock
  \usebibmacro{doi+eprint+url}%
  \newunit\newblock
  \usebibmacro{addendum+pubstate}%
  \setunit{\bibpagerefpunct}\newblock
  \usebibmacro{pageref}%
  \newunit\newblock
  \iftoggle{bbx:related}
    {\usebibmacro{related:init}%
     \usebibmacro{related}}
    {}%
  \usebibmacro{finentry}%
}

%%
%% Copyright (C) 2010-2014,2016-2019 by
%%   Joseph Wright <joseph.wright@morningstar2.co.uk>
%%
%% It may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License (LPPL), either version 1.3c of
%% this license or (at your option) any later version.  The latest
%% version of this license is in the file:
%%
%%    http://www.latex-project.org/lppl.txt
%%
%% This work is "maintained" (as per LPPL maintenance status) by
%%   Joseph Wright.
%%
%% This work consists of the files biblatex-chem.bib,
%%                                 biblatex-chem.tex,
%%                                 biblatex-chem-acs.tex,
%%                                 biblatex-chem-angew.tex,
%%                                 biblatex-chem-biochem.tex,
%%                                 biblatex-chem-rsc.tex,
%%                                 chem-acs.bbx,
%%                                 chem-acs.cbx,
%%                                 chem-angew.bbx,
%%                                 chem-angew.cbx,
%%                                 chem-biochem.bbx,
%%                                 chem-biochem.cbx,
%%                                 chem-rsc.bbx and
%%                                 chem-rsc.cbx,
%%           and the derived files biblatex-chem.pdf,
%%                                 biblatex-chem-acs.pdf,
%%                                 biblatex-chem-angew.pdf,
%%                                 biblatex-chem-biochem.pdf and
%%                                 biblatex-chem-rsc.pdf.
%%
%%
%% End of file `chem-angew.bbx'.
