[Tex/LaTex] How to make only cited bibitems appear

bibliographies

When citing items using BibTeX, only the cited items show up in the bibliography (unless you use \nocite{*}).
Can I have the same effect for normal \bibitems that are at the end of my .tex file?
(I have a long list that I use for several longer reports.)

Best Answer

If you are prepared to accept that \bibitem should be ended by a blank line, then you can modify some of the code from source2e and refcheck to make this work. You have run latex three times to get the correct numbering in the text.

Sample output

\documentclass{article}

\makeatletter
\let\@@citation@@=\citation

\renewcommand{\citation}[1]{\@@citation@@{#1}%
\@for\@tempa:=#1\do{\@ifundefined{cit@\@tempa}%
  {\global\@namedef{cit@\@tempa}{}}{}}%
}

\def\@lbibitem[#1]#2#3\par{%
  \@ifundefined{cit@#2}{}{\item[\@biblabel{#1}\hfill]}%
  \if@filesw
      {\let\protect\noexpand
       \immediate
       \write\@auxout{\string\bibcite{#2}{#1}}}\fi\ignorespaces
  \@ifundefined{cit@#2}{}{#3}}
\def\@bibitem#1#2\par{%
  \@ifundefined{cit@#1}{}{\item}%
  \if@filesw \immediate\write\@auxout
    {\string\bibcite{#1}{\the\value{\@listctr}}}\fi\ignorespaces
  \@ifundefined{cit@#1}{}{#2}}
\makeatother

\begin{document}

Test a citation \cite{one} and another \cite{three}.  Also one more
for luck: \cite{five}.

\begin{thebibliography}{9}

\bibitem{one} Reference one.

\bibitem{two} Reference two.

\bibitem[Special]{three} Reference three.

\bibitem{four} Reference four.

\bibitem{five} Reference five.

\bibitem[Unusual]{six} Reference six.

\end{thebibliography}

\end{document}

The first part of the code is refchecks modification to the \cite command so that use of a bibitem is recorded. The second part is a modification of the core latex \bibitem, so that it always records the label in the .aux file, but only prints out the body if the item has been cited.

If you need to use this with hyperref so there are links to the bibliography, then the coding has to be adapted to hyperref's versions on \@lbibitem and \@bibitem, since hyperref simply overwrites the current definition. This is given as follows, notice the positioning of the loading of the hyperref package:

\documentclass{article}

\makeatletter
\let\@@citation@@=\citation

\renewcommand{\citation}[1]{\@@citation@@{#1}%
\@for\@tempa:=#1\do{\@ifundefined{cit@\@tempa}%
  {\global\@namedef{cit@\@tempa}{}}{}}%
}
\makeatother

\usepackage{hyperref}

\makeatletter
\def\@lbibitem[#1]#2#3\par{%
  \@ifundefined{cit@#2}{}{\@skiphyperreftrue
  \H@item[%
    \ifx\Hy@raisedlink\@empty
      \hyper@anchorstart{cite.#2\@extra@b@citeb}%
        \@BIBLABEL{#1}%
      \hyper@anchorend
    \else
      \Hy@raisedlink{%
        \hyper@anchorstart{cite.#2\@extra@b@citeb}\hyper@anchorend
      }%
      \@BIBLABEL{#1}%
    \fi
    \hfill
  ]%
  \@skiphyperreffalse}%
  \if@filesw
    \begingroup
      \let\protect\noexpand
      \immediate\write\@auxout{%
        \string\bibcite{#2}{#1}%
      }%
    \endgroup
  \fi
  \ignorespaces
  \@ifundefined{cit@#2}{}{#3}}

\def\@bibitem#1#2\par{%
  \@ifundefined{cit@#1}{}{\@skiphyperreftrue\H@item\@skiphyperreffalse
  \Hy@raisedlink{%
    \hyper@anchorstart{cite.#1\@extra@b@citeb}\relax\hyper@anchorend
    }}%
  \if@filesw
    \begingroup
      \let\protect\noexpand
      \immediate\write\@auxout{%
        \string\bibcite{#1}{\the\value{\@listctr}}%
      }%
    \endgroup
  \fi
  \ignorespaces
  \@ifundefined{cit@#1}{}{#2}}
\makeatother

\begin{document}

Test a citation \cite{one} and another \cite{three}.  Also one more
for luck: \cite{five}.

\begin{thebibliography}{9}

\bibitem{one} Reference one.

\bibitem{two} Reference two.

\bibitem[Special]{three} Reference three.

\bibitem{four} Reference four.

\bibitem{five} Reference five.

\bibitem[Unusual]{six} Reference six.

\end{thebibliography}

\end{document}
Related Question