How can I create entirely new data types with BibLaTeX/Biber?











up vote
30
down vote

favorite
22












The title is pretty self-explanatory.
I need something similar to Add new field to biblatex entries, but for completely new entry types.
(IIUC, the linked just shuffles one type onto another.)



I'm trying to define a @stackexchange type where I can cite StackExchange questions:



@stackexchange{se:l3help,
sitekey = {tex},
% sitetopic = {TeX, LaTeX, ConTeX t, and related typesetting systems},
askid = {45838},
title = {What can textit{I} do to help the LaTeX3 Project},
askdate = {2012/02/26}
}


where id must match the regexp [0-9]+, asked is a date, and sitekey provides a hard-coded mapping to the appropriate sitetopic when available.
If sitekey isn't available, then sitetopic is required. If sitetopic is supplied, then it overwrites the mapping from sitekey. id, title, and asked are all required.



If there are any other fields you think should be included, feel free; I tried to pare down the data type only to the 'special' ones. I would hope that this Q can cover most of the bases for different ways fields can act.



MWE (Adapted per comments)



begin{filecontents}{references.bib}
@stackexchange{se:l3help,
sitetopic = {tex},
title = {What can textit{I} do to help the LaTeX3 Project},
askdate = {2012/02/26},
askid = {45838},
askp = {Brent Longorough},
askerid = 344,
ansp = {Frank Mittelbach},
anspid = 10109,
ansdate = {2012/03/01},
ansid = 46427
}
end{filecontents}
begin{filecontents}{stackexchange.dbx}
DeclareDatamodelEntrytypes{stackexchange}
DeclareDatamodelFields{
sitekey,
sitetopic,
askdate,
askedit,
askid,
askp,
askpid,
ansp,
anspid,
ansid,
ansdate,
ansedit}
DeclareDatamodelEntryfields[stackexchange]{
sitekey,
sitetopic,
askdate,
askedit,
askid,
askp,
askpid,
ansp,
anspid,
ansid,
ansdate,
ansedit,
title}
end{filecontents}
documentclass{article}

usepackage{csquotes}
usepackage[datamodel=stackexchange,backend=biber]{biblatex}
addbibresource{references.bib}

newbibmacro*{stackexchangequestion}{%
mkbibquote{printfield{title}}
mkbibparens{printfield{askid}}
addperiod
}

newbibmacro*{stackexchangeask}{%
Asked mkbibdatelong{printfield{askdate}}%
iffieldundef{askedit}{}{ mkbibparens{edited mkbibdatelong{printfield{askedit}}}}%
by
printfield{askp} mkbibparens{printfield{askpid}}
addperiod
}

newbibmacro*{stackexchangeans}{%
Answered mkbibdatelong{printfield{ansdate}}%
iffieldundef{ansedit}{}{ mkbibparens{edited mkbibdatelong{printfield{ansedit}}}}%
by
printfield{ansp} mkbibparens{printfield{anspid}}
addperiod
}

newbibmacro*{stackexchangesite}{%
printfield{sitetopic}}

DeclareBibliographyDriver{stackexchange}{%
usebibmacro{bibindex}%
usebibmacro{begentry}%
usebibmacro{stackexchangequestion}%
usebibmacro{stackexchangeask}%
usebibmacro{stackexchangeans}%
usebibmacro{stackexchangesite}%
usebibmacro{finentry}}

begin{document}

cite{se:l3help}

printbibliography
end{document}


Throws a Missing endcsname error, likely caused by misuse of usebibmacro.










share|improve this question
























  • I think you want to look at this answer. It has a simple example of how to use DeclareDatamodel* commands, which you would then pair with newbibmacros and so forth.
    – jon
    May 7 '14 at 5:13










  • What exactly would you like an answer to this question look like that the answer jon linked cannot give you? (Here, we did not define a new entry type, but heavily modified an existing one; that might also be of interest to you.)
    – moewe
    May 7 '14 at 5:59










  • @jon I'll take a look at those and see if I can get a working solution. Thanks for the links :)
    – Sean Allred
    May 7 '14 at 14:51










  • @moewe To be perfectly honest I never found the linked questions. I'm going to try and get it working with this new information; hopefully nothing has changed with the way BibLaTeX/Biber does things in two years.
    – Sean Allred
    May 7 '14 at 14:52










  • @All: See edits.
    – Sean Allred
    May 7 '14 at 15:42















up vote
30
down vote

favorite
22












The title is pretty self-explanatory.
I need something similar to Add new field to biblatex entries, but for completely new entry types.
(IIUC, the linked just shuffles one type onto another.)



I'm trying to define a @stackexchange type where I can cite StackExchange questions:



@stackexchange{se:l3help,
sitekey = {tex},
% sitetopic = {TeX, LaTeX, ConTeX t, and related typesetting systems},
askid = {45838},
title = {What can textit{I} do to help the LaTeX3 Project},
askdate = {2012/02/26}
}


where id must match the regexp [0-9]+, asked is a date, and sitekey provides a hard-coded mapping to the appropriate sitetopic when available.
If sitekey isn't available, then sitetopic is required. If sitetopic is supplied, then it overwrites the mapping from sitekey. id, title, and asked are all required.



If there are any other fields you think should be included, feel free; I tried to pare down the data type only to the 'special' ones. I would hope that this Q can cover most of the bases for different ways fields can act.



MWE (Adapted per comments)



begin{filecontents}{references.bib}
@stackexchange{se:l3help,
sitetopic = {tex},
title = {What can textit{I} do to help the LaTeX3 Project},
askdate = {2012/02/26},
askid = {45838},
askp = {Brent Longorough},
askerid = 344,
ansp = {Frank Mittelbach},
anspid = 10109,
ansdate = {2012/03/01},
ansid = 46427
}
end{filecontents}
begin{filecontents}{stackexchange.dbx}
DeclareDatamodelEntrytypes{stackexchange}
DeclareDatamodelFields{
sitekey,
sitetopic,
askdate,
askedit,
askid,
askp,
askpid,
ansp,
anspid,
ansid,
ansdate,
ansedit}
DeclareDatamodelEntryfields[stackexchange]{
sitekey,
sitetopic,
askdate,
askedit,
askid,
askp,
askpid,
ansp,
anspid,
ansid,
ansdate,
ansedit,
title}
end{filecontents}
documentclass{article}

usepackage{csquotes}
usepackage[datamodel=stackexchange,backend=biber]{biblatex}
addbibresource{references.bib}

newbibmacro*{stackexchangequestion}{%
mkbibquote{printfield{title}}
mkbibparens{printfield{askid}}
addperiod
}

newbibmacro*{stackexchangeask}{%
Asked mkbibdatelong{printfield{askdate}}%
iffieldundef{askedit}{}{ mkbibparens{edited mkbibdatelong{printfield{askedit}}}}%
by
printfield{askp} mkbibparens{printfield{askpid}}
addperiod
}

newbibmacro*{stackexchangeans}{%
Answered mkbibdatelong{printfield{ansdate}}%
iffieldundef{ansedit}{}{ mkbibparens{edited mkbibdatelong{printfield{ansedit}}}}%
by
printfield{ansp} mkbibparens{printfield{anspid}}
addperiod
}

newbibmacro*{stackexchangesite}{%
printfield{sitetopic}}

DeclareBibliographyDriver{stackexchange}{%
usebibmacro{bibindex}%
usebibmacro{begentry}%
usebibmacro{stackexchangequestion}%
usebibmacro{stackexchangeask}%
usebibmacro{stackexchangeans}%
usebibmacro{stackexchangesite}%
usebibmacro{finentry}}

begin{document}

cite{se:l3help}

printbibliography
end{document}


Throws a Missing endcsname error, likely caused by misuse of usebibmacro.










share|improve this question
























  • I think you want to look at this answer. It has a simple example of how to use DeclareDatamodel* commands, which you would then pair with newbibmacros and so forth.
    – jon
    May 7 '14 at 5:13










  • What exactly would you like an answer to this question look like that the answer jon linked cannot give you? (Here, we did not define a new entry type, but heavily modified an existing one; that might also be of interest to you.)
    – moewe
    May 7 '14 at 5:59










  • @jon I'll take a look at those and see if I can get a working solution. Thanks for the links :)
    – Sean Allred
    May 7 '14 at 14:51










  • @moewe To be perfectly honest I never found the linked questions. I'm going to try and get it working with this new information; hopefully nothing has changed with the way BibLaTeX/Biber does things in two years.
    – Sean Allred
    May 7 '14 at 14:52










  • @All: See edits.
    – Sean Allred
    May 7 '14 at 15:42













up vote
30
down vote

favorite
22









up vote
30
down vote

favorite
22






22





The title is pretty self-explanatory.
I need something similar to Add new field to biblatex entries, but for completely new entry types.
(IIUC, the linked just shuffles one type onto another.)



I'm trying to define a @stackexchange type where I can cite StackExchange questions:



@stackexchange{se:l3help,
sitekey = {tex},
% sitetopic = {TeX, LaTeX, ConTeX t, and related typesetting systems},
askid = {45838},
title = {What can textit{I} do to help the LaTeX3 Project},
askdate = {2012/02/26}
}


