emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Bug: Percentage in caption (even escaped) does not work in LaTeX export
@ 2021-07-30 15:00 Charest, Luc
  2021-08-03 13:51 ` Eric S Fraga
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Charest, Luc @ 2021-07-30 15:00 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

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


Hello,

   I simplified the problem down to :

#+CAPTION: Org Mode works 99.99\% of the time.
#+BEGIN_SRC -n
// this is only a proof of concept
#+END_SRC

As soon as I put the percentage sign in the caption, the LaTeX export backend fails with this message :

org-latex-src-block: Not enough arguments for format string

I tried "%", "\%", "\\%", "%%"... nothing works !

If I get it wrong, sorry but please update the documentation accordingly, as I found nothing in the specs regarding this matter specifically.

My versions :
Emacs  : GNU Emacs 26.2 (build 1, x86_64-w64-mingw32)
of 2019-04-13
Package: Org mode version 9.4.6 (9.4.6-4-g093c94-elpa)

Thanks !

[http://www.hydroquebec.com/images/logo-hqp-courriel.gif]<http://www.hydroquebec.com/residentiel/>

Luc Charest
Conseiller Optimisation I
Optimisation Modélisation Mathématique
Direction Production et Exploitation
Hydro-Québec Production
Édifice Jean-Lesage
75, boulevard René-Lévesque Ouest, 9e étage
Montréal (Québec) H2Z 1A4
Tél. : 514 289-2211,7049
Interne0-289-7049
www.hydroquebec.com<http://www.hydroquebec.com/residentiel/>
[http://www.hydroquebec.com/images/2019g289-signature-courriel-bandeau.gif]

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

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

* Re: Bug: Percentage in caption (even escaped) does not work in LaTeX export
  2021-07-30 15:00 Bug: Percentage in caption (even escaped) does not work in LaTeX export Charest, Luc
@ 2021-08-03 13:51 ` Eric S Fraga
  2021-08-31 15:38   ` Timothy
  2021-09-03 12:17 ` Maxim Nikulin
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 12+ messages in thread
From: Eric S Fraga @ 2021-08-03 13:51 UTC (permalink / raw)
  To: Charest, Luc; +Cc: emacs-orgmode@gnu.org

On Friday, 30 Jul 2021 at 15:00, Charest, Luc wrote:
>    I simplified the problem down to :
>
> #+CAPTION: Org Mode works 99.99\% of the time.
> #+BEGIN_SRC -n
>
> // this is only a proof of concept
> #+END_SRC
>
> As soon as I put the percentage sign in the caption, the LaTeX export backend fails with this message :
>
> org-latex-src-block: Not enough arguments for format string

whereas for me it fails because no language was given for the src block,
i.e. different error message in the latex compilation.  What happens if
you do specify a language?

If I specify a language, the export is fine with % unescaped, by the
way.

-- 
: Eric S Fraga via Emacs 28.0.50, Org release_9.4.6-607-g185706
: Latest paper written in org: https://arxiv.org/abs/2106.05096


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

* Re: Bug: Percentage in caption (even escaped) does not work in LaTeX export
  2021-08-03 13:51 ` Eric S Fraga
@ 2021-08-31 15:38   ` Timothy
  2021-08-31 16:43     ` Charest, Luc
  0 siblings, 1 reply; 12+ messages in thread
From: Timothy @ 2021-08-31 15:38 UTC (permalink / raw)
  To: Charest, Luc; +Cc: emacs-orgmode, Eric S Fraga

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

Hi Luc,

Eric wrote:
> What happens if you do specify a language?
>
> If I specify a language, the export is fine with % unescaped, by the
> way.

Have you been able to give this a shot?

All the best,
Timothy

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

* RE: Bug: Percentage in caption (even escaped) does not work in LaTeX export
  2021-08-31 15:38   ` Timothy
