83

Is there a way to make Latexmk invoke makeglossaries (part of the glossaries package)?

Specifically I want to use latexmk to compile a LaTeX document that uses the glossaries package to produce a glossary. Currently I'm using this rather clumsy sequence of commands:

makeglossaries document
latexmk -pdf document.tex
makeglossaries document
latexmk -pdf document.tex

The first run of makeglossaries is required to produce files that the glossaries package needs, or otherwise causes latexmk to fail, but there's still no data in the (non-existant) .aux file, so that first makeglossaries invocation produces nothing (except for an empty file).

So, how to do this using latexmk alone (and let that deal with invoking makeglossaries at the right time)?

1
59

You need to write a configuration file that tells latexmk which files are interesting for the glossary and how to handle them. Fortunately, the author provides some sample rc files.

Putting

add_cus_dep('glo', 'gls', 0, 'makeglo2gls');
sub makeglo2gls {
    system("makeindex -s '$_[0]'.ist -t '$_[0]'.glg -o '$_[0]'.gls '$_[0]'.glo");
}

in ~/.latexmkrc (globally in your home directory) or ./latexmkrc (locally in your document directory) should do the trick.

6
  • Do you have to add something to your build command? – Andriy Drozdyuk Jun 7 '12 at 19:05
  • 1
    Also, why does it go into directory .latexmkrc/latexmkrc? I thought it was simply the .latexmkrc or latexmkrc file? – Andriy Drozdyuk Jun 7 '12 at 19:14
  • 2
    @drozzy: It’s the file .latexmkrc or latexmkrc in the directory where the LaTeX master file you are working on resides. – mhp Jun 17 '12 at 5:25
  • 1
    The link is broken, here is the official mirror: ctan.org/tex-archive/support/latexmk/example_rcfiles – NCode Aug 31 '19 at 14:51
  • The answer is about makeindex not about glossaries package. Is this correct? – white_gecko Dec 1 '19 at 11:29
87
+50

At Hans-Peter E. Kristiansen’s request, I provide another answer, extending the one given by kay102.

Add the following lines to some latexmk initialization file (e.g., on Linux systems, ~/.latexmkrc):

add_cus_dep('glo', 'gls', 0, 'run_makeglossaries');
add_cus_dep('acn', 'acr', 0, 'run_makeglossaries');

sub run_makeglossaries {
  if ( $silent ) {
    system "makeglossaries -q '$_[0]'";
  }
  else {
    system "makeglossaries '$_[0]'";
  };
}

As a consequence, if latexmk is invoked with the -silent option makeglossaries will be silent, too.

Moreover, add

push @generated_exts, 'glo', 'gls', 'glg';
push @generated_exts, 'acn', 'acr', 'alg';
$clean_ext .= ' %R.ist %R.xdy';

to update the cleanup functionality of latexmk appropriately.

10
  • Cool - I am assuming that you find that kay102's answer is the best(however marginal). Can you improve this answer by adding extension from glossaries to remove with the -c or -C option? or is this bad for some reason? – hpekristiansen Feb 12 '12 at 20:32
  • @Hans-Peter E. Kristiansen: I’ve extended my answer accordingly. – mhp Feb 12 '12 at 21:10
  • 1
    @Hans-Peter E. Kristiansen: The .bbl file isn’t related to the glossaries package. So it could be worth posing your comment as a regular question. (Most probably, you need $bibtex_use = 2;.) – mhp Feb 12 '12 at 22:16
  • 5
    On Windows, with MikTeX or TeXLive, the two commands should be system "makeglossaries", "-q", $_[0]; and system "makeglossaries", $_[0];... – Paul Gaborit Jun 15 '16 at 22:46
  • 2
    Like @PaulGaborit said, the window-specific version should not have the single quotes around $_[0]. Is there a way to have a if-then-else condition to detect platform and apply the rule accordingly without further manual intervention? – Dr Krishnakumar Gopalakrishnan Dec 25 '18 at 19:03
14

You can also let latexmk call makeglossaries directly by adding the following to your latexmkrc file (in my case I had to add it to $HOME/.latexmkrc):

add_cus_dep('glo', 'gls', 0, 'makeglossaries');
add_cus_dep('acn', 'acr', 0, 'makeglossaries');

sub makeglossaries {
    system "makeglossaries $_[0]";
    if ( -z "$_[0].glo" ) {
        open GLS, ">$_[0].gls";
        close GLS;
    }
    return 0;
}
2
  • 1
    With the current version of the makeglossaries program, testing for empty .glo and .acn files isn’t necessary any longer. – mhp Jan 10 '12 at 19:34
  • But this seems to be the only working solution for older makeglossaries versions and empty .glos ... – ejoerns Apr 21 '14 at 0:47
