5

The bibliography environment results in a list.

However, I want to produce a tabular with two columns. The first column is the id, like 1, 2..., and the second column is the content of the bib.

Any one have a solution?

This result should look like:

----------------------
| 1 | this is bib one |
----------------------
| 2 | this is bib two |
----------------------

UPDATE:

I try to code from scratch, and get a workable solution.

\documentclass{article}
\usepackage{array}
\usepackage{tabularx}

\makeatletter
\renewcommand\@biblabel[1]{#1}
\newenvironment{tablebibliography}%
{\center\tabularx{\textwidth}{|c|X|}\hline%
\setcounter{enumiv}{0}%
\let\p@enumiv\@empty%
\renewcommand\theenumiv{\@arbic\c@enumiv}}%
{%\\\hline
\endtabularx\endcenter}

\def\@@preparetbibitem{%
\addtocounter{enumiv}{1}%
\ifnum\the\value{enumiv}>1\\\hline\fi}

\def\tbibitem{\@ifnextchar[\@tlbibitem\@tbibitem}
\def\@tlbibitem[#1]#2{\@@preparetbibitem\@biblabel{#1} & \if@filesw%
{\let\protect\noexpand\immediate\write\@auxout%
{\string\bibcite{#2}{#1}}}\fi\ignorespaces}

\def\@tbibitem#1{\@@preparetbibitem\@biblabel{\the\value{enumiv}} & \if@filesw%
\immediate\write\@auxout%
{\string\bibcite{#1}{\the\value{enumiv}}}\fi\ignorespaces}
\makeatother

\begin{document}
This is reference\cite{itemone}, this is another reference\cite{itemtwo}.
\begin{tablebibliography}
\tbibitem{itemone} first item
\tbibitem[label]{itemtwo} second item
\tbibitem{itemthree} 3rd item \\\hline
\end{tablebibliography}
\end{document}

However, there are two issues:

  • the 1st cell of the 1st line is not center-aligned.
  • I must add \\\hline at the end of the last item, because I can't put it before \endtabularx, or it will complain \hline noalign.

How to solve these problems?

UPDATE:

I replace tabularx with tabu, and the problems metioned above are solved. But the \cite link is broken. I don't kown how the \bibitem defines the link destination, so, what is the \label counterpart for \cite as to \ref?

\usepackage{longtable}
\usepackage{tabu}
\newenvironment{tablebibliography}%
{\center\longtabu to \textwidth{|X[-1,c]|X[1,p]|}\hline%
\setcounter{enumiv}{0}%
\let\p@enumiv\@empty%
\renewcommand\theenumiv{\@arbic\c@enumiv}}%
{\\\hline
\endlongtabu\endcenter}

UPDATE:

I solved the link problem.

\def\@tbiblink#1{%
\Hy@raisedlink{\hyper@anchorstart{cite.#1}\relax\hyper@anchorend}}

So \cite or \hyper@link links to \@tbiblink.

2 Answers2

4

bib tabular

With tabular I could do it directly, but with tabularx I needed the environ package to collect the body. However hyperref links via \cite do not work, this is another matter.

\documentclass{article}
\usepackage{array}
\usepackage{tabularx}
\usepackage{environ}

\newcounter{tbib}

\makeatletter
\def\tbibitem{\ifnum\value{tbib}>0 \expandafter\\\fi
              \refstepcounter{tbib}%
              \@ifnextchar[\@ltbibitem\@tbibitem}

\def\@ltbibitem[#1]#2{\if@filesw {\let\protect\noexpand
                      \immediate\write\@auxout{\string\bibcite{#2}{#1}}}\fi 
                      #1&}

\def\@tbibitem#1{\if@filesw \immediate\write\@auxout
                  {\string\bibcite{#1}{\the\value{tbib}}}\fi 
                 \arabic{tbib}&}
\makeatother

\NewEnviron{thetablebibliography}%
{\setcounter{tbib}{0}%
 \let\bibitem\tbibitem
 \begin{center}
 \begin{tabularx}{\textwidth}{|c|X|}
 \hline \BODY\\\hline
 \end{tabularx}\end{center}}

\makeatother

%% but \cite does not create valid links with 
%% \usepackage{hyperref}

\begin{document}\thispagestyle{empty}
This is reference~\cite{itemone}, this is another reference~\cite{itemtwo}.

\begin{thetablebibliography}
\bibitem{itemone} first item
\bibitem[label]{itemtwo} second item
\bibitem{itemthree} 3rd item
\end{thetablebibliography}

\end{document}
% version avec tabular qui marche sans environ 
% \def\tbibitem{\tbibnewline\refstepcounter{tbib}%
%               \@ifnextchar[\@ltbibitem\@tbibitem}
% \newenvironment{thetablebibliography}%
% {\setcounter{tbib}{0}%
%  \let\bibitem\tbibitem
%  \def\tbibnewline{\gdef\tbibnewline{\tabularnewline}}%
%  \begin{center}
%  \begin{tabular}{|c|c|}
%  \hline }
% {\\\hline\end{tabular}\end{center}}
0

The final solution is presented. May be ltxtable is better than tabu, but it's not an issue for now.

\documentclass{article}

\usepackage{hyperref}
\usepackage{tabu}
\usepackage{longtable}
\newcommand\usetablebibliography{%
\renewenvironment{thebibliography}[1]{\begin{tablebibliography}}{\end{tablebibliography}}%
\renewcommand{\bibitem}{\tbibitem}%
}%
\makeatletter
\newcommand\settablebibheader[2]{\providecommand\tbibheaderone{#1}\providecommand\tbibheadertwo{#2}}
\renewcommand\@biblabel[1]{#1}
\newenvironment{tablebibliography}%
{\center\longtabu to \textwidth{|X[-1,c]|X[1,p]|}\hline%
\setcounter{enumiv}{0}%
\let\p@enumiv\@empty%
\renewcommand\theenumiv{\@arbic\c@enumiv}%
\@ifundefined{tbibheaderone}{}{\tbibheaderone & \tbibheadertwo \\\hline}%
}%
{\\\hline
\endlongtabu\endcenter}

\def\@@preparetbibitem{%
\addtocounter{enumiv}{1}%
\ifnum\the\value{enumiv}>1\\\hline\fi}

\def\@tbiblink#1{%
\Hy@raisedlink{\hyper@anchorstart{cite.#1}\relax\hyper@anchorend}}

\def\tbibitem{\@ifnextchar[\@tlbibitem\@tbibitem}

\def\@tlbibitem[#1]#2{\@@preparetbibitem\@tbiblink{#2}\@biblabel{#1} & \if@filesw%
{\let\protect\noexpand\immediate\write\@auxout%
{\string\bibcite{#2}{#1}}}\fi\ignorespaces}

\def\@tbibitem#1{\@@preparetbibitem\@tbiblink{#1}\@biblabel{\the\value{enumiv}} & \if@filesw%
\immediate\write\@auxout%
{\string\bibcite{#1}{\the\value{enumiv}}}\fi\ignorespaces}
\makeatother


\begin{filecontents}{\jobname.bib}
@Book{test1,
    author    = {Goossens, Michel and Mittelbach,
        Frank and Samarin, Alexander},
    title     = {The LaTeX Companion},
    publisher = {Addison-Wesley},
    location  = {Reading, Mass.},
    year      = {1994},
}
\end{filecontents}

\begin{document}

this is a citation\cite{test1}

\settablebibheader{header1}{header2}
\usetablebibliography
\bibliographystyle{unsrt}
\bibliography{\jobname}
\end{document}

The compile command is:

pdflatex test
bibtex   test
pdflatex test
pdflatex test

And the result looks like:

enter image description here