11

I am very much committed to the natbib package, but still want to cite different fields of my bibliography items. For this I am using this code found in a google forum.

\begin{filecontents}{mytestbib.bib}
@book{author00,
title = {{A Title}},
publisher = {Alpha},
year = {2008},
editor = {Author, A},
address = {London}
}
@book{buthor00,
title = {{B Title}},
publisher = {Bravo},
year = {1990},
author = {Buthor, B},
address = {New York}
}
\end{filecontents}

\documentclass{article}
\usepackage{natbib}
\usepackage{hyperref}
\usepackage{keyval}

\input{https://groups.google.com/forum}% symbolically, for readability's sake 
\newbibfield{editor}
\bibinput{mytestbib}

\begin{document}
\usebibentry{author00}{editor} has edited `\usebibentry{author00}{title}'

\citeauthor{buthor00} has written `\usebibentry{buthor00}{title}'
\bibliographystyle{plainnat}
\bibliography{mytestbib}
\end{document}

This results in

enter image description here

and poses problems I cannot solve:

  1. I would like \usebibentry{author00}{editor} to result in 'Author', analog to \citeauthor{buthor00}
  2. Is it possible to have the \usebibentry{key}{field} also create a hyperlink, analog to \citeauthor?
  3. %% LaTeX2e file `mytestbib.bib' ... is being put into the document and causes an error, can that be avoided?
  4. The bibitem 'author00' does not appear in 'References' (a minor problem, since also \cited regularly in my document)

Below is an amendment in response to egreg's answer

JabRef produces .bib files that look in principle the same as if produced by \begin{filecontents}, i.e.

% This file was created with JabRef 2.7.
% Encoding: Cp1252

@book{author00,
title = {{A Title}},
publisher = {Alpha},
year = {2008},
editor = {Author, A},
address = {London}
}
@book{buthor00,
title = {{B Title}},
publisher = {Bravo},
year = {1990},
author = {Buthor, B},
address = {New York}
}

My question regarding the hyperlink was imprecise, indeed. It should point at the entry in the 'References', which I now create as follows:

\newcommand{\editorlastname}[1]{\nocite{#1}%
  \begingroup\edef\x{\usebibentry{#1}{editor}}%
  \expandafter\endgroup\expandafter\removename\x\removename}
\def\removename#1,#2\removename{#1}

By the way, the \editorlastname works fine for me, but in case of multiple editors only the first editor is printed. (Should that be of importance to another user)

5
  • 1. This seems to be exactly the output you request, so I don't follow; 3. Use the filecontents* environment together with the filecontents package - so put that in your preamble, not before \documentclass; 4. Try adding \nocite{author00} in your document.
    – Werner
    Apr 28, 2012 at 16:53
  • 1. Author has edited 'A Title' - is what I would prefer to read 3. That's neat, didn't know that. However, I am using JabRef and JabRef puts some comment in the beginning of my bibfiles as well, this was just for illustration.
    – D.Roepo
    Apr 28, 2012 at 17:02
  • The code you copied is now available as the package usebib.
    – egreg
    Apr 28, 2012 at 20:17
  • In my test the commented initial lines are ignored.
    – egreg
    Apr 29, 2012 at 14:53
  • Yes, same here. My bad.
    – D.Roepo
    Apr 29, 2012 at 20:02

1 Answer 1

5

Use the filecontents* environment when you want to output a .bib file, so the initial commented lines are omitted. However this should not be a problem when you use the relatively new usebib package, which is derived from the comp.text.tex code you mention.

\begin{filecontents*}{\jobname.bib}
@book{author00,
title = {{A Title}},
publisher = {Alpha},
year = {2008},
editor = {Author, A},
address = {London}
}
@book{buthor00,
title = {{B Title}},
publisher = {Bravo},
year = {1990},
author = {Buthor, B},
address = {New York}
}
\end{filecontents*}

\documentclass{article}
\usepackage{natbib}
\usepackage{hyperref}
\usepackage{keyval}

\usepackage{usebib}
\newbibfield{editor}
\bibinput{\jobname}

\begin{document}
\usebibentry{author00}{editor} has edited `\usebibentry{author00}{title}'

\citeauthor{buthor00} has written `\usebibentry{buthor00}{title}'

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

Note that I've used \jobname as the name of the .bib file, just not to clutter my (already overcluttered) directory of examples. Any name is just as good.

To get the last name, when the editor is stated in the .bib file in the form

lastname, firstname

you can say in your preamble

\newcommand{\editorlastname}[1]{%
  \begingroup\edef\x{\usebibentry{#1}{editor}}%
  \expandafter\endgroup\expandafter\removename\x\removename}
\def\removename#1,#2\removename{#1}

and then use \editorlastname{author00}. For hyperlinks one should know what to point at.

9
  • 1
    A very useful package which should be promoted more prominently around here! However, the output doesn't change, so my initial problems remain - i.e using editors full name instead of last name only, no hyperlinks created and errors due to .bib file preceding comments (e.g. produced by JabRef). Is there a work-around?
    – D.Roepo
    Apr 29, 2012 at 12:16
  • @D.Roepo The package doesn't even try to parse author or editor names: coping with BibTeX syntax on the LaTeX side is quite complicated. I'll try getting only the last name. Can you add a real example of a JabRef generated file that gives problems?
    – egreg
    Apr 29, 2012 at 12:24
  • Do you know how I can find out what the labels for the bibentries are? Then something like this \hyperref[*label*]{\editorlastname{author00}} could be used.
    – D.Roepo
    Apr 29, 2012 at 20:03
  • @D.Roepo You can use \hyperlink{cite.author00}{\editorlastname{author00}}. However, \citeauthor works also with editors, so \citeauthor{author00} is the best way, if there's no author in the entry.
    – egreg
    Apr 29, 2012 at 20:15
  • 1
    @HalilŞEN Add \newbibfield{Title}, but then do \usebibentry{author00}{title}; field names are normalized to lowercase for \usebibentry.
    – egreg
    Feb 9, 2016 at 10:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .