38

Is there any way to use BibTex entries in thebibliography environment like following:

\begin{thebibliography}{9}
@article{greenwade93,
    author  = "George D. Greenwade",
    title   = "The {C}omprehensive {T}ex {A}rchive {N}etwork ({CTAN})",
    year    = "1993",
    journal = "TUGBoat",
    volume  = "14",
    number  = "3",
    pages   = "342--351"
}

@book{goossens93,
    author    = "Michel Goossens and Frank Mittelbach and Alexander Samarin",
    title     = "The LaTeX Companion",
    year      = "1993",
    publisher = "Addison-Wesley",
    address   = "Reading, Massachusetts"
}

\end{thebibliography}

For example consider that you want to quickly write a note that refers to three papers those their BibTex entries are available (perhaps via scholar.google.com ), it is easier that you directly insert them to your document instead of creating a .bib file or converting bibtex entries.

Real Dreams
  • 8,298
  • 12
  • 56
  • 78
  • no that is not possible. –  Jun 21 '12 at 06:31
  • 5
    You can use \begin{filecontents}{\jobname.bib} ... \end{filecontents} to create your bibliography on the fly, from one document. For extended filecontent environments, use the filecontents package. – matth Jun 21 '12 at 07:50
  • You may want to look at the package amsrefs, where a syntax similar to that of .bib files is used. – egreg Jun 21 '12 at 08:52
  • 2
    @VafaKhalighi it means you only have to keep track of one file. I often don't want to share my entire bib file with collaborators. – StrongBad Jun 21 '12 at 09:19
  • @matth that seems like the start of an answer, but filecontents doesn't overwrite existing files so changes to the environment often don't get propagated. – StrongBad Jun 21 '12 at 09:21
  • 2
    @DanielE.Shub That seems to be one of the reasons why the filecontents package was developed. The normal filecontents environment does not overwrite files, but the filecontents environment provided by the filecontents packge does. – matth Jun 21 '12 at 09:44
  • 1
    possible duplicate: http://tex.stackexchange.com/questions/10201/inlining-bibtex-bibliography-in-latex-file – matth Jun 21 '12 at 10:01

3 Answers3

36

Converting my comments into an answer: Maybe the filecontents package can do what you want.

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@article{greenwade93,
    author  = "George D. Greenwade",
    title   = "The {C}omprehensive {T}ex {A}rchive {N}etwork ({CTAN})",
    year    = "1993",
    journal = "TUGBoat",
    volume  = "14",
    number  = "3",
    pages   = "342--351"
}
@book{goossens93,
    author    = "Michel Goossens and Frank Mittelbach and Alexander Samarin",
    title     = "The LaTeX Companion",
    year      = "1993",
    publisher = "Addison-Wesley",
    address   = "Reading, Massachusetts"
}
\end{filecontents}

\usepackage{natbib}

\begin{document}
Lorem ipsum~\citep{goossens93}.
Dolor sit amet~\citet{greenwade93}.
\bibliographystyle{plainnat}
\bibliography{\jobname} 
\end{document}

In contrast to the filecontents environment provided by LaTeX2e, the filecontents environment provided by the filecontents packge does overwrite existing files.

matth
  • 12,381
  • 2
    remember that bibtex still has to be run separately. no getting around that. – barbara beeton Jun 21 '12 at 13:06
  • 2
    @barbarabeeton: put \immediate\write18{bibtex \jobname} after the end of filecontents environment and run pdflatex with shell-escape option. – Vafa Khalighi Jun 22 '12 at 06:27
  • 1
    Thanks a lot! I am not sure why but I had to remove the escape from the file name. Namely I had to change \jobname to jobname. – djhurio Jul 29 '21 at 13:51
3

You can do it, to a perhaps limited extent, with amsrefs:

\documentclass{article}
\usepackage{amsrefs}
\newenvironment{rezabib}
  {\bibdiv\biblist\setupbib}
  {\endbiblist\endbibdiv}

\def\setupbib{\catcode`@=\active}
\begingroup\lccode`~=`@
  \lowercase{\endgroup\def~}#1#{\gatherkey{#1}}
\def\gatherkey#1#2{\gatherkeyaux{#1}#2\gatherkeyaux}
\def\gatherkeyaux#1#2,#3\gatherkeyaux{\bib{#2}{#1}{#3}}

\begin{document}

\nocite{*}

\begin{rezabib}
@article{greenwade93,
    author  = {George D. Greenwade},
    title   = {The {C}omprehensive {T}ex {A}rchive {N}etwork ({CTAN})},
    year    = {1993},
    journal = {TUGBoat},
    volume  = {14},
    number  = {3},
    pages   = {342--351}
}

@book{goossens93,
    author    = {Michel Goossens and Frank Mittelbach and Alexander Samarin},
    title     = {The LaTeX Companion},
    year      = {1993},
    publisher = {Addison-Wesley},
    address   = {Reading, Massachusetts}
}
\end{rezabib}

\end{document}

Note that you must use braces for delimiting fields. There should be no additional space in the starting lines for entries

@book{goossens93,

The limitation on spaces might be lifted, but not the one on delimiting braces. You can't use @ in fields (also this may be lifted, if really needed).

egreg
  • 1,121,712
0

Since the update of the filecontens environment in 2019, the filecontents package is not needed anymore. Here's how a bibliography could be included directly into the .tex file:

\documentclass{article}
\usepackage[style=numeric]{biblatex}
\begin{filecontents}{references.bib}
@book{goossens93,
    author    = {Michel Goossens and Frank Mittelbach and Alexander Samarin},
    title     = {The LaTeX Companion},
    year      = {1993},
    publisher = {Addison-Wesley},
    address   = {Reading, Massachusetts}
}
\end{filecontents}
\addbibresource{references.bib}

\title{My article} \author{John Doe}

\begin{document} \maketitle \section{Introduction}

Here's the reference: \cite{goossens93}

\printbibliography \end{document}

finnan
  • 101