4

I'm using emacs+latexmk to compile my document, and okular to view it. Okular automatically updates the pdf when it changes, but the issue is that during compilation Okular displays a black document since the pdf is currently being created. So during the compilation it means that I can't look at my file, which is a bit annoying since often I want to continue to read my document meanwhile.

What is the simpler way, in latexmk, to compile the document in another folder, and copy back the final pdf once it's fully compiled? That way, okular will let me read my pdf during the compilation process.

EDIT

Concerning the solution proposed by John, it seems that when I run this command:

$ latexmk main.tex 
Rc files read:
  /home/me/.latexmkrc
Latexmk: This is Latexmk, John Collins, 29 September 2020, version: 4.70b.
Set environment variable BIBINPUTS='build:'
Set environment variable TEXINPUTS='build:.:/home/me/.emacs.d/.local/straight/build-27.2/auctex/latex:'
Latexmk: applying rule 'pdflatex'...
Rule 'pdflatex': The following rules & subrules became out-of-date:
      'pdflatex'
------------
Run number 1 of rule 'pdflatex'
------------
------------
Running 'pdflatex  -recorder -output-directory="." -aux-directory="build"  "main.tex"'
------------
/nix/store/hr2wphv47rqnrckmcjavn1r5yq7a8ks0-texlive-combined-full-2021.20210408/bin/pdflatex: unrecognized option '-aux-directory=build'
This is pdfTeX, Version 3.141592653-2.6-1.40.22 (TeX Live 2021/NixOS.org) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./main.tex
LaTeX2e <2020-10-01> patch level 4
[...]

the file is not just copied at the end. Indeed, if I regularly check the size of the pdf during the compilation, I see that the size increases:

$ md5sum main.pdf; ls -alh main.pdf
58e69aead33520a0228f797773ff0030  main.pdf
-rw-r--r-- 1 me users 1,1M sept. 30 09:52 main.pdf

$ md5sum main.pdf; ls -alh main.pdf
42ad0da92fc621c7106101b21ad60a96  main.pdf
-rw-r--r-- 1 me users 18K sept. 30 09:52 main.pdf

$ md5sum main.pdf; ls -alh main.pdf
0cbf954ce5a1723304561673088bec65  main.pdf
-rw-r--r-- 1 me users 115K sept. 30 09:52 main.pdf

$ md5sum main.pdf; ls -alh main.pdf
59ee8e1ac1c2aa785b0f4b3444b5f0b3  main.pdf
-rw-r--r-- 1 me users 148K sept. 30 09:52 main.pdf

$ md5sum main.pdf; ls -alh main.pdf
1f15aff8cb79793a9a0b31495e5a7c21  main.pdf
-rw-r--r-- 1 me users 178K sept. 30 09:52 main.pdf

I tried to force the output to be in another folder and copy it back using $pdf_update_command like that:

$emulate_aux = 1;
$out_dir = 'out';
$aux_dir = 'build';
$pdf_previewer = '';
$pdf_update_method = 4;
$pdf_update_command = 'cp out/*.pdf .';
$pdf_mode = 1; # Default to pdf.

but the update command is not run, not sure why.

EDIT So as pointed out in comments, $pdf_update_command is ran only in -pvc mode. So I tried instead:

$out_dir = 'out';
$pdflatex = 'pdflatex %O %S && cp %D .';
$pdf_mode = 1; # Defaults to pdf.

and now the upgrade is great (I get a refresh at every iteration, without any flickering), but there is an issue with bibliography, it is not found anymore:

MWE:

\documentclass{article}

\usepackage{filecontents}
\usepackage[style=trad-alpha]{biblatex}

\begin{filecontents}{myrefs.bib}
@Book{Knuth:1990,
    author    = {Knuth, Donald E.},
    title     = {The {\TeX}book},
    year      = {1990},
    isbn      = {0-201-13447-0},
    publisher = {Addison\,\textendash\,Wesley},
}

@Book{Lamport:94,
    author    = {Lamport, Leslie},
    title     = {\LaTeX: A Document Preparation System},
    year      = {1994},
    isbn      = {0-021-52983-1},
    publisher = {Addison\,\textendash\,Wesley},
}
\end{filecontents}

\addbibresource{myrefs.bib}

\begin{document}

\section{Main Body}
{\LaTeX} is a Turing-complete (procedural) markup language and typesetting processor~\cite{Lamport:94}.

\printbibliography

