emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Issues with nested begin..end blocks in inline math environments
@ 2019-12-06 19:42 Matt Huszagh
  2019-12-07 11:30 ` Fraga, Eric
  0 siblings, 1 reply; 7+ messages in thread
From: Matt Huszagh @ 2019-12-06 19:42 UTC (permalink / raw)
  To: emacs-orgmode

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

I'm experiencing incorrect and seemingly inconsistent behavior when nesting
`\begin` `\end` environments inside `\(\)` or `$$`. For example, the
following is valid latex code:
```
\(\begin{aligned}
b_n &= \frac{1}{\pi} \int_{0}^{\pi} x \sin{\left(nx\right)}dx \\
b_0 &= 0 \\
\int_{0}^{\pi} udv &= \left[uv\right]_{0}^{\pi}
  - \int_{0}^{\pi} vdu \\
dv &= \sin{\left(nx\right)}dx \\
v(x) &= \int \sin{\left(nx\right)}dx \\
&= -\frac{\cos{\left(nx\right)}}{n} \\
\left[uv\right]_{0}^{\pi} &=
  -\left[\frac{x\cos{\left(nx\right)}}{n}\right]_{0}^{\pi} \\
&= \left\{\begin{alignedat}{2}
  \frac{\pi}{n}&, && \quad \text{$n$ odd} \\
  -\frac{\pi}{n}&, && \quad \text{$n$ positive even} \\
\end{alignedat}\right. \\
\int_{0}^{\pi} vdu &=
  \int_{0}^{\pi} -\frac{\cos{\left(nx\right)}}{n} dx \\
&= -\frac{1}{n^2} \left.\sin{\left(nx\right)}\right|_{0}^{\pi} \\
&= 0 \\
b_{n\in \mathbb{Z}^+} &= \left\{\begin{alignedat}{2}
  \frac{1}{n}&, && \quad \text{$n$ odd} \\
  -\frac{1}{n}&, && \quad \text{$n$ positive even} \\
\end{alignedat}\right. \\
\end{aligned}\)
```
But this confuses org which is generating images for the `\begin{aligned}`
at the top and the `\text` macros, but nothing else. Iterations on this
sometimes work fine, however. For example,
```
\(\begin{aligned}
b_{n\in \mathbb{Z}^+} &= \left\{\begin{alignedat}{2}
  \frac{1}{n}&, && \quad \text{$n$ odd} \\
  -\frac{1}{n}&, && \quad \text{$n$ positive even} \\
\end{alignedat}\right. \\
\end{aligned}\)
```
typesets correctly. Has anyone else had trouble with the latex fragment
regex's?

[-- Attachment #2: Type: text/html, Size: 1897 bytes --]

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

* Re: Issues with nested begin..end blocks in inline math environments
  2019-12-06 19:42 Issues with nested begin..end blocks in inline math environments Matt Huszagh
@ 2019-12-07 11:30 ` Fraga, Eric
  2019-12-07 19:28   ` Matt Huszagh
  2019-12-15  4:01   ` Matt Huszagh
  0 siblings, 2 replies; 7+ messages in thread
From: Fraga, Eric @ 2019-12-07 11:30 UTC (permalink / raw)
  To: Matt Huszagh; +Cc: emacs-orgmode@gnu.org

On Friday,  6 Dec 2019 at 11:42, Matt Huszagh wrote:
> I'm experiencing incorrect and seemingly inconsistent behavior when nesting
> `\begin` `\end` environments inside `\(\)` or `$$`. For example, the
> following is valid latex code:

The identification of LaTeX fragments is somewhat fragile (in my
experience).  I would suggest you enclose complex LaTeX code fragments
within an #+begin_export latex ... #+end_export environment.

Assuming your export target is LaTeX and/or PDF.

-- 
Eric S Fraga via Emacs 27.0.50, Org release_9.3-34-g2eee3c

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

* Re: Issues with nested begin..end blocks in inline math environments
  2019-12-07 11:30 ` Fraga, Eric
@ 2019-12-07 19:28   ` Matt Huszagh
  2019-12-08  7:04     ` Matt Huszagh
  2019-12-15  4:01   ` Matt Huszagh
  1 sibling, 1 reply; 7+ messages in thread
From: Matt Huszagh @ 2019-12-07 19:28 UTC (permalink / raw)
  To: Fraga, Eric; +Cc: emacs-orgmode@gnu.org

"Fraga, Eric" <e.fraga@ucl.ac.uk> writes:
> The identification of LaTeX fragments is somewhat fragile (in my
> experience).  I would suggest you enclose complex LaTeX code fragments
> within an #+begin_export latex ... #+end_export environment.
>
> Assuming your export target is LaTeX and/or PDF.

Thanks for the suggestion. Unfortunately these are mostly just used as
latex previews in the org buffer. I'm going to try to improve support
for this when I get the time since I use this capability extensively. If
you have any thoughts/suggestions on best ways to improve this I'd be
happy to hear them.

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

* Re: Issues with nested begin..end blocks in inline math environments
  2019-12-07 19:28   ` Matt Huszagh
@ 2019-12-08  7:04     ` Matt Huszagh
  0 siblings, 0 replies; 7+ messages in thread
From: Matt Huszagh @ 2019-12-08  7:04 UTC (permalink / raw)
  To: Fraga, Eric; +Cc: emacs-orgmode@gnu.org

"Fraga, Eric" <e.fraga@ucl.ac.uk> writes:
> The identification of LaTeX fragments is somewhat fragile (in my
> experience).  I would suggest you enclose complex LaTeX code fragments
> within an #+begin_export latex ... #+end_export environment.
>
> Assuming your export target is LaTeX and/or PDF.

Changing the beginning and end regexes to the following seems to work
well (I've just added a non-capturing group for \(\) or $$ so that
begin-end blocks can be nested inside inline math environments).

(setq org-element--latex-begin-environment "^[ \t]*\\(?:\\\\(\\|\\$\\)?\\\\begin{\\([A-Za-z0-9*]+\\)}")
(setq org-element--latex-end-environment "\\\\end{%s}[ \t]*\\(?:\\\\)\\|\\$\\)?$")

I'm going to test this a bit before submitting it as a patch to make
sure it doesn't cause any issues. If you have any tricky fragments you
want to test it on please let me how it works!

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

* Re: Issues with nested begin..end blocks in inline math environments
  2019-12-07 11:30 ` Fraga, Eric
  2019-12-07 19:28   ` Matt Huszagh
@ 2019-12-15  4:01   ` Matt Huszagh
  2019-12-15  8:58     ` Nicolas Goaziou
  1 sibling, 1 reply; 7+ messages in thread
From: Matt Huszagh @ 2019-12-15  4:01 UTC (permalink / raw)
  To: Fraga, Eric; +Cc: emacs-orgmode@gnu.org

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


I'm submitting this as a patch. I've used it on hundreds of latex
fragments over the past week or so and haven't experienced any issues
(which is expected since the change is small).


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

From a699b699ed4132839c39f1152868bb13364422c7 Mon Sep 17 00:00:00 2001
From: Matt Huszagh <huszaghmatt@gmail.com>
Date: Sat, 14 Dec 2019 19:54:41 -0800
Subject: [PATCH] org-element.el: allow environment blocks in math delimiters

* lisp/org-element.el (org-element--latex-begin-environment): Add a
non-capturing block for `\(' or `$' so that previously recognized
latex environments can also appear within an inline math environment.

* lisp/org-element.el (org-element--latex-end-environment): Match the
begin environment noncapturing block with `$' or `\)'.
---
 lisp/org-element.el | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lisp/org-element.el b/lisp/org-element.el
index 110ff5624..6d7ec32c6 100644
--- a/lisp/org-element.el
+++ b/lisp/org-element.el
@@ -2222,14 +2222,14 @@ containing `:key', `:value', `:begin', `:end', `:post-blank' and
 ;;;; Latex Environment
 
 (defconst org-element--latex-begin-environment
-  "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}"
+  "^[ \t]*\\(?:\\\\(\\|\\$\\)?\\\\begin{\\([A-Za-z0-9*]+\\)}"
   "Regexp matching the beginning of a LaTeX environment.
 The environment is captured by the first group.
 
 See also `org-element--latex-end-environment'.")
 
 (defconst org-element--latex-end-environment
-  "\\\\end{%s}[ \t]*$"
+  "\\\\end{%s}[ \t]*\\(?:\\\\)\\|\\$\\)?$"
   "Format string matching the ending of a LaTeX environment.
 See also `org-element--latex-begin-environment'.")
 
-- 
2.24.0


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

* Re: Issues with nested begin..end blocks in inline math environments
  2019-12-15  4:01   ` Matt Huszagh
@ 2019-12-15  8:58     ` Nicolas Goaziou
  2019-12-16  8:25       ` Matt Huszagh
  0 siblings, 1 reply; 7+ messages in thread
From: Nicolas Goaziou @ 2019-12-15  8:58 UTC (permalink / raw)
  To: Matt Huszagh; +Cc: emacs-orgmode@gnu.org, Fraga, Eric

Hello,

Matt Huszagh <huszaghmatt@gmail.com> writes:

> I'm submitting this as a patch. I've used it on hundreds of latex
> fragments over the past week or so and haven't experienced any issues
> (which is expected since the change is small).
>
> From a699b699ed4132839c39f1152868bb13364422c7 Mon Sep 17 00:00:00 2001
> From: Matt Huszagh <huszaghmatt@gmail.com>
> Date: Sat, 14 Dec 2019 19:54:41 -0800
> Subject: [PATCH] org-element.el: allow environment blocks in math delimiters
>
> * lisp/org-element.el (org-element--latex-begin-environment): Add a
> non-capturing block for `\(' or `$' so that previously recognized
> latex environments can also appear within an inline math environment.
>
> * lisp/org-element.el (org-element--latex-end-environment): Match the
> begin environment noncapturing block with `$' or `\)'.

I don't think this patch is a good idea, as it mixes elements from
different types: LaTeX fragment (inline), and LaTeX environment (block).
For example, the regexp you modify is used as a paragraph separator,
which doesn't make sense for inline LaTeX.

As a reminder, Org does not claim to support raw LaTeX syntax besides
very simple constructs. If you want to insert elaborate LaTeX code, you
can always use:

    #+begin_export latex
    ...
    #+end_export

Regards,

-- 
Nicolas Goaziou

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

* Re: Issues with nested begin..end blocks in inline math environments
  2019-12-15  8:58     ` Nicolas Goaziou
@ 2019-12-16  8:25       ` Matt Huszagh
  0 siblings, 0 replies; 7+ messages in thread
From: Matt Huszagh @ 2019-12-16  8:25 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode@gnu.org, Fraga, Eric

Nicolas Goaziou <mail@nicolasgoaziou.fr> writes:

> I don't think this patch is a good idea, as it mixes elements from
> different types: LaTeX fragment (inline), and LaTeX environment (block).
> For example, the regexp you modify is used as a paragraph separator,
> which doesn't make sense for inline LaTeX.

No worries. Thanks for taking a look at this anyway.

Matt

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

end of thread, other threads:[~2019-12-16  8:26 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-12-06 19:42 Issues with nested begin..end blocks in inline math environments Matt Huszagh
2019-12-07 11:30 ` Fraga, Eric
2019-12-07 19:28   ` Matt Huszagh
2019-12-08  7:04     ` Matt Huszagh
2019-12-15  4:01   ` Matt Huszagh
2019-12-15  8:58     ` Nicolas Goaziou
2019-12-16  8:25       ` Matt Huszagh

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