emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Inline LaTeX formulae
@ 2015-05-05 20:14 Titus von der Malsburg
  2015-05-05 20:23 ` Rasmus
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Titus von der Malsburg @ 2015-05-05 20:14 UTC (permalink / raw)
  To: emacs-orgmode

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



Hi all,

I just updated to the development version of org and encountered a
problem with inline LaTeX formulae.  I have the following test document:

    #+OPTIONS: toc:nil tex:t
    Test: ($p\ll.001$)

Before the update the output used to be (export to PDF via pdflatex):

    Test: p⋘.001

(The symbold after the p is “much less than“, hope that comes out.)

Now I get:

    Test: ($p⋘.001$)

In the .tex file, it looks like this:

    Test: (\$p\(\ll\).001\$)

Playing with the tex variable in the options doesn’t help: for tex:nil
and tex:verbatim, I get the same output.  Interestingly, when I remove
the parentheses, it works again.  Seems like a bug where inline LaTeX is
not recognized when it occurs in parentheses.

Is there a quick fix for that?

  Titus


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

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

* Re: Inline LaTeX formulae
  2015-05-05 20:14 Inline LaTeX formulae Titus von der Malsburg
@ 2015-05-05 20:23 ` Rasmus
  2015-05-05 20:27 ` Marcin Borkowski
  2015-05-05 21:32 ` Titus von der Malsburg
  2 siblings, 0 replies; 14+ messages in thread
From: Rasmus @ 2015-05-05 20:23 UTC (permalink / raw)
  To: emacs-orgmode

Titus von der Malsburg <malsburg@posteo.de> writes:

> Hi all,
>
> I just updated to the development version of org and encountered a
> problem with inline LaTeX formulae.  I have the following test document:
>
>     #+OPTIONS: toc:nil tex:t
>     Test: ($p\ll.001$)

Try:
      Test: (\(p\ll.001\))

($x$) doesn't work.  You could use $(x)$.  Strangely
org-highlight-latex-and-related still highlights it.

—Rasmus

-- 
Slowly unravels in a ball of yarn and the devil collects it

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

* Re: Inline LaTeX formulae
  2015-05-05 20:14 Inline LaTeX formulae Titus von der Malsburg
  2015-05-05 20:23 ` Rasmus
@ 2015-05-05 20:27 ` Marcin Borkowski
  2015-05-05 21:32 ` Titus von der Malsburg
  2 siblings, 0 replies; 14+ messages in thread
From: Marcin Borkowski @ 2015-05-05 20:27 UTC (permalink / raw)
  To: emacs-orgmode


On 2015-05-05, at 22:14, Titus von der Malsburg <malsburg@posteo.de> wrote:

> Hi all,
>
> I just updated to the development version of org and encountered a
> problem with inline LaTeX formulae.  I have the following test document:
>
>     #+OPTIONS: toc:nil tex:t
>     Test: ($p\ll.001$)
>
> Is there a quick fix for that?

Does it help if you replace $...$ with \(...\)?

>   Titus

Hth,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University

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

* Re: Inline LaTeX formulae
  2015-05-05 20:14 Inline LaTeX formulae Titus von der Malsburg
  2015-05-05 20:23 ` Rasmus
  2015-05-05 20:27 ` Marcin Borkowski
@ 2015-05-05 21:32 ` Titus von der Malsburg
  2015-05-05 22:56   ` Marcin Borkowski
  2015-05-06 10:24   ` Rasmus
  2 siblings, 2 replies; 14+ messages in thread
From: Titus von der Malsburg @ 2015-05-05 21:32 UTC (permalink / raw)
  To: emacs-orgmode; +Cc: Rasmus

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


Rasums wrote:
> Titus von der Malsburg <malsburg <at> posteo.de> writes:
> 
> > Hi all,
> >
> > I just updated to the development version of org and encountered a
> > problem with inline LaTeX formulae.  I have the following test document:
> >
> >     #+OPTIONS: toc:nil tex:t
> >     Test: ($p\ll.001$)
> 
> Try:
>       Test: (\(p\ll.001\))
> 
> ($x$) doesn't work.  You could use $(x)$.  Strangely
> org-highlight-latex-and-related still highlights it.
> 
> –Rasmus

