4

Currently I use TexStudio, with the command pdflatex -synctex=1 -interaction=nonstopmode -output-directory=./aux/ "paper".tex which works fine to compile and put all auxilary files in the aux directory.

However, I need to manually create an aux directory every time in the directory where my .tex file is placed for it to work, otherwise TexStudio will return the error:

Process started: pdflatex -synctex=1 -interaction=nonstopmode -output-directory=./aux/ "paper".tex

Process exited with error(s)

My question is, how can I make it so my aux directory is created automatically, preferable through my pdflatex command? Also, as a secondary question, can I automatically copy my created pdf from this aux directory to the current directory (the parent of aux directory)?

4
  • Can you use something else than TeXStudio for the compile process? Or do you need to use TeXStudio for the entire workflow? Commented Feb 16, 2021 at 10:54
  • 1
    it is much better not to use output-directory. If you put the auxiliary files to a non-standard place you will have to configure every additional tool to look there and this not easy, you can find quite a lot questions from people having various problems with it. Commented Feb 16, 2021 at 11:25
  • @OleAldric I don't think it matters if I use TexStudio or not, suppose I use the mentioned pdflatex command in a terminal if you prefer.
    – J. Schmidt
    Commented Feb 16, 2021 at 12:33
  • on the command line, latexmk -outdir=aux -pdf filename where filename is your .tex file. This creates an aux directory and puts both auxiliary and final output file there. When you're done you can just cp aux/*.pdf ./ . Commented Feb 16, 2021 at 14:39

2 Answers 2

1

I use .latexmkrc in my project folders, and then I compile from the command line. Here is how my latexmkrc file looks like:

This solution will create the temp and build folder if it doesn't exist and you can have these on a per project basis. (by using the file .latexmkrc in the project folder)

ensure_path('TEXINPUTS', './some/paths//'); # "//" searches recursively

$clean_ext = 'aux, fdb_latexmk, fls, out, xdv'; # files to clean
#$pdflatex = ""; # add your chain here.
$pdf_mode = [..]; # see latexmk documentation for your os 
$pdf_update_method = [..]; # see latexmk documentation for your viewer.
$pdf_previewer = 'start "some/pdf/viewer" %O %S'; # If you use the -pvc switch

sub latex_fix_aux {
  my @move_exts = ('dvi', 'fls', 'pdf', 'ps', 'synctex.gz' );
  my $auxD = '';
  my $outD = '';

  foreach (@_) {
     if ( /^-{1,2}aux-directory=(.*)$/ ) {
        $auxD = $1;
     }
     elsif ( /^-{1,2}output-directory=(.*)$/ ) {
        $outD = $1;
     }
  }

  if ( $outD eq '' ) { $outD = '.'; }
  if ( $auxD eq '' ) { $auxD = $outD; }

  my @args_act = ();
  my $set_outD = 0;
  
  foreach (@_) {
     if ( /^-{1,2}(aux|output)-directory=.*$/ ) {
        if ( ! $set_outD ) {
       push @args_act, "-output-directory=$auxD";
       $set_outD = 1;
    }
     }
     else {
        push @args_act, $_;
     }
  }
    
  my $outD1 = $outD;
  my $auxD1 = $auxD;
  foreach ( $auxD1, $outD1 ) {
     if ( ($_ ne '')  && ! m([\\/\:]$) ) {
        $_ .= '/';
     }
     while ( s[^\.\/][] ) {}
   }

  print "Running: '@args_act'\n";
  my $ret = system @args_act;
  if ($auxD ne $outD) {
     print "Move @move_exts files from '$auxD' to '$outD'\n";
     foreach my $ext (@move_exts) {
        copy "$auxD1$root_filename.$ext", "$outD1$root_filename.$ext";
        unlink "$auxD1$root_filename.$ext";
     }
  }
  return $ret;
}

$out_dir = '.build/'; # pdf and fls etc. goes here.
$aux_dir = '.temp/'; # other aux files goes here

Then I usually run latexmk in live mode like this:

latexmk -pvc -jobname=projectName

Which will start latexmk in live mode (continuous compile on save) and the files will be named projectName.pdf etc.

1

Assuming you're on a *nix environement...

If you don't want to use something like latexmk, you could just create a simple shell script, e.g., ~/bin/mypdflatex.sh, which could be as simple as:

#!/bin/bash
filename="$1"
[[ -d ./aux ]] || mkdir ./aux
pdflatex -synctex=1 -interaction=nonstopmode -output-directory=./aux/ "$filename"

Make it excutable:

chmod a+x ~/bin/mypdflatex.sh

Then in TeXStudio, under Options > Configure TeXStudio > Commands > pdfLaTeX put:

/path/to/bin/mypdflatex.sh %.tex

Something comparable could be done on Windows, most likely. (But don't ask me how.)

1
  • Nice and simple. You could maybe top it off with && mv *.pdf .. or similar, moving the compiled PDF up a directory.
    – Ingmar
    Commented Mar 15, 2022 at 6:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .