## Tuesday, September 23, 2008

### Fixing natbib: Adding tie between author and citation

Something that bugs me about natbib is that \citet puts a formal SPACE between the authors and the citation list, and so LaTeX like...
. . . as discussed by \citet{SK86}.
that shows up near the end of a typeset line can turn into
as discussed by Stephens and Krebs[1].
Not only does this put a number at the beginning of a line, which is a typography no-no when you aren't starting a numerical item in a list, but it separates the citation list from its neighbors. If natbib would just us a TIE (~ in TeX) instead, then the "Krebs" in the example above would never get separated from the "[1]."

The best solution is to fix natbib so that this character is configurable. That would really help when using superscript references because you could get rid of the character all together (right now superscripts get a space before them, and that looks a little silly (especially when superscripts show up on the next line!)). However, that could take a while to get fixed (CTAN isn't quite SourceForge)... So I put together the following fix, which should work OK for \citet, \citet*, \Citet, and \Citet* regardless of how many optional arguments (0, 1, or 2) are present.

(note: the \makeatletter and \makeatother commands are not needed if these lines are put into a .sty file or in a natbib.cfg file (in the same directory as your TeX document source).
% Unfortunately, natbib does not TIE textual % references to their citations. So authors % sometimes get separated from citations when % they come at the end of the line. The following % lines attempt to fix this problem.%% The lines below do the equivalent of . . .%%       \renewcommand\citet[1]%%           {\citeauthor{#1}~\citep{#1}}%% but they handle the star and capitalization and % optional argument cases too.%% (the \makeatletter and \makeatother are not needed if %  these lines are put into a .sty file or in a natbib.cfg%  file)\makeatletter%%%% These lines test for star and number of arguments %%% and call the workhorses below%% Test for star (mid-sentence and start-sentence forms)\def\citet{\@ifstar{\citetstar}{\citetnostar}}\def\Citet{\@ifstar{\Citetstar}{\Citetnostar}}%% No star found. Now test for argument count.\def\citetnostar%    {\@ifnextchar[{\squarecitet}{\simplecitet}}\def\squarecitet[#1]%    {\@ifnextchar[{\twocitet[#1]}{\onecitet[#1]}}\def\Citetnostar%    {\@ifnextchar[{\squareCitet}{\simpleCitet}}\def\squareCitet[#1]%    {\@ifnextchar[{\twoCitet[#1]}{\oneCitet[#1]}}%% Star found. Now test for argument count.\def\citetstar%    {\@ifnextchar[{\squarecitetstar}{\simplecitetstar}}\def\squarecitetstar[#1]%    {\@ifnextchar[{\twocitetstar[#1]}{\onecitetstar[#1]}}\def\Citetstar%    {\@ifnextchar[{\squareCitetstar}{\simpleCitetstar}}\def\squareCitetstar[#1]%    {\@ifnextchar[{\twoCitetstar[#1]}{\oneCitetstar[#1]}}%\makeatother%%%% The following actually do the \cite work%% The \citet cases (no arg, one arg, and two args)\def\simplecitet#1%    {\citeauthor{#1}~\citep{#1}}\def\onecitet[#1]#2%    {\citeauthor{#2}~\citep[#1]{#2}}\def\twocitet[#1][#2]#3%    {\citeauthor{#3}~\citep[#1][#2]{#3}}%% The \citet* cases (no arg, one arg, and two args)\def\simplecitetstar#1%    {\citeauthor*{#1}~\citep{#1}}\def\onecitetstar[#1]#2%    {\citeauthor*{#2}~\citep[#1]{#2}}\def\twocitetstar[#1][#2]#3%    {\citeauthor*{#3}~\citep[#1][#2]{#3}}%% The \Citet cases (no arg, one arg, and two args)\def\simpleCitet#1%    {\Citeauthor{#1}~\citep{#1}}\def\oneCitet[#1]#2%    {\Citeauthor{#2}~\citep[#1]{#2}}\def\twoCitet[#1][#2]#3%    {\Citeauthor{#3}~\citep[#1][#2]{#3}}%% The \Citet* cases (no arg, one arg, and two args)\def\simpleCitetstar#1%    {\Citeauthor*{#1}~\citep{#1}}\def\oneCitetstar[#1]#2%    {\Citeauthor*{#2}~\citep[#1]{#2}}\def\twoCitetstar[#1][#2]#3%    {\Citeauthor*{#3}~\citep[#1][#2]{#3}}
Even if this problem gets fixed within natbib, it still might serve as a good example of how to deal with TWO or more optional arguments in macros...

UPDATE: For author-year citations (i.e., "Harvard style citations"), you must replace the \citep with \citeyearpar. That is, replace the final section of the code above with this section:
%%% The following actually do the \cite work%% The \citet cases (no arg, one arg, and two args)\def\simplecitet#1%    {\citeauthor{#1}~\citeyearpar{#1}}\def\onecitet[#1]#2%    {\citeauthor{#2}~\citeyearpar[#1]{#2}}\def\twocitet[#1][#2]#3%    {\citeauthor{#3}~\citeyearpar[#1][#2]{#3}}%% The \citet* cases (no arg, one arg, and two args)\def\simplecitetstar#1%    {\citeauthor*{#1}~\citeyearpar{#1}}\def\onecitetstar[#1]#2%    {\citeauthor*{#2}~\citeyearpar[#1]{#2}}\def\twocitetstar[#1][#2]#3%    {\citeauthor*{#3}~\citeyearpar[#1][#2]{#3}}%% The \Citet cases (no arg, one arg, and two args)\def\simpleCitet#1%    {\Citeauthor{#1}~\citeyearpar{#1}}\def\oneCitet[#1]#2%    {\Citeauthor{#2}~\citeyearpar[#1]{#2}}\def\twoCitet[#1][#2]#3%    {\Citeauthor{#3}~\citeyearpar[#1][#2]{#3}}%% The \Citet* cases (no arg, one arg, and two args)\def\simpleCitetstar#1%    {\Citeauthor*{#1}~\citeyearpar{#1}}\def\oneCitetstar[#1]#2%    {\Citeauthor*{#2}~\citeyearpar[#1]{#2}}\def\twoCitetstar[#1][#2]#3%    {\Citeauthor*{#3}~\citeyearpar[#1][#2]{#3}}
Those lines seem to work for me.