Thanks, Rasmus (and Marcin), replacing the $ by \( and \) works.  I
didn’t know about the parentheses notation.  To my experience most
people use the $ notation for inline math, and therefore it would make
sense to properly support that notation, too.  Especially since it used
to work in earlier versions of org (if I’m not mistaken) and there is
nothing in the manual suggesting that it shouldn’t work.  The contrary
is the case.  Quote from the manual:

  To avoid conflicts with currency specifications, single ‘$’ characters
  are only recognized as math delimiters if the enclosed text contains
  at most two line breaks, is directly attached to the ‘$’ characters
  with no whitespace in between, and if the closing ‘$’ is followed by
  whitespace, punctuation or a dash.

Parentheses are punctuation, so the $ in my example should be
interpreted as math delimiters.  I think whoever wrote the code, simply
overlooked parentheses when implementing the punctuation part.  This
should be fixed.

  Titus


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

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

* Re: Inline LaTeX formulae
  2015-05-05 21:32 ` Titus von der Malsburg
@ 2015-05-05 22:56   ` Marcin Borkowski
  2015-05-06 10:24   ` Rasmus
  1 sibling, 0 replies; 14+ messages in thread
From: Marcin Borkowski @ 2015-05-05 22:56 UTC (permalink / raw)
  To: emacs-orgmode, Rasmus


On 2015-05-05, at 23:32, Titus von der Malsburg <malsburg@posteo.de> wrote:

> Thanks, Rasmus (and Marcin), replacing the $ by \( and \) works.  I
> didn’t know about the parentheses notation.  To my experience most

Also, this: http://tex.stackexchange.com/questions/510

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University

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

* Re: Inline LaTeX formulae
  2015-05-05 21:32 ` Titus von der Malsburg
  2015-05-05 22:56   ` Marcin Borkowski
@ 2015-05-06 10:24   ` Rasmus
  2015-05-06 20:35     ` Titus von der Malsburg
  1 sibling, 1 reply; 14+ messages in thread
From: Rasmus @ 2015-05-06 10:24 UTC (permalink / raw)
  To: emacs-orgmode

Hi Titus,

Titus von der Malsburg <malsburg@posteo.de> writes:

> Parentheses are punctuation, so the $ in my example should be
> interpreted as math delimiters.  I think whoever wrote the code, simply
> overlooked parentheses when implementing the punctuation part.

I believe the problem is complicated.

In principal you could change the regexp in
org-element-latex-fragment-parser to something like

   "\\(\\s.\\|\\s-\\|\\s)\\|\\s\"\\|\\s_\\)"

You'd only be relying on syntax tables, which I believe is what you are
suggesting.  But this is also dangerous, see e.g. the recent change of
org-string-nw-p which previously relied on "\\S" cf.:

      http://article.gmane.org/gmane.emacs.orgmode/95473

> This should be fixed.

Patches welcome.  You could try to submit a patch where you add closing
parenthesis characters.

It could even be patches to org.texi!

Thanks,
Rasmus

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

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

* Re: Inline LaTeX formulae
  2015-05-06 10:24   ` Rasmus
@ 2015-05-06 20:35     ` Titus von der Malsburg
  2015-05-07  7:42       ` Eric S Fraga
  0 siblings, 1 reply; 14+ messages in thread
From: Titus von der Malsburg @ 2015-05-06 20:35 UTC (permalink / raw)
  To: Rasmus; +Cc: emacs-orgmode

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


On 2015-05-06 Wed 03:24, Rasmus wrote:
> Hi Titus,
>
> Titus von der Malsburg <malsburg@posteo.de> writes:
>
>> Parentheses are punctuation, so the $ in my example should be
>> interpreted as math delimiters.  I think whoever wrote the code, simply
>> overlooked parentheses when implementing the punctuation part.
>
> I believe the problem is complicated.