12

A lightweight solution is adding

add_cus_dep('glo', 'gls', 0, 'makeglo2gls');
add_cus_dep('acn', 'acr', 0, 'makeglo2gls');
sub makeglo2gls {
        system("makeglossaries $_[0]");
}

to your .latexmk file.

7
  • 1
    You still need the add_cus_dep('acn', 'acr', 0, 'makeglo2gls') line for acronyms. – wcmatthysen Jun 24 '11 at 8:57
  • You are right. I have added the line. – kay102 Jun 26 '11 at 16:30
  • 2
    Slight improvement to the makeglo2gls subroutine: Call makeglossaries with/without the -q option, depending on whether $silent is true/false. – mhp Jan 10 '12 at 19:30
  • I'm trying to use this to make a list of acronyms but it is not working? Any ideas? – Darling Jan 15 '12 at 11:31
  • @kay102: Is your solution better than the others? – hpekristiansen Feb 12 '12 at 16:37
11

My sources and generated files are in separated directories. Running mhp's solution I got the error: (require "doc.xdy") ERROR: Could not find file "doc.xdy". Then I extended the solution to:

file: conf/glossaries.latexmk
-----------------------------
print("LATEXMK: Glossaries Módule...\n");

use File::Basename;

add_cus_dep('glo', 'gls', 0, 'run_makeglossaries');
add_cus_dep('acn', 'acr', 0, 'run_makeglossaries');

sub run_makeglossaries {
   $dir = dirname($_[0]);
   $file = basename($_[0]);
   if ( $silent ) {
      system "makeglossaries -q -d '$dir' '$file'";
   }
   else {
      system "makeglossaries -d '$dir' '$file'";
   };
}

push @generated_exts, 'glo', 'gls', 'glg';
push @generated_exts, 'acn', 'acr', 'alg';
$clean_ext .= ' %R.ist %R.xdy';

Then I run as:

cd src
latexmk \
  -f -pdf -bibtex-cond \
  -r "../conf/glossaries.latexmk" \
  -auxdir=../.build -outdir=../.build \
  -pdflatex="xelatex -interaction=nonstopmode -synctex=1 %O %S" doc
3
  • 1
    What does this do? -r "../conf/glossaries.latexmk"? – tommy.carstensen Dec 1 '17 at 15:55
  • @tommy.carstensen Specifies the configuration to throw to latexmk. In this case he is not using the system-wide settings but rather specific settings for this particular project / document / whatever. – flindeberg Nov 15 '18 at 9:51
  • This works if your auxiliary files are within a different directory, which was my original problem. – Max Dec 5 '19 at 13:10
5

mhp's code didn't work out for me on my windows 8 machine, so i changed his code to:

add_cus_dep('glo', 'gls', 0, 'run_makeglossaries');
add_cus_dep('acn', 'acr', 0, 'run_makeglossaries');

sub run_makeglossaries {
  if ( $silent ) {
    system "makeglossaries -q $_[0]";
  }
  else {
    system "makeglossaries $_[0]";
  };
}

push @generated_exts, 'glo', 'gls', 'glg';
push @generated_exts, 'acn', 'acr', 'alg';
$clean_ext .= ' %R.ist %R.xdy';

primarily i just removed the single quotes around $_[0]. otherwise makeglossaries wasn't able to find the filename.aux file properly.

1
3

The current version of the glossary sample latexmkrc file bundled on CTAN http://mirrors.ctan.org/support/latexmk/example_rcfiles/glossary_latexmkrc contains

add_cus_dep( 'acn', 'acr', 0, 'makeglossaries' );
add_cus_dep( 'glo', 'gls', 0, 'makeglossaries' );
$clean_ext .= " acr acn alg glo gls glg";
sub makeglossaries {
  my ($base_name, $path) = fileparse( $_[0] );
  pushd $path;
  my $return = system "makeglossaries", $base_name;
  popd;
  return $return;
}

for glossaries.

This also resolves the quoting issues on Windows mentioned (and resolved) in Oliver's answer for me.

2
  • 1
    Yes the sample worked right away for me as well (the other suggestions did not). – Arsenal Dec 14 '20 at 22:25
  • I use VS Code. I named this file .latexmkrc and put it into the same folder as the main TeX file. Voilá, the glossaries are there. Thanks for citing official documentation. – Thomas Weller Apr 8 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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