where id must match the regexp [0-9]+, asked is a date, and sitekey provides a hard-coded mapping to the appropriate sitetopic when available.
If sitekey isn't available, then sitetopic is required. If sitetopic is supplied, then it overwrites the mapping from sitekey. id, title, and asked are all required.



If there are any other fields you think should be included, feel free; I tried to pare down the data type only to the 'special' ones. I would hope that this Q can cover most of the bases for different ways fields can act.



MWE (Adapted per comments)



begin{filecontents}{references.bib}
@stackexchange{se:l3help,
sitetopic = {tex},
title = {What can textit{I} do to help the LaTeX3 Project},
askdate = {2012/02/26},
askid = {45838},
askp = {Brent Longorough},
askerid = 344,
ansp = {Frank Mittelbach},
anspid = 10109,
ansdate = {2012/03/01},
ansid = 46427
}
end{filecontents}
begin{filecontents}{stackexchange.dbx}
DeclareDatamodelEntrytypes{stackexchange}
DeclareDatamodelFields{
sitekey,
sitetopic,
askdate,
askedit,
askid,
askp,
askpid,
ansp,
anspid,
ansid,
ansdate,
ansedit}
DeclareDatamodelEntryfields[stackexchange]{
sitekey,
sitetopic,
askdate,
askedit,
askid,
askp,
askpid,
ansp,
anspid,
ansid,
ansdate,
ansedit,
title}
end{filecontents}
documentclass{article}

usepackage{csquotes}
usepackage[datamodel=stackexchange,backend=biber]{biblatex}
addbibresource{references.bib}

newbibmacro*{stackexchangequestion}{%
mkbibquote{printfield{title}}
mkbibparens{printfield{askid}}
addperiod
}

newbibmacro*{stackexchangeask}{%
Asked mkbibdatelong{printfield{askdate}}%
iffieldundef{askedit}{}{ mkbibparens{edited mkbibdatelong{printfield{askedit}}}}%
by
printfield{askp} mkbibparens{printfield{askpid}}
addperiod
}

newbibmacro*{stackexchangeans}{%
Answered mkbibdatelong{printfield{ansdate}}%
iffieldundef{ansedit}{}{ mkbibparens{edited mkbibdatelong{printfield{ansedit}}}}%
by
printfield{ansp} mkbibparens{printfield{anspid}}
addperiod
}

newbibmacro*{stackexchangesite}{%
printfield{sitetopic}}

DeclareBibliographyDriver{stackexchange}{%
usebibmacro{bibindex}%
usebibmacro{begentry}%
usebibmacro{stackexchangequestion}%
usebibmacro{stackexchangeask}%
usebibmacro{stackexchangeans}%
usebibmacro{stackexchangesite}%
usebibmacro{finentry}}

begin{document}

cite{se:l3help}

printbibliography
end{document}


Throws a Missing endcsname error, likely caused by misuse of usebibmacro.










share|improve this question















The title is pretty self-explanatory.
I need something similar to Add new field to biblatex entries, but for completely new entry types.
(IIUC, the linked just shuffles one type onto another.)



I'm trying to define a @stackexchange type where I can cite StackExchange questions:



@stackexchange{se:l3help,
sitekey = {tex},
% sitetopic = {TeX, LaTeX, ConTeX t, and related typesetting systems},
askid = {45838},
title = {What can textit{I} do to help the LaTeX3 Project},
askdate = {2012/02/26}
}


where id must match the regexp [0-9]+, asked is a date, and sitekey provides a hard-coded mapping to the appropriate sitetopic when available.
If sitekey isn't available, then sitetopic is required. If sitetopic is supplied, then it overwrites the mapping from sitekey. id, title, and asked are all required.



If there are any other fields you think should be included, feel free; I tried to pare down the data type only to the 'special' ones. I would hope that this Q can cover most of the bases for different ways fields can act.



MWE (Adapted per comments)



begin{filecontents}{references.bib}
@stackexchange{se:l3help,
sitetopic = {tex},
title = {What can textit{I} do to help the LaTeX3 Project},
askdate = {2012/02/26},
askid = {45838},
askp = {Brent Longorough},
askerid = 344,
ansp = {Frank Mittelbach},
anspid = 10109,
ansdate = {2012/03/01},
ansid = 46427
}
end{filecontents}
begin{filecontents}{stackexchange.dbx}
DeclareDatamodelEntrytypes{stackexchange}
DeclareDatamodelFields{
sitekey,
sitetopic,
askdate,
askedit,
askid,
askp,
askpid,
ansp,
anspid,
ansid,
ansdate,
ansedit}
DeclareDatamodelEntryfields[stackexchange]{
sitekey,
sitetopic,
askdate,
askedit,
askid,
askp,
askpid,
ansp,
anspid,
ansid,
ansdate,
ansedit,
title}
end{filecontents}
documentclass{article}

usepackage{csquotes}
usepackage[datamodel=stackexchange,backend=biber]{biblatex}
addbibresource{references.bib}

newbibmacro*{stackexchangequestion}{%
mkbibquote{printfield{title}}
mkbibparens{printfield{askid}}
addperiod
}

newbibmacro*{stackexchangeask}{%
Asked mkbibdatelong{printfield{askdate}}%
iffieldundef{askedit}{}{ mkbibparens{edited mkbibdatelong{printfield{askedit}}}}%
by
printfield{askp} mkbibparens{printfield{askpid}}
addperiod
}

newbibmacro*{stackexchangeans}{%
Answered mkbibdatelong{printfield{ansdate}}%
iffieldundef{ansedit}{}{ mkbibparens{edited mkbibdatelong{printfield{ansedit}}}}%
by
printfield{ansp} mkbibparens{printfield{anspid}}
addperiod
}

newbibmacro*{stackexchangesite}{%
printfield{sitetopic}}

DeclareBibliographyDriver{stackexchange}{%
usebibmacro{bibindex}%
usebibmacro{begentry}%
usebibmacro{stackexchangequestion}%
usebibmacro{stackexchangeask}%
usebibmacro{stackexchangeans}%
usebibmacro{stackexchangesite}%
usebibmacro{finentry}}

begin{document}

cite{se:l3help}

printbibliography
end{document}


Throws a Missing endcsname error, likely caused by misuse of usebibmacro.







biblatex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 13 '17 at 12:35









Community

1




1










asked May 7 '14 at 4:50









Sean Allred

17.8k658196




17.8k658196












  • I think you want to look at this answer. It has a simple example of how to use DeclareDatamodel* commands, which you would then pair with newbibmacros and so forth.
    – jon
    May 7 '14 at 5:13










  • What exactly would you like an answer to this question look like that the answer jon linked cannot give you? (Here, we did not define a new entry type, but heavily modified an existing one; that might also be of interest to you.)
    – moewe
    May 7 '14 at 5:59










  • @jon I'll take a look at those and see if I can get a working solution. Thanks for the links :)
    – Sean Allred
    May 7 '14 at 14:51










  • @moewe To be perfectly honest I never found the linked questions. I'm going to try and get it working with this new information; hopefully nothing has changed with the way BibLaTeX/Biber does things in two years.
    – Sean Allred
    May 7 '14 at 14:52










  • @All: See edits.
    – Sean Allred
    May 7 '14 at 15:42


















  • I think you want to look at this answer. It has a simple example of how to use DeclareDatamodel* commands, which you would then pair with newbibmacros and so forth.
    – jon
    May 7 '14 at 5:13










  • What exactly would you like an answer to this question look like that the answer jon linked cannot give you? (Here, we did not define a new entry type, but heavily modified an existing one; that might also be of interest to you.)
    – moewe
    May 7 '14 at 5:59










  • @jon I'll take a look at those and see if I can get a working solution. Thanks for the links :)
    – Sean Allred
    May 7 '14 at 14:51










  • @moewe To be perfectly honest I never found the linked questions. I'm going to try and get it working with this new information; hopefully nothing has changed with the way BibLaTeX/Biber does things in two years.
    – Sean Allred
    May 7 '14 at 14:52










  • @All: See edits.
    – Sean Allred
    May 7 '14 at 15:42
















I think you want to look at this answer. It has a simple example of how to use DeclareDatamodel* commands, which you would then pair with newbibmacros and so forth.
– jon
May 7 '14 at 5:13




I think you want to look at this answer. It has a simple example of how to use DeclareDatamodel* commands, which you would then pair with newbibmacros and so forth.
– jon
May 7 '14 at 5:13












What exactly would you like an answer to this question look like that the answer jon linked cannot give you? (Here, we did not define a new entry type, but heavily modified an existing one; that might also be of interest to you.)
– moewe
May 7 '14 at 5:59




What exactly would you like an answer to this question look like that the answer jon linked cannot give you? (Here, we did not define a new entry type, but heavily modified an existing one; that might also be of interest to you.)
– moewe
May 7 '14 at 5:59












@jon I'll take a look at those and see if I can get a working solution. Thanks for the links :)
– Sean Allred
May 7 '14 at 14:51




@jon I'll take a look at those and see if I can get a working solution. Thanks for the links :)
– Sean Allred
May 7 '14 at 14:51












