summaryrefslogtreecommitdiff
path: root/support/latexmk/example_rcfiles/chapterbib-latexmkrc
blob: e5c2cb62501fa141f60f2cfb779cff9c8f54d261 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# This file gives a configuration suitable when chapterbib is being used.
# Latexmk without special configuration works with chapterbib only if
# chapter bibliographies are used, but not when an overall consolidated
# document-level bibliography is used. (This is because one of the runs of
# bibtex gives an error of a kind that causes latexmk to stop further
# processing.  The chapterbib documentation mentions this problem
# explicitly and says the error is to be ignored.)

# The configuration in this file allows the use of a document level
# bibliographys without any errors.  It works equally when there is no
# document level bibliography and when chapterbib is not used.


$bibtex = 'bibtex %O %S';
$bibtex_save = $bibtex;
$bibtex = 'internal bibtex_fix %R %D %S';

$clean_ext .= " %R-mod.blg %R-mod.aux %R-mod.bbl";

sub bibtex_fix {
    my ($root, $dest, $source) = @_;
    local ($base_bare, $path, $ext) = fileparse( $source, '\.[^\.]*' );
    if ($path eq './') { $path = ''; }
    local $base = $path.$base_bare;
    my $ret = 0;
    if ( $base_bare eq $root ) {
        print "--- Will run bibtex on modified '$root.aux' file\n";
        my $aux_mod_base = $base."-mod";
        local $out_fh = new FileHandle "> $aux_mod_base$ext";
        if (!$out_fh) { die "Cannot write to '$aux_mod_base$ext'\n"; }
        local $level = 0;
        fix_aux( $source );
        close $out_fh;
        # Override source, dest, and basenames, since they are to have the path given
        # in the arguments to this subroutine, instead of the path given in the
        # corresponding names in the rule.  Latexmk may change directory before
        # calling this subroutine, and adjusts the arguments accordingly:
        $ret = Run_subst( $bibtex_save, 2, undef, "$aux_mod_base$ext", "$aux_mod_base.bbl", $aux_mod_base );
        foreach ( 'bbl', 'blg' ) {
           copy "$aux_mod_base.$_", "$path$root.$_";
        }
    }
    else {
        $ret = Run_subst( $bibtex_save, 2, undef, $source, $dest, $base );
    }
    return $ret;
}

sub fix_aux {
   # Read aux file, outputting flattened version to file handle $out_fh and
   # removing \bibdata and \bibstyle lines that were in included .aux files.
   my $aux_file = $_[0];
   print "Processing '$aux_file'\n";
   my $aux_fh = new FileHandle $aux_file;
   if (!$aux_fh) { die "$My_name: Couldn't read aux file '$aux_file'\n"; }
   $level++;
   while (<$aux_fh>) {
      if ( ($level > 1) &&
           ( /^\\bibdata\{(.*)\}/ || /^\\bibstyle\{(.*)\}/ )
         )
      { next; }
      elsif ( /^\\\@input\{(.*)\}/ ) {
          fix_aux( $path.$1 );
          next;
      }
      else {
         print $out_fh $_;
      }
   }
   close($aux_fh);
   $level--;
}