41

I'm trying to use BibTeX with a letter; I hope this is not too eccentric. The letter class doesn't have BibTeX support, though. I found Environment thebibliography undefined when using letter. However, the solution described does not work perfectly. The bibliography appears on its own page (in my case it is just two entries), and the numbering is out of order with the plain style ie. [2] appears before [1] in the letter.

Can these issues be fixed? Alternatively, what are my choices? Could I use another letter class? Some time ago I posted to comp.text.tex, and the people there were rather despising about the letter class, and pointed me to various alternatives which they said were better. Do any of these alternatives have built-in BibTeX support?

EDIT1: Both the problems mentioned above were user error.

  1. The wrong order, as pointed out by Joseph, was because I was using the plain
    style.

  2. The bibliography on a separate page was because I put the \bibliographystyle
    and \bibliography entries after closing the letter environment instead of
    before. Again, Joseph's example was helpful in setting me straight.

In summary, there is nothing wrong with the original post, though no doubt Joseph's version is an improvement, and is certainly shorter. Both the original version and Joseph's version work fine for me. I put the text in Joseph's post between \makeatletter and \makeatother into a separate sty file when using it with my letter.

EDIT2: Adding \usepackage{natbib} to the preamble breaks the compile. Whether I put it before or after the "homemade" bib sty file doesn't make a difference. I get

ERROR: LaTeX Error: Environment thebibliography undefined.
1
  • See my update - for me natbib is fine
    – Joseph Wright
    May 12, 2011 at 18:20

2 Answers 2

32

Taking the definition from the article class, I get good results with

\begin{filecontents}{letter.bib}
@article{First,
  author  = "Other, A. N.",
  title   = "Some things {I} did",
  journal = "J. Irreproducible Results",
  year    = "2011"
}

@article{Second,
  author  = "Aaa, S{\o}mebloke",
  title   = "Tigers",
  journal = "Ann. Improbable Res.",
  year    = "2011"
}
\end{filecontents}

