9

I'm writing a BibTeX parser and I was wondering if I ever need to preserve the white space in the input file, or if I can safely ignore it. More specifically:

  1. Is title={Title} identical to title={ Title } and title=" Title "?
  2. Is title={The title} identical to title={The title}?
  3. Is there any input file where BibTeX would produce different output if I first replaced all sequences of white space by a single space?
  • Are you referring to BibTeX's so-called "purification" step, in which, say, {\H o} and {\v r} are replaced by o and r, respectively? And, are you concerned about interword whitespace, e.g., the distinction between Hello World and HelloWorld? – Mico Oct 2 '15 at 4:55
  • Put differently, are you writing a BibTeX parser or a bib file parser? – Mico Oct 2 '15 at 5:08
  • Spaces outside field contents are mostly irrelevant, but, of course, title={Title} and title={ Title } are different. – egreg Oct 2 '15 at 8:43
  • @Mico I'm writing a parser for bib files. I intend to keep single spaces (so Hello World would be unchanged), but replace longer sequences of spaces, tabs, newlines, etc. by a single space. (So Hello World would become Hello World. – Mangara Oct 2 '15 at 11:55
  • @Mangara It seems that BibTeX strips spaces at either end of a field. – egreg Oct 2 '15 at 12:04
8
+50

Is title={Title} identical to title={ Title } and title=" Title "?

Yes. See below for a sample .bib file and the result of running it through BibTeX, viz., a corresponding .bbl file. The .bbl file is created by BibTeX and is read by LaTeX during subsequent compilations; LaTeX, not BibTeX, does the actual typesetting the formatted bibliography. To study what BibTeX does, then, it is necessary to examine the .bbl file.

Is title={The title} identical to title={The title}?

Yes. Again, see below.

Is there any input file where BibTeX would produce different output if I first replaced all sequences of white space by a single space? [emphasis added]

I could imagine that if one concocted some fiendishly clever LaTeX macros that expand to various forms of whitespace, and if one inserted such macros in the title field (or any other field, for that matter), BibTeX might end up generating different results depending on whether or not any hard-coded whitespace groups present in the input were replaced by a single whitespace character. (I'm assuming the bib file parser you're building doesn't process, i.e., expand LaTeX macros that may be present in a field.) Conversely, if no such fiendish macros are around, i.e., if all the whitespace that's present in a given field is "hard-coded" as space, tab, or newline characters, replacing sequences of whitespace characters with a single whitespace character should be entirely innocuous.


Here's a test bib file with five entries. The first three serve to test the effects of inserting various amounts of whitespace at the start and end of the title field; the final two serve to test the effects of various amounts of interword whitespace betweeen words in the title field.

@article{test1,
  author = "xyz",
  title  = {Title},
  journal= "Test",
  year   = 3001,
}
@article{test2,
  author = "xyz",
  title  = {   Title   },
  journal= "Test",
  year   = 3002,
}
@article{test3,
  author = "xyz",
  title  = " Title ",
  journal= "Test",
  year   = 3003,
}
@article{test4,
  author = "xyz",
  title  = {The Title},
  journal= "Test",
  year   = 3004,
}
@article{test5,
  author = "xyz",
  title  = {The     Title},
  journal= "Test",
  year   = 3005,
}

Loading the natbib package and processing the bib file with the bibliography style plainnat produces the following .bbl file:

\begin{thebibliography}{5}
...

\bibitem[xyz(3001)]{test1}
xyz.
\newblock Title.
\newblock \emph{Test}, 3001.

\bibitem[xyz(3002)]{test2}
xyz.
\newblock Title.
\newblock \emph{Test}, 3002.

\bibitem[xyz(3003)]{test3}
xyz.
\newblock Title.
\newblock \emph{Test}, 3003.

\bibitem[xyz(3004)]{test4}
xyz.
\newblock The title.
\newblock \emph{Test}, 3004.

\bibitem[xyz(3005)]{test5}
xyz.
\newblock The title.
\newblock \emph{Test}, 3005.

\end{thebibliography}

As you can tell, the content of the formatted title field is identical in the first three cases, and it is also identical in the final two cases.

If you extended the preceding example to include various amounts of whitespace at the beginning and end of other fields as well as between words in other fields, you'd find that the behavior shown above for the title field caries over to other fields as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.