Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

My BibTeX files contain references to files, like PDF files. I use the field "file" to indicate the referenced files. This is useful to associate files with a BibTeX entry. Moreover one can directly open the file from some software, such as JabRef. I would like to concatenate BibTeX files located in different folders and keep valid relative links. Valid links will allow to access the files from the concatenated BibTeX file.

A dummy command cat will lose all relative links.

Here is an example of what I except from the concatenation. Below are two BibTeX files located in two different folders. The field 'file' indicates the location of the referenced file. In the concatenated result file, these 'file' fields are modified to take into account the location of the files.

Root dir
|- RootA
|  - A.bib
|- RootB
|  - B.bib

with A.bib

@MISC{XY,
  title = {John Doe's Book},
  author = {John Doe},
  file = {johnDoe.pdf:johnDoe.pdf:pdf},
}

with B.bib

@MISC{XX,
  title = {Jane Doe's Book},
  author = {Jane Doe},
  file = {janeDoe.pdf:janeDoe.pdf:pdf},
}

I would like the concatenation of the two files to look like this:

@MISC{XY,
  title = {John Doe's Book},
  author = {John Doe},
  file = {johnDoe.pdf:RootA/johnDoe.pdf:pdf},
}

@MISC{XX,
  title = {Jane Doe's Book},
  author = {Jane Doe},
  file = {janeDoe.pdf:RootB/janeDoe.pdf:pdf},
}

The tools I have used so far do not do such things...

The basic idea I have to do this, is to use a combination of command lines tools such as sed and cut. But it seems painful and not robust. And I should also mention that one should make difference between absolute links and relative links. Finally, the result file depends on where we run the concatenation command...

Does anyone has a better idea, or know a software that performs that operation?

share|improve this question

migrated from stackoverflow.com Jul 12 '12 at 18:02

This question came from our site for professional and enthusiast programmers.

    
This is something that could be done with BibTool. –  Nathan Grigg Jul 13 '12 at 6:20

2 Answers 2

up vote 1 down vote accepted

BibTool is a powerful tool to manipulate BibTeX files. One thing it can do is rewrite a certain field using regular expressions.

BibTool accepts commands either from an external file or directly on the command line preceded by --. The command rewrite.rule={file "foo" "bar"} replaces each foo inside any file entry with bar, where foo can be a (grep-style) regular expression.

Obviously the regular expression depends on how your data is structured, but assuming that every file looks like {label:filename:filetype} and there are no colons in your filenames, you could use the following command to insert RootA before each file in A.bib, printing the result to the screen.

bibtool -- 'rewrite.rule={file ":\([^/].*\):" ":RootA/\1:"}' -- 'rewrite.limit=1' A.bib 

The rule also requires the first character of the filename to be something other than /, which will leave absolute paths alone.

share|improve this answer

As @NathanGrigg mentionned, one can use bibtool to do this. If one wants to use shell variables to perform substitution, one can not use quotes, and one has to escape all special characters.

The following command adds the directory name to the file field value with the shell variable dirToAdd

dirToAdd=RootA
bibtool -- rewrite.rule={file\ \":\\\([^/].*\\\):\"\ \":${dirToAdd}/\\1:\"} -- rewrite.limit=1 A.bib

Run on this following bibtex entry

@MISC{XY,
  title = {John Doe's Book},
  author = {John Doe},
  file = {johnDoe.pdf:johnDoe.pdf:pdf},
}

the previous command will produce

@MISC{XY,
  title = {John Doe's Book},
  author = {John Doe},
  file = {johnDoe.pdf:RootA/johnDoe.pdf:pdf},
}

pybtex is also a good solution to parse bibtex file.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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