emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [tip] LaTeX preview of a region
@ 2021-06-12 23:51 Juan Manuel Macías
  0 siblings, 0 replies; only message in thread
From: Juan Manuel Macías @ 2021-06-12 23:51 UTC (permalink / raw)
  To: orgmode

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 


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-06-12 23:51 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-12 23:51 [tip] LaTeX preview of a region Juan Manuel Macías

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).