1

I want to write a sequence of LaTeX commands to bbl auxiliaries via the special BibTeX declaration @PREAMBLE, and I want break line where I want. Is there any trick to do so?

For example the following BibTeX code

@PREAMBLE{"\makeatletter"}
@PREAMBLE{"\providecommand\dosomething[0]{}"}
@PREAMBLE{"\makeatother"}

will add the following preamble to the .bbl output

\makeatletter\providecommand\dosomething[0]{}\makeatother

I would like to see something like

\makeatletter
\providecommand\dosomething[0]{}
\makeatother

For longer code, the lines will be broken arbitrarily. I want to control the breaking.

moewe
  • 175,683
  • I'm not sure I understand what you want to do. Can you explain in more details and - even more important - add an example document and example @preamble entries. Please see https://tex.meta.stackexchange.com/q/228/35864 and https://tex.meta.stackexchange.com/q/4407/35864. – moewe Jan 02 '19 at 16:35

1 Answers1

0

The BibTeX sources bibtex.web (link to CTAN, also available on the GitHub mirror) has a few comments on @preamble (ll. 5498-5590)

@:database-file commands}{\quad \.{preamble}@>
The \.{preamble} command lets a user have \TeX\ stuff inserted (by the
standard styles, at least) directly into the \.{.bbl} file.  It is
intended primarily for allowing \TeX\ macro definitions used within
the bibliography entries (for better sorting, for example).  One
\.{preamble} command per \.{.bib} file should suffice.

A \.{preamble} command has either braces or parentheses as outer
delimiters.  Inside is the preamble string, which has the same syntax
as a field value: a nonempty list of field tokens separated by
|concat_char|s.  There are three types of field tokens---nonnegative
numbers, macro names, and delimited strings.

Note that BibTeX expects one @preamble per .bib file (on average). That is, it allows you to have as many @preamble instructions as the maximum number of .bib files. Indeed later on we find (ll. 8043-8044)

The |s_preamble| array is big enough to allow an average of one
\.{preamble\$} command per \.{.bib} file.

The function that takes care of the preamble instructions and is exposed in the BibTeX programming language as preamble$ is documented as (ll. 10555-10571)

@
The |built_in| function {\.{preamble\$}} pushes onto the stack the
concatenation of all the \.{preamble} strings read from the database
files.

@<|execute_fn|({\.{preamble\$}})@>=
procedure x_preamble;
begin
ex_buf_length := 0;
preamble_ptr := 0;
while (preamble_ptr < num_preamble_strings) do
    begin
    add_buf_pool (s_preamble[preamble_ptr]);
    incr(preamble_ptr);
    end;
add_pool_buf_and_push;          {push the concatenation string onto the stack}
end;

That means that BibTeX will simply concatenate the strings given in @preamble to one long string that can be dropped onto the BibTeX stack. It does not add anything in between and there is no simple way to decompose the string again.

Since BibTeX reads the @preamble contents just as normal field contents, line breaks are converted into spaces, so that

@PREAMBLE{"
\makeatletter
\providecommand\dosomething[0]{}
\makeatother
"}

still comes out as

 \makeatletter \providecommand\dosomething[0]{} \makeatother

I'm afraid that without significant extra work (not sure if that could work, but the idea is to manually add some kind of end of line marker and to split the string at this marker within BibTeX) or modifying the BibTeX sources and recompiling there is no way to get line breaks in the generated @preamble strings.

Is there any reason why you would want to control the line breaks? I would have thought that since the .bbl file is an auxiliary file one is only very rarely inclined to work with it directly. But then I would have also thought that one would not want to add so many long @preamble instructions that line breaks become important.

If you write your own .bst file you can hard-code 'preamble code' for the .bbl that is automatically added by the .bst (and not @preamble from .bib files). With that approach it is possible to insert line breaks as you please.

moewe
  • 175,683
  • I wanted to control the line breaks to render the added code readable by humans. – Spherical Triangle Jan 03 '19 at 11:35
  • You may want to add instructions when you do not deal with your own .bst (to incorporate some fixes, for example). – Spherical Triangle Jan 03 '19 at 11:39
  • I agree to say that the best way is to write your own .bst and your own LaTeX .cls or .sty. – Spherical Triangle Jan 03 '19 at 11:41
  • @SphericalTriangle Re the readability for humans: Sure, I suspected that that might be the reason. But since the .bbl is not the place where the code originates from and where it is usually written (by a human), I think this is a secondary concern. I also believe that it is better to avoid lengthy @preamble definitions. (Out of curiosity: What exactly do you intend to do with them?) Depending on the use case there may be more convenient/better/at-least-not-worse places to patch the code. – moewe Jan 03 '19 at 12:35
  • @SphericalTriangle Unfortunately, I don't quite understand what you mean in your second comment. Can you elaborate? – moewe Jan 03 '19 at 12:36
  • Basically I had to deal with a publisher .bst which badly breaks references at page-break (or column-break). One fix is to add \nolinebreak at the very end of each bibitem. With @PREAMBLE we can redefine a LaTeX command to do so: here the fix is provided by my .bib data. The end result is better, but not satisfying. Finally I adopted the solution proposed by @mico here. – Spherical Triangle Jan 03 '19 at 13:10