emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Abdó Roig-Maranges" <abdo.roig@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Make latex preview formulas the same color as text face
Date: Fri, 03 Aug 2012 19:20:51 +0200	[thread overview]
Message-ID: <87zk6b99jg.fsf@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 233 bytes --]


Hi

I attach a patch with an enhancement to the latex preview code. When
:foreground or :background colors are set to auto, it choses the
appropriate color according to the properties of the actual text face.

Abdó Roig.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: the patch --]
[-- Type: text/x-patch, Size: 5669 bytes --]

From f6ede272f226fbeb2cf43fb747ae75be911ad3ce Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Abd=C3=B3=20Roig-Maranges?= <abdo.roig@gmail.com>
Date: Thu, 2 Aug 2012 00:50:24 +0200
Subject: [PATCH] Option to get latex preview in the same color as text face

* org.el (org-format-latex-options): added `auto' to docstring.
(org-format-latex): Get face colors at point and put them
inside opt.
(org-create-formula-image-with-dvipng): Corrected bug when colors
are not `default'.
(org-create-formula-image-with-imagemagick): Corrected bug in
the handling of "Transparent" bg color.
(org-dvipng-color-format): Same as org-latex-color-format for
dvipng-style color specification.

If `auto' is used for the :foreground or :background value in
org-format-latex-options, the the appropriate color is chosen
from the face in which the formula is displayed.
---
 lisp/org.el | 38 +++++++++++++++++++++++++++++++-------
 1 file changed, 31 insertions(+), 7 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index f49a9ac..2cc137d 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -3346,8 +3346,10 @@ points to a file, `org-agenda-diary-entry' will be used instead."
 This is a property list with the following properties:
 :foreground  the foreground color for images embedded in Emacs, e.g. \"Black\".
              `default' means use the foreground of the default face.
+             `auto' means use the foreground from the text face.
 :background  the background color, or \"Transparent\".
              `default' means use the background of the default face.
+             `auto' means use the background from the text face.
 :scale       a scaling factor for the size of the images, to get more pixels
 :html-foreground, :html-background, :html-scale
              the same numbers for HTML export.
@@ -17168,6 +17170,7 @@ Some of the options can be changed using the variable
 	 (absprefix (expand-file-name prefix dir))
 	 (todir (file-name-directory absprefix))
 	 (opt org-format-latex-options)
+	 (optnew org-format-latex-options)
 	 (matchers (plist-get opt :matchers))
 	 (re-list org-latex-regexps)
 	 (org-format-latex-header-extra
@@ -17213,14 +17216,27 @@ Some of the options can be changed using the variable
 	      (setq txt (match-string n)
 		    beg (match-beginning n) end (match-end n)
 		    cnt (1+ cnt))
-	      (let (print-length print-level) ; make sure full list is printed
+	      (let ((face (face-at-point))
+		    (fg (plist-get opt :foreground))
+		    (bg (plist-get opt :background))
+		    print-length print-level)         ; make sure full list is printed
+		(when forbuffer
+	          ; Get the colors from the face at point
+		  (goto-char beg)
+		  (when (eq fg 'auto)
+		    (setq fg (face-attribute face :foreground nil 'default)))
+		  (when (eq bg 'auto)
+		    (setq bg (face-attribute face :background nil 'default)))
+		  (setq optnew (copy-sequence opt))
+		  (plist-put optnew :foreground fg)
+		  (plist-put optnew :background bg))
 		(setq hash (sha1 (prin1-to-string
 				  (list org-format-latex-header
 					org-format-latex-header-extra
 					org-export-latex-default-packages-alist
 					org-export-latex-packages-alist
 					org-format-latex-options
-					forbuffer txt)))
+					forbuffer txt fg bg)))
 		      linkfile (format "%s_%s.png" prefix hash)
 		      movefile (format "%s_%s.png" absprefix hash)))
 	      (setq link (concat block "[[file:" linkfile "]]" block))
@@ -17239,7 +17255,7 @@ Some of the options can be changed using the variable
 		  (setq executables-checked t))
 		(unless (file-exists-p movefile)
 		  (org-create-formula-image-with-dvipng
-		   txt movefile opt forbuffer)))
+		   txt movefile optnew forbuffer)))
 	       ((eq processing-type 'imagemagick)
 		(unless executables-checked
 		  (org-check-external-command
@@ -17247,7 +17263,7 @@ Some of the options can be changed using the variable
 		  (setq executables-checked t))
 		(unless (file-exists-p movefile)
 		  (org-create-formula-image-with-imagemagick
-		   txt movefile opt forbuffer))))
+		   txt movefile optnew forbuffer))))
 	      (if overlays
 		  (progn
 		    (mapc (lambda (o)
@@ -17403,8 +17419,10 @@ inspection."
 		 "Black"))
 	 (bg (or (plist-get options (if buffer :background :html-background))
 		 "Transparent")))
-    (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
-    (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
+    (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
+      (setq fg (org-dvipng-color-format fg)))
+    (if (eq bg 'default) (setq bg (org-dvipng-color :background))
+      (setq bg (org-dvipng-color-format bg)))
     (with-temp-file texfile
       (insert (org-splice-latex-header
 	       org-format-latex-header
@@ -17475,7 +17493,7 @@ inspection."
       (setq fg (org-latex-color-format fg)))
     (if (eq bg 'default) (setq bg (org-latex-color :background))
       (setq bg (org-latex-color-format
-		(if (string= bg "Transparent")(setq bg "white")))))
+		(if (string= bg "Transparent") "white" bg))))
     (with-temp-file texfile
       (insert (org-splice-latex-header
 	       org-format-latex-header
@@ -17626,6 +17644,12 @@ SNIPPETS-P indicates if this is run to create snippet images for HTML."
 					   ((eq attr :background) 'background))))
 		   (color-values (face-attribute 'default attr nil))))))
 
+(defun org-dvipng-color-format (color-name)
+  "Convert COLOR-NAME to a RGB color value for dvipng."
+  (apply 'format "rgb %s %s %s"
+	 (mapcar 'org-normalize-color
+		   (color-values color-name))))
+
 (defun org-latex-color (attr)
   "Return a RGB color for the LaTeX color package."
   (apply 'format "%s,%s,%s"
-- 
1.7.11.3


             reply	other threads:[~2012-08-03 17:20 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-03 17:20 Abdó Roig-Maranges [this message]
2012-08-03 17:57 ` Make latex preview formulas the same color as text face Bastien

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=87zk6b99jg.fsf@gmail.com \
    --to=abdo.roig@gmail.com \
    --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).