@moewe To be perfectly honest I never found the linked questions. I'm going to try and get it working with this new information; hopefully nothing has changed with the way BibLaTeX/Biber does things in two years.
– Sean Allred
May 7 '14 at 14:52




@moewe To be perfectly honest I never found the linked questions. I'm going to try and get it working with this new information; hopefully nothing has changed with the way BibLaTeX/Biber does things in two years.
– Sean Allred
May 7 '14 at 14:52












@All: See edits.
– Sean Allred
May 7 '14 at 15:42




@All: See edits.
– Sean Allred
May 7 '14 at 15:42










1 Answer
1






active

oldest

votes

















up vote
40
down vote



accepted
+100










The answer is now updated for the date handling in newer biblatex versions >= 3.6, lots of what had to be done manually before will now be done automatically. Check the edit history for earlier versions.



This is quite a full-on question, but I will try to answer it anyway.



Declare new fields, the new entry types and its fields



First, when declaring the data model, we need to make sure biblatex actually knows the type of the fields/lists we declare (this is done via the optional argument to DeclareDatamodelFields). You can read much more about this in §4.5.3 Data Model Specification, p. 151 of the biblatex documentation.



There are two main types for entry field: field and list. fields usually hold one value, while lists can hold more items separated by an and in the .bib file (see also §2.2.1 Data Types, p. 13).



I declared the sitekey and sitetopic to be literal fields (such as the title field).
askp and ansp are clearly name lists (even though in this context the list will probably only ever hold one item).
A lot of the fields you are asking for seem to revolve about dates, so there are askdate, ansdate, askeditdate, anseditdate and their dateparts (which will be created automatically, but they still need to be added to DeclareDatamodelEntryfields manually).
Finally, I decided the ids should be verbatim fields.



begin{filecontents}{stackexchange.dbx}
DeclareDatamodelEntrytypes{stackexchange}
DeclareDatamodelFields[type=field,datatype=literal]{
sitekey,
sitetopic,
}
DeclareDatamodelFields[type=list,datatype=name]{
askp,
ansp,
}
DeclareDatamodelFields[type=field, datatype=date, skipout]{
askdate,
ansdate,
askeditdate,
anseditdate}
DeclareDatamodelFields[type=field, datatype=verbatim]{
askid,
askpid,
anspid,
ansid,
}
DeclareDatamodelEntryfields[stackexchange]{
sitekey,
sitetopic,
askid,
askp,
askpid,
ansp,
anspid,
ansid,
askyear,
askmonth,
askday,
ansyear,
ansmonth,
ansday,
askedityear,
askeditmonth,
askeditday,
ansedityear,
anseditmonth,
anseditday,
title}
end{filecontents}


The DeclareDatamodelEntryfields just makes those fields relevant to @stackexchange known to that entry type.



Declare new bibliography strings (if necessary)



We also want to display words like 'asked' and 'answered', instead of hard-coding them, localisation with .lbx files is the way to go. This is only necessary if you wish to include strings in the output. It is not necessary to include a string for each field you define. The fact that the name of some bibstrings coincides with the name of some fields is purely accidental. There is no requirement to define bibstrings for certain field types.



The most complete way to define new strings is by means of an .lbx file, because that method allows you to define long and short forms of the strings. You can also define new strings directly in the preamble, but then you can only give one form that is used as short and long version.



Our new localisation file english-stack.lbx inherits everything from english.lbx but adds asked, answered and edited as bibstrings.



begin{filecontents*}{english-stack.lbx}
ProvidesFile{english-stack.lbx}[2014/05/07 english with additions for stackexchange]
InheritBibliographyExtras{english}
NewBibliographyString{asked,answered,edited}
DeclareBibliographyStrings{%
inherit = {english},
asked = {{asked}{asked}},
answered = {{answered}{answered}},
edited = {{edited}{edited}},
}
end{filecontents*}


We have to tell biblatex to use that language definition



DeclareLanguageMapping{english}{english-stack}


Defining the bibliography output



Then finally, we can go on to define the bibliography driver. The bibliography driver is the main definition that decides how the entry is printed in the bibliography. A bibliography driver typically consists of calls to several bibliography macros as well as calls to punctuation and printing commands.



Field formats



Field formats define the formatting of each printed field. We avoid hard-coding anything and use the macros provided by biblatex.



So instead of wrapping printfield{title} in mkbibquote, we define the field format to do so.
We use setunit for punctuation and bibstrings in lieu of hard-coded words.