\end{document}
8
  • itectec.com/unixlinux/… Commented Sep 29, 2021 at 20:00
  • @hpekristiansen thanks for the reference. The first solution (disable okular sync) is not the best solution for me because I like having okular autorefresh. And I can't make $pdf_update_command work, not sure what I'm doing wrong.
    – tobiasBora
    Commented Sep 30, 2021 at 8:41
  • I no longer use latexmk, so I can not help. Are you sure that latexmk has permission to copy a file? What does the log say? Relevant for others who want to help is which OS you are on!? Commented Sep 30, 2021 at 19:07
  • Try to hardcode your filename instead of using the wildcard. -just as a test. Commented Sep 30, 2021 at 19:13
  • 1
    If you still want to do the copying of the pdf file, it's best to redefine $pdflatex so that pdflatex and then cp are invoked: $pdflatex = 'pdflatex %O %S && cp %D .';. Commented Oct 2, 2021 at 1:32

2 Answers 2

1

If you don't mind having the aux, log, etc files in one directory, and the pdf file in another, recent versions (at least 4.73) of latexmk can do exactly what you want. Here's suitable code in a latexmkrc file:

$emulate_aux = 1;
$out_dir = 'output';
$aux_dir = 'aux';

The critical item is the first one. It tells latexmk that it must not supply the -aux-directory option to pdflatex (or whichever program you use), but must emulate its effect. The compilation itself produces the pdf file in the aux directory, and then latexmk moves it to the output directory, which is a rapid operation. (It also moves the fls file and any synctex file, as well.)

The reason that latexmk has this possibility is that it is only in the MiKTeX implementation that the tex programs support the -aux-directory option. TeXLive's programs don't. The official TeXLive position is that such things should be handled by a program such as latexmk. So that is exactly what's now implemented.

7
  • This is interesting, thanks, but does not work, see my edit.
    – tobiasBora
    Commented Sep 30, 2021 at 8:10
  • Your latexmk is v. 4.70b. For this answer to work you need at least 4.73. In TeXLive 2021 (which you are using), the current version is 4.75. Commented Oct 2, 2021 at 1:08
  • I've updated the answer to make explicit the minimum version of latexmk needed. Commented Oct 2, 2021 at 1:10
  • Thanks for the clarification on the version. So I don't think my distribution is packing a recent enough TexLive (it packs 2021-0325, from 2021-04-08 I think), so I'll accept this answer for now, and come back when my texlive is upgraded. Meanwhile, I tried to use your $pdflatex = 'pdflatex %O %S && cp %D .'; trick... but unfortunately bibliography is not found anymore (see my edit).
    – tobiasBora
    Commented Oct 4, 2021 at 10:29
  • 1
    Yes, this solution does work with biblatex. I had no problem to compile the document you showed in the latest version of your question. That was with the current latexmk, v. 4.75. It should work also with subdirectories. There are significant changes since v. 4.70b that are relevant to bibliographies and subdirectories. If something doesn't work, let me know, with details of what latexmk wrote to the screen, and with the necessary source files to reproduce the problem. Commented Oct 5, 2021 at 13:56
0

It looks like I've a too old latexmk for the solution of John Collins to work, and configuring out and build seems to break biblatex. So I found two solutions I don't really like (because I need to disable okular auto-update feature) but that work quite well:

Solution 1 (emacs reloads okular after the last compilation)

Using latexmk in emacs + disable auto-reload in Okular (Configuration > Configure Okular > Reload document when file is updated (tab general, approximate translation)), emacs automatically calls Okular at the end of the compilation.

To configure emacs, I'm actually using doom emacs (with options (latex +latexmk)) and my .config contains something like that (should work with emacs, but my actual configuration is a bit different so let me know if it fails):

(after! latex
  ;; you can get the list of builtin viewers using C-h v TeX-view-program-list-builtin RET
  ;; But for some reasons, the default Okular viewer is broken due to a "file:"...
  (add-to-list 'TeX-view-program-list '("Okular" ("okular --unique %o"
                                                  (mode-io-correlate "#src:%n%a"))))
  (setq TeX-view-program-selection '((output-pdf "Okular")))

Solution 2 (latexmk reloads okular at every compilation)

Disable auto-reload in Okular (Configuration > Configure Okular > Reload document when file is updated (tab general, approximate translation)), and add in ~/.latexmkrc:

$pdflatex = 'pdflatex %O %S && okular --unique %D';

With this solution, okular will be reloaded by latexmk directly (even if ran from command line) at every new compilation (nice to have an incremental preview).

You must log in to answer this question.

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