blog dds

2007.02.04

How to Embed Citations in Diagrams

Diagrams in scientific publications occasionally link to other elements of the publication, such as bibliographic or section references. Maintaining consistency between the citations in the diagram and the publication can be tricky, but a small Perl script can automate this process.

This can of course work, only if one uses declarative tools for publishing, in my case LaTeX, BibTeX, and Graphviz.

The following Perl script (download its complete source from here) reads as its first argument the auxiliary file generated by LaTeX. It will then filter its standard input or subsequent arguments replacing \cite{...} commands with the labels that LaTeX assigns to the corresponding entries.

open(IN, $fname = shift @ARGV) || die "Unable to open $fname: $!\n";
while (<IN>) {
	$name{$1} = $2 if (/\\bibcite\{([^}]+)\}\{([^}]+)\}/);
}

while(<>) {
	while (/\\cite\{([^}]*)\}/) {
		$keys = $1;
		@keys = split(/\,/, $keys);
		undef $names;
		for $k (@keys) {
			$names .= ',' if (defined($names));
			print STDERR "Undefined key [$k]\n" unless (defined($name{$k}));
			$names .= $name{$k};
		}
		s/\\cite\{([^}]*)\}/[$names]/;
	}
	print;
}

For example, if the input file is