DeclareFieldFormat[stackexchange]{title}{mkbibquote{#1isdot}}
DeclareFieldFormat{askpid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{anspid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{askid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/q/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{ansid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/a/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}


As an added bonus, the id fields now link to the proper stackexchange site.



newbibmacro*{stackexchangequestion}{%
printfield{title}%
setunit{addspace}%
printfield{askid}%
}


Bibliography macros



Bibliography macros can be used to compartmentalise the output definitions.



No formatting should be applied to the printfield commands, formatting is dealt with by DeclareFieldFormat.



newbibmacro*{stackexchangeask}{%
bibstring{asked}%
setunit{addspace}%
printaskdate%
iffieldundef{askedityear}%
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printaskeditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{askp}%
setunit{addspace}%
printfield{askpid}%
}

newbibmacro*{stackexchangeans}{%
bibstring{answered}%
setunit{addspace}%
printansdate%
iffieldundef{ansedityear}
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printanseditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{ansp}%
setunit{addspace}%
printfield{anspid}%
}

newbibmacro*{stackexchangesite}{%
printfield{sitetopic}}


Bibliography driver



The driver defines the final output structure. Usually a driver makes heavy use of bibmacros, but it need not do that.



DeclareBibliographyDriver{stackexchange}{%
usebibmacro{bibindex}%
usebibmacro{begentry}%
usebibmacro{stackexchangequestion}%
newunitnewblock
usebibmacro{stackexchangeask}%
newunitnewblock
usebibmacro{stackexchangeans}%
newunitnewblock
usebibmacro{stackexchangesite}%
newunitnewblock
usebibmacro{finentry}}


MWE



RequirePackage{filecontents}
begin{filecontents*}{jobname.bib}
@stackexchange{se:l3help,
sitetopic = {tex},
title = {What can textit{I} do to help the LaTeX3 Project},
askdate = {2012-02-26},
askid = {45838},
askp = {Brent Longorough},
askpid = {344},
ansp = {Frank Mittelbach},
anspid = {10109},
ansdate = {2012-03-01},
ansid = {46427},
}
end{filecontents*}
begin{filecontents}{stackexchange.dbx}
DeclareDatamodelEntrytypes{stackexchange}
DeclareDatamodelFields[type=field,datatype=literal]{
sitekey,
sitetopic,
}
DeclareDatamodelFields[type=list,datatype=name]{
askp,
ansp,
}
DeclareDatamodelFields[type=field, datatype=date, skipout]{
askdate,
ansdate,
askeditdate,
anseditdate}
DeclareDatamodelFields[type=field, datatype=verbatim]{
askid,
askpid,
anspid,
ansid,
}
DeclareDatamodelEntryfields[stackexchange]{
sitekey,
sitetopic,
askid,
askp,
askpid,
ansp,
anspid,
ansid,
askyear,
askmonth,
askday,
ansyear,
ansmonth,
ansday,
askedityear,
askeditmonth,
askeditday,
ansedityear,
anseditmonth,
anseditday,
title}
end{filecontents}
documentclass[english]{article}
usepackage{babel}
usepackage{csquotes}
usepackage[datamodel=stackexchange,backend=biber]{biblatex}
usepackage{hyperref}
addbibresource{jobname.bib}

begin{filecontents*}{english-stack.lbx}
ProvidesFile{english-stack.lbx}[2014/05/07 english with additions for stackexchange]
InheritBibliographyExtras{english}
NewBibliographyString{asked,answered,edited}
DeclareBibliographyStrings{%
inherit = {english},
asked = {{asked}{asked}},
answered = {{answered}{answered}},
edited = {{edited}{edited}},
}
end{filecontents*}
DeclareLanguageMapping{english}{english-stack}

DeclareFieldFormat[stackexchange]{title}{mkbibquote{#1isdot}}
DeclareFieldFormat{askpid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{anspid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{askid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/q/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{ansid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/a/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

newbibmacro*{stackexchangequestion}{%
printfield{title}%
setunit{addspace}%
printfield{askid}%
}

newbibmacro*{stackexchangeask}{%
bibstring{asked}%
setunit{addspace}%
printaskdate%
iffieldundef{askedityear}%
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printaskeditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{askp}%
setunit{addspace}%
printfield{askpid}%
}

newbibmacro*{stackexchangeans}{%
bibstring{answered}%
setunit{addspace}%
printansdate%
iffieldundef{ansedityear}
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printanseditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{ansp}%
setunit{addspace}%
printfield{anspid}%
}

newbibmacro*{stackexchangesite}{%
printfield{sitetopic}}

DeclareBibliographyDriver{stackexchange}{%
usebibmacro{bibindex}%
usebibmacro{begentry}%
usebibmacro{stackexchangequestion}%
newunitnewblock
usebibmacro{stackexchangeask}%
newunitnewblock
usebibmacro{stackexchangeans}%
newunitnewblock
usebibmacro{stackexchangesite}%
newunitnewblock
usebibmacro{finentry}}

begin{document}
nocite{*}
cite{se:l3help}

printbibliography
end{document}


enter image description here





To allow the use of askdate as labeldate, so the authoryear style (and other similar ones) does not print 'n.d.', we can use DeclareLabeldate (§4.5.8 Special Fields, p. 176) as follows



DeclareLabeldate{%
field{date}
field{eventdate}
field{origdate}
field{askdate}
field{urldate}
literal{nodate}
}


There also is DeclareLabelname (p. 176) with a similar syntax, if you want to see either askp or ansp as the author in authoryear/authortitle styles.






share|improve this answer



















  • 6




    Very nice, and very complete answer (I knew I should refrain). I would've had to steal the code too; but it probably would've taken me much longer to get everything in place.
    – jon
    May 7 '14 at 18:18






  • 6




    This is an absolutely fantastic answer, so first and foremost I want to thank you. That said, when using an authoryear style, the citation engine determines that there is no date: ... Project n.d.. Is there a way to map the askdate onto this field (which I'm presuming would be date)?
    – Sean Allred
    May 8 '14 at 3:08












  • @SeanAllred Have a look at the edited answer. With biblatex we can explicitly choose which date fields are considered for the labeldate with DeclareLabeldate.
    – moewe
    May 8 '14 at 5:15






  • 1




    I will look into auto-generation of the internals for all datamodel date fields in biblatex 3.4.
    – PLK
    Mar 6 '16 at 21:32






  • 2




    biblatex 3.4/biber 2.5 now automatically generates the internals (code between makeatletter/makeatother above) for all date fields defined in the data model so you won't need to do this part any more.
    – PLK
    Mar 7 '16 at 22:41













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f175776%2fhow-can-i-create-entirely-new-data-types-with-biblatex-biber%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
40
down vote



accepted
+100










The answer is now updated for the date handling in newer biblatex versions >= 3.6, lots of what had to be done manually before will now be done automatically. Check the edit history for earlier versions.



This is quite a full-on question, but I will try to answer it anyway.



Declare new fields, the new entry types and its fields



First, when declaring the data model, we need to make sure biblatex actually knows the type of the fields/lists we declare (this is done via the optional argument to DeclareDatamodelFields). You can read much more about this in §4.5.3 Data Model Specification, p. 151 of the biblatex documentation.



There are two main types for entry field: field and list. fields usually hold one value, while lists can hold more items separated by an and in the .bib file (see also §2.2.1 Data Types, p. 13).



I declared the sitekey and sitetopic to be literal fields (such as the title field).
askp and ansp are clearly name lists (even though in this context the list will probably only ever hold one item).
A lot of the fields you are asking for seem to revolve about dates, so there are askdate, ansdate, askeditdate, anseditdate and their dateparts (which will be created automatically, but they still need to be added to DeclareDatamodelEntryfields manually).
Finally, I decided the ids should be verbatim fields.



begin{filecontents}{stackexchange.dbx}
DeclareDatamodelEntrytypes{stackexchange}
DeclareDatamodelFields[type=field,datatype=literal]{
sitekey,
sitetopic,
}
DeclareDatamodelFields[type=list,datatype=name]{
askp,
ansp,
}
DeclareDatamodelFields[type=field, datatype=date, skipout]{
askdate,
ansdate,
askeditdate,
anseditdate}
DeclareDatamodelFields[type=field, datatype=verbatim]{
askid,
askpid,
anspid,
ansid,
}
DeclareDatamodelEntryfields[stackexchange]{
sitekey,
sitetopic,
askid,
askp,
askpid,
ansp,
anspid,
ansid,
askyear,
askmonth,
askday,
ansyear,
ansmonth,
ansday,
askedityear,
askeditmonth,
askeditday,
ansedityear,
anseditmonth,
anseditday,
title}
end{filecontents}


The DeclareDatamodelEntryfields just makes those fields relevant to @stackexchange known to that entry type.



Declare new bibliography strings (if necessary)



We also want to display words like 'asked' and 'answered', instead of hard-coding them, localisation with .lbx files is the way to go. This is only necessary if you wish to include strings in the output. It is not necessary to include a string for each field you define. The fact that the name of some bibstrings coincides with the name of some fields is purely accidental. There is no requirement to define bibstrings for certain field types.



The most complete way to define new strings is by means of an .lbx file, because that method allows you to define long and short forms of the strings. You can also define new strings directly in the preamble, but then you can only give one form that is used as short and long version.



Our new localisation file english-stack.lbx inherits everything from english.lbx but adds asked, answered and edited as bibstrings.



begin{filecontents*}{english-stack.lbx}
ProvidesFile{english-stack.lbx}[2014/05/07 english with additions for stackexchange]
InheritBibliographyExtras{english}
NewBibliographyString{asked,answered,edited}
DeclareBibliographyStrings{%
inherit = {english},
asked = {{asked}{asked}},
answered = {{answered}{answered}},
edited = {{edited}{edited}},
}
end{filecontents*}


We have to tell biblatex to use that language definition



DeclareLanguageMapping{english}{english-stack}


Defining the bibliography output



Then finally, we can go on to define the bibliography driver. The bibliography driver is the main definition that decides how the entry is printed in the bibliography. A bibliography driver typically consists of calls to several bibliography macros as well as calls to punctuation and printing commands.



Field formats



Field formats define the formatting of each printed field. We avoid hard-coding anything and use the macros provided by biblatex.



So instead of wrapping printfield{title} in mkbibquote, we define the field format to do so.
We use setunit for punctuation and bibstrings in lieu of hard-coded words.



DeclareFieldFormat[stackexchange]{title}{mkbibquote{#1isdot}}
DeclareFieldFormat{askpid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{anspid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{askid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/q/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{ansid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/a/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}


As an added bonus, the id fields now link to the proper stackexchange site.



newbibmacro*{stackexchangequestion}{%
printfield{title}%
setunit{addspace}%
printfield{askid}%
}


Bibliography macros



Bibliography macros can be used to compartmentalise the output definitions.



No formatting should be applied to the printfield commands, formatting is dealt with by DeclareFieldFormat.



newbibmacro*{stackexchangeask}{%
bibstring{asked}%
setunit{addspace}%
printaskdate%
iffieldundef{askedityear}%
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printaskeditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{askp}%
setunit{addspace}%
printfield{askpid}%
}

newbibmacro*{stackexchangeans}{%
bibstring{answered}%
setunit{addspace}%
printansdate%
iffieldundef{ansedityear}
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printanseditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{ansp}%
setunit{addspace}%
printfield{anspid}%
}

newbibmacro*{stackexchangesite}{%
printfield{sitetopic}}


Bibliography driver



The driver defines the final output structure. Usually a driver makes heavy use of bibmacros, but it need not do that.



DeclareBibliographyDriver{stackexchange}{%
usebibmacro{bibindex}%
usebibmacro{begentry}%
usebibmacro{stackexchangequestion}%
newunitnewblock
usebibmacro{stackexchangeask}%
newunitnewblock
usebibmacro{stackexchangeans}%
newunitnewblock
usebibmacro{stackexchangesite}%
newunitnewblock
usebibmacro{finentry}}


MWE



RequirePackage{filecontents}
begin{filecontents*}{jobname.bib}
@stackexchange{se:l3help,
sitetopic = {tex},
title = {What can textit{I} do to help the LaTeX3 Project},
askdate = {2012-02-26},
askid = {45838},
askp = {Brent Longorough},
askpid = {344},
ansp = {Frank Mittelbach},
anspid = {10109},
ansdate = {2012-03-01},
ansid = {46427},
}
end{filecontents*}
begin{filecontents}{stackexchange.dbx}
DeclareDatamodelEntrytypes{stackexchange}
DeclareDatamodelFields[type=field,datatype=literal]{
sitekey,
sitetopic,
}
DeclareDatamodelFields[type=list,datatype=name]{
askp,
ansp,
}
DeclareDatamodelFields[type=field, datatype=date, skipout]{
askdate,
ansdate,
askeditdate,
anseditdate}
DeclareDatamodelFields[type=field, datatype=verbatim]{
askid,
askpid,
anspid,
ansid,
}
DeclareDatamodelEntryfields[stackexchange]{
sitekey,
sitetopic,
askid,
askp,
askpid,
ansp,
anspid,
ansid,
askyear,
askmonth,
askday,
ansyear,
ansmonth,
ansday,
askedityear,
askeditmonth,
askeditday,
ansedityear,
anseditmonth,
anseditday,
title}
end{filecontents}
documentclass[english]{article}
usepackage{babel}
usepackage{csquotes}
usepackage[datamodel=stackexchange,backend=biber]{biblatex}
usepackage{hyperref}
addbibresource{jobname.bib}

begin{filecontents*}{english-stack.lbx}
ProvidesFile{english-stack.lbx}[2014/05/07 english with additions for stackexchange]
InheritBibliographyExtras{english}
NewBibliographyString{asked,answered,edited}
DeclareBibliographyStrings{%
inherit = {english},
asked = {{asked}{asked}},
answered = {{answered}{answered}},
edited = {{edited}{edited}},
}
end{filecontents*}
DeclareLanguageMapping{english}{english-stack}

DeclareFieldFormat[stackexchange]{title}{mkbibquote{#1isdot}}
DeclareFieldFormat{askpid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{anspid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{askid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/q/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{ansid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/a/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

newbibmacro*{stackexchangequestion}{%
printfield{title}%
setunit{addspace}%
printfield{askid}%
}

newbibmacro*{stackexchangeask}{%
bibstring{asked}%
setunit{addspace}%
printaskdate%
iffieldundef{askedityear}%
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printaskeditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{askp}%
setunit{addspace}%
printfield{askpid}%
}

newbibmacro*{stackexchangeans}{%
bibstring{answered}%
setunit{addspace}%
printansdate%
iffieldundef{ansedityear}
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printanseditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{ansp}%
setunit{addspace}%
printfield{anspid}%
}

newbibmacro*{stackexchangesite}{%
printfield{sitetopic}}

DeclareBibliographyDriver{stackexchange}{%
usebibmacro{bibindex}%
usebibmacro{begentry}%
usebibmacro{stackexchangequestion}%
newunitnewblock
usebibmacro{stackexchangeask}%
newunitnewblock
usebibmacro{stackexchangeans}%
newunitnewblock
usebibmacro{stackexchangesite}%
newunitnewblock
usebibmacro{finentry}}

begin{document}
nocite{*}
cite{se:l3help}

printbibliography
end{document}


enter image description here





To allow the use of askdate as labeldate, so the authoryear style (and other similar ones) does not print 'n.d.', we can use DeclareLabeldate (§4.5.8 Special Fields, p. 176) as follows



DeclareLabeldate{%
field{date}
field{eventdate}
field{origdate}
field{askdate}
field{urldate}
literal{nodate}
}


There also is DeclareLabelname (p. 176) with a similar syntax, if you want to see either askp or ansp as the author in authoryear/authortitle styles.






share|improve this answer



















  • 6




    Very nice, and very complete answer (I knew I should refrain). I would've had to steal the code too; but it probably would've taken me much longer to get everything in place.
    – jon
    May 7 '14 at 18:18






  • 6




    This is an absolutely fantastic answer, so first and foremost I want to thank you. That said, when using an authoryear style, the citation engine determines that there is no date: ... Project n.d.. Is there a way to map the askdate onto this field (which I'm presuming would be date)?
    – Sean Allred
    May 8 '14 at 3:08












  • @SeanAllred Have a look at the edited answer. With biblatex we can explicitly choose which date fields are considered for the labeldate with DeclareLabeldate.
    – moewe
    May 8 '14 at 5:15






  • 1




    I will look into auto-generation of the internals for all datamodel date fields in biblatex 3.4.
    – PLK
    Mar 6 '16 at 21:32






  • 2




    biblatex 3.4/biber 2.5 now automatically generates the internals (code between makeatletter/makeatother above) for all date fields defined in the data model so you won't need to do this part any more.
    – PLK
    Mar 7 '16 at 22:41

















up vote
40
down vote



accepted
+100










The answer is now updated for the date handling in newer biblatex versions >= 3.6, lots of what had to be done manually before will now be done automatically. Check the edit history for earlier versions.



This is quite a full-on question, but I will try to answer it anyway.



Declare new fields, the new entry types and its fields



First, when declaring the data model, we need to make sure biblatex actually knows the type of the fields/lists we declare (this is done via the optional argument to DeclareDatamodelFields). You can read much more about this in §4.5.3 Data Model Specification, p. 151 of the biblatex documentation.



There are two main types for entry field: field and list. fields usually hold one value, while lists can hold more items separated by an and in the .bib file (see also §2.2.1 Data Types, p. 13).



I declared the sitekey and sitetopic to be literal fields (such as the title field).
askp and ansp are clearly name lists (even though in this context the list will probably only ever hold one item).
A lot of the fields you are asking for seem to revolve about dates, so there are askdate, ansdate, askeditdate, anseditdate and their dateparts (which will be created automatically, but they still need to be added to DeclareDatamodelEntryfields manually).
Finally, I decided the ids should be verbatim fields.



begin{filecontents}{stackexchange.dbx}
DeclareDatamodelEntrytypes{stackexchange}
DeclareDatamodelFields[type=field,datatype=literal]{
sitekey,
sitetopic,
}
DeclareDatamodelFields[type=list,datatype=name]{
askp,
ansp,
}
DeclareDatamodelFields[type=field, datatype=date, skipout]{
askdate,
ansdate,
askeditdate,
anseditdate}
DeclareDatamodelFields[type=field, datatype=verbatim]{
askid,
askpid,
anspid,
ansid,
}
DeclareDatamodelEntryfields[stackexchange]{
sitekey,
sitetopic,
askid,
askp,
askpid,
ansp,
anspid,
ansid,
askyear,
askmonth,
askday,
ansyear,
ansmonth,
ansday,
askedityear,
askeditmonth,
askeditday,
ansedityear,
anseditmonth,
anseditday,
title}
end{filecontents}


The DeclareDatamodelEntryfields just makes those fields relevant to @stackexchange known to that entry type.



Declare new bibliography strings (if necessary)



We also want to display words like 'asked' and 'answered', instead of hard-coding them, localisation with .lbx files is the way to go. This is only necessary if you wish to include strings in the output. It is not necessary to include a string for each field you define. The fact that the name of some bibstrings coincides with the name of some fields is purely accidental. There is no requirement to define bibstrings for certain field types.



The most complete way to define new strings is by means of an .lbx file, because that method allows you to define long and short forms of the strings. You can also define new strings directly in the preamble, but then you can only give one form that is used as short and long version.



Our new localisation file english-stack.lbx inherits everything from english.lbx but adds asked, answered and edited as bibstrings.



begin{filecontents*}{english-stack.lbx}
ProvidesFile{english-stack.lbx}[2014/05/07 english with additions for stackexchange]
InheritBibliographyExtras{english}
NewBibliographyString{asked,answered,edited}
DeclareBibliographyStrings{%
inherit = {english},
asked = {{asked}{asked}},
answered = {{answered}{answered}},
edited = {{edited}{edited}},
}
end{filecontents*}


We have to tell biblatex to use that language definition



DeclareLanguageMapping{english}{english-stack}


Defining the bibliography output



Then finally, we can go on to define the bibliography driver. The bibliography driver is the main definition that decides how the entry is printed in the bibliography. A bibliography driver typically consists of calls to several bibliography macros as well as calls to punctuation and printing commands.



Field formats



Field formats define the formatting of each printed field. We avoid hard-coding anything and use the macros provided by biblatex.



So instead of wrapping printfield{title} in mkbibquote, we define the field format to do so.
We use setunit for punctuation and bibstrings in lieu of hard-coded words.



DeclareFieldFormat[stackexchange]{title}{mkbibquote{#1isdot}}
DeclareFieldFormat{askpid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{anspid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{askid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/q/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{ansid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/a/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}


As an added bonus, the id fields now link to the proper stackexchange site.



newbibmacro*{stackexchangequestion}{%
printfield{title}%
setunit{addspace}%
printfield{askid}%
}


Bibliography macros



Bibliography macros can be used to compartmentalise the output definitions.



No formatting should be applied to the printfield commands, formatting is dealt with by DeclareFieldFormat.



newbibmacro*{stackexchangeask}{%
bibstring{asked}%
setunit{addspace}%
printaskdate%
iffieldundef{askedityear}%
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printaskeditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{askp}%
setunit{addspace}%
printfield{askpid}%
}

newbibmacro*{stackexchangeans}{%
bibstring{answered}%
setunit{addspace}%
printansdate%
iffieldundef{ansedityear}
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printanseditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{ansp}%
setunit{addspace}%
printfield{anspid}%
}

newbibmacro*{stackexchangesite}{%
printfield{sitetopic}}


Bibliography driver



The driver defines the final output structure. Usually a driver makes heavy use of bibmacros, but it need not do that.



DeclareBibliographyDriver{stackexchange}{%
usebibmacro{bibindex}%
usebibmacro{begentry}%
usebibmacro{stackexchangequestion}%
newunitnewblock
usebibmacro{stackexchangeask}%
newunitnewblock
usebibmacro{stackexchangeans}%
newunitnewblock
usebibmacro{stackexchangesite}%
newunitnewblock
usebibmacro{finentry}}


MWE



RequirePackage{filecontents}
begin{filecontents*}{jobname.bib}
@stackexchange{se:l3help,
sitetopic = {tex},
title = {What can textit{I} do to help the LaTeX3 Project},
askdate = {2012-02-26},
askid = {45838},
askp = {Brent Longorough},
askpid = {344},
ansp = {Frank Mittelbach},
anspid = {10109},
ansdate = {2012-03-01},
ansid = {46427},
}
end{filecontents*}
begin{filecontents}{stackexchange.dbx}
DeclareDatamodelEntrytypes{stackexchange}
DeclareDatamodelFields[type=field,datatype=literal]{
sitekey,
sitetopic,
}
DeclareDatamodelFields[type=list,datatype=name]{
askp,
ansp,
}
DeclareDatamodelFields[type=field, datatype=date, skipout]{
askdate,
ansdate,
askeditdate,
anseditdate}
DeclareDatamodelFields[type=field, datatype=verbatim]{
askid,
askpid,
anspid,
ansid,
}
DeclareDatamodelEntryfields[stackexchange]{
sitekey,
sitetopic,
askid,
askp,
askpid,
ansp,
anspid,
ansid,
askyear,
askmonth,
askday,
ansyear,
ansmonth,
ansday,
askedityear,
askeditmonth,
askeditday,
ansedityear,
anseditmonth,
anseditday,
title}
end{filecontents}
documentclass[english]{article}
usepackage{babel}
usepackage{csquotes}
usepackage[datamodel=stackexchange,backend=biber]{biblatex}
usepackage{hyperref}
addbibresource{jobname.bib}

begin{filecontents*}{english-stack.lbx}
ProvidesFile{english-stack.lbx}[2014/05/07 english with additions for stackexchange]
InheritBibliographyExtras{english}
NewBibliographyString{asked,answered,edited}
DeclareBibliographyStrings{%
inherit = {english},
asked = {{asked}{asked}},
answered = {{answered}{answered}},
edited = {{edited}{edited}},
}
end{filecontents*}
DeclareLanguageMapping{english}{english-stack}

DeclareFieldFormat[stackexchange]{title}{mkbibquote{#1isdot}}
DeclareFieldFormat{askpid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{anspid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{askid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/q/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{ansid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/a/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

newbibmacro*{stackexchangequestion}{%
printfield{title}%
setunit{addspace}%
printfield{askid}%
}

newbibmacro*{stackexchangeask}{%
bibstring{asked}%
setunit{addspace}%
printaskdate%
iffieldundef{askedityear}%
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printaskeditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{askp}%
setunit{addspace}%
printfield{askpid}%
}

newbibmacro*{stackexchangeans}{%
bibstring{answered}%
setunit{addspace}%
printansdate%
iffieldundef{ansedityear}
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printanseditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{ansp}%
setunit{addspace}%
printfield{anspid}%
}

newbibmacro*{stackexchangesite}{%
printfield{sitetopic}}

DeclareBibliographyDriver{stackexchange}{%
usebibmacro{bibindex}%
usebibmacro{begentry}%
usebibmacro{stackexchangequestion}%
newunitnewblock
usebibmacro{stackexchangeask}%
newunitnewblock
usebibmacro{stackexchangeans}%
newunitnewblock
usebibmacro{stackexchangesite}%
newunitnewblock
usebibmacro{finentry}}

begin{document}
nocite{*}
cite{se:l3help}

printbibliography
end{document}


enter image description here





To allow the use of askdate as labeldate, so the authoryear style (and other similar ones) does not print 'n.d.', we can use DeclareLabeldate (§4.5.8 Special Fields, p. 176) as follows



DeclareLabeldate{%
field{date}
field{eventdate}
field{origdate}
field{askdate}
field{urldate}
literal{nodate}
}


There also is DeclareLabelname (p. 176) with a similar syntax, if you want to see either askp or ansp as the author in authoryear/authortitle styles.






share|improve this answer



















  • 6




    Very nice, and very complete answer (I knew I should refrain). I would've had to steal the code too; but it probably would've taken me much longer to get everything in place.
    – jon
    May 7 '14 at 18:18






  • 6




    This is an absolutely fantastic answer, so first and foremost I want to thank you. That said, when using an authoryear style, the citation engine determines that there is no date: ... Project n.d.. Is there a way to map the askdate onto this field (which I'm presuming would be date)?
    – Sean Allred
    May 8 '14 at 3:08












  • @SeanAllred Have a look at the edited answer. With biblatex we can explicitly choose which date fields are considered for the labeldate with DeclareLabeldate.
    – moewe
    May 8 '14 at 5:15






  • 1




    I will look into auto-generation of the internals for all datamodel date fields in biblatex 3.4.
    – PLK
    Mar 6 '16 at 21:32






  • 2




    biblatex 3.4/biber 2.5 now automatically generates the internals (code between makeatletter/makeatother above) for all date fields defined in the data model so you won't need to do this part any more.
    – PLK
    Mar 7 '16 at 22:41















up vote
40
down vote



accepted
+100







up vote
40
down vote



accepted
+100




+100




The answer is now updated for the date handling in newer biblatex versions >= 3.6, lots of what had to be done manually before will now be done automatically. Check the edit history for earlier versions.



This is quite a full-on question, but I will try to answer it anyway.



Declare new fields, the new entry types and its fields



First, when declaring the data model, we need to make sure biblatex actually knows the type of the fields/lists we declare (this is done via the optional argument to DeclareDatamodelFields). You can read much more about this in §4.5.3 Data Model Specification, p. 151 of the biblatex documentation.



There are two main types for entry field: field and list. fields usually hold one value, while lists can hold more items separated by an and in the .bib file (see also §2.2.1 Data Types, p. 13).



I declared the sitekey and sitetopic to be literal fields (such as the title field).
askp and ansp are clearly name lists (even though in this context the list will probably only ever hold one item).
A lot of the fields you are asking for seem to revolve about dates, so there are askdate, ansdate, askeditdate, anseditdate and their dateparts (which will be created automatically, but they still need to be added to DeclareDatamodelEntryfields manually).
Finally, I decided the ids should be verbatim fields.



begin{filecontents}{stackexchange.dbx}
DeclareDatamodelEntrytypes{stackexchange}
DeclareDatamodelFields[type=field,datatype=literal]{
sitekey,
sitetopic,
}
DeclareDatamodelFields[type=list,datatype=name]{
askp,
ansp,
}
DeclareDatamodelFields[type=field, datatype=date, skipout]{
askdate,
ansdate,
askeditdate,
anseditdate}
DeclareDatamodelFields[type=field, datatype=verbatim]{
askid,
askpid,
anspid,
ansid,
}
DeclareDatamodelEntryfields[stackexchange]{
sitekey,
sitetopic,
askid,
askp,
askpid,
ansp,
anspid,
ansid,
askyear,
askmonth,
askday,
ansyear,
ansmonth,
ansday,
askedityear,
askeditmonth,
askeditday,
ansedityear,
anseditmonth,
anseditday,
title}
end{filecontents}


The DeclareDatamodelEntryfields just makes those fields relevant to @stackexchange known to that entry type.



Declare new bibliography strings (if necessary)



We also want to display words like 'asked' and 'answered', instead of hard-coding them, localisation with .lbx files is the way to go. This is only necessary if you wish to include strings in the output. It is not necessary to include a string for each field you define. The fact that the name of some bibstrings coincides with the name of some fields is purely accidental. There is no requirement to define bibstrings for certain field types.



The most complete way to define new strings is by means of an .lbx file, because that method allows you to define long and short forms of the strings. You can also define new strings directly in the preamble, but then you can only give one form that is used as short and long version.



Our new localisation file english-stack.lbx inherits everything from english.lbx but adds asked, answered and edited as bibstrings.



begin{filecontents*}{english-stack.lbx}
ProvidesFile{english-stack.lbx}[2014/05/07 english with additions for stackexchange]
InheritBibliographyExtras{english}
NewBibliographyString{asked,answered,edited}
DeclareBibliographyStrings{%
inherit = {english},
asked = {{asked}{asked}},
answered = {{answered}{answered}},
edited = {{edited}{edited}},
}
end{filecontents*}


We have to tell biblatex to use that language definition



DeclareLanguageMapping{english}{english-stack}


Defining the bibliography output



Then finally, we can go on to define the bibliography driver. The bibliography driver is the main definition that decides how the entry is printed in the bibliography. A bibliography driver typically consists of calls to several bibliography macros as well as calls to punctuation and printing commands.



Field formats



Field formats define the formatting of each printed field. We avoid hard-coding anything and use the macros provided by biblatex.



So instead of wrapping printfield{title} in mkbibquote, we define the field format to do so.
We use setunit for punctuation and bibstrings in lieu of hard-coded words.



DeclareFieldFormat[stackexchange]{title}{mkbibquote{#1isdot}}
DeclareFieldFormat{askpid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{anspid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{askid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/q/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{ansid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/a/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}


As an added bonus, the id fields now link to the proper stackexchange site.



newbibmacro*{stackexchangequestion}{%
printfield{title}%
setunit{addspace}%
printfield{askid}%
}


Bibliography macros



Bibliography macros can be used to compartmentalise the output definitions.



No formatting should be applied to the printfield commands, formatting is dealt with by DeclareFieldFormat.



newbibmacro*{stackexchangeask}{%
bibstring{asked}%
setunit{addspace}%
printaskdate%
iffieldundef{askedityear}%
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printaskeditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{askp}%
setunit{addspace}%
printfield{askpid}%
}

newbibmacro*{stackexchangeans}{%
bibstring{answered}%
setunit{addspace}%
printansdate%
iffieldundef{ansedityear}
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printanseditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{ansp}%
setunit{addspace}%
printfield{anspid}%
}

newbibmacro*{stackexchangesite}{%
printfield{sitetopic}}


Bibliography driver



The driver defines the final output structure. Usually a driver makes heavy use of bibmacros, but it need not do that.



DeclareBibliographyDriver{stackexchange}{%
usebibmacro{bibindex}%
usebibmacro{begentry}%
usebibmacro{stackexchangequestion}%
newunitnewblock
usebibmacro{stackexchangeask}%
newunitnewblock
usebibmacro{stackexchangeans}%
newunitnewblock
usebibmacro{stackexchangesite}%
newunitnewblock
usebibmacro{finentry}}


MWE



RequirePackage{filecontents}
begin{filecontents*}{jobname.bib}
@stackexchange{se:l3help,
sitetopic = {tex},
title = {What can textit{I} do to help the LaTeX3 Project},
askdate = {2012-02-26},
askid = {45838},
askp = {Brent Longorough},
askpid = {344},
ansp = {Frank Mittelbach},
anspid = {10109},
ansdate = {2012-03-01},
ansid = {46427},
}
end{filecontents*}
begin{filecontents}{stackexchange.dbx}
DeclareDatamodelEntrytypes{stackexchange}
DeclareDatamodelFields[type=field,datatype=literal]{
sitekey,
sitetopic,
}
DeclareDatamodelFields[type=list,datatype=name]{
askp,
ansp,
}
DeclareDatamodelFields[type=field, datatype=date, skipout]{
askdate,
ansdate,
askeditdate,
anseditdate}
DeclareDatamodelFields[type=field, datatype=verbatim]{
askid,
askpid,
anspid,
ansid,
}
DeclareDatamodelEntryfields[stackexchange]{
sitekey,
sitetopic,
askid,
askp,
askpid,
ansp,
anspid,
ansid,
askyear,
askmonth,
askday,
ansyear,
ansmonth,
ansday,
askedityear,
askeditmonth,
askeditday,
ansedityear,
anseditmonth,
anseditday,
title}
end{filecontents}
documentclass[english]{article}
usepackage{babel}
usepackage{csquotes}
usepackage[datamodel=stackexchange,backend=biber]{biblatex}
usepackage{hyperref}
addbibresource{jobname.bib}

begin{filecontents*}{english-stack.lbx}
ProvidesFile{english-stack.lbx}[2014/05/07 english with additions for stackexchange]
InheritBibliographyExtras{english}
NewBibliographyString{asked,answered,edited}
DeclareBibliographyStrings{%
inherit = {english},
asked = {{asked}{asked}},
answered = {{answered}{answered}},
edited = {{edited}{edited}},
}
end{filecontents*}
DeclareLanguageMapping{english}{english-stack}

DeclareFieldFormat[stackexchange]{title}{mkbibquote{#1isdot}}
DeclareFieldFormat{askpid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{anspid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{askid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/q/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{ansid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/a/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

newbibmacro*{stackexchangequestion}{%
printfield{title}%
setunit{addspace}%
printfield{askid}%
}

newbibmacro*{stackexchangeask}{%
bibstring{asked}%
setunit{addspace}%
printaskdate%
iffieldundef{askedityear}%
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printaskeditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{askp}%
setunit{addspace}%
printfield{askpid}%
}

newbibmacro*{stackexchangeans}{%
bibstring{answered}%
setunit{addspace}%
printansdate%
iffieldundef{ansedityear}
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printanseditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{ansp}%
setunit{addspace}%
printfield{anspid}%
}

newbibmacro*{stackexchangesite}{%
printfield{sitetopic}}

DeclareBibliographyDriver{stackexchange}{%
usebibmacro{bibindex}%
usebibmacro{begentry}%
usebibmacro{stackexchangequestion}%
newunitnewblock
usebibmacro{stackexchangeask}%
newunitnewblock
usebibmacro{stackexchangeans}%
newunitnewblock
usebibmacro{stackexchangesite}%
newunitnewblock
usebibmacro{finentry}}

begin{document}
nocite{*}
cite{se:l3help}

printbibliography
end{document}


enter image description here





To allow the use of askdate as labeldate, so the authoryear style (and other similar ones) does not print 'n.d.', we can use DeclareLabeldate (§4.5.8 Special Fields, p. 176) as follows



DeclareLabeldate{%
field{date}
field{eventdate}
field{origdate}
field{askdate}
field{urldate}
literal{nodate}
}


There also is DeclareLabelname (p. 176) with a similar syntax, if you want to see either askp or ansp as the author in authoryear/authortitle styles.






share|improve this answer














The answer is now updated for the date handling in newer biblatex versions >= 3.6, lots of what had to be done manually before will now be done automatically. Check the edit history for earlier versions.



This is quite a full-on question, but I will try to answer it anyway.



Declare new fields, the new entry types and its fields



First, when declaring the data model, we need to make sure biblatex actually knows the type of the fields/lists we declare (this is done via the optional argument to DeclareDatamodelFields). You can read much more about this in §4.5.3 Data Model Specification, p. 151 of the biblatex documentation.



There are two main types for entry field: field and list. fields usually hold one value, while lists can hold more items separated by an and in the .bib file (see also §2.2.1 Data Types, p. 13).



I declared the sitekey and sitetopic to be literal fields (such as the title field).
askp and ansp are clearly name lists (even though in this context the list will probably only ever hold one item).
A lot of the fields you are asking for seem to revolve about dates, so there are askdate, ansdate, askeditdate, anseditdate and their dateparts (which will be created automatically, but they still need to be added to DeclareDatamodelEntryfields manually).
Finally, I decided the ids should be verbatim fields.



begin{filecontents}{stackexchange.dbx}
DeclareDatamodelEntrytypes{stackexchange}
DeclareDatamodelFields[type=field,datatype=literal]{
sitekey,
sitetopic,
}
DeclareDatamodelFields[type=list,datatype=name]{
askp,
ansp,
}
DeclareDatamodelFields[type=field, datatype=date, skipout]{
askdate,
ansdate,
askeditdate,
anseditdate}
DeclareDatamodelFields[type=field, datatype=verbatim]{
askid,
askpid,
anspid,
ansid,
}
DeclareDatamodelEntryfields[stackexchange]{
sitekey,
sitetopic,
askid,
askp,
askpid,
ansp,
anspid,
ansid,
askyear,
askmonth,
askday,
ansyear,
ansmonth,
ansday,
askedityear,
askeditmonth,
askeditday,
ansedityear,
anseditmonth,
anseditday,
title}
end{filecontents}


The DeclareDatamodelEntryfields just makes those fields relevant to @stackexchange known to that entry type.



Declare new bibliography strings (if necessary)



We also want to display words like 'asked' and 'answered', instead of hard-coding them, localisation with .lbx files is the way to go. This is only necessary if you wish to include strings in the output. It is not necessary to include a string for each field you define. The fact that the name of some bibstrings coincides with the name of some fields is purely accidental. There is no requirement to define bibstrings for certain field types.



The most complete way to define new strings is by means of an .lbx file, because that method allows you to define long and short forms of the strings. You can also define new strings directly in the preamble, but then you can only give one form that is used as short and long version.



Our new localisation file english-stack.lbx inherits everything from english.lbx but adds asked, answered and edited as bibstrings.



begin{filecontents*}{english-stack.lbx}
ProvidesFile{english-stack.lbx}[2014/05/07 english with additions for stackexchange]
InheritBibliographyExtras{english}
NewBibliographyString{asked,answered,edited}
DeclareBibliographyStrings{%
inherit = {english},
asked = {{asked}{asked}},
answered = {{answered}{answered}},
edited = {{edited}{edited}},
}
end{filecontents*}


We have to tell biblatex to use that language definition



DeclareLanguageMapping{english}{english-stack}


Defining the bibliography output



Then finally, we can go on to define the bibliography driver. The bibliography driver is the main definition that decides how the entry is printed in the bibliography. A bibliography driver typically consists of calls to several bibliography macros as well as calls to punctuation and printing commands.



Field formats



Field formats define the formatting of each printed field. We avoid hard-coding anything and use the macros provided by biblatex.



So instead of wrapping printfield{title} in mkbibquote, we define the field format to do so.
We use setunit for punctuation and bibstrings in lieu of hard-coded words.



DeclareFieldFormat[stackexchange]{title}{mkbibquote{#1isdot}}
DeclareFieldFormat{askpid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{anspid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{askid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/q/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{ansid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/a/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}


As an added bonus, the id fields now link to the proper stackexchange site.



newbibmacro*{stackexchangequestion}{%
printfield{title}%
setunit{addspace}%
printfield{askid}%
}


Bibliography macros



Bibliography macros can be used to compartmentalise the output definitions.



No formatting should be applied to the printfield commands, formatting is dealt with by DeclareFieldFormat.



newbibmacro*{stackexchangeask}{%
bibstring{asked}%
setunit{addspace}%
printaskdate%
iffieldundef{askedityear}%
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printaskeditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{askp}%
setunit{addspace}%
printfield{askpid}%
}

newbibmacro*{stackexchangeans}{%
bibstring{answered}%
setunit{addspace}%
printansdate%
iffieldundef{ansedityear}
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printanseditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{ansp}%
setunit{addspace}%
printfield{anspid}%
}

newbibmacro*{stackexchangesite}{%
printfield{sitetopic}}


Bibliography driver



The driver defines the final output structure. Usually a driver makes heavy use of bibmacros, but it need not do that.



DeclareBibliographyDriver{stackexchange}{%
usebibmacro{bibindex}%
usebibmacro{begentry}%
usebibmacro{stackexchangequestion}%
newunitnewblock
usebibmacro{stackexchangeask}%
newunitnewblock
usebibmacro{stackexchangeans}%
newunitnewblock
usebibmacro{stackexchangesite}%
newunitnewblock
usebibmacro{finentry}}


MWE



RequirePackage{filecontents}
begin{filecontents*}{jobname.bib}
@stackexchange{se:l3help,
sitetopic = {tex},
title = {What can textit{I} do to help the LaTeX3 Project},
askdate = {2012-02-26},
askid = {45838},
askp = {Brent Longorough},
askpid = {344},
ansp = {Frank Mittelbach},
anspid = {10109},
ansdate = {2012-03-01},
ansid = {46427},
}
end{filecontents*}
begin{filecontents}{stackexchange.dbx}
DeclareDatamodelEntrytypes{stackexchange}
DeclareDatamodelFields[type=field,datatype=literal]{
sitekey,
sitetopic,
}
DeclareDatamodelFields[type=list,datatype=name]{
askp,
ansp,
}
DeclareDatamodelFields[type=field, datatype=date, skipout]{
askdate,
ansdate,
askeditdate,
anseditdate}
DeclareDatamodelFields[type=field, datatype=verbatim]{
askid,
askpid,
anspid,
ansid,
}
DeclareDatamodelEntryfields[stackexchange]{
sitekey,
sitetopic,
askid,
askp,
askpid,
ansp,
anspid,
ansid,
askyear,
askmonth,
askday,
ansyear,
ansmonth,
ansday,
askedityear,
askeditmonth,
askeditday,
ansedityear,
anseditmonth,
anseditday,
title}
end{filecontents}
documentclass[english]{article}
usepackage{babel}
usepackage{csquotes}
usepackage[datamodel=stackexchange,backend=biber]{biblatex}
usepackage{hyperref}
addbibresource{jobname.bib}

begin{filecontents*}{english-stack.lbx}
ProvidesFile{english-stack.lbx}[2014/05/07 english with additions for stackexchange]
InheritBibliographyExtras{english}
NewBibliographyString{asked,answered,edited}
DeclareBibliographyStrings{%
inherit = {english},
asked = {{asked}{asked}},
answered = {{answered}{answered}},
edited = {{edited}{edited}},
}
end{filecontents*}
DeclareLanguageMapping{english}{english-stack}

DeclareFieldFormat[stackexchange]{title}{mkbibquote{#1isdot}}
DeclareFieldFormat{askpid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{anspid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/users/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{askid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/q/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

DeclareFieldFormat{ansid}{%
mkbibparens{ifhyperref
{href{http://tex.stackexchange.com/a/#1}{nolinkurl{#1}}}
{nolinkurl{#1}}}}

newbibmacro*{stackexchangequestion}{%
printfield{title}%
setunit{addspace}%
printfield{askid}%
}

newbibmacro*{stackexchangeask}{%
bibstring{asked}%
setunit{addspace}%
printaskdate%
iffieldundef{askedityear}%
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printaskeditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{askp}%
setunit{addspace}%
printfield{askpid}%
}

newbibmacro*{stackexchangeans}{%
bibstring{answered}%
setunit{addspace}%
printansdate%
iffieldundef{ansedityear}
{}
{printtext[parens]{%
bibstring{edited}%
setunit{addspace}%
printanseditdate}}%
setunit{addspace}%
bibstring{byauthor}%
setunit{addspace}%
printnames{ansp}%
setunit{addspace}%
printfield{anspid}%
}

newbibmacro*{stackexchangesite}{%
printfield{sitetopic}}

DeclareBibliographyDriver{stackexchange}{%
usebibmacro{bibindex}%
usebibmacro{begentry}%
usebibmacro{stackexchangequestion}%
newunitnewblock
usebibmacro{stackexchangeask}%
newunitnewblock
usebibmacro{stackexchangeans}%
newunitnewblock
usebibmacro{stackexchangesite}%
newunitnewblock
usebibmacro{finentry}}

begin{document}
nocite{*}
cite{se:l3help}

printbibliography
end{document}


enter image description here





To allow the use of askdate as labeldate, so the authoryear style (and other similar ones) does not print 'n.d.', we can use DeclareLabeldate (§4.5.8 Special Fields, p. 176) as follows



DeclareLabeldate{%
field{date}
field{eventdate}
field{origdate}
field{askdate}
field{urldate}
literal{nodate}
}


There also is DeclareLabelname (p. 176) with a similar syntax, if you want to see either askp or ansp as the author in authoryear/authortitle styles.







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 hours ago

























answered May 7 '14 at 18:11









moewe

84.5k9108325




84.5k9108325








  • 6




    Very nice, and very complete answer (I knew I should refrain). I would've had to steal the code too; but it probably would've taken me much longer to get everything in place.
    – jon
    May 7 '14 at 18:18






  • 6




    This is an absolutely fantastic answer, so first and foremost I want to thank you. That said, when using an authoryear style, the citation engine determines that there is no date: ... Project n.d.. Is there a way to map the askdate onto this field (which I'm presuming would be date)?
    – Sean Allred
    May 8 '14 at 3:08












  • @SeanAllred Have a look at the edited answer. With biblatex we can explicitly choose which date fields are considered for the labeldate with DeclareLabeldate.
    – moewe
    May 8 '14 at 5:15






  • 1




    I will look into auto-generation of the internals for all datamodel date fields in biblatex 3.4.
    – PLK
    Mar 6 '16 at 21:32






  • 2




    biblatex 3.4/biber 2.5 now automatically generates the internals (code between makeatletter/makeatother above) for all date fields defined in the data model so you won't need to do this part any more.
    – PLK
    Mar 7 '16 at 22:41
















  • 6




    Very nice, and very complete answer (I knew I should refrain). I would've had to steal the code too; but it probably would've taken me much longer to get everything in place.
    – jon
    May 7 '14 at 18:18






  • 6




    This is an absolutely fantastic answer, so first and foremost I want to thank you. That said, when using an authoryear style, the citation engine determines that there is no date: ... Project n.d.. Is there a way to map the askdate onto this field (which I'm presuming would be date)?
    – Sean Allred
    May 8 '14 at 3:08












  • @SeanAllred Have a look at the edited answer. With biblatex we can explicitly choose which date fields are considered for the labeldate with DeclareLabeldate.
    – moewe
    May 8 '14 at 5:15






  • 1




    I will look into auto-generation of the internals for all datamodel date fields in biblatex 3.4.
    – PLK
    Mar 6 '16 at 21:32






  • 2




    biblatex 3.4/biber 2.5 now automatically generates the internals (code between makeatletter/makeatother above) for all date fields defined in the data model so you won't need to do this part any more.
    – PLK
    Mar 7 '16 at 22:41










6




6




Very nice, and very complete answer (I knew I should refrain). I would've had to steal the code too; but it probably would've taken me much longer to get everything in place.
– jon
May 7 '14 at 18:18




Very nice, and very complete answer (I knew I should refrain). I would've had to steal the code too; but it probably would've taken me much longer to get everything in place.
– jon
May 7 '14 at 18:18




6




6




This is an absolutely fantastic answer, so first and foremost I want to thank you. That said, when using an authoryear style, the citation engine determines that there is no date: ... Project n.d.. Is there a way to map the askdate onto this field (which I'm presuming would be date)?
– Sean Allred
May 8 '14 at 3:08






This is an absolutely fantastic answer, so first and foremost I want to thank you. That said, when using an authoryear style, the citation engine determines that there is no date: ... Project n.d.. Is there a way to map the askdate onto this field (which I'm presuming would be date)?
– Sean Allred
May 8 '14 at 3:08














@SeanAllred Have a look at the edited answer. With biblatex we can explicitly choose which date fields are considered for the labeldate with DeclareLabeldate.
– moewe
May 8 '14 at 5:15




@SeanAllred Have a look at the edited answer. With biblatex we can explicitly choose which date fields are considered for the labeldate with DeclareLabeldate.
– moewe
May 8 '14 at 5:15




1




1




I will look into auto-generation of the internals for all datamodel date fields in biblatex 3.4.
– PLK
Mar 6 '16 at 21:32




I will look into auto-generation of the internals for all datamodel date fields in biblatex 3.4.
– PLK
Mar 6 '16 at 21:32




2




2




biblatex 3.4/biber 2.5 now automatically generates the internals (code between makeatletter/makeatother above) for all date fields defined in the data model so you won't need to do this part any more.
– PLK
Mar 7 '16 at 22:41






biblatex 3.4/biber 2.5 now automatically generates the internals (code between makeatletter/makeatother above) for all date fields defined in the data model so you won't need to do this part any more.
– PLK
Mar 7 '16 at 22:41




















draft saved

draft discarded




















































Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f175776%2fhow-can-i-create-entirely-new-data-types-with-biblatex-biber%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown