emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tip] Export to PDF with latexmk 'continuous preview' option


From: Juan Manuel Macías
Subject: [tip] Export to PDF with latexmk 'continuous preview' option
Date: Tue, 05 Mar 2024 00:58:34 +0000

A little-known (and sometimes very useful) latexmk option is the -pvc
option. According to the latexmk manual:

      [...] The second previewing option is the powerful -pvc option
      (mnemonic: "preview continuously"). In this case, latexmk
      runs continuously, regularly monitoring all the source files
      to see if any have changed. Every time a change is detected,
      latexmk runs all the programs necessary to generate a new
      version of the document. A good previewer will then
      automatically update its display. Thus the user can simply
      edit a file and, when the changes are written to disk,
      latexmk completely automates the cycle of updating the .dvi
      (and/or the .ps and .pdf) file, and refreshing the
      previewer's display. It's not quite WYSIWYG, but usefully
      close.

In order to use this option from Org, I have defined a simple minor mode
that runs latexmk with the -pvc option and creates a buffer to monitor
the process. Every time the document, or any file involved, is saved,
the PDF is updated. We can define in our `latexmkrc' our favorite
external PDF viewer (Atril, Okular, Evince, etc.). I have this line:

┌────
│ $pdf_previewer = "atril %O %S > /dev/null 2>&1 &";
└────

And here's the code (for documents that are long, complex, or take a
while to export, it may be better to use the asynchronous version of
`org-latex-export-to-latex'):

┌────
│ (defun my-org-compile-latexmk-interactive ()
│   (let* ((tex-file (org-export-output-file-name ".tex")))
│     (start-process-shell-command
│      "latexmk"
│      (format "*%s-latexmk-process*" (file-name-sans-extension tex-file))
│      (concat
│       "latexmk -f -pvc -lualatex -e '$lualatex=q/lualatex %O -shell-escape 
%S/' "
│       tex-file))))
│
│ (define-minor-mode org-interactive-compile-pdf-mode
│   "TODO"
│   :lighter " OrgInteractivePDF"
│   (if org-interactive-compile-pdf-mode
│       (progn
│       (my-org-compile-latexmk-interactive)
│       (add-hook 'after-save-hook #'org-latex-export-to-latex nil t))
│     (remove-hook 'after-save-hook #'org-latex-export-to-latex t)
│     (when (equal (process-status "latexmk") 'run)
│       (kill-process "latexmk"))))
└────

And a screencast:

<https://cloud.disroot.org/s/ztFfa27kdsnNkGc>

--
Juan Manuel Macías -- Composición tipográfica, tratamiento de datos, diseño 
editorial y ortotipografía



reply via email to

[Prev in Thread] Current Thread [Next in Thread]