I had a look at the code in org-element.el (thanks for the
pointer).  There is clearly a bigger problem here: There is a
regexp listing all characters that must follow the final $ to make it
qualify as a math delimiter (whitespace, punctuation).  As I suspected,
the parentheses are missing from that list (and adding them solves my
problem).  However, a metric ton of other punctuation marks are missing
as well, Spanish question marks, curly/smart quotes, real dashes (a
minus is not a dash!), …, even the exclamation mark is missing.

If syntax tables allow a proper solution, great.  I understand that
syntax tables have a different meaning in different modes but does that
matter?  We’re only concerned about org-mode, right?  Are there
scenarios where the org parser is used in a non-org-mode buffer?

I also saw that the commit that introduced the problem had the following
description:

  Avoid using slow regexp.

So if the problem is only due to mistaken optimization, it would perhaps
be best to just revert to the previous code.

  Titus

> In principal you could change the regexp in
> org-element-latex-fragment-parser to something like
>
>    "\\(\\s.\\|\\s-\\|\\s)\\|\\s\"\\|\\s_\\)"
>
> You'd only be relying on syntax tables, which I believe is what you are
> suggesting.  But this is also dangerous, see e.g. the recent change of
> org-string-nw-p which previously relied on "\\S" cf.:
>
>       http://article.gmane.org/gmane.emacs.orgmode/95473
>
>> This should be fixed.
>
> Patches welcome.  You could try to submit a patch where you add closing
> parenthesis characters.
>
> It could even be patches to org.texi!
>
> Thanks,
> Rasmus


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

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

* Re: Inline LaTeX formulae
  2015-05-06 20:35     ` Titus von der Malsburg
@ 2015-05-07  7:42       ` Eric S Fraga
  2015-05-07 18:00         ` Titus von der Malsburg
  0 siblings, 1 reply; 14+ messages in thread
From: Eric S Fraga @ 2015-05-07  7:42 UTC (permalink / raw)
  To: Titus von der Malsburg; +Cc: emacs-orgmode, Rasmus

On Wednesday,  6 May 2015 at 13:35, Titus von der Malsburg wrote:

[...]

> So if the problem is only due to mistaken optimization, it would perhaps
> be best to just revert to the previous code.

Or if the "optimisation" was key to getting acceptable performance,
maybe discourage the use of $...$ and emphasise the use of \(...\).  The
latter is less fragile and is easy to generate by typing $ using the
following function and binding:

#+begin_src emacs-lisp 
  ;; from Nicolas Richard <theonewiththeevillook@yahoo.fr>
  ;; Date: Fri, 8 Mar 2013 16:23:02 +0100
  ;; Message-ID: <87vc913oh5.fsf@yahoo.fr>
  (defun yf/org-electric-dollar nil
  "When called once, insert \\(\\) and leave point in between.
  When called twice, replace the previously inserted \\(\\) by one $."
    (interactive)
    (if (and (looking-at "\\\\)") (looking-back "\\\\("))
        (progn (delete-char 2)
               (delete-char -2)
               (insert "$"))
      (insert "\\(\\)")
      (backward-char 2)))
  (define-key org-mode-map (kbd "$") 'yf/org-electric-dollar)
#+end_src

The problem with $ in org mode is that it is a valid standalone
character (i.e. currency symbol).  Having it perform two functions
(symbol and LaTeX directive) makes it difficult to optimise
performance.

-- 
: Eric S Fraga (0xFFFCF67D), Emacs 24.4.1, Org release_8.3beta-820-gd92ef9

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

* Re: Inline LaTeX formulae
  2015-05-07  7:42       ` Eric S Fraga
@ 2015-05-07 18:00         ` Titus von der Malsburg
  2015-05-07 20:00           ` Nicolas Goaziou
  0 siblings, 1 reply; 14+ messages in thread
From: Titus von der Malsburg @ 2015-05-07 18:00 UTC (permalink / raw)
  To: Eric S Fraga; +Cc: emacs-orgmode, Rasmus

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


