[Top][All Lists]

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

[tip] LaTeX preview of a region

From: Juan Manuel Macías
Subject: [tip] LaTeX preview of a region
Date: Sat, 12 Jun 2021 23:51:18 +0000

Hi all,

When typesetting a long book, I often need to fine tune many paragraphs
(typographically speaking). So I've write this code to avoid compiling
the whole document every time. The idea is activate a preview of any
region of the document, compiled with all the local configuration
(export filters, setup file, export options, etc.). In other words, the
preview *must be identical* to the compiled/exported pdf. Although it is
for a particular use case, I share it here in case it is useful to

My intention is to give these four variables a local value for each
document or major project. But this would be the default value:

#+begin_src emacs-lisp
  (setq my-prev/preamble "preamble.tex"
        my-prev/env-begin "@@latex:\\begin{dummy}@@"
        my-prev/env-end "@@latex:\\end{dummy}@@"
        my-prev/conf "#+OPTIONS: ':t\n
                      #+LANGUAGE: es\n"
                      #+SETUPFILE: normal.setup\n
                      #+INCLUDE: export-filters\n")

The first variable makes sense because I always use a separate file as a
preamble for large projects. The second and third variables correspond
to the environment to enclose the preview (by default, a `dummmy'
environment: `\newenvironment*{dummy}{}{}'). And the fourth is the
configuration to export the preview.

And these two functions enable or disable the preview on a region (here
a sample video:

#+begin_src emacs-lisp
  (defun my-org-latex-prev-region ()
    (let* ((frag (save-restriction
                   (narrow-to-region (region-beginning) (region-end))
           (my-prev/preamble-l (buffer-local-value 'my-prev/preamble 
           (my-prev/env-begin-l (buffer-local-value 'my-prev/env-begin 
           (my-prev/env-end-l (buffer-local-value 'my-prev/env-end 
           (my-prev/conf-l (buffer-local-value 'my-prev/conf (current-buffer)))
           (org-preview-latex-default-process 'luamagick)
           (org-format-latex-header (concat (with-temp-buffer
                                              (buffer-string)) "\n"
           (prev (with-temp-buffer
                  (insert (concat my-prev/conf-l "\n\n" my-prev/env-begin-l 
"\n\n" frag "\n\n" my-prev/env-end-l))
                  (goto-char (point-min))
                  (overlay-get  (car (overlays-at (point))) 'display))))
          ((ov (make-overlay (region-beginning) (region-end))))
        (overlay-put ov 'overlay-latex-prev t)
        (overlay-put ov 'display prev))))

(defun my-org-latex-remove-prev-region ()
    (remove-overlays nil nil 'overlay-latex-prev t))

Best regards,

Juan Manuel 

reply via email to

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