如何重新定义书目以将书目列表放入表格中?

如何重新定义书目以将书目列表放入表格中?

环境bibliography产生一个列表。

但是我想制作一个tabular有两列的表格,第一列是id,比如12...,第二列是bib的内容。

有人有解决方案吗?

结果看起来应该是这样的:

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

更新:

我尝试从头开始编写代码,并得到一个可行的解决方案。

\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}

然而,有两个问题:

  • 第一行的第一个单元格没有居中对齐。
  • 我必须将其添加\\\hline到最后一项的末尾,因为我不能将其放在之前\endtabularx,否则它会抱怨\hline noalign

如何解决这些问题?

更新:

我把 替换tabularxtabu,上述问题解决了。但是\cite链接断了。我不知道 是如何\bibitem定义链接目标的,那么 的对应\label项是什么?\cite\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}

更新:

我解决了链接问题。

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

因此\cite\hyper@link链接到\@tbiblink

答案1

书目表

tabular可以直接这样做,但tabularx需要environ包裹来收集尸体。但是hyperref通过的链接\cite不起作用,这是另一回事。

\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}}

答案2

最终解决方案已给出。可能ltxtable比 更好tabu,但目前这不是问题。

\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}

编译命令为:

pdflatex test
bibtex   test
pdflatex test
pdflatex test

结果如下:

在此处输入图片描述

相关内容