[Tex/LaTex] BibTeX: Help in modifying .bst style file requested

bibliographiesbibtex

I am using BibTeX for my bibliography and everything works as expected. For formatting output I am using natdin, which does almost exactly what I want, except for one or two small things:

It uses -- to denote page ranges, and I'd like to have a spatium before and after the dash, i.e. use \,--\,. When I look into the .bst file, I find this:

FUNCTION {n.dashify}
{ 't :=
  ""
    { t empty$ not }
    { t #1 #1 substring$ "-" =
        { t #1 #2 substring$ "--" = not
            { "--" *
              t #2 global.max$ substring$ 't :=
            }
            {   { t #1 #1 substring$ "-" = }
                { "-" *
                  t #2 global.max$ substring$ 't :=
                }
              while$
            }
          if$
        }
        { t #1 #1 substring$ *
          t #2 global.max$ substring$ 't :=
        }
      if$
    }
  while$
}

Unfortunately I have no idea what this does, exactly, or how to modify it in order to accomplish what I am looking for.

So here's an MWE:

\documentclass[paper=a4]{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,ngerman]{babel}
\usepackage[square]{natbib}

\usepackage{lipsum}

\begin{document}
    \section{Lorem Ispum}
        According to \citet{Nescio.2000} we find that 
        \lipsum[1]

    \bibliography{test}
    \bibliographystyle{natdin} 
\end{document}

The accompanying test.bib file contains

@article{Nescio.2000,
 author = {Nescio, Nomen and Public, John Q. and Else, Somebody},
 year = {2000},
 title = {What Miscellaneous Anomaly is This? A Field Guide for the Utterly Confused},
 pages = {95--105},
 volume = {08/15},
 journal = {Internationale Zeitschrift für Tetrapilotomie und Potiosektion}
 }

natdin is widely available on CTAN and used as is.

As you can see in the rendered example, I get S. 95-105 when I really want S.~95\,--\,105.

Best Answer

The n.dashify function is used for both page number ranges and other elements like ISBN numbers, so in order to get the style to work the way you want, we need to separate out those two uses in the style. The simplest way to do this is to create a version of the function for page ranges (which we will call n.dashify) and create a new version (like the previous n.dashify which we will call n.dashify.plain. Additionally, we need to change the functions that process non-page number ranges to use the n.dashify.plain function instead of our new function.

First, make a copy of natdin.bst and put it in your local texmf folder (on a TeX Live system this is usually ~/texmf/tex/bibtex/bst (~/Library/texmf/tex/bibtex/bst on a Mac)).

Then, make a copy of the original n.dashify function in the file and give it a new name: n.dashify.plain

Next, replace the old n.dashify function with the following one:

FUNCTION {n.dashify}
{ 't :=
  ""
    { t empty$ not }
    { t #1 #1 substring$ "-" =
        { t #1 #2 substring$ "--" = not
            { "\,--\," *
              t #2 global.max$ substring$ 't :=
            }
            {   { t #1 #1 substring$ "-" = }
                { "" *
                  t #2 global.max$ substring$ 't :=
                }
              while$
             "\,--\," * }
          if$
        }
        { t #1 #1 substring$ *
          t #2 global.max$ substring$ 't :=
        }
      if$
    }
  while$
}

You should now have two n.dashify type functions in the .bst file: n.dashify and n.dashify.plain.

Finally, change the following two functions (which format URLs and ISSN/ISBNs) to use the n.dashify.plain function:

FUNCTION { format.doi.urn }
{ urn empty$
     { doi empty$
          { "" }
          { "DOI" doi n.dashify.plain tie.or.space.connect }
       if$
     }
     { "URN" urn n.dashify.plain tie.or.space.connect }
  if$
}

FUNCTION { format.isbn.issn }
{ isbn empty$
     { issn empty$
          { "" }
          { "ISSN" issn n.dashify.plain tie.or.space.connect }
       if$
     }
     { "ISBN" isbn n.dashify.plain tie.or.space.connect }
  if$
}

Here's a sample document. I've duplicated the bib entries to show the behaviour with pages entries of the form n--m and n-m in the .bib file (see discussion in the comments above) and added some ISBN numbers to check that things work as expected.

\begin{filecontents}{\jobname.bib}
@article{Nescio.2000,
 author = {Nescio, Nomen and Public, John Q. and Else, Somebody},
 year = {2000},
 title = {What Miscellaneous Anomaly is This? A Field Guide for the Utterly Confused},
 pages = {95--105},
 volume = {08/15},
 journal = {Internationale Zeitschrift für Tetrapilotomie und Potiosektion},
 isbn = {1-84356-028-3}
 }
@article{Nescio.2000b,
 author = {Nescio, Nomen and Public, John Q. and Else, Somebody},
 year = {2000},
 title = {What Miscellaneous Anomaly is This? A Field Guide for the Utterly Confused},
 pages = {95-105},
 volume = {08/15},
 journal = {Internationale Zeitschrift für Tetrapilotomie und Potiosektion},
 issn = {1-84356-028-3}
 }
\end{filecontents}
\documentclass[paper=a4]{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,ngerman]{babel}
\usepackage[square]{natbib}

\usepackage{lipsum}

\begin{document}
    \section{Lorem Ispum}
        According to \citet{Nescio.2000b,Nescio.2000} we find that 
        \lipsum[1]

    \bibliography{\jobname}
    \bibliographystyle{natdin-copy} 
\end{document}

output of code