[Tex/LaTex] Add text before and after bibitem with BibTeX

bibliographiesbibtex

This is a follow up to Adding information to bibitems in which \@lbibitem is tweaked to allow the addition of text before the BibTeX entry. This works fine, however I want to be able to add text before and after an entry, e.g. to add a (*) at the beginning to indicate required reading and some text after the entry to indicate specific sections or bookchapters:

*William Easterly. Can the west save Africa? Journal of Economic Literature, pages 373–447, 2009. Please read sections 2-4

Here is a slightly adapted minimum example from the answer linked above:

\documentclass[a4paper,10pt]{article}
\usepackage[round]{natbib}
\usepackage{datatool}
\usepackage{etoolbox}

%% database for personal temporary notes
\DTLnewdb{bibnotes}

%% command for pre notes
\def\bibpre#1#2{%
  \DTLnewrow{bibnotes}
  \DTLnewdbentry{bibnotes}{pre}{#1}
  \DTLnewdbentry{bibnotes}{mynote}{#2}
}

% command for post notes
\def\bibpost#1#2{%
  \DTLnewrow{bibnotes}
  \DTLnewdbentry{bibnotes}{post}{#1}
  \DTLnewdbentry{bibnotes}{mynote}{#2}
}

%% patching the output
\makeatletter
%% if natbib is loaded
%% this does not work as intended as notes are always placed before the bib entry
\patchcmd{\@lbibitem}%
  {\item[\hfil\NAT@anchor{#2}{\NAT@num}]}%
  {%
    \item[\NAT@anchor{#2}{\NAT@num}\hfil]%
    \DTLforeach[\DTLiseq{\pre}{#2}]{bibnotes}{\pre=pre,\mynote=mynote}{\mynote}%
    \DTLforeach[\DTLiseq{\post}{#2}]{bibnotes}{\post=post,\mynote=mynote}{\mynote}%    
  }{}{\message{^^JPatching failed^^J}}%

% \apptocmd{\@lbibitem}%
%   {\DTLforeach[\DTLiseq{\post}{#2}]{bibnotes}{\post=post,\mynote=mynote}{\mynote}}
%   {}{\message{^^Japptocmd failed^^J}}%

\makeatother

\begin{document}

\citet{easterly2009can,frankel1999does}

%% comments to be displayed before/after bibtex entry
\bibpre{easterly2009can}{*}
\bibpost{frankel1999does}{please read ch.\ 4}
\bibpre{frankel1999does}{*}

\bibliographystyle{plainnat}
\bibliography{bib}
\end{document}

The bib.bib file I used is

@Article{ easterly2009can,
    Author = "William Easterly",
    Title = "Can the West Save Africa?",
    Journal = "Journal of Economic Literature",
    Pages = "373--447",
    year = 2009
}

@Article{ frankel1999does,
    Author = "J.A. Frankel and D. Romer",
    Title = "Does trade cause growth?",
    Journal = "American Economic Review",
    Pages = "379--399",
    year = 1999
}

This results in

References

*William Easterly. Can the west save africa? Journal of Economic Literature,
pages 373–447, 2009.

*please read ch. 4J.A. Frankel and D. Romer. Does trade cause growth? Amer-
ican Economic Review, pages 379–399, 1999.

I found that changing the order of commands in \patchcmd{\@lbibitem} gives an error message and \apptocmd{\@lbibitem} also adds notes before the bib entry, not after.

Best Answer

Have you try biblatex? Bi­bla­tex is a com­plete reim­ple­men­ta­tion of the bib­li­o­graphic fa­cil­i­ties pro­vided by LaTeX in con­junc­tion with BibTeX.

With biblatex isn't that hard to add pre and post annotations to entries. Like this:

\documentclass[a4paper,10pt]{article}

\usepackage[natbib,                                % To use natbib commands
        style=authoryear,                          % Style of cites and bib entries
        backend=bibtex8]                           % To use bibtex instead biber
        {biblatex}
\addbibresource{bib.bib}                          % Loading .bib file

\renewbibmacro{in:}{%                              % To remove "In:" from articles
  \ifentrytype{article}{}{%
  \printtext{\bibstring{in}\intitlepunct}}}
\DefineBibliographyStrings{english}{pages={pages}} % To change default pp.
\DefineBibliographyStrings{english}{page={page}}   % To change default p.

\begin{document}

\citet{easterly2009can,frankel1999does}            % Cite as if you were using natbib

\section*{References}
% use \fullcite[prenote][posnote]{bibentrylabel}
\noindent \fullcite[*][Please read sections 32-33]{easterly2009can} \\
\noindent \fullcite[*][Please read sections 2-4]{frankel1999does}     \end{document}

If you want to add the same annotations but in your .bib file you should do something like this:

\documentclass[a4paper,10pt]{article}

\usepackage[natbib,                                % To use natbib commands
        style=authoryear,                          % Style of cites and bib entries
        backend=bibtex8]                           % To use bibtex instead biber
        {biblatex}
\addbibresource{bib.bib}                          % Loading .bib file

\renewbibmacro{in:}{%                              % To remove "In:" from articles
  \ifentrytype{article}{}{%
  \printtext{\bibstring{in}\intitlepunct}}}
\DefineBibliographyStrings{english}{pages={pages}} % To change default pp.
\DefineBibliographyStrings{english}{page={page}}   % To change default p.

\begin{document}

\citet{easterly2009can,frankel1999does}            % Cite as if you were using natbib

\printbibliography                                 % Equivalent to \bibliography{bib}

\end{document}

with this bib.bib:

@Article{ easterly2009can2,
  Author = "William Easterly",
  Title = "Can the West Save Africa?",
  Journal = "Journal of Economic Literature",
  Pages = "373--447",
  year = 2009,
  note = "A comment that should be before",
  addendum = "A comment after"
},

@Article{ frankel1999does2,
  Author = "J.A. Frankel and D. Romer",
  Title = "Does trade cause growth?",
  Journal = "American Economic Review",
  Pages = "379--399",
  year = 1999,
  note = "A comment that should be before",
  addendum = "A comment after"
}

However, I couldn't find how to reorder the block entities in each bibtex entry. May be you have more luck.

I should remark that citation and bibliographic styles may be changed. Check biblatex documentation for this. Here is a question for customizing biblatex styles.


Following the question: How to cite all bib entries that have no annotations in a simple way? (making minor changes to the .bib file that not affect normal use of the file)

\documentclass[a4paper,10pt]{article}

\usepackage[natbib,                                % To use natbib commands
        style=authoryear,                          % Style of cites and bib entries
        backend=bibtex8]                           % To use bibtex instead biber
        {biblatex}
\addbibresource{biblong.bib}                       % Loading .bib file

\renewbibmacro{in:}{%                              % To remove "In:" from articles
  \ifentrytype{article}{}{%
  \printtext{\bibstring{in}\intitlepunct}}}
\DefineBibliographyStrings{english}{pages={pages}} % To change default pp.
\DefineBibliographyStrings{english}{page={page}}   % To change default p.

\begin{document}

\citet{easterly2009can,frankel1999does}            % Cite as if you were using natbib

\section*{References}
% use \fullcite[prenote][posnote]{bibentrylabel}
\noindent \fullcite[*][Please read sections 32-33]{easterly2009can} \\
\noindent \fullcite[*][Please read sections 2-4]{frankel1999does} \\

\nocite{*} % Use of \nocite is not necessary if they were cited elsewhere
% The following command only print entries that not have the keyword annotated
\printbibliography[notkeyword=annotated,heading=none]

\end{document}

with this biblLong.bib:

@Article{ easterly2009can2,
  Author = "William Easterly",
  Title = "Can the West Save Africa?",
  Journal = "Journal of Economic Literature",
  Pages = "373--447",
  year = 2009
}

@Article{ frankel1999does2,
  Author = "J.A. Frankel and D. Romer",
  Title = "Does trade cause growth?",
  Journal = "American Economic Review",
  Pages = "379--399",
  year = 1999
}

@Article{ lennon1965help,
  Author = "John Lennon",
  Title = "Help!",
  Journal = "The Beatles",
  Pages = "1--14",
  year = 1965
}

@Article{ mccartney2003let,
  Author = "Paul McCartney",
  Title = "Let it be... Naked",
  Journal = "The Beatles",
  Pages = "1--11",
  year = 2003
}
Related Question