On 2015-05-07 Thu 00:42, Eric S Fraga wrote:
> On Wednesday,  6 May 2015 at 13:35, Titus von der Malsburg wrote:
>
> [...]
>
>> So if the problem is only due to mistaken optimization, it would perhaps
>> be best to just revert to the previous code.
>
> Or if the "optimisation" was key to getting acceptable performance,
> maybe discourage the use of $...$ and emphasise the use of \(...\).  The
> latter is less fragile and is easy to generate by typing $ using the
> following function and binding:
>
> #+begin_src emacs-lisp 
>   ;; from Nicolas Richard <theonewiththeevillook@yahoo.fr>
>   ;; Date: Fri, 8 Mar 2013 16:23:02 +0100
>   ;; Message-ID: <87vc913oh5.fsf@yahoo.fr>
>   (defun yf/org-electric-dollar nil
>   "When called once, insert \\(\\) and leave point in between.
>   When called twice, replace the previously inserted \\(\\) by one $."
>     (interactive)
>     (if (and (looking-at "\\\\)") (looking-back "\\\\("))
>         (progn (delete-char 2)
>                (delete-char -2)
>                (insert "$"))
>       (insert "\\(\\)")
>       (backward-char 2)))
>   (define-key org-mode-map (kbd "$") 'yf/org-electric-dollar)
> #+end_src
>
> The problem with $ in org mode is that it is a valid standalone
> character (i.e. currency symbol).  Having it perform two functions
> (symbol and LaTeX directive) makes it difficult to optimise
> performance.

The double use of the $ character makes things somewhat tricky,
true.  However, the org manual makes it perfectly clear how the $ sign
should be interpreted and earlier versions of org implemented that
specification.  Hence, there is no need to reeducate users about how
they should write inline formulas.  This is simply a bug and it should
be fixed.

Rasmus’ proposal is simple and correctly implements what is specified in
the manual.  Below is a patch that implements this solution.  Does
anyone see a concrete problem?  If not, it should be merged.

  Titus


org-element: Ensure correct detection of latex fragments

* org-element.el (org-element-latex-fragment-parser): Ensure correct
  detection of latex fragments in
  `org-element-latex-fragment-parser'.  Uses syntax tables to detect
  whitespaces and punctuation marks following the final $ sign.

In order to qualify as a math delimiter, the final $ sign of a LaTeX
fragment has to be followed by a whitespace or punctuation mark but the
regexp used in the previous code matched only a small number of
punctuation marks and therefore missed some latex fragments.


diff --git a/lisp/org-element.el b/lisp/org-element.el
index 7aab9f6..d0c0485 100644
--- a/lisp/org-element.el
+++ b/lisp/org-element.el
@@ -2963,7 +2963,7 @@ Assume point is at the beginning of the LaTeX fragment."
                         (search-forward "$" nil t 2)
                         (not (memq (char-before (match-beginning 0))
                                    '(?\s ?\t ?\n ?, ?.)))
-                        (looking-at "\\([- \t.,?;:'\"]\\|$\\)")
+                        (looking-at "\\(\\s.\\|\\s-\\|\\s)\\|\\s\"\\|\\s_\\)")
                         (point)))
                (case (char-after (1+ (point)))
                  (?\( (search-forward "\\)" nil t))



[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

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

* Re: Inline LaTeX formulae
  2015-05-07 18:00         ` Titus von der Malsburg
@ 2015-05-07 20:00           ` Nicolas Goaziou
  2015-05-08 19:52             ` Titus von der Malsburg
  0 siblings, 1 reply; 14+ messages in thread
From: Nicolas Goaziou @ 2015-05-07 20:00 UTC (permalink / raw)
  To: Titus von der Malsburg; +Cc: emacs-orgmode, Rasmus

Hello,

Titus von der Malsburg <malsburg@posteo.de> writes:

> Rasmus’ proposal is simple and correctly implements what is specified in
> the manual.  Below is a patch that implements this solution.  Does
> anyone see a concrete problem?  If not, it should be merged.

I do: you removed end of line matcher. Also, I do not see the point of
matching "\s_".

You also need to update `org-latex-regexps' accordingly.

Thank you.


Regards,

-- 
Nicolas Goaziou

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

* Re: Inline LaTeX formulae
  2015-05-07 20:00           ` Nicolas Goaziou
@ 2015-05-08 19:52             ` Titus von der Malsburg
  2015-05-08 21:40               ` Nicolas Goaziou
  0 siblings, 1 reply; 14+ messages in thread
From: Titus von der Malsburg @ 2015-05-08 19:52 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode

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


On 2015-05-07 Thu 13:00, Nicolas Goaziou wrote:
> Titus von der Malsburg <malsburg@posteo.de> writes:
>
>> Rasmus’ proposal is simple and correctly implements what is specified in
>> the manual.  Below is a patch that implements this solution.  Does
>> anyone see a concrete problem?  If not, it should be merged.
>
> I do: you removed end of line matcher. Also, I do not see the point of
> matching "\s_".

Both are fixed in the patch below.  I also included opening
parentheses.  It’s an unlikely case but for consistency they should be
included.

> You also need to update `org-latex-regexps' accordingly.

Done.

I also updated org.texi.  Dashes are punctuation and don’t need special
mention.  I also clarified that quotes and parentheses are considered to
be punctuation (although Emacs has separate syntax classes for them).

Thanks for your feedback.

  Titus
  
diff --git a/doc/org.texi b/doc/org.texi
index 7b78417..d926de4 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -10347,9 +10347,10 @@ Text within the usual @LaTeX{} math delimiters.  To avoid conflicts with
 currency specifications, single @samp{$} characters are only recognized as
 math delimiters if the enclosed text contains at most two line breaks, is
 directly attached to the @samp{$} characters with no whitespace in between,
-and if the closing @samp{$} is followed by whitespace, punctuation or a dash.
-For the other delimiters, there is no such restriction, so when in doubt, use
-@samp{\(...\)} as inline math delimiters.
+and if the closing @samp{$} is followed by whitespace or punctuation
+(parentheses and quotes are considered to be punctuation in this
+context).  For the other delimiters, there is no such restriction, so when in
+doubt, use @samp{\(...\)} as inline math delimiters.
 @end itemize
 
 @noindent For example:
diff --git a/lisp/org-element.el b/lisp/org-element.el
index 7aab9f6..8f57c90 100644
--- a/lisp/org-element.el
+++ b/lisp/org-element.el
@@ -2963,7 +2963,7 @@ Assume point is at the beginning of the LaTeX fragment."
 			 (search-forward "$" nil t 2)
 			 (not (memq (char-before (match-beginning 0))
 				    '(?\s ?\t ?\n ?, ?.)))
-			 (looking-at "\\([- \t.,?;:'\"]\\|$\\)")
+			 (looking-at "\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|$\\)")
 			 (point)))
 		(case (char-after (1+ (point)))
 		  (?\( (search-forward "\\)" nil t))
diff --git a/lisp/org.el b/lisp/org.el
index 6139876..46a73b6 100755
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -540,8 +540,8 @@ An entry can be toggled between COMMENT and normal with
   '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
     ;; ("$" "\\([ 	(]\\|^\\)\\(\\(\\([$]\\)\\([^ 	\r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ 	\r\n,.$]\\)\\4\\)\\)\\([ 	.,?;:'\")]\\|$\\)" 2 nil)
     ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
-    ("$1" "\\([^$]\\|^\\)\\(\\$[^ 	\r\n,;.$]\\$\\)\\([- 	.,?;:'\")\000]\\|$\\)" 2 nil)
-    ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ 	\r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ 	\r\n,.$]\\)\\$\\)\\)\\([- 	.,?;:'\")\000]\\|$\\)" 2 nil)
+    ("$1" "\\([^$]\\|^\\)\\(\\$[^ 	\r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
+    ("$"  "\\([^$]\\|^\\)\\(\\(\\$\\([^ 	\r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ 	\r\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
     ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
     ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
     ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))




[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

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

* Re: Inline LaTeX formulae
  2015-05-08 19:52             ` Titus von der Malsburg
@ 2015-05-08 21:40               ` Nicolas Goaziou
  2015-05-08 22:25                 ` Titus von der Malsburg
  0 siblings, 1 reply; 14+ messages in thread
From: Nicolas Goaziou @ 2015-05-08 21:40 UTC (permalink / raw)
  To: Titus von der Malsburg; +Cc: emacs-orgmode

Titus von der Malsburg <malsburg@posteo.de> writes:

> Both are fixed in the patch below.

Thank you.

> I also included opening parentheses. It’s an unlikely case but for
> consistency they should be included.

If there's no need to exclude opening parenthesis, I suggest to use

  "\\([[:punct:][:space:]]\\|$\\)"

instead of

  "\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|$\\)"

It is shorter and clearer.

> I also updated org.texi.  Dashes are punctuation and don’t need special
> mention.  I also clarified that quotes and parentheses are considered to
> be punctuation (although Emacs has separate syntax classes for them).

OK.

Could you provide a commit message for this patch and send it again,
with change above?


Regards,

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

* Re: Inline LaTeX formulae
  2015-05-08 21:40               ` Nicolas Goaziou
@ 2015-05-08 22:25                 ` Titus von der Malsburg
  2015-05-09  8:01                   ` Nicolas Goaziou
  0 siblings, 1 reply; 14+ messages in thread
From: Titus von der Malsburg @ 2015-05-08 22:25 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode

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


On 2015-05-08 Fri 14:40, Nicolas Goaziou wrote:
> Titus von der Malsburg <malsburg@posteo.de> writes:
>
>> Both are fixed in the patch below.
>
> Thank you.
>
>> I also included opening parentheses. It’s an unlikely case but for
>> consistency they should be included.
>
> If there's no need to exclude opening parenthesis, I suggest to use
>
>   "\\([[:punct:][:space:]]\\|$\\)"

This looks indeed much nicer but it’s not what we want.  The Emacs
documentation says:

  ‘[:punct:]’
    This matches any punctuation character. (At present, for multibyte
    characters, it matches anything that has non-word syntax.)

If this matches any non-word multibyte character, it also matches things
like the multiplication sign along with a lot of other non-punctuation
characters.  So if we’d use [:punct:] we would incorrectly detect a
latex fragment in the following example:

  50$=10$×5

The patch with commit message is below.  I did the FSF copyright
paperwork a while ago (for a contribution to Emacs) so the TINYCHANGE
tag may not be required.

> instead of
>
>   "\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|$\\)"
>
> It is shorter and clearer.
>
>> I also updated org.texi.  Dashes are punctuation and don’t need special
>> mention.  I also clarified that quotes and parentheses are considered to
>> be punctuation (although Emacs has separate syntax classes for them).
>
> OK.
>
> Could you provide a commit message for this patch and send it again,
> with change above?

Fix detection of latex fragments

* org-element.el (org-element-latex-fragment-parser):
* org.el (org-latex-regexps): Fix the detection of latex
  fragments.  Uses syntax tables to detect whitespaces and punctuation
  marks following the final $ sign.

In order to qualify as a math delimiter, the final $ sign of a LaTeX
fragment has to be followed by a whitespace or punctuation mark but the
regexp used in the previous code matched only a small number of
punctuation marks and therefore missed some latex fragments.

diff --git a/doc/org.texi b/doc/org.texi
index 7b78417..d926de4 100644
--- a/doc/org.texi
+++ b/doc/org.texi
@@ -10347,9 +10347,10 @@ Text within the usual @LaTeX{} math delimiters.  To avoid conflicts with
 currency specifications, single @samp{$} characters are only recognized as
 math delimiters if the enclosed text contains at most two line breaks, is
 directly attached to the @samp{$} characters with no whitespace in between,
-and if the closing @samp{$} is followed by whitespace, punctuation or a dash.
-For the other delimiters, there is no such restriction, so when in doubt, use
-@samp{\(...\)} as inline math delimiters.
+and if the closing @samp{$} is followed by whitespace or punctuation
+(parentheses and quotes are considered to be punctuation in this
+context).  For the other delimiters, there is no such restriction, so when in
+doubt, use @samp{\(...\)} as inline math delimiters.
 @end itemize
 
 @noindent For example:
diff --git a/lisp/org-element.el b/lisp/org-element.el
index 7aab9f6..8f57c90 100644
--- a/lisp/org-element.el
+++ b/lisp/org-element.el
@@ -2963,7 +2963,7 @@ Assume point is at the beginning of the LaTeX fragment."
 			 (search-forward "$" nil t 2)
 			 (not (memq (char-before (match-beginning 0))
 				    '(?\s ?\t ?\n ?, ?.)))
-			 (looking-at "\\([- \t.,?;:'\"]\\|$\\)")
+			 (looking-at "\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|$\\)")
 			 (point)))
 		(case (char-after (1+ (point)))
 		  (?\( (search-forward "\\)" nil t))
diff --git a/lisp/org.el b/lisp/org.el
index 6139876..46a73b6 100755
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -540,8 +540,8 @@ An entry can be toggled between COMMENT and normal with
   '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
     ;; ("$" "\\([ 	(]\\|^\\)\\(\\(\\([$]\\)\\([^ 	\r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ 	\r\n,.$]\\)\\4\\)\\)\\([ 	.,?;:'\")]\\|$\\)" 2 nil)
     ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
-    ("$1" "\\([^$]\\|^\\)\\(\\$[^ 	\r\n,;.$]\\$\\)\\([- 	.,?;:'\")\000]\\|$\\)" 2 nil)
-    ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ 	\r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ 	\r\n,.$]\\)\\$\\)\\)\\([- 	.,?;:'\")\000]\\|$\\)" 2 nil)
+    ("$1" "\\([^$]\\|^\\)\\(\\$[^ 	\r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
+    ("$"  "\\([^$]\\|^\\)\\(\\(\\$\\([^ 	\r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ 	\r\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
     ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
     ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
     ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))



[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

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

* Re: Inline LaTeX formulae
  2015-05-08 22:25                 ` Titus von der Malsburg
@ 2015-05-09  8:01                   ` Nicolas Goaziou
  0 siblings, 0 replies; 14+ messages in thread
From: Nicolas Goaziou @ 2015-05-09  8:01 UTC (permalink / raw)
  To: Titus von der Malsburg; +Cc: emacs-orgmode

Titus von der Malsburg <malsburg@posteo.de> writes:

> This looks indeed much nicer but it’s not what we want.  The Emacs
> documentation says:
>
>   ‘[:punct:]’
>     This matches any punctuation character. (At present, for multibyte
>     characters, it matches anything that has non-word syntax.)
>
> If this matches any non-word multibyte character, it also matches things
> like the multiplication sign along with a lot of other non-punctuation
> characters.  So if we’d use [:punct:] we would incorrectly detect a
> latex fragment in the following example:
>
>   50$=10$×5

Too bad, then. The current regexp is really ugly.

> Fix detection of latex fragments
>
> * org-element.el (org-element-latex-fragment-parser):
> * org.el (org-latex-regexps): Fix the detection of latex
>   fragments.  Uses syntax tables to detect whitespaces and punctuation
>   marks following the final $ sign.
>
> In order to qualify as a math delimiter, the final $ sign of a LaTeX
> fragment has to be followed by a whitespace or punctuation mark but the
> regexp used in the previous code matched only a small number of
> punctuation marks and therefore missed some latex fragments.

Applied. Thank you.


Regards,

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

end of thread, other threads:[~2015-05-09  8:00 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-05-05 20:14 Inline LaTeX formulae Titus von der Malsburg
2015-05-05 20:23 ` Rasmus
2015-05-05 20:27 ` Marcin Borkowski
2015-05-05 21:32 ` Titus von der Malsburg
2015-05-05 22:56   ` Marcin Borkowski
2015-05-06 10:24   ` Rasmus
2015-05-06 20:35     ` Titus von der Malsburg
2015-05-07  7:42       ` Eric S Fraga
2015-05-07 18:00         ` Titus von der Malsburg
2015-05-07 20:00           ` Nicolas Goaziou
2015-05-08 19:52             ` Titus von der Malsburg
2015-05-08 21:40               ` Nicolas Goaziou
2015-05-08 22:25                 ` Titus von der Malsburg
2015-05-09  8:01                   ` Nicolas Goaziou

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