Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a general and central preamble for a lot of similar-style documents (several dozens) and most of the documents have a Bibliography at the beginning, so I call

\bibliographystyle{alphadin}
\bibliography{refs}

in the central preamble to avoid a lot of duplicate code and to be able to change the bibliography style centrally. But sadly, a minority of these documents has an empty bibliography. And these give me a warning about an empty thebibliography environment. But the bigger problem is: the bibliography headline still appears in the pdf output.

Is there a way to only get the bibliography headline and no warning when there is a non-empty bibliography? What would be the correct counters or booleans to address? Or is there an easy-to-use option?

Minmal example:

\documentclass{article}

\begin{document}
  \bibliographystyle{alphadin}
  \bibliography{test}
\end{document}

Run latex, bibtex, latex, latex. Get for test.bbl

LaTeX Warning: Empty `thebibliography' environment on input line 10.

Entering the "patch" doesn't solve the problem. What I want ist: If there's no \cite{} within the document, don't display the bibliography.

Content of bbl:

\begin{thebibliography}{}

% this bibliography is generated by alphadin.bst [8.2] from 2005-12-21

\providecommand{\url}[1]{\texttt{#1}}
\expandafter\ifx\csname urlstyle\endcsname\relax
  \providecommand{\doi}[1]{doi: #1}\else
  \providecommand{\doi}{doi: \begingroup \urlstyle{rm}\Url}\fi

\end{thebibliography}
share|improve this question
    
I'm puzzled why you would have the instruction \bibliography{refs} in the preamble of any of your documents. This instruction belongs in the body of the document, in the place where the bibliography section should be created. –  Mico Sep 28 '12 at 10:50
    
Sorry, I was to unclear about that. It is in the body, but my file "preamble.tex" ends with \begin{document} \bibliography{refs} because it's the same for almost all documents (but not all, hence the question/problem). –  Foo Bar Sep 28 '12 at 10:53
    
Thanks for this clarification. But, now, what strikes me as being rather unusual is that you would have \bibliography{refs} as the very first instruction following \begin{document}. –  Mico Sep 28 '12 at 10:57

3 Answers 3

up vote 5 down vote accepted

A test if the argument of thebibliography is empty

\documentclass{article}
\let\myBib\thebibliography
\let\endmyBib\endthebibliography

\renewcommand\thebibliography[1]{\ifx\relax#1\relax\else\myBib{#1}\fi}
\begin{document}
foo
  \bibliographystyle{alphadin}
  \bibliography{test}
\end{document}
share|improve this answer
    
Thanks, that works perfect. :) –  Foo Bar Sep 28 '12 at 11:49
    
Do you need the \myBibend definition (and/or should it be \let to \endthebibliography)? –  Mico Sep 28 '12 at 11:52
    
This of course will execute \endtrivlist –  egreg Sep 28 '12 at 12:13
    
@Mico: yes, you're right. That should be the other way round, but makes no difference in this special case. –  Herbert Sep 28 '12 at 12:18

Method environment thebibliography that is really empty

This method catches the case (with or without empty line inbetween):

\begin{thebibliography}{...}

\end{thebibliography}

The patch for environment thebibliography looks for \end right after \begin{thebibliography}. Also an empty line before \end is supported. If \end is found, then the environment is ignored.

\documentclass{article}

\makeatletter
\@ifdefinable{org@thebibliography}{%
  \let\org@thebibliography\thebibliography
  \def\thebibliography#1{%
    \@ifnextchar\par{%
      \par@thebibliography{#1}%
    }{%
      \check@thebibliography{#1}{}%
    }%
  }%
  \newcommand{\par@thebibliography}[2]{%
    \check@thebibliography{#1}{\par}%
  }%
  \newcommand{\check@thebibliography}[2]{%
    \@ifnextchar\end{%
      \let\endthebibliography\relax
    }{%
      \org@thebibliography{#1}#2%
    }%
  }%
}
\makeatother

\begin{document}
  \bibliographystyle{alpha}
  \bibliography{test}
\end{document}

No warnings, no pages.

(Edit: matching the example in the question).

Method that checks the protocol file .blg of bibtex

If there are not any citations, bibtex complains with an error:

I found no \citation commands---while reading file test.aux

This error message is also written in the .blg file.

The patch looks for that error message and excludes the bibliography, if the error message can be found.

\documentclass{article}

\makeatletter
\newif\ifblg@empty
\IfFileExists{\jobname.blg}\@firstofone\@gobble{%
  \RequirePackage{xstring}%
  \begingroup
    \fullexpandarg
    % Method for reading the file is taken from `graphics.sty'.
    \newif\ifblg@read
    \blg@readtrue
    \openin\@inputcheck=\jobname.blg\relax
    \let\do\@makeother
    \dospecials
    \loop
      \read\@inputcheck to\@tempa
      \ifeof\@inputcheck
        \blg@readfalse
      \else
        \IfBeginWith*{\@tempa}
                    {I found no \@backslashchar citation commands}{%
          \global\blg@emptytrue
          \blg@readfalse
        }{}%
      \fi
      \ifblg@read
    \repeat
    \immediate\closein\@inputcheck
  \endgroup
}
\ifblg@empty
  \RequirePackage{version}%
  \excludeversion{thebibliography}%
