How to make only cited bibitems appear?
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.
\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 refcheck
s 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}
You could change your input format to put the text in an argument. Then it is rather easy to suppress one entry:
\documentclass{article}
\usepackage{xpatch}
\makeatletter
\xpretocmd\@citex{\csgdef{used@cite@#2}{x}}{}{\failed}
\newcommand\checkbibentry[2]{\ifcsdef{used@cite@#1}{\bibitem{#1}{#2}}{}}
\makeatletter
\begin{document}
\cite{a} \cite{c}
\begin{thebibliography}{99}
\checkbibentry{a}{some text to a}
\checkbibentry{b}{some text to b}
\checkbibentry{c}{some text to c}
\end{thebibliography}
\end{document}