# Alignment of apalike bibliography style with multibib

I want to use multiple bibliographies with the apalike style and multibib.

This style however has the problem that the numbers are not aligned.

The problem is also described in this question and an answer exists which used to work for me so far because I was not using multibib for the bibliography.

I have the following code.

test_article.tex

\documentclass{article}
\usepackage[numbers]{natbib}
\usepackage{multibib}
\newcites{book,article}{Books,Articles}
\usepackage{etoolbox}

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


article.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}
}


Problem: If I remove multibib and use only a single bibliography everything works fine. I already tried to change the patchcmd from patching \thebibliography to \thebibliographybook and \thebibliographyarticle, but that didn't work either.

This is how it looks now (the article entry is not aligned):

And this is how it should look:

Question: How can I fix this problem and get the alignment correct?

Is there a similar workaround available when using multibib instead a single bibliography?

• It is implied by the linked answer, but just to warn other users: apalike is simply not intended to be used with numeric citations. This can be seen from the fact that the style does not compute the longest citation label and does not pass the longest label on to the thebibliography environment. The linked workaround just forces one particular indentation and does not respond dynamically to the actual longest label. The best solution would be to use a BibTeX style that actually supports numeric citations (or modify apalike to do so). Jan 22, 2021 at 15:24
• Note also that apalike is not a native natbib style. In general this need not be a problem, but I think I once saw an interesting edge case where this was an issue. Unfortunately, I can't find a link at the moment, so treat this as hearsay. Jan 22, 2021 at 15:27
• @moewe Do you know a BibTeX style that is same as apalike but supports numeric citations? I need the style/structure to be the same as in the example. Jan 22, 2021 at 15:43

Before we start, a few words of warning

It is implied by the linked answer, but just to make it explicit for other users: apalike is simply not intended to be used with numeric citations. This can be seen from the fact that the style does not compute the longest citation label and does not pass the longest label on to the thebibliography environment. A .bbl generated with apalike will contain

\begin{thebibliography}{}

\bibitem[Book, 2021]{testBook}
Book, A. (2021).
\newblock {\em This is a test book}.
\newblock Book Publisher.

\end{thebibliography}


and not

\begin{thebibliography}{1}

\bibitem[Book, 2021]{testBook}
Book, A. (2021).
\newblock {\em This is a test book}.
\newblock Book Publisher.

\end{thebibliography}


The linked workaround just forces one particular indentation and does not respond dynamically to the actual longest label. The best solution would be to use a BibTeX style that actually supports numeric citations (or modify apalike to do so).

So one way towards resolving the problem would be to teach apalike to use numbers.

This can be done by transplanting the relevant code from plain.bst back to apalike.bst. The exact steps needed are probably easiest shown in a diff between the original apalike.bst and the modified version apalike-numeric.bst that allows for numeric citations.

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


You can grab the complete apalike-numeric.bst at https://gist.github.com/moewew/c4c9761752b66f245a39a768ea6071e4.

Then

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


I think there are some edge cases where you will still not get the best overall alignment in case the two bibliographies have fewer than 10 entries each, but more than 10 counted together, but I couldn't find a good way to resolve that problem. (Each bibliography will be aligned nicely on its own, but the two bibliographies could have different indents.)

If you want to continue to use a workaround that essentially hard-codes the indent, you can try the following.

The problem with the workaround from Alignment numbered apalike bibliography style in the context of multibib is that thebibliography is redefined by the package so that the patch no longer works.

I propose the following workaround that also patches the bibliography environment (the bit that does the work in multibib is not thebibliography but \std@thebibliography). The patch is slightly different to the linked workaround, we just pretend that the longest label was 10, which should be good if your bibliography contains between 10 and 99 entries. (If you have more or fewer, adjust the 10.)

Unfortunately, we have to do the patching in \AtBeginDocument, where I couldn't get the simple

\patchcmd{\std@thebibliography}
{\@bibsetup{#1}}
{\ifblank{#1}{\@bibsetup{10}}{\@bibsetup{#1}}}
{}{\error}


to work for category code reasons. So we have to use the following monstrosity

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

• This works perfectly. I still have to wait 4 hours before I can award the bounty, will do that ASAP. Thank you very much! Jan 22, 2021 at 16:28