\fi
\makeatother

\begin{document}
  \bibliographystyle{alphadin}
  \bibliography{test}
\end{document}

Method based on empty argument of thebibliography

@egreg suggested to check the argument of thebibliography. If it is empty, then it is assumed that the bibliography is empty (updated):

\documentclass{article}
\usepackage{version}
\makeatletter
\@ifdefinable{org@thebibliography}{%
  \let\org@thebibliography\thebibliography
  \def\thebibliography#1{%
    \def\@tempa{#1}%
    \ifx\@tempa\@empty
      \expandafter\@firstoftwo
    \else
      \expandafter\@secondoftwo
    \fi
    {%
      \let\endthebibliography\relax
      \end{thebibliography}%
      \excludeversion{thebibliography}%
      \begin{thebibliography}{}%
    }{%
      \org@thebibliography{#1}%
    }%
  }%
}
\makeatother

\begin{document}
  \bibliographystyle{alphadin}
  \bibliography{test}
\end{document}
share|improve this answer
    
Thanks, but this still gives me the warning... I don't use natbib, could this be the reason? –  Foo Bar Sep 28 '12 at 10:20
    
There are tons of bibliography packages. I had to use one for an example. If you are using biblatex, then it does not use the environment thebibliography, but it suppresses the section title if there aren't references. It gives a warning. Do you mean that warning? –  Heiko Oberdiek Sep 28 '12 at 10:27
    
I added a minimal exampel and the exact warning. You code does not solve the problem. –  Foo Bar Sep 28 '12 at 10:50
    
Your example works for me with the patch and without warning. What's the contents of your empty .bbl file? –  Heiko Oberdiek Sep 28 '12 at 10:54
1  
@HeikoOberdiek Another idea might be to check whether the argument to \begin{thebibliography} is empty. –  egreg Sep 28 '12 at 11:01

No citation in the document will make BibTeX write

\begin{thebibliography}{}

in the .bbl file. So we can check whether the argument is empty, because a citation will put the widest entry in the argument.

\documentclass{article}

\let\origthebibliography\thebibliography
\let\origendthebibliography\endthebibliography
\newif\ifemptybibliography
\renewenvironment{thebibliography}[1]
 {\if\relax\detokenize{#1}\relax\emptybibliographytrue\else\origthebibliography{#1}\fi}
 {\ifemptybibliography\else\origendthebibliography\fi}


\begin{document}
x%\cite{HH}


  \bibliographystyle{alphadin}
  \bibliography{/usr/local/texlive/2012/texmf-dist/doc/plain/texbytopic/tex}
\end{document}

Check uncommenting the citation.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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