我想使用具有和apalike
风格的多个书目multibib
。
然而这种样式存在数字不对齐的问题。
该问题还描述于这问题和一个回答存在,到目前为止对我来说很有用,因为我没有使用多书目来编写参考书目。
我有以下代码。
测试文章.tex
\documentclass{article}
\usepackage[numbers]{natbib}
\usepackage{multibib}
\newcites{book,article}{Books,Articles}
\usepackage{etoolbox}
% This is the workaround from the linked answer
\makeatletter
\patchcmd{\thebibliography}{\c@NAT@ctr\z@}{\c@NAT@ctr\z@
\renewcommand{\makelabel}[1]{\makebox[1.2em][r]{##1\hfill}}%
\setlength{\labelwidth}{1.2em}%
\setlength{\labelsep}{.5em}%
\setlength{\leftmargin}{\dimexpr\labelwidth+\labelsep}%
}{}{}
\makeatother
% end of the workaround
\begin{document}
Test \citebook{testBook} and \citearticle{testArticle}.
\bibliographystylebook{apalike}
\bibliographystylearticle{apalike}
\bibliographybook{bookBib}
\bibliographyarticle{articleBib}
\end{document}
bookBib.bib:
@book{testBook,
title={This is a test book},
author={Book, Author},
year={2021},
publisher={Book Publisher}
}
文章.bib:
@article{testArticle,
title={This is a test article with a long title that should display that an entry with more than one lines is not aligned},
author={Article, Author},
journal={Test Journal},
volume={1},
number={1},
pages={1--2},
year={2021},
publisher={Article Publisher}
}
问题:如果我删除multibib
并仅使用单个参考书目,则一切正常。我已经尝试将 patchcmd 从 patching 更改\thebibliography
为\thebibliographybook
and \thebibliographyarticle
,但这也没有用。
现在看起来是这样的(文章条目未对齐):
它看起来应该是这样的:
问题:我该如何修复此问题并实现正确对齐?
multibib
当使用单个参考书目时,是否有类似的解决方法?
答案1
在我们开始之前,先说几句警告
链接的答案暗示了这一点,但只是为了向其他用户明确说明:apalike
根本不打算与数字引用一起使用。这可以从样式不计算最长引用标签并且不将最长标签传递给环境这一事实中看出thebibliography
。.bbl
生成的带有apalike
将包含
\begin{thebibliography}{}
\bibitem[Book, 2021]{testBook}
Book, A. (2021).
\newblock {\em This is a test book}.
\newblock Book Publisher.
\end{thebibliography}
并不是
\begin{thebibliography}{1}
\bibitem[Book, 2021]{testBook}
Book, A. (2021).
\newblock {\em This is a test book}.
\newblock Book Publisher.
\end{thebibliography}
链接的解决方法只是强制一个特定的缩进,而不会动态响应实际最长的标签。最好的解决方案是使用真正支持数字引用的 BibTeX 样式(或进行修改apalike
以支持数字引用)。
因此解决这个问题的方法之一就是教会apalike
使用数字。
这可以通过将相关代码从 移植plain.bst
回 来完成apalike.bst
。所需的确切步骤可能最容易在原始版本和允许数字引用的apalike.bst
修改版本之间的差异中显示。apalike-numeric.bst
--- apalike.bst 2010-12-10 10:19:51.000000000 +0100
+++ apalike-numeric.bst 2021-01-22 16:58:26.762782000 +0100
@@ -1,3 +1,14 @@
+%%%% apalike-numeric.bst
+%%%%
+%%%% a slight modification of apalike that still computes the longest
+%%%% numeric label (so we assume numeric citations unlike the normal
+%%%% apalike setup)
+%%%%
+%%%% 2021-01-22 MW
+%%%%
+%%%% https://tex.stackexchange.com/q/579509/35864
+%%%%
+%%%%
% BibTeX `apalike' bibliography style (version 0.99a, 8-Dec-10), adapted from
% the `alpha' style, version 0.99a; for BibTeX version 0.99a.
%
@@ -65,7 +76,7 @@
year
}
{}
- { label extra.label sort.label }
+ { label extra.label sort.label numlabel }
INTEGERS { output.state before.all mid.sentence after.sentence after.block }
@@ -1028,7 +1039,7 @@
INTEGERS { last.extra.num } % there are none in the bibliography
-FUNCTION {initialize.extra.label.stuff} % and hence there is no `longest.label'
+FUNCTION {initialize.extra.label.stuff}
{ #0 int.to.chr$ 'last.label :=
"" 'next.extra :=
#0 'last.extra.num :=
@@ -1083,12 +1094,37 @@
SORT % by sort.label, year, title---giving final bibliography order
+STRINGS { longest.numlabel }
+
+INTEGERS { number.numlabel longest.numlabel.width }
+
+FUNCTION {initialize.longest.numlabel}
+{ "" 'longest.numlabel :=
+ #1 'number.numlabel :=
+ #0 'longest.numlabel.width :=
+}
+
+FUNCTION {longest.numlabel.pass}
+{ number.numlabel int.to.str$ 'numlabel :=
+ number.numlabel #1 + 'number.numlabel :=
+ numlabel width$ longest.numlabel.width <
+ 'skip$
+ { numlabel 'longest.numlabel :=
+ numlabel width$ 'longest.numlabel.width :=
+ }
+ if$
+}
+
+EXECUTE {initialize.longest.numlabel}
+
+ITERATE {longest.numlabel.pass}
+
FUNCTION {begin.bib}
{ preamble$ empty$ % no \etalchar in apalike
'skip$
{ preamble$ write$ newline$ }
if$
- "\begin{thebibliography}{}" write$ newline$ % no labels in apalike
+ "\begin{thebibliography}{" longest.numlabel * "}" * write$ newline$
}
EXECUTE {begin.bib}
apalike-numeric.bst
您可以在以下位置获取完整内容https://gist.github.com/moewew/c4c9761752b66f245a39a768ea6071e4。
然后
\documentclass{article}
\usepackage[numbers]{natbib}
\usepackage{multibib}
\newcites{book,article}{Books,Articles}
\begin{filecontents}{articleBib.bib}
@article{testArticle,
title={This is a test article with a long title that should display that an entry with more than one lines is not aligned},
author={Article, Author},
journal={Test Journal},
volume={1},
number={1},
pages={1--2},
year={2021},
publisher={Article Publisher}
}
\end{filecontents}
\begin{filecontents}{bookBib.bib}
@book{testBook,
title={This is a test book},
author={Book, Author},
year={2021},
publisher={Book Publisher}
}
\end{filecontents}
\begin{document}
Test \citebook{testBook} and \citearticle{testArticle}.
\bibliographystylebook{apalike-numeric}
\bibliographystylearticle{apalike-numeric}
\bibliographyarticle{articleBib}
\bibliographybook{bookBib}
\end{document}
看起来已经好多了。
我认为存在一些极端情况,如果两个参考书目各自的条目数少于 10 个,但合计条目数超过 10 个,您仍然无法获得最佳的整体对齐效果,但我找不到解决该问题的好方法。(每个参考书目都会单独对齐,但两个参考书目可能有不同的缩进。)
如果您想继续使用本质上对缩进进行硬编码的解决方法,您可以尝试以下操作。
解决方法的问题对齐编号阿帕类似书目样式在的上下文中,multibib
它thebibliography
被包重新定义,因此补丁不再起作用。
我建议使用以下解决方法,它还会修补参考书目环境(在 中起作用的部分multibib
不是thebibliography
,而是\std@thebibliography
)。该修补程序与链接的解决方法略有不同,我们只是假装最长的标签是10
,如果您的参考书目包含 10 到 99 个条目,这应该很好。(如果您有更多或更少,请调整10
。)
不幸的是,我们必须进行修补\AtBeginDocument
,我无法获得简单的
\patchcmd{\std@thebibliography}
{\@bibsetup{#1}}
{\ifblank{#1}{\@bibsetup{10}}{\@bibsetup{#1}}}
{}{\error}
由于类别代码的原因,因此我们必须使用以下怪异的方法
\documentclass{article}
\usepackage[numbers]{natbib}
\usepackage{multibib}
\newcites{book,article}{Books,Articles}
\usepackage{etoolbox}
\makeatletter
\newcommand*{\patch@std@thebibliography}{}
\edef\patch@std@thebibliography{%
\noexpand\patchcmd{\noexpand\std@thebibliography}
{\noexpand\@bibsetup{\string#1}}
{\noexpand\ifblank{\string#1}{\noexpand\@bibsetup{10}}{\noexpand\@bibsetup{\string#1}}}
{}{\noexpand\error}}
\AtBeginDocument{\patch@std@thebibliography}
\makeatother
\begin{filecontents}{articleBib.bib}
@article{testArticle,
title={This is a test article with a long title that should display that an entry with more than one lines is not aligned},
author={Article, Author},
journal={Test Journal},
volume={1},
number={1},
pages={1--2},
year={2021},
publisher={Article Publisher}
}
\end{filecontents}
\begin{filecontents}{bookBib.bib}
@book{testBook,
title={This is a test book},
author={Book, Author},
year={2021},
publisher={Book Publisher}
}
\end{filecontents}
\begin{document}
Test \citebook{testBook} and \citearticle{testArticle}.
\bibliographystylebook{apalike}
\bibliographystylearticle{apalike}
\bibliographybook{bookBib}
\bibliographyarticle{articleBib}
\end{document}