69

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)?

52

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.

  • 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
  • The link is broken, here is the official mirror: ctan.org/tex-archive/support/latexmk/example_rcfiles – NCode Aug 31 at 14:51
76
+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.

  • 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
  • Thanks - just what I needed. Bounty coming up when grace period is over. – hpekristiansen Feb 12 '12 at 21:28
  • 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
  • 3
    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
12

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;
}
  • 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
11

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.

  • 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
9

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
  • 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
4

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.

  • This is true on Windows 10 as of Dec 2018 and the top voted answer must reflect this. – Krishna Dec 25 '18 at 19:01

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.