@ 2021-08-31 16:43     ` Charest, Luc
  0 siblings, 0 replies; 12+ messages in thread
From: Charest, Luc @ 2021-08-31 16:43 UTC (permalink / raw)
  To: Timothy; +Cc: emacs-orgmode@gnu.org, Eric S Fraga

Hello Timothy,

   Yeah, I tried, and it did not worked.

   I had exchanged emails with Eric, we came to the conclusion that the source of the problem might be a clash between my emacs version 26 and an updated version of the org-mode. I have to try to update my emacs to see where it goes from there, but have to find some time to do it.

   I was thinking also I might had screw up my installation some time ago by attempting to install Org-roam. It stopped at some point complaining about not finding the database, which, from what I understand, was supposed to be downloaded and compiled manually.

   Anyway, I'll try updating my emacs with a fresh install, I'll keep you inform. 

Thank you for the follow-up ! 

-----Message d'origine-----
De : Timothy <tecosaur@gmail.com> 
Envoyé : 31 août 2021 11:38
À : Charest, Luc <charest.luc@hydroquebec.com>
Cc : Eric S Fraga <e.fraga@ucl.ac.uk>; emacs-orgmode@gnu.org
Objet : Re: Bug: Percentage in caption (even escaped) does not work in LaTeX export

[You don't often get email from tecosaur@gmail.com. Learn why this is important at http://aka.ms/LearnAboutSenderIdentification.]

Courriel provenant de l'externe
ATTENTION, avant d'accéder à une pièce jointe ou à un lien de ce courriel, assurez-vous que celui-ci provient d'un tiers de confiance.

Hi Luc,

Eric wrote:
> What happens if you do specify a language?
>
> If I specify a language, the export is fine with % unescaped, by the 
> way.

Have you been able to give this a shot?

All the best,
Timothy


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

* Re: Bug: Percentage in caption (even escaped) does not work in LaTeX export
  2021-07-30 15:00 Bug: Percentage in caption (even escaped) does not work in LaTeX export Charest, Luc
  2021-08-03 13:51 ` Eric S Fraga
@ 2021-09-03 12:17 ` Maxim Nikulin
  2021-09-07 12:21   ` [PATCH] ox-latex: Allow percent sign in 'src-block' caption Maxim Nikulin
  2021-09-25 16:04 ` Bug: Percentage in caption (even escaped) does not work in LaTeX export Bastien
  2022-05-08 16:15 ` Timothy
  3 siblings, 1 reply; 12+ messages in thread
From: Maxim Nikulin @ 2021-09-03 12:17 UTC (permalink / raw)
  To: emacs-orgmode

On 30/07/2021 22:00, Charest, Luc wrote:
>     I simplified the problem down to :
> 
> #+CAPTION: Org Mode works 99.99\% of the time.
> #+BEGIN_SRC -n
> // this is only a proof of concept
> #+END_SRC
> 
> As soon as I put the percentage sign in the caption, the LaTeX export 
> backend fails with this message :
> 
> org-latex-src-block: Not enough arguments for format string

Confirmed

User input is combined with format string in `org-latex-src-block' so 
percent character in caption is treated as a format specifier. Maybe 
emacs versions newer than 26 does not throw an error on extra specifier 
with missed argument. =:latex-cation-above nil= might still cause 
corrupted LaTeX code.

Code of `org-latex-src-block' is quite convoluted to provide a quick 
fix. Besides particular reported case it is better to check variants 
with listings and minted packages, custom environment.

Absence of language and line numbering option are irrelevant. Backslash 
before percent likely should not be used in Org files. My minimal example

     #+caption: User input "%S" is combined with format string
     #+begin_src emacs-lisp
       t
     #+end_src

