[-- Attachment #1: Type: text/plain, Size: 1320 bytes --] This patch fixes an issue in which captions for custom listing environments are not converted correctly. To illustrate the issue, take the following MWE. file.org: ``` #+caption: Perform inter-sample interpolation. #+begin_src python import numpy as np #+end_src ``` file.el: ``` ;; Loading straight isn't necessary if you don't use it, just need to ;; load org. (load "~/.config/emacs/straight/repos/straight.el/bootstrap.el") (straight-use-package 'org) (find-file "file.org") (setq org-latex-listings 'minted) (setq org-latex-custom-lang-environments '((python "\\begin{listing} \\begin{minted}[%o]{python} %s\\end{minted} \\caption{%c} \\label{%l} \\end{listing}"))) (org-latex-export-to-latex) ``` Run the example with: emacs -Q --batch -l file.el Before the patch, you get: ``` [...] \begin{listing} \begin{minted}[]{python} import numpy as np \end{minted} \caption{(((Perform inter-sample interpolation.)))} \label{nil} \end{listing} [...] ``` After the patch, you get: ``` [...] \begin{listing} \begin{minted}[]{python} import numpy as np \end{minted} \caption{Perform inter-sample interpolation.} \label{nil} \end{listing} [...] ``` I'm not actually 100% confident that the patch is the optimal way to do this. The API wasn't totally clear to me in this case. Any advice here is appreciated. Matt [-- Attachment #2: 0001-ox-latex.el-Fix-caption-format-for-custom-latex-src-.patch --] [-- Type: text/x-patch, Size: 1093 bytes --] From 5b0601d6d3b8034202c4b9b820c64a719e3129b9 Mon Sep 17 00:00:00 2001 From: Matt Huszagh <huszaghmatt@gmail.com> Date: Wed, 29 Dec 2021 19:35:42 -0800 Subject: [PATCH] ox-latex.el: Fix caption format for custom latex src block 2021-12-29 Matt Huszagh <huszaghmatt@gmail.com> * lisp/ox-latex.el (org-latex-src-block): Use `org-export-get-caption' to extract caption from element. Otherwise, the full caption contains a large number of unnecessary parentheses. --- lisp/ox-latex.el | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el index 8187119ec..a9c6a4b5c 100644 --- a/lisp/ox-latex.el +++ b/lisp/ox-latex.el @@ -3021,7 +3021,8 @@ contextual information." custom-env) (format-spec custom-env `((?s . ,formatted-src) - (?c . ,caption) + (?c . ,(org-export-data (org-export-get-caption src-block) + info)) (?f . ,float) (?l . ,(org-latex--label src-block info)) (?o . ,(or (plist-get attributes :options) ""))))))) -- 2.31.1
Matt Huszagh <huszaghmatt@gmail.com> writes: > This patch fixes an issue in which captions for custom listing > environments are not converted correctly. To illustrate the issue, > take the following MWE. Thanks! > - (?c . ,caption) > + (?c . ,(org-export-data (org-export-get-caption src-block) > + info)) I think that it will be better if you use org-latex--caption/label-string instead. It will take care about short captions as well. Best, Ihor
Ihor Radchenko <yantar92@gmail.com> writes:
> I think that it will be better if you use
> org-latex--caption/label-string instead. It will take care about short
> captions as well.
Changing this to (?c . ,caption-str) yields for the original example
\caption{\caption{Perform inter-sample interpolation.}
}
I could use substring to remove the leading \caption{ and trailing
}. What do you think?
Matt
Matt Huszagh <huszaghmatt@gmail.com> writes:
> Ihor Radchenko <yantar92@gmail.com> writes:
>
>> I think that it will be better if you use
>> org-latex--caption/label-string instead. It will take care about short
>> captions as well.
>
> Changing this to (?c . ,caption-str) yields for the original example
>
> \caption{\caption{Perform inter-sample interpolation.}
> }
>
> I could use substring to remove the leading \caption{ and trailing
> }. What do you think?
Removing caption does not sound reliable. Also, you need to
update the patch according to recent commit by Timothy.
Timothy, looking at the new implementation of org-latex-src-block, I do
not see custom-env being passed as an argument of
org-latex-src-block--custom. This is likely a bug.
Best,
Ihor
[-- Attachment #1: Type: text/plain, Size: 310 bytes --] Hi Ihor, > Timothy, looking at the new implementation of org-latex-src-block, I do > not see custom-env being passed as an argument of > org-latex-src-block–custom. This is likely a bug. Ah yes, this was an oversight. Corrected in 30953bd7b701c870152cd60f52f2d484970caeb9. All the best, Timothy
[-- Attachment #1: Type: text/plain, Size: 1123 bytes --] Hi Matt, As mentioned by Ihor, you’ll want to take a look at the curent version of ox-latex, specifically `org-latex-src-block--custom' which now contains `(?c . ,caption)'. You may want to consider modifying `org-latex--caption/label-string' to accept the form `(element info &optional content-only)' or reimplementing just the bits needed for this purpose — which ever works out nicer. Looking at `org-latex--caption/label-string' a substring approach looks quite fragile, though you might be able to get away with an application of `replace-regexp-in-string', though I’d personally consider this a bit of a last resort. Matt Huszagh <huszaghmatt@gmail.com> writes: > Ihor Radchenko <yantar92@gmail.com> writes: > >> I think that it will be better if you use >> org-latex–caption/label-string instead. It will take care about short >> captions as well. > > Changing this to (?c . ,caption-str) yields for the original example > > {Perform inter-sample interpolation. > } > > I could use substring to remove the leading { and trailing > }. What do you think? All the best, Timothy
Timothy <tecosaur@gmail.com> writes:
> As mentioned by Ihor, you’ll want to take a look at the curent version of
> ox-latex, specifically `org-latex-src-block--custom' which now contains `(?c .
> ,caption)'. You may want to consider modifying `org-latex--caption/label-string' to
> accept the form `(element info &optional content-only)' or reimplementing just the
> bits needed for this purpose — which ever works out nicer. Looking at
> `org-latex--caption/label-string' a substring approach looks quite fragile, though
> you might be able to get away with an application of `replace-regexp-in-string',
> though I’d personally consider this a bit of a last resort.
Thanks for the directions Timothy.
I failed to reproduce the old issue with the latest commit to main, so
it looks like this patch is no longer needed. Nice! I'll post back if I
notice any issues in the future, but otherwise I'd say we can consider
this closed.
Matt
Matt Huszagh <huszaghmatt@gmail.com> writes:
> This patch fixes an issue in which captions for custom listing
> environments are not converted correctly. To illustrate the issue,
> take the following MWE.
Since the original issue cannot be reproduced (see the other email in
the thread) I am closing this.
Canceled.
Best,
Ihor