emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] lisp/ox-latex.el: put labels inside example blocks
@ 2022-12-01 23:36 hugo
  2022-12-08 11:39 ` Ihor Radchenko
  0 siblings, 1 reply; 5+ messages in thread
From: hugo @ 2022-12-01 23:36 UTC (permalink / raw)
  To: Emacs Orgmode

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

I exported an essay written in org mode to a LaTeX file (and then a pdf)
today. It had `example' blocks in several consequent sections, each with
a name (i.e. `#+name'). I have `org-latex-prefer-user-labels' set to t,
and used these names to refer to the examples later on.

I noticed that some of these labels were resolved wrong (i.e. printed as
the wrong number) in the resulting pdf. Examining the tex source, this
was because the labels had been printed outside (just before) the
example blocks, thus:

,---
|  \label{ex:foo}
|  \begin{example}
|     My example here
|  \end{example}
`---


In tnhese cases, sometimes LaTeX interpreted the surrounding section as
the subject of the label, and printed the number of that section when
the label was referred to.

The attached patch forces example environments to be printed thus:

,---
|  \begin{example}\label{ex:foo}
|     My example here
|  \end{example}
`---

This solves the problem.

The logic/conditions for when to print a label, and how it is generated
remain unchanged.

Hope this is helpful for someone else too!

Hugo

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-lisp-ox-latex.el-put-labels-inside-example-blocks.patch --]
[-- Type: text/x-diff; name=0001-lisp-ox-latex.el-put-labels-inside-example-blocks.patch, Size: 1423 bytes --]

From ce920752084eaa776a885d403c18ddd981e92203 Mon Sep 17 00:00:00 2001
From: Hugo Heagren <hugo@heagren.com>
Date: Thu, 1 Dec 2022 23:24:10 +0000
Subject: [PATCH] lisp/ox-latex.el: put labels inside example blocks

* lisp/ox-latex.el (org-latex-example-block): Instead of
`org-latex--wrap-label', manually add label after
\begin{<environment>}.

This produces proper label resolution in LaTeX. Labels before the
environment begins can be interpreted as referring to the surrounding
section.
---
 lisp/ox-latex.el | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 3d18ee7ff..10e4292d2 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2127,13 +2127,13 @@ information."
     (let ((environment (or (org-export-read-attribute
 			    :attr_latex example-block :environment)
 			   "verbatim")))
-      (org-latex--wrap-label
-       example-block
-       (format "\\begin{%s}\n%s\\end{%s}"
-	       environment
-	       (org-export-format-code-default example-block info)
-	       environment)
-       info))))
+      (format "\\begin{%s}%s\n%s\\end{%s}"
+	      environment
+              (if (org-element-property :name example-block)
+                  (org-latex--label example-block info nil 'full)
+                "")
+	      (org-export-format-code-default example-block info)
+	      environment))))
 
 
 ;;;; Export Block
-- 
2.20.1


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

* Re: [PATCH] lisp/ox-latex.el: put labels inside example blocks
  2022-12-01 23:36 [PATCH] lisp/ox-latex.el: put labels inside example blocks hugo
@ 2022-12-08 11:39 ` Ihor Radchenko
  2022-12-08 12:36   ` Max Nikulin
  0 siblings, 1 reply; 5+ messages in thread
From: Ihor Radchenko @ 2022-12-08 11:39 UTC (permalink / raw)
  To: hugo; +Cc: Emacs Orgmode

hugo@heagren.com writes:

> The attached patch forces example environments to be printed thus:
>
> ,---
> |  \begin{example}\label{ex:foo}
> |     My example here
> |  \end{example}
> `---
>
> This solves the problem.
>
> The logic/conditions for when to print a label, and how it is generated
> remain unchanged.
>
> Hope this is helpful for someone else too!

Thanks!
Applied onto bugfix.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=304f18cb7

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


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

* Re: [PATCH] lisp/ox-latex.el: put labels inside example blocks
  2022-12-08 11:39 ` Ihor Radchenko
@ 2022-12-08 12:36   ` Max Nikulin
  2022-12-08 12:46     ` Ihor Radchenko
  0 siblings, 1 reply; 5+ messages in thread
From: Max Nikulin @ 2022-12-08 12:36 UTC (permalink / raw)
  To: hugo; +Cc: Emacs Orgmode

On 08/12/2022 18:39, Ihor Radchenko wrote:
> hugo@heagren.com writes:
>> The attached patch forces example environments to be printed thus:
>>
>> ,---
>> |  \begin{example}\label{ex:foo}
>> |     My example here
>> |  \end{example}
>> `---
> 
> Applied onto bugfix.
> https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=304f18cb7

Please, revert this patch. Default environment for #+begin_example is 
verbatim. With this patch the following PDF is generated

     \label{org9283054}
     code

for

     #+name: test
     #+begin_example
       code
     #+end_example

It is the reason why \label must be outside. I do not remember if line 
break penalty helps after \label.


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

* Re: [PATCH] lisp/ox-latex.el: put labels inside example blocks
  2022-12-08 12:36   ` Max Nikulin
@ 2022-12-08 12:46     ` Ihor Radchenko
  2022-12-08 12:54       ` Ihor Radchenko
  0 siblings, 1 reply; 5+ messages in thread
From: Ihor Radchenko @ 2022-12-08 12:46 UTC (permalink / raw)
  To: Max Nikulin; +Cc: hugo, Emacs Orgmode

Max Nikulin <manikulin@gmail.com> writes:

>> Applied onto bugfix.
>> https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=304f18cb7
>
> Please, revert this patch. Default environment for #+begin_example is 
> verbatim. With this patch the following PDF is generated
>
>      \label{org9283054}
>      code
>
> for
>
>      #+name: test
>      #+begin_example
>        code
>      #+end_example
>
> It is the reason why \label must be outside. I do not remember if line 
> break penalty helps after \label.

Oops. You are indeed right.
Reverted.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


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

* Re: [PATCH] lisp/ox-latex.el: put labels inside example blocks
  2022-12-08 12:46     ` Ihor Radchenko
@ 2022-12-08 12:54       ` Ihor Radchenko
  0 siblings, 0 replies; 5+ messages in thread
From: Ihor Radchenko @ 2022-12-08 12:54 UTC (permalink / raw)
  To: Max Nikulin; +Cc: hugo, Emacs Orgmode

Ihor Radchenko <yantar92@posteo.net> writes:

>> Please, revert this patch. Default environment for #+begin_example is 
>> verbatim. With this patch the following PDF is generated
>>
>>      \label{org9283054}
>>      code
>>
>> for
>>
>>      #+name: test
>>      #+begin_example
>>        code
>>      #+end_example
>>
>> It is the reason why \label must be outside. I do not remember if line 
>> break penalty helps after \label.
>
> Oops. You are indeed right.
> Reverted.

Hugo, you may consider writing an alternative patch that only puts label
inside for the environments where we know that it is safe.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


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

end of thread, other threads:[~2022-12-08 12:55 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-01 23:36 [PATCH] lisp/ox-latex.el: put labels inside example blocks hugo
2022-12-08 11:39 ` Ihor Radchenko
2022-12-08 12:36   ` Max Nikulin
2022-12-08 12:46     ` Ihor Radchenko
2022-12-08 12:54       ` Ihor Radchenko

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