I would like to be able to specify a list that will create categories and add entries to them.
I am using pgffor for specifying the category name and the heading title in a convenient way. The categories I am using in this example is arbitrary, but the use of a field value and categories in general is not.
MWE
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[defernumbers=true,sorting=ydnt,doi=false,eprint=false,isbn=false]{biblatex}
\renewcommand*{\bibfont}{\footnotesize}
\usepackage{pgffor}
\xdef\bibcats{}
\newcommand{\addcat}[2]{%
\DeclareBibliographyCategory{#1}%
\defbibheading{#1}{\subsection{#2}}%
}
\foreach \catg/\head in {phdthesis/PhD. Thesis,article/Articles}{%
\edef\tmp{\noexpand\addcat{\catg}{\head}}
\tmp
\listxadd{\bibcats}{\catg}
}
\addbibresource{biblatex-examples.bib}
\AtDataInput{%
\ifboolexpr{%
not test {\iffieldundef{type}}
and
test {\xifinlist{\thefield{type}}{\bibcats}}
}
{\addtocategory{\strfield{type}}{\strfield{entrykey}}}%
{\ifboolexpr{%
test {\ifnumgreater{\thefield{year}}{2000}}
and
test {\xifinlist{\thefield{entrytype}}{\bibcats}}
}%
{\addtocategory{\strfield{entrytype}}{\strfield{entrykey}}}%
{}%
}%
}
\begin{document}
\nocite{*}
\printbibheading
\bibbycategory
\end{document}
Result
The above works except for setting the headings and throws the error Package biblatex Error: Heading <phdthesis/article> could not be found. resulting in:

Desired Result
The result I hope to achieve works by replacing the foreach loop with
\addcat{phdthesis}{PhD. Thesis}
\addcat{article}{Articles}
\forcsvlist{\listxadd\bibcats}{phdthesis,article}
giving

Questions
I would like to have the creation of categories depend on a list. Given that the non-foreach loop method works, I suspect the problem mainly comes from expansion problems.
- How do I create categories and corresponding headings based on lists?
- Why does the way I have tried to deal with expansion not work?
\foreachfrom pgffor works in combination with\defbibheadingfrom biblatex. A slightly modified version using a\docsvloopfrom etoolbox does not cause an issue without using the\gnewcommand. – Alex Grede Apr 29 '14 at 10:42\foreachmust run inside a group, or something like that. I tend to useetoolboxanyway, sincebiblatexloades it, and that sounds like a better option; perhaps you might put that as an answer. Your code was essentially perfect! – Paul Stanley Apr 29 '14 at 10:46etoolboxsolution was very hacked together quickly. I don't think I will find the elegant way I was hoping for, for this specific case :) – Alex Grede Apr 29 '14 at 10:53