DIY Your BibTeX Style File

This is a short article explaining how to do handy hacks on a BibTeX style file. To customize your own style, you can type in command

latex makebst.tex

to enter an interactive program and generate your own .bst file. But this is VERY tedious. For those of you who just want to modify an existing style file a little bit to cater certain formatting requirement, this article introduces some basic knowledge about a .bst file. I hope that it is a good starting point to guide you into the world of hacking. When reading this article, you will find it convenient to also read an existing style file, say plain.bst, shipped with your latex installation, side by side.

Basic Structure of a BibTeX Style File

The following shows roughly what is inside a .bst file, which logically contains four sections:

ENTRY
{ author
title
journal
year

}
{ … }
{ … }

MACRO { … }{ … }

INTEGERS { … }
STRINGS { … }
FUNCTION { … }{ … }

READ
EXECUTE { … }
ITERATE { … }
SORT
EXECUTE { … }
ITERATE { … }
REVERSE { … }

At the very top is the ENTRY command. The first argument of ENTRY defines legitimate entries in your BibTeX database, such as author, title, journal, year, etc. The second and third augments define internal (but global) integer variables and string variables.

The MACRO commands define abbreviations. For example, the command

MACRO {ieeetcad}{"IEEE Trans. Comput.-Aided Design Integrated Circuits"}