\documentclass{letter}
\makeatletter
\newenvironment{thebibliography}[1]
     {\list{\@biblabel{\@arabic\c@enumiv}}%
           {\settowidth\labelwidth{\@biblabel{#1}}%
            \leftmargin\labelwidth
            \advance\leftmargin\labelsep
            \usecounter{enumiv}%
            \let\p@enumiv\@empty
            \renewcommand\theenumiv{\@arabic\c@enumiv}}%
      \sloppy
      \clubpenalty4000
      \@clubpenalty \clubpenalty
      \widowpenalty4000%
      \sfcode`\.\@m}
     {\def\@noitemerr
       {\@latex@warning{Empty `thebibliography' environment}}%
      \endlist}
\newcommand\newblock{\hskip .11em\@plus.33em\@minus.07em}
\makeatother
\begin{document}
\begin{letter}{Some person}
\opening{Hello}

Some text \cite{First}, more text \cite{Second}.

\bibliographystyle{unsrt}
\bibliography{letter}

\end{letter}
\end{document}

I've used the unsrt style here, as plain would put the references in alphabetical rather than citation order. I've also modified the definition of thebiliography a little, removing the section-related stuff as this does not really seem relevant to a letter. (I also took out the code related to the openbib option for the article class, again as it does not seem relevant.)


The second edit to the question asks about natbib. For me, this works if I load natbib after defining thebiliography and if I make \bibsection 'safe':

\begin{filecontents}{letter.bib}
@article{First,
  author  = "Other, A. N.",
  title   = "Some things {I} did",
  journal = "J. Irreproducible Results",
  year    = "2011"
}

@article{Second,
  author  = "Aaa, S{\o}mebloke",
  title   = "Tigers",
  journal = "Ann. Improbable Res.",
  year    = "2011"
}
\end{filecontents}

\documentclass{letter}
\makeatletter
\newenvironment{thebibliography}[1]
     {\list{\@biblabel{\@arabic\c@enumiv}}%
           {\settowidth\labelwidth{\@biblabel{#1}}%
            \leftmargin\labelwidth
            \advance\leftmargin\labelsep
            \usecounter{enumiv}%
            \let\p@enumiv\@empty
            \renewcommand\theenumiv{\@arabic\c@enumiv}}%
      \sloppy
      \clubpenalty4000
      \@clubpenalty \clubpenalty
      \widowpenalty4000%
      \sfcode`\.\@m}
     {\def\@noitemerr
       {\@latex@warning{Empty `thebibliography' environment}}%
      \endlist}
\newcommand\newblock{\hskip .11em\@plus.33em\@minus.07em}
\makeatother
\usepackage[numbers]{natbib}
\let\bibsection\relax
\begin{document}
\begin{letter}{Some person}
\opening{Hello}

Some text \cite{First}, more text \cite{Second}.

\bibliographystyle{unsrtnat}
\bibliography{letter}

\end{letter}
\end{document}
4
  • (The filecontents environment is only used to include some external files directly into the example, so that it compiles. It is not necessary for the solution.)
    – Joseph Wright
    May 12, 2011 at 7:13
  • Thanks for the fast response. bibtex seems unhappy with your entries though. I get errors like Warning--string name "``other" is undefined --line 6 of file letter.bib I was expecting an "="---line 6 of file letter.bib : author = ``other, a. : N.'', I'm skipping whatever remains of this entry Warning--string name "``aaa" is undefined --line 13 of file letter.bib "{" immediately follows a field name---line 13 of file letter.bib : author = ``aaa, S : {\o}mebloke'', I'm skipping whatever remains of this entry May 12, 2011 at 8:24
  • 1
    @Faheem: This is WFM exactly as posted on Windows. From your post, it looks like your editor might have changed the " in my example code into 'pretty printing' LaTeX ligatures, which of course will not work. Try creating your own .bib file directly using whatever editor you usually prefer for that. It's only an example, after all.
    – Joseph Wright
    May 12, 2011 at 9:10
  • 1
    Thanks for the update. I assuming making \bibsection 'safe' corresponds to the line \let\bibsection\relax? So, I was wondering how one would manage if one wanted to stick the letter bib code into a separate style file, as this relax command comes after the import of natbib. Also, could you explain what this relax command does? May 12, 2011 at 20:39
7

I had the same problem and I think that I found a more elegant and more extensive solution.

The developers of komascript.de provide a class scrartcl that is basically equivalent to the standard article class but allows the integration of letters. The beauty is, you cannot only print a bibliography but also use sections and other tools that you might have missed from the article class. A working example:

\begin{filecontents}{letter.bib}
@article{First,
  author  = "Other, A. N.",
  title   = "Some things {I} did",
  journal = "J. Irreproducible Results",
  year    = "2011"
}

@article{Second,
  author  = "Aaa, S{\o}mebloke",
  title   = "Tigers",
  journal = "Ann. Improbable Res.",
  year    = "2011"
}
\end{filecontents}

\documentclass{scrartcl}
\usepackage{scrletter}
\usepackage[numbers]{natbib}
\usepackage{lipsum}

\begin{document}
\setkomavar{fromname}{Name of Sender}
\setkomavar{fromaddress}{Address of Sender}
\begin{letter}{Name of Recipient\\Address of Recipient}
  \opening{Dear Recipient,}

  \section{Interesting Stuff}
  The first~\cite{First} citation.
  \lipsum%

  \section{Another Citation}
  And the second~\cite{Second} citation.

  \closing{Yours sincerely,}

  \bibliographystyle{unsrtnat}
  \bibliography{letter}
\end{letter}
\end{document}
3
  • Yes, I've also become a fan of the KOMA classes. They're not perfect, but they are much more flexible than the default letter and article classes. I now use scrlttr2 as my default letter class. Thanks for the answer. I'm unclear what the scrletter class is for, though. Apr 13, 2016 at 15:35
  • 1
    scrletter is a relatively new package that provides the letter environment that can be used within the custom article class scrartcl. Apr 19, 2016 at 10:51
  • I see. That sounds useful. Apr 19, 2016 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.