P.S. "Not enough arguments for format string" error usually means that a 
string provided by user is passed as format or incorrect number of 
arguments is specified for a format-like function. The former case is a 
quite frequent mistake:
- https://orgmode.org/list/87bl5tzof2.fsf@posteo.net/,
- 
https://orgmode.org/list/CAJ51ETqZm+8rwcOMYMh2MEXe8Si0Nzn2Wq3K55N041-RAxhSew@mail.gmail.com/ 
(vs. correct variant 
https://emacs.stackexchange.com/questions/38276/get-description-of-an-org-mode-link).

Use

(defun my-safe-fun (msg)
  (message "%s" msg))

not just

(defun not-a-fun (msg)
  (message msg))




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

* [PATCH] ox-latex: Allow percent sign in 'src-block' caption
  2021-09-03 12:17 ` Maxim Nikulin
@ 2021-09-07 12:21   ` Maxim Nikulin
  2021-09-07 13:17     ` Timothy
  2021-09-25 16:04     ` Bastien
  0 siblings, 2 replies; 12+ messages in thread
From: Maxim Nikulin @ 2021-09-07 12:21 UTC (permalink / raw)
  To: emacs-orgmode

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

On 03/09/2021 19:17, Maxim Nikulin wrote:
> On 30/07/2021 22:00, Charest, Luc wrote:
>>
>> org-latex-src-block: Not enough arguments for format string
> 
> Confirmed
> 
> User input is combined with format string in `org-latex-src-block'
> so percent character in caption is treated as a format specifier.> Maybe emacs versions newer than 26 does not throw an error
> on extra specifier with missed argument. =:latex-cation-above nil=
> might still cause corrupted LaTeX code.

I have decided that it is better to provide a partial fix than to leave
the problem as is.  This patch covers only plain verbatim environment,
more advanced formatting using listings or minted should be addressed
by another change (Anybody?).  Do not close the bug after applying
this patch.

[-- Attachment #2: 0001-ox-latex-Allow-percent-sign-in-src-block-caption.patch --]
[-- Type: text/x-patch, Size: 6132 bytes --]

From 5e6930e3c3d30f13e6e33a6fc504b445cacc579d Mon Sep 17 00:00:00 2001
In-Reply-To: <sgt3om$mae$1@ciao.gmane.io>
References: <sgt3om$mae$1@ciao.gmane.io>
From: Max Nikulin <manikulin@gmail.com>
Date: Tue, 7 Sep 2021 19:01:11 +0700
Subject: [PATCH] ox-latex: Allow percent sign in 'src-block' caption

* lisp/ox-latex.el (org-latex-src-block): Prevent leak of percent sign
from caption to `format' first argument causing export failure due to
insufficient argument number.  The fix covers only the case of verbatim
environment.
* testing/lisp/test-ox.el
(test-org-export/latex-src-block-verbatim-caption): New test for src
block LaTeX export with cases of various formatting of caption and
verbatim environment.  Check that percent signs in caption and source
block body do not lead to errors.

Reported-by: Charest, Luc <charest.luc@hydroquebec.com>
<https://lists.gnu.org/archive/html/emacs-orgmode/2021-08/msg00017.html>
---
 lisp/ox-latex.el        | 30 ++++++-------
 testing/lisp/test-ox.el | 97 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 111 insertions(+), 16 deletions(-)

diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 99076f8b9..b325b8892 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -2994,22 +2994,20 @@ contextual information."
       (cond
        ;; Case 1.  No source fontification.
        ((or (not lang) (not listings))
-	(let* ((caption-str (org-latex--caption/label-string src-block info))
-	       (float-env
-		(cond ((string= "multicolumn" float)
-		       (format "\\begin{figure*}[%s]\n%s%%s\n%s\\end{figure*}"
-			       (plist-get info :latex-default-figure-position)
-			       (if caption-above-p caption-str "")
-			       (if caption-above-p "" caption-str)))
-		      (caption (concat
-				(if caption-above-p caption-str "")
-				"%s"
-				(if caption-above-p "" (concat "\n" caption-str))))
-		      (t "%s"))))
-	  (format
-	   float-env
-	   (concat (format "\\begin{verbatim}\n%s\\end{verbatim}"
-			   (org-export-format-code-default src-block info))))))
+	(let ((caption-str (org-latex--caption/label-string src-block info))
+              (verbatim (format "\\begin{verbatim}\n%s\\end{verbatim}"
+                                (org-export-format-code-default src-block info))))
+          (cond ((string= "multicolumn" float)
+                 (format "\\begin{figure*}[%s]\n%s%s\n%s\\end{figure*}"
+                         (plist-get info :latex-default-figure-position)
+                         (if caption-above-p caption-str "")
+                         verbatim
+                         (if caption-above-p "" caption-str)))
+                (caption (concat
+                          (if caption-above-p caption-str "")
+                          verbatim
+                          (if caption-above-p "" (concat "\n" caption-str))))
+                (t verbatim))))
        ;; Case 2.  Custom environment.
        (custom-env
 	(let ((caption-str (org-latex--caption/label-string src-block info))
diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el
index 3f39645af..54558041b 100644
--- a/testing/lisp/test-ox.el
+++ b/testing/lisp/test-ox.el
@@ -3907,6 +3907,103 @@ Another text. (ref:text)
 	      (org-export-format-code-default
 	       (org-element-map tree 'src-block #'identity info t) info))))))
 
