emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Highlighting LaTeX fragments
@ 2013-02-09 16:47 Rasmus
  2013-02-09 18:05 ` Nicolas Goaziou
  0 siblings, 1 reply; 7+ messages in thread
From: Rasmus @ 2013-02-09 16:47 UTC (permalink / raw)
  To: emacs-orgmode

Hi,

I was quite fond of org-highlight-latex-fragments-and-specials which
was recently removed ¹.  I'm sure there were good reasons for removing
it.

Basically stuff like α would be displayed with a special face.
Likewise, 
\begin{equation}
X
\end{equation}
would be highlighted.

Does anybody have a good idea on how to replicate this?

Thanks,
Rasmus


Footnotes: 
 ¹   http://orgmode.org/w/org-mode.git?p=org-mode.git;a=commit;h=a2f56264c918b679b53c5b7df0ef2e01a77c63d4

-- 
C is for Cookie

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Highlighting LaTeX fragments
  2013-02-09 16:47 Highlighting LaTeX fragments Rasmus
@ 2013-02-09 18:05 ` Nicolas Goaziou
  2013-02-09 18:46   ` Rasmus
  0 siblings, 1 reply; 7+ messages in thread
From: Nicolas Goaziou @ 2013-02-09 18:05 UTC (permalink / raw)
  To: Rasmus; +Cc: emacs-orgmode

Hello,

Rasmus <rasmus@gmx.us> writes:

> Basically stuff like α would be displayed with a special face.

It's still the case. This part is done by `org-fontify-entities'
(toggled by `org-pretty-entities').

> Likewise,
> \begin{equation}
> X
> \end{equation}
> would be highlighted.
>
> Does anybody have a good idea on how to replicate this?

Maybe rewrite a similar function with all references to the export
sub-system removed. Also make sure it doesn't overlap with existing
facilities.

Fontifying a latex environment is perfectly fine, but fontifying it only
if some export option has been defined somewhere is a bit too much.

To begin with, it should be useful to know what is missing exactly.


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Highlighting LaTeX fragments
  2013-02-09 18:05 ` Nicolas Goaziou
@ 2013-02-09 18:46   ` Rasmus
  2013-02-09 23:15     ` Nicolas Goaziou
  0 siblings, 1 reply; 7+ messages in thread
From: Rasmus @ 2013-02-09 18:46 UTC (permalink / raw)
  To: emacs-orgmode

Nicolas Goaziou <n.goaziou@gmail.com> writes:

>> Basically stuff like α would be displayed with a special face.
>
> It's still the case. This part is done by `org-fontify-entities'
> (toggled by `org-pretty-entities').

This just turns \alpha into α.  It does not give it a special color
(on my system at least).

> Maybe rewrite a similar function with all references to the export
> sub-system removed. Also make sure it doesn't overlap with existing
> facilities.

Would the best way to go about it be using regexps?

> To begin with, it should be useful to know what is missing exactly.

Colors.  E.g. it used to be that if an equation was too long to be
supported by $-signs it would go from brown (on my system) to the
normal black, giving visual feedback as to whether \(·\) should be
used. 

Also, it made it quicker to distinguish inline "math" from text (also
display math but this can be replaced by babel blocks).

–Rasmus

-- 
. . . The proofs are technical in nature and provides no real understanding.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Highlighting LaTeX fragments
  2013-02-09 18:46   ` Rasmus
@ 2013-02-09 23:15     ` Nicolas Goaziou
  2013-02-16 15:37       ` Rasmus
  0 siblings, 1 reply; 7+ messages in thread
From: Nicolas Goaziou @ 2013-02-09 23:15 UTC (permalink / raw)
  To: Rasmus; +Cc: emacs-orgmode

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

Rasmus <rasmus@gmx.us> writes:

> Nicolas Goaziou <n.goaziou@gmail.com> writes:
>
>> To begin with, it should be useful to know what is missing exactly.
>
> Colors.  E.g. it used to be that if an equation was too long to be
> supported by $-signs it would go from brown (on my system) to the
> normal black, giving visual feedback as to whether \(·\) should be
> used. 
>
> Also, it made it quicker to distinguish inline "math" from text (also
> display math but this can be replaced by babel blocks).

Would you mind testing the following patch? I don't like it much because
it's an all or nothing fontification. I think latex snippets, entities
and sub/superscript should be separated.

Anyway, does it replace the missing functionality?


Regards,

-- 
Nicolas Goaziou

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Fontify-latex-entities-and-sub-superscript-again.patch --]
[-- Type: text/x-patch, Size: 4505 bytes --]

From f0f165ef1b3a3e3d161da509cf0548171a6f68fb Mon Sep 17 00:00:00 2001
From: Nicolas Goaziou <n.goaziou@gmail.com>
Date: Sun, 10 Feb 2013 00:07:48 +0100
Subject: [PATCH] Fontify latex, entities and sub/superscript again

* lisp/org-faces.el (org-latex-and-special): Renamed from
  `org-latex-and-export-specials', which wasn't appropriate anymore.
* lisp/org.el (org-highlight-latex-and-special,
  org-latex-and-special-regexp): New variables.
(org-compute-latex-and-special-regexp, org-do-latex-and-special): New
function, revived from a previous commit.
(org-set-regexps-and-options, org-set-font-lock-defaults): Use new
functions.
---
 lisp/org-faces.el |  2 +-
 lisp/org.el       | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/lisp/org-faces.el b/lisp/org-faces.el
index de5a08c..a841ba3 100644
--- a/lisp/org-faces.el
+++ b/lisp/org-faces.el
@@ -765,7 +765,7 @@ level org-n-level-faces"
   :version "24.1"
   :type 'boolean)
 
-(defface org-latex-and-export-specials
+(defface org-latex-and-special
   (let ((font (cond ((assq :inherit custom-face-attributes)
 		     '(:inherit underline))
 		    (t '(:underline t)))))
diff --git a/lisp/org.el b/lisp/org.el
index 2bfca4e..908fcb4 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -3889,6 +3889,11 @@ org-level-* faces."
   :group 'org-appearance
   :type 'boolean)
 
+(defcustom org-highlight-latex-and-special nil
+  "Non-nil means fontify LaTeX stuff, entities and sub/superscript."
+  :group 'org-appearance
+  :type 'boolean)
+
 (defcustom org-hide-emphasis-markers nil
   "Non-nil mean font-lock should hide the emphasis marker characters."
   :group 'org-appearance
@@ -4987,6 +4992,7 @@ but the stars and the body are.")
 	    (mapcar (lambda (w) (substring w 0 -1))
 		    (list org-scheduled-string org-deadline-string
 			  org-clock-string org-closed-string)))
+      (org-compute-latex-and-special-regexp)
       (org-set-font-lock-defaults))))
 
 (defun org-file-contents (file &optional noerror)
@@ -5837,9 +5843,49 @@ by a #."
       (goto-char e)
       t)))
 
+(defvar org-latex-and-special-regexp nil
+  "Regular expression for highlighting LaTeX, entities and sub/superscript.")
 (defvar org-match-substring-regexp)
 (defvar org-match-substring-with-braces-regexp)
 
+(defun org-compute-latex-and-special-regexp ()
+  "Compute regular expression for LaTeX stuff, entities and sub/superscript."
+  (org-set-local
+   'org-latex-and-special-regexp
+   (if (not org-highlight-latex-and-special) nil
+     (let* ((re-sub
+	     (cond ((eq org-use-sub-superscripts '{})
+		    (list org-match-substring-with-braces-regexp))
+		   (org-use-sub-superscripts
+		    (list org-match-substring-regexp))))
+	    (matchers (plist-get org-format-latex-options :matchers))
+            (re-latex (delq nil
+			    (mapcar (lambda (x)
+				      (and (member (car x) matchers) (nth 1 x)))
+				    org-latex-regexps)))
+            (re-macros (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")))
+       (mapconcat 'identity (append re-latex re-macros re-sub) "\\|")))))
+
+(defun org-do-latex-and-special (limit)
+  "Search down to LIMIT and fontify LaTeX snippets and entities.
+Fontification happens only if `org-latex-and-special-regexp' is
+non-nil."
+  (when org-latex-and-special-regexp
+    (let (rtn d)
+      (while (and (not rtn)
+                  (re-search-forward org-latex-and-special-regexp limit t))
+	(unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
+                                                   'face))
+                      '(org-code org-verbatim underline))
+          (setq
+           rtn t
+           d (if (memq (char-after (1+ (match-beginning 0))) '(?_ ?^)) 1 0))
+          (font-lock-prepend-text-property
+           (+ d (match-beginning 0)) (match-end 0) 'face 'org-latex-and-special)
+          (add-text-properties (+ d (match-beginning 0)) (match-end 0)
+                               '(font-lock-multiline t))))
+      rtn)))
+
 (defun org-restart-font-lock ()
   "Restart `font-lock-mode', to force refontification."
   (when (and (boundp 'font-lock-mode) font-lock-mode)
@@ -6000,6 +6046,7 @@ needs to be inserted at a specific position in the font-lock sequence.")
 		  "\\(.*:" org-archive-tag ":.*\\)")
 		 '(1 'org-archived prepend))
 	   ;; Specials
+	   '(org-do-latex-and-special)
 	   '(org-fontify-entities)
 	   '(org-raise-scripts)
 	   ;; Code
-- 
1.8.1.3


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: Highlighting LaTeX fragments
  2013-02-09 23:15     ` Nicolas Goaziou
@ 2013-02-16 15:37       ` Rasmus
  2013-02-16 21:22         ` Nicolas Goaziou
  0 siblings, 1 reply; 7+ messages in thread
From: Rasmus @ 2013-02-16 15:37 UTC (permalink / raw)
  To: n.goaziou; +Cc: emacs-orgmode


> Would you mind testing the following patch? I don't like it much because
> it's an all or nothing fontification. I think latex snippets, entities
> and sub/superscript should be separated.

I have no experienced any problems.  I agree that it would be better
not to be able not to fontitfy rouge entities and super/subscripts.

> Anyway, does it replace the missing functionality?

Yes, it seems so replicate the old behavior so far.

–Rasmus

-- 
A clever person solves a problem. A wise person avoids it

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Highlighting LaTeX fragments
  2013-02-16 15:37       ` Rasmus
@ 2013-02-16 21:22         ` Nicolas Goaziou
  2013-02-16 22:02           ` Rasmus
  0 siblings, 1 reply; 7+ messages in thread
From: Nicolas Goaziou @ 2013-02-16 21:22 UTC (permalink / raw)
  To: Rasmus; +Cc: emacs-orgmode

Hello,

Rasmus <rasmus@gmx.us> writes:

>> Would you mind testing the following patch? I don't like it much because
>> it's an all or nothing fontification. I think latex snippets, entities
>> and sub/superscript should be separated.
>
> I have no experienced any problems.  I agree that it would be better
> not to be able not to fontitfy rouge entities and super/subscripts.
>
>> Anyway, does it replace the missing functionality?
>
> Yes, it seems so replicate the old behavior so far.

I have applied a small variant of the patch. You can configure
`org-highlight-latex-and-related' variable to fontify said syntax
separately.


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Highlighting LaTeX fragments
  2013-02-16 21:22         ` Nicolas Goaziou
@ 2013-02-16 22:02           ` Rasmus
  0 siblings, 0 replies; 7+ messages in thread
From: Rasmus @ 2013-02-16 22:02 UTC (permalink / raw)
  To: emacs-orgmode

Nicolas Goaziou <n.goaziou@gmail.com> writes:

>>> Would you mind testing the following patch? I don't like it much because
>>> it's an all or nothing fontification. I think latex snippets, entities
>>> and sub/superscript should be separated.
>>
>> I have no experienced any problems.  I agree that it would be better
>> not to be able not to fontitfy rouge entities and super/subscripts.
>>
>>> Anyway, does it replace the missing functionality?
>>
>> Yes, it seems so replicate the old behavior so far.
>
> I have applied a small variant of the patch. You can configure
> `org-highlight-latex-and-related' variable to fontify said syntax
> separately.

Thanks, I'll check it out. 

-- 
Got mashed potatoes. Ain't got no T-Bone. No T-Bone

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2013-02-16 22:03 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-02-09 16:47 Highlighting LaTeX fragments Rasmus
2013-02-09 18:05 ` Nicolas Goaziou
2013-02-09 18:46   ` Rasmus
2013-02-09 23:15     ` Nicolas Goaziou
2013-02-16 15:37       ` Rasmus
2013-02-16 21:22         ` Nicolas Goaziou
2013-02-16 22:02           ` Rasmus

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).