Karakas Online

6.1. Mass insertion of cross-references in LyX

If you have to add hundreds of cross-references in just one section (e.g. more than 500, as in Credits for version 2.0 of the PHP-Nuke HOWTO), you will soon notice that, although a single cross-reference is inserted very easily in LyX (just choose Insert->Cross-reference from the menu, then choose the label of the reference you want), it becomes a real pain if you have to enter hundreds of them.

My solution to this was to write a script that reads a LyX file and outputs another LyX file that contains references to all labels of the first one. It was then easier to copy the references from the file thus created, paste them in Credits for version 2.0 of the PHP-Nuke HOWTO and then delete the unneeded ones, than try to insert all cross-references by hand using the LyX menu.

The following script, call it lyxrefs, will print a LyX file in standard output, containing cross-references to each and every label of the LyX file whose name was passed on the command line as argument:

function preample() {
cat <<-EOF
#LyX 1.2 created this file. For more info see http://www.lyx.org/
\lyxformat 220
\textclass article
\language english
\inputencoding auto
\fontscheme default
\graphics default
\paperfontsize default
\papersize Default
\paperpackage a4
\use_geometry 0
\use_amsmath 0
\use_natbib 0
\use_numerical_citations 0
\paperorientation portrait
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\defskip medskip
\quotes_language english
\quotes_times 2
\papercolumns 1
\papersides 1
\paperpagestyle default
function label() {
echo ""
echo "\layout Standard"
echo ""
echo ""
echo "\begin_inset LatexCommand \label{cit:$n}"
echo ""
echo "\end_inset"
# Output LyX commands for Chapter "All references"
cat <<-EOF
\layout Section
All references
# Output all references.
$AWK 'BEGIN {FS=" "} /\\begin_inset LatexCommand \\label{/ {gsub("label","ref");
printf("\n%s\n\n%s%s\n\n%s\n","\\layout Standard",
"\\begin_inset LatexCommand",$3,"\\end_inset")}' $1 > all-references.tmp
cat all-references.tmp
rm all-references.tmp
# Output LyX commands for Chapter "All references"
cat <<-EOF
\layout Section
All figure references
# Output only the figures.
$AWK 'BEGIN {FS=" "} /\\begin_inset LatexCommand \\label{fig-/ {gsub("label","ref");
printf("\n%s\n\n%s%s\n\n%s\n","\\layout Standard",
"\\begin_inset LatexCommand",$3,"\\end_inset")}' $1 > fig-references.tmp
cat fig-references.tmp
rm fig-references.tmp
# Output LyX commands for Chapter "All references"
cat <<-EOF
\layout Section
All table references
# Output only the tables.
$AWK 'BEGIN {FS=" "} /\\begin_inset LatexCommand \\label{tab-/ {gsub("label","ref");
printf("\n%s\n\n%s%s\n\n%s\n","\\layout Standard",
"\\begin_inset LatexCommand",$3,"\\end_inset")}' $1 > tab-references.tmp
cat tab-references.tmp
rm tab-references.tmp
echo "\the_end"

The script will even create three sections, with cross-references to all labels, all figures and all tables respectively. If you named it lyxrefs, you would call it as follows:

lyxrefs some-LyX-file.lyx > refs.lyx

Then refs.lyx will contain cross-references to all labels of some-LyX-file.lyx. You can open refs.lyx with LyX, copy all or part of the cross-references there and paste them in some-LyX-file.lyx. The cross-references in Credits for version 2.0 of the PHP-Nuke HOWTO were entered this way.