says that if you type `ieeetcad’ in your .bib file, it will be expanded as `IEEE Trans. Comput.-Aided Design Integrated Circuits’ by BibTeX. So instead of typing the long journal name for each cited article in this journal, you only need to enter `ieeetcad’. This trick functions essentially the same as your defining @string macros in your .bib file.

Then come the INTEGERS and STRINGS commands, defining variables that are needed by the immediate FUNCTION. The FUNCTION commands define all the utility functions. It is the most important ingredient in a .bst file. Hacking is mostly done to the FUNCTIONs.

After all the above preparations, BibTeX starts to do its job. The sequence of commands READ, EXECUTE, ITERATE, SORT, EXECUTE, REVERSE… exactly tells the working flow how BibTeX processes and typesets the bibliography entries. First it READs in your BibTeX database file .bib, then it EXECUTEs some commands (defined previously as FUNCTIONs), ITERATEs through all the read-in reference entries, SORTs the entries (e.g. according to author name), and then EXECUTEs, ITERATEs on the sorted entries, REVERSEs, again and again, until all the references are satisfactorily typeset. Note that BibTeX READs only once, but can EXECUTE, ITERATE, REVERSE, and SORT multiple times, until the job is done.

Formatting

All the formatting of the reference entries are done in FUNCTIONs. The FUNCTIONs are implemented in stack-base, i.e., a LIFO fashion. (Well, you really don’t need to understand this, if all what you care about is formatting layout.)

For formatting of author names, let’s locate the FUNCTION format.names. You will see inside the function there is something like

"{ff~}{vv~}{ll}{, jj}" format.name$

This governs how a name is formatted. ff stands for first name, vv for von part, ll for last name, and jj for suffix, such as Sr. and Jr. This format says that the first name is followed immediately (separated by only a space but not a line-break) by the von part, which in turn is immediately followed by last name. After the last name comes a comma and a space, then the suffix. As another example, see the acm style:

"{vv~}{ll}{, jj}{, f.}" format.name$

This format indicates that the von part is presented in the front, immediately followed by last name, then a comma, a space and the suffix. First name goes in the very last, followed by a period. Note that ff means to display the first name fully, while f means that only the initial letter of the first name is displayed.

You will notice that in the acm style, all the letters of a name, except the initial letter, are typeset in smallcaps. This is because in the FUNCTION format.authors, there is a line of code

author format.names scapify

where scapify is another function that small-capifies the lower case letters.

Another example. Read the FUNCTION article. From the codes, you probably get a grasp of how different fields of a reference are listed if the reference is of article type. First are the authors, then comes the article title, which is followed by journal information. The format.journal.vol.num.date is another FUNCTION doing more typesetting. But it only cares about volume, number and date. The pages information is only taken care in the FUNCTION format.pages.

Well, I have to stop here. If I explained all the FUNCTIONs, I probably needed to write a hundred-page book. As a hacker, I am sure that you would like to read the codes in existing style files yourself, and you will know a lot more about how to format in the end.

Further Reading

Patrick W. Daly. A Master Bibliographic Style File, for numerical, author–year, multilingual applications.
Nicolas Markey. Tame the BeaST, The B to X of BibTEX.
Oren Patashnik. Designing BibTeX Styles.
Michael Shell and David Hoadley. BibTEX Tips and FAQ.

Advertisements

14 Responses to “DIY Your BibTeX Style File”


  1. 1 Steffen December 13, 2007 at 8:23 am

    Thanks for that, it didn’t quite solve the trouble I was having with a BibTeX style, but it made me looking into the .bst again, this time finding what I wanted to change. Cheers!

  2. 2 Owen August 7, 2008 at 9:36 am

    Thanks, was useful, and thank you for links too

  3. 3 Rand September 26, 2008 at 6:27 pm

    Hello, I want to change the word “and” to the word “dan” because I am using different language. I changed it in the bibliography page but the citation still give me “and”, for example, if I have 2 authors X and Y, the citation writes (X and Y, 1996), I want to be (X dan Y, 1996). So I have it as the word “dan” in the references but it is “and” when I cite the references. What should I do. I don’t want to use different style of the bibliography, I just want to change this word. In .bst file, I changed every word “and” to “dan” and its worked except in one place which is:

    FUNCTION {and}
    { ‘skip$
    { pop$ #0 }
    if$
    }

    when I changed it, it gave me error.

    I need help
    Thank you

  4. 4 Jie September 27, 2008 at 1:00 am

    Rand, could you please tell me what bst style you use? Or are you using the natbib package? Some common bst styles do not generate citations like `(X and Y, 1996)’. (Instead they just give numeric citations such as `[1]’.) So it is crucial that I know how you generate this bibliography before I can give suggestions.

  5. 5 Rand October 11, 2008 at 8:42 am

    hello Jie,

    I solved the problem, I changed the word “and” to the word “dan” that I want.

    my .bst file is from Chicago style, however I entered so many changes on it to be applicable on my tamplet. And this .bst file give the citation like this: (X and Y, 1996) or X and Y (1996).

    Thank you for the concern.

    I have now new problem. I hope really you can help me.

    I want to add the word “Page” in the top of the page numbers in the table of contents. I have only the numbers in the table of contents and now I want to put Page above of them. How can I do that?

    thanks beforehand,
    regards,
    Rand

  6. 6 Rand October 11, 2008 at 9:59 am

    regarding to the same question above, please also if you can help me with:

    And Also I want to add the word “Page” to the list of tables and list of Figures.

    Rand

  7. 7 Jie October 11, 2008 at 10:34 am

    Rand,

    Do you mean, instead of

    1 Introduction ……………… 1
    1,1 Related work …………. 1
    2 Method …………………… 2

    you want

    Page
    1 Introduction ……………… 1
    1,1 Related work …………. 1
    2 Method …………………… 2

    ?

    If so, this is easy. Add the following line right after \tableofcontents:

    \addtocontents{toc}{\hfill{Page}\par}

    Similarly, for figures and tables, add the following lines:

    \addtocontents{lof}{\hfill{Page}\par}
    \addtocontents{lot}{\hfill{Page}\par}

    By the way, in the above, toc means `table of contents’, `lof’ means `list of figures’, and `lot’ means `list of tables’. You will find .toc, .lof and .lot files after you latex your .tex file.

  8. 8 Rand October 11, 2008 at 6:11 pm

    Than you very much dear Jie,

    Yes I know what toc, lot and lof mean. But I didn’t know the correct sequence of commands that we should use with them.

    I applied the command statement that you gave me, it is worked. I just changed \hfill in to \hspace{} so that I can control the horizontal distance.

  9. 9 Rand November 21, 2008 at 8:17 am

    Hello Dear Jie,

    I have one chapter in my thesis as “main title” and “subtitle”. How can I write it in \chapter{…} such that the main title will appear in first line then the subtitle in the second line. I mean I want the full title to appear something like this:

    CHAPTER 1
    ROUNDING THEOREM
    The Possibilty of Applying the Decimal Numbers

    the command that I have is:
    \chapter{….}
    I want the subtitle smaller than the main title. Also I want the full title to appear in the table of contents.

    Is there any possibility to do that

    Dear Jie, if there is an answer for this, then can you please send it also to my email randalfaris@yahoo.co.uk because I sometimes didn’t log in this website.

    Thank you before hand.
    best regards

  10. 10 Jie November 21, 2008 at 3:41 pm

    Rand,

    You can do something like this:

    \chapter[main title\\subtitle]{main title\\{\huge subtitle}}

    The contents inside the curly braces will show exactly what you want, while the texts inside the square bracket appear in your table of contents. The \huge is an ad hoc command to adjust the font size of the subtitle.

    It will still be better to define a macro to use if you have subtitles for all the chapters:

    \newcommand\mychapter[2]{\chapter[#1\\#2]{#1\\{\huge #2}}}

  11. 11 Stefan May 16, 2009 at 7:43 am

    Hi Jie,

    I created a custom *.bst-file with makebst, which worked perfectly for my Bibliography.
    When I am citing (with natbib), I get the following label within the text: ‘AuthorX et al. (2005, 384)’ (doing \cite[384]{authorx}. But how can I replace the comma with a colon, like ‘AuthorX et al. (2005: 384)’?
    I have tried a lot, like finding the entry in the *.bst-file, re-running the tiring makebst-procedure or using the \bibpunct-function (with \bibpunct{(}{)}{;}{a}{}{:}) but without any success. It can’t be too difficult though!? It would be great to get a hint on that.

    Thanks in advance

    Stefan

    • 12 Stefan May 16, 2009 at 8:10 am

      Ok, sorry, just found it out: I didn’t use the \bibpunct command properly (put it on the wrong line), and now got the right label in doing:

      \bibpunct[: ]{(}{)}{;}{a}{}{:}

      Citation comes as follows:
      \citep[485]{authorx97} -> (Authorx 1997: 485)

      That’s what I was looking for.

      Stefan


  1. 1 First initials only in BibTeX « Mostly Ramblings Trackback on January 11, 2009 at 12:53 pm
  2. 2 Tomás S. Grigera Trackback on October 15, 2009 at 10:44 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Categories

Blog Stats

  • 244,135 hits

%d bloggers like this: