emacs-orgmode
[Top][All Lists]
Advanced

[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
someone.

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")
#+end_src

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:
https://lunotipia.juanmanuelmacias.com/images/latex-preview.mp4):

#+begin_src emacs-lisp
  (defun my-org-latex-prev-region ()
    (interactive)
    (let* ((frag (save-restriction
                   (narrow-to-region (region-beginning) (region-end))
                   (buffer-string)))
           (my-prev/preamble-l (buffer-local-value 'my-prev/preamble 
(current-buffer)))
           (my-prev/env-begin-l (buffer-local-value 'my-prev/env-begin 
(current-buffer)))
           (my-prev/env-end-l (buffer-local-value 'my-prev/env-end 
(current-buffer)))
           (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
                                              (insert-file-contents 
my-prev/preamble-l)
                                              (buffer-string)) "\n"
                                              "\\pagestyle{empty}"))
           (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))
                  (mark-whole-buffer)
                  (org-latex-convert-region-to-latex)
                  (org-latex-preview)
                  (goto-char (point-min))
                  (overlay-get  (car (overlays-at (point))) 'display))))
      (let*
          ((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 ()
    (interactive)
    (remove-overlays nil nil 'overlay-latex-prev t))
#+end_src


Best regards,

Juan Manuel 



reply via email to

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