emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Juan Manuel Macías" <maciaschain@posteo.net>
To: orgmode <emacs-orgmode@gnu.org>
Subject: [tip] LaTeX preview of a region
Date: Sat, 12 Jun 2021 23:51:18 +0000	[thread overview]
Message-ID: <87h7i2ej61.fsf@posteo.net> (raw)

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	other threads:[~2021-06-12 23:51 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87h7i2ej61.fsf@posteo.net \
    --to=maciaschain@posteo.net \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public 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).