+(ert-deftest test-org-export/latex-src-block-verbatim-caption ()
+  "Test `org-latex-src-block' caption for verbatim environment.
+Check that percent sign does not become a part of format.
+This test does not cover listings and custom environments."
+  (let ((export
+	 (lambda (buffer-text)
+	   (org-test-with-parsed-data
+	       buffer-text
+	     (let* ((backend (org-export-get-backend 'latex))
+		    (info (org-combine-plists
+			   (org-export--get-export-attributes backend)
+			   (org-export-get-environment backend)))
+		    (result (org-latex-src-block
+			     (org-element-map tree 'src-block #'identity info t)
+			     t info)))
+	       ;; Remove properties to make failure reports more clear.
+	       (set-text-properties 0 (length result) nil result)
+	       result)))))
+
+    (should (equal
+	     "\
+\\begin{verbatim}
+\"No float, no listings, 20%S\"
+\\end{verbatim}
+\\captionof{figure}{Caption of verbatim is below, 20\\%s}
+"
+	     (funcall export
+		      "\
+#+CAPTION: Caption of verbatim is below, 20%s
+#+BEGIN_SRC emacs-lisp
+  \"No float, no listings, 20%S\"
+#+END_SRC")))
+
+    ;; `org-latex-caption-above' has no associated property or keyword.
+    (should (equal
+	     "\
+\\captionof{figure}{Caption of verbatim is above, 40\\%s}
+\\begin{verbatim}
+\"No float, no listings, 40%S\"
+\\end{verbatim}"
+	     (let ((org-latex-caption-above t))
+	       (funcall export
+			"\
+#+CAPTION: Caption of verbatim is above, 40%s
+#+BEGIN_SRC emacs-lisp
+  \"No float, no listings, 40%S\"
+#+END_SRC"))))
+
+    (should (equal
+	     "\
+\\begin{figure*}[tp]
+\\caption{Caption is above, 60\\%s}
+\\begin{verbatim}
+\"Float, no listings, 60%S\"
+\\end{verbatim}
+\\end{figure*}"
+	     (let ((org-latex-caption-above t)
+		   (org-latex-default-figure-position "tp"))
+	       (funcall export
+			"\
+#+CAPTION: Caption is above, 60%s
+#+ATTR_LATEX: :float multicolumn
+#+BEGIN_SRC emacs-lisp
+  \"Float, no listings, 60%S\"
+#+END_SRC"))))
+
+    (should (equal
+	     "\
+\\begin{figure*}[tp]
+\\begin{verbatim}
+\"Float, no lang, listings, 80%S\"
+\\end{verbatim}
+\\caption{Caption is below, 60\\%s}
+\\end{figure*}"
+	     (let ((org-latex-listings 'minted) ; inactive due to missing lang
+		   (org-latex-default-figure-position "tp"))
+	       ;; Namely "multicolumn" value to get just figure environment
+	       ;; looks like a bug.
+	       (funcall export
+			"\
+#+CAPTION: Caption is below, 60%s
+#+ATTR_LATEX: :float multicolumn
+#+BEGIN_SRC
+  \"Float, no lang, listings, 80%S\"
+#+END_SRC"))))
+
+    (should (equal
+	     "\
+\\begin{verbatim}
+\"No caption, no float, no listings, 100%S\"
+\\end{verbatim}"
+	     (funcall export
+		      "\
+#+BEGIN_SRC emacs-lisp
+  \"No caption, no float, no listings, 100%S\"
+#+END_SRC")))))
+
 
 \f
 ;;; Smart Quotes
-- 
2.25.1


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

* Re: [PATCH] ox-latex: Allow percent sign in 'src-block' caption
  2021-09-07 12:21   ` [PATCH] ox-latex: Allow percent sign in 'src-block' caption Maxim Nikulin
@ 2021-09-07 13:17     ` Timothy
  2021-09-25 16:04     ` Bastien
  1 sibling, 0 replies; 12+ messages in thread
From: Timothy @ 2021-09-07 13:17 UTC (permalink / raw)
  To: Maxim Nikulin; +Cc: emacs-orgmode

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

Hi Maxim,

> I have decided that it is better to provide a partial fix than to leave
> the problem as is.  This patch covers only plain verbatim environment,
> more advanced formatting using listings or minted should be addressed
> by another change (Anybody?).  Do not close the bug after applying
> this patch.
>
> [2. text/x-patch; 0001-ox-latex-Allow-percent-sign-in-src-block-caption.patch]…

Thanks for the patch. At a glance it looks good, and it’s great to see you’ve
gone to the effort of writing some tests for it too! That’s something we should
probably do more of 😅. Hopefully someone else will get around to giving your
work a closer look and maybe merge it soon-ish :)

All the best,
Timothy

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

* Re: Bug: Percentage in caption (even escaped) does not work in LaTeX export
  2021-07-30 15:00 Bug: Percentage in caption (even escaped) does not work in LaTeX export Charest, Luc
  2021-08-03 13:51 ` Eric S Fraga
  2021-09-03 12:17 ` Maxim Nikulin
@ 2021-09-25 16:04 ` Bastien
  2021-09-30 14:20   ` Max Nikulin
  2022-05-08 16:15 ` Timothy
  3 siblings, 1 reply; 12+ messages in thread
From: Bastien @ 2021-09-25 16:04 UTC (permalink / raw)
  To: Charest, Luc; +Cc: emacs-orgmode@gnu.org

(I'm marking this as a confirmed bug, even if a first patch as been applied.)


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

* Re: [PATCH] ox-latex: Allow percent sign in 'src-block' caption
  2021-09-07 12:21   ` [PATCH] ox-latex: Allow percent sign in 'src-block' caption Maxim Nikulin
  2021-09-07 13:17     ` Timothy
@ 2021-09-25 16:04     ` Bastien
  1 sibling, 0 replies; 12+ messages in thread
From: Bastien @ 2021-09-25 16:04 UTC (permalink / raw)
  To: Maxim Nikulin; +Cc: emacs-orgmode

Hi Maxim,

Maxim Nikulin <manikulin@gmail.com> writes:

> On 03/09/2021 19:17, Maxim Nikulin wrote:
>> On 30/07/2021 22:00, Charest, Luc wrote:
>>>
>>> org-latex-src-block: Not enough arguments for format string
>> 
>> Confirmed
>> 
>> User input is combined with format string in `org-latex-src-block'
>> so percent character in caption is treated as a format specifier.>
>> Maybe emacs versions newer than 26 does not throw an error
>> on extra specifier with missed argument. =:latex-cation-above nil=
>> might still cause corrupted LaTeX code.
>
> I have decided that it is better to provide a partial fix than to leave
> the problem as is.  This patch covers only plain verbatim environment,
> more advanced formatting using listings or minted should be addressed
> by another change (Anybody?).  

Applied, thanks.

> Do not close the bug after applying this patch.

I've opened a bug from the original report, I'll check it does appear
in updates.orgmode.org correctly.


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

* Re: Bug: Percentage in caption (even escaped) does not work in LaTeX export
  2021-09-25 16:04 ` Bug: Percentage in caption (even escaped) does not work in LaTeX export Bastien
@ 2021-09-30 14:20   ` Max Nikulin
  0 siblings, 0 replies; 12+ messages in thread
From: Max Nikulin @ 2021-09-30 14:20 UTC (permalink / raw)
  To: emacs-orgmode

On 25/09/2021 23:04, Bastien wrote:
> (I'm marking this as a confirmed bug, even if a first patch as been applied.)

Since it was there earlier, it is tracked twice now:
- User input is combined with format string in org-latex-src-block
- Bug: Percentage in caption does not work in LaTeX export

Let's assume that one entry is to fix "minted" and another one for 
"listings" environment. Though one more obscure issue remains: 
"multicolumn" option does do what can be expected from its name.



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

* Re: Bug: Percentage in caption (even escaped) does not work in LaTeX export
  2021-07-30 15:00 Bug: Percentage in caption (even escaped) does not work in LaTeX export Charest, Luc
                   ` (2 preceding siblings ...)
  2021-09-25 16:04 ` Bug: Percentage in caption (even escaped) does not work in LaTeX export Bastien
@ 2022-05-08 16:15 ` Timothy
  2022-10-06  6:57   ` Ihor Radchenko
  3 siblings, 1 reply; 12+ messages in thread
From: Timothy @ 2022-05-08 16:15 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 143 bytes --]

Hi All,

This should be finally fixed in
<https://list.orgmode.org/87levcm2bx.fsf@gmail.com> when it’s merged.

All the best,
Timothy

[-- Attachment #1.2: Type: text/html, Size: 10801 bytes --]

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

* Re: Bug: Percentage in caption (even escaped) does not work in LaTeX export
  2022-05-08 16:15 ` Timothy
@ 2022-10-06  6:57   ` Ihor Radchenko
  0 siblings, 0 replies; 12+ messages in thread
From: Ihor Radchenko @ 2022-10-06  6:57 UTC (permalink / raw)
  To: Timothy; +Cc: emacs-orgmode

Timothy <tecosaur@gmail.com> writes:

> This should be finally fixed in
> <https://list.orgmode.org/87levcm2bx.fsf@gmail.com> when it’s merged.

The referenced patch has been applied, and the bug should be fully
resolved on main now.
Fixed.

-- 
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] 12+ messages in thread

end of thread, other threads:[~2022-10-06  7:08 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-30 15:00 Bug: Percentage in caption (even escaped) does not work in LaTeX export Charest, Luc
2021-08-03 13:51 ` Eric S Fraga
2021-08-31 15:38   ` Timothy
2021-08-31 16:43     ` Charest, Luc
2021-09-03 12:17 ` Maxim Nikulin
2021-09-07 12:21   ` [PATCH] ox-latex: Allow percent sign in 'src-block' caption Maxim Nikulin
2021-09-07 13:17     ` Timothy
2021-09-25 16:04     ` Bastien
2021-09-25 16:04 ` Bug: Percentage in caption (even escaped) does not work in LaTeX export Bastien
2021-09-30 14:20   ` Max Nikulin
2022-05-08 16:15 ` Timothy
2022-10-06  6:57   ` 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).