* interaction between org-latex-custom-lang-environments and name when exporting to latex @ 2015-11-16 12:59 Alan Schmitt 2015-11-20 7:55 ` Alan Schmitt 0 siblings, 1 reply; 15+ messages in thread From: Alan Schmitt @ 2015-11-16 12:59 UTC (permalink / raw) To: emacs-orgmode [-- Attachment #1: Type: text/plain, Size: 1385 bytes --] Hello, I want to export some code for a custom language environment (to be able to use minted's global customization), and I find there is a strange interaction with adding a name to the block. Here is an ECM. Init file: --8<---------------cut here---------------start------------->8--- (defvar emacsd-dir "~/.emacs.d/") (add-to-list 'load-path (concat emacsd-dir "org/emacs/site-lisp/org")) (require 'org) (require 'ox-latex) (setq org-latex-listings 'minted) (setq org-latex-custom-lang-environments '((emacs-lisp "common-lisp-code"))) --8<---------------cut here---------------end--------------->8--- Org file: --8<---------------cut here---------------start------------->8--- #+name: test #+BEGIN_SRC emacs-lisp (message "hello") #+END_SRC --8<---------------cut here---------------end--------------->8--- Exported latex (only for the source block): --8<---------------cut here---------------start------------->8--- \begin{common-lisp-code} (message "hello") \label{orgsrcblock1} \end{common-lisp-code} --8<---------------cut here---------------end--------------->8--- The problem is the “\label” in the middle of the source block, which plays badly with minted. Is this a known bug? Thanks, Alan -- OpenPGP Key ID : 040D0A3B4ED2E5C7 Athmospheric CO₂ (Updated November 9, 2015, Mauna Loa Obs.): 399.06 ppm [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 472 bytes --] ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: interaction between org-latex-custom-lang-environments and name when exporting to latex 2015-11-16 12:59 interaction between org-latex-custom-lang-environments and name when exporting to latex Alan Schmitt @ 2015-11-20 7:55 ` Alan Schmitt 2015-11-20 10:41 ` Rasmus 0 siblings, 1 reply; 15+ messages in thread From: Alan Schmitt @ 2015-11-20 7:55 UTC (permalink / raw) To: emacs-orgmode [-- Attachment #1: Type: text/plain, Size: 1257 bytes --] Hello, I was wondering if someone had been able to reproduce this, and if so should I file it as a bug? Thanks, Alan On 2015-11-16 13:59, Alan Schmitt <alan.schmitt@polytechnique.org> writes: > Hello, > > I want to export some code for a custom language environment (to be able > to use minted's global customization), and I find there is a strange > interaction with adding a name to the block. Here is an ECM. > > Init file: > (defvar emacsd-dir "~/.emacs.d/") > (add-to-list 'load-path (concat emacsd-dir "org/emacs/site-lisp/org")) > (require 'org) > > (require 'ox-latex) > > (setq org-latex-listings 'minted) > > (setq org-latex-custom-lang-environments > '((emacs-lisp "common-lisp-code"))) > > Org file: > #+name: test > #+BEGIN_SRC emacs-lisp > (message "hello") > #+END_SRC > > Exported latex (only for the source block): > \begin{common-lisp-code} > (message "hello") > \label{orgsrcblock1} > > \end{common-lisp-code} > > The problem is the “\label” in the middle of the source block, which > plays badly with minted. > > Is this a known bug? > > Thanks, > > Alan -- OpenPGP Key ID : 040D0A3B4ED2E5C7 Athmospheric CO₂ (Updated November 9, 2015, Mauna Loa Obs.): 399.06 ppm [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 472 bytes --] ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: interaction between org-latex-custom-lang-environments and name when exporting to latex 2015-11-20 7:55 ` Alan Schmitt @ 2015-11-20 10:41 ` Rasmus 2015-11-20 11:06 ` Alan Schmitt 2015-11-23 11:13 ` Alan Schmitt 0 siblings, 2 replies; 15+ messages in thread From: Rasmus @ 2015-11-20 10:41 UTC (permalink / raw) To: emacs-orgmode Alan Schmitt <alan.schmitt@polytechnique.org> writes: > I was wondering if someone had been able to reproduce this, and if so > should I file it as a bug? Sure. It's how org-latex-src-block handles custom environments (see "Case 2."). At the moment this is hardcoded. Perhaps the second element of org-latex-custom-lang-environments should allow a format string, so that an element could be something like, (emacs-lisp "\begin{common-lisp-code}[myoptions, label=%l] %s \end{common-lisp-code}") (modulus escaping). WDYT? Rasmus -- If you can mix business and politics wonderful things can happen! ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: interaction between org-latex-custom-lang-environments and name when exporting to latex 2015-11-20 10:41 ` Rasmus @ 2015-11-20 11:06 ` Alan Schmitt 2015-11-23 11:13 ` Alan Schmitt 1 sibling, 0 replies; 15+ messages in thread From: Alan Schmitt @ 2015-11-20 11:06 UTC (permalink / raw) To: Rasmus; +Cc: emacs-orgmode [-- Attachment #1: Type: text/plain, Size: 864 bytes --] On 2015-11-20 11:41, Rasmus <rasmus@gmx.us> writes: > Alan Schmitt <alan.schmitt@polytechnique.org> writes: > >> I was wondering if someone had been able to reproduce this, and if so >> should I file it as a bug? > > Sure. It's how org-latex-src-block handles custom environments (see "Case > 2."). At the moment this is hardcoded. > > Perhaps the second element of org-latex-custom-lang-environments should > allow a format string, so that an element could be something like, > > (emacs-lisp "\begin{common-lisp-code}[myoptions, label=%l] > %s > \end{common-lisp-code}") > > (modulus escaping). > > WDYT? That would be great indeed. I see the code is not too hairy there, so I'll try to write a patch. Best, Alan -- OpenPGP Key ID : 040D0A3B4ED2E5C7 Athmospheric CO₂ (Updated November 9, 2015, Mauna Loa Obs.): 399.06 ppm [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 472 bytes --] ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: interaction between org-latex-custom-lang-environments and name when exporting to latex 2015-11-20 10:41 ` Rasmus 2015-11-20 11:06 ` Alan Schmitt @ 2015-11-23 11:13 ` Alan Schmitt 2015-11-23 17:54 ` Rasmus 1 sibling, 1 reply; 15+ messages in thread From: Alan Schmitt @ 2015-11-23 11:13 UTC (permalink / raw) To: Rasmus; +Cc: emacs-orgmode [-- Attachment #1: Type: text/plain, Size: 1948 bytes --] Hello, On 2015-11-20 11:41, Rasmus <rasmus@gmx.us> writes: > Alan Schmitt <alan.schmitt@polytechnique.org> writes: > >> I was wondering if someone had been able to reproduce this, and if so >> should I file it as a bug? > > Sure. It's how org-latex-src-block handles custom environments (see "Case > 2."). At the moment this is hardcoded. I've started to look into this, and I would like some advice regarding the design of the solution. What I propose is to have either a string or a function in `org-latex-custom-lang-environments'. A string would be used as it is currently, whereas a function would be applied. Here is what it could look like: #+begin_src emacs-lisp ;; Case 2. Custom environment. (custom-env (let ((caption-str (org-latex--caption/label-string src-block info)) (formatted-src (org-export-format-code-default src-block info))) (cond ((stringp custom-env) (format "\\begin{%s}\n%s\\end{%s}\n" custom-env (concat (and caption-above-p caption-str) formatted-src (and (not caption-above-p) caption-str)) custom-env)) ((functionp custom-env) (funcall custom-env formatted-src (org-latex--label src-block info))) (t (user-error "`org-latex-custom-lang-environments' should map %s to either a string or a function" lang))))) #+end_src My main design question is what the arguments to the function should be. I thought the formatted source code and label would work, but I'm wondering if it may be better to simply give the `src-block' and `info' arguments directly. WDYT? Alan -- OpenPGP Key ID : 040D0A3B4ED2E5C7 Athmospheric CO₂ (Updated November 22, 2015, Mauna Loa Obs.): 400.35 ppm [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 472 bytes --] ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: interaction between org-latex-custom-lang-environments and name when exporting to latex 2015-11-23 11:13 ` Alan Schmitt @ 2015-11-23 17:54 ` Rasmus 2015-11-24 8:07 ` Alan Schmitt 0 siblings, 1 reply; 15+ messages in thread From: Rasmus @ 2015-11-23 17:54 UTC (permalink / raw) To: emacs-orgmode Hi Alan, Alan Schmitt <alan.schmitt@polytechnique.org> writes: > What I propose is to have either a string or a function in > `org-latex-custom-lang-environments'. A string would be used as it is > currently, whereas a function would be applied. Here is what it could > look like: I think a format-string is fine, though I don’t oppose allowing a function as well. I don’t foresee a great necessity of the latter, though. I'd use format-spec. E.g. here’s how hyperref is filled (let ((template (plist-get info :latex-hyperref-template))) (and (stringp template) (format-spec template (org-latex--format-spec info)))) Then the cdr of an element in org-latex-custom-lang-environments can be something like: \begin{%e}[label=%l, myfunkyoption=val] %c \end{%e} The only downside is that you’d have to somehow discriminate between the "old" case where the cdr is a single word denoting an environment. Something like this might work: (string-match-p "\\`\\w+\\'" "hest1") > My main design question is what the arguments to the function should be. > I thought the formatted source code and label would work, but I'm > wondering if it may be better to simply give the `src-block' and `info' > arguments directly. Well, functions would be user written. In general I’d favor info and src-block, but I don’t think it’s very user-friendly, as it requires quite some knowledge about how ox works. Also, when using such functions, the user would have to write a lot of boilerplate to get e.g . the label. Rasmus -- Not everything that goes around comes back around, you know ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: interaction between org-latex-custom-lang-environments and name when exporting to latex 2015-11-23 17:54 ` Rasmus @ 2015-11-24 8:07 ` Alan Schmitt 2015-11-24 13:14 ` Rasmus 0 siblings, 1 reply; 15+ messages in thread From: Alan Schmitt @ 2015-11-24 8:07 UTC (permalink / raw) To: Rasmus; +Cc: emacs-orgmode [-- Attachment #1: Type: text/plain, Size: 2337 bytes --] Hi Rasmus, On 2015-11-23 18:54, Rasmus <rasmus@gmx.us> writes: > Hi Alan, > > Alan Schmitt <alan.schmitt@polytechnique.org> writes: > >> What I propose is to have either a string or a function in >> `org-latex-custom-lang-environments'. A string would be used as it is >> currently, whereas a function would be applied. Here is what it could >> look like: > > I think a format-string is fine, though I don’t oppose allowing a function > as well. I don’t foresee a great necessity of the latter, though. > > I'd use format-spec. E.g. here’s how hyperref is filled > > (let ((template (plist-get info :latex-hyperref-template))) > (and (stringp template) > (format-spec template (org-latex--format-spec info)))) I did not know about `format-spec', it's really nice! > Then the cdr of an element in org-latex-custom-lang-environments can be > something like: > > \begin{%e}[label=%l, myfunkyoption=val] > %c > \end{%e} What would "%e" be bound to? Since this is a customization for a given source block, it should be fixed, so I think we only need "%l" and "%c". > The only downside is that you’d have to somehow discriminate between the > "old" case where the cdr is a single word denoting an environment. > Something like this might work: > > (string-match-p "\\`\\w+\\'" "hest1") This was the main reason why I did not want to go that route. This said, after understanding this regexp, I see how I can explain how the two cases are distinguished in the docstring. >> My main design question is what the arguments to the function should be. >> I thought the formatted source code and label would work, but I'm >> wondering if it may be better to simply give the `src-block' and `info' >> arguments directly. > > Well, functions would be user written. In general I’d favor info and > src-block, but I don’t think it’s very user-friendly, as it requires quite > some knowledge about how ox works. Also, when using such functions, the > user would have to write a lot of boilerplate to get e.g . the label. Yes. I'll give the template string a try and will report back. Thanks, Alan -- OpenPGP Key ID : 040D0A3B4ED2E5C7 Athmospheric CO₂ (Updated November 22, 2015, Mauna Loa Obs.): 400.35 ppm [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 472 bytes --] ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: interaction between org-latex-custom-lang-environments and name when exporting to latex 2015-11-24 8:07 ` Alan Schmitt @ 2015-11-24 13:14 ` Rasmus 2015-11-25 7:59 ` Alan Schmitt 0 siblings, 1 reply; 15+ messages in thread From: Rasmus @ 2015-11-24 13:14 UTC (permalink / raw) To: alan.schmitt; +Cc: emacs-orgmode Hi, Alan Schmitt <alan.schmitt@polytechnique.org> writes: >> Then the cdr of an element in org-latex-custom-lang-environments can be >> something like: >> >> \begin{%e}[label=%l, myfunkyoption=val] >> %c >> \end{%e} > > What would "%e" be bound to? Since this is a customization for a given > source block, it should be fixed, so I think we only need "%l" and "%c". You are right on the %e of course. You should check the things we bind in the top let-binding. The useful ones should be accessible. A quick guess (you’ll have to decide): - lang - caption - label - float - perhaps more from attributes? >> The only downside is that you’d have to somehow discriminate between the >> "old" case where the cdr is a single word denoting an environment. >> Something like this might work: >> >> (string-match-p "\\`\\w+\\'" "hest1") > > This was the main reason why I did not want to go that route. This said, > after understanding this regexp, I see how I can explain how the two > cases are distinguished in the docstring. Perhaps it’s safer to use [[:alnum:]] or even [a-zA-Z0-9]. \w follows the syntax table which might go weird, though I’m not sure how. >> Well, functions would be user written. In general I’d favor info and >> src-block, but I don’t think it’s very user-friendly, as it requires quite >> some knowledge about how ox works. Also, when using such functions, the >> user would have to write a lot of boilerplate to get e.g . the label. > > Yes. I'll give the template string a try and will report back. Thanks Alan! Rasmus -- Warning: Everything saved will be lost ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: interaction between org-latex-custom-lang-environments and name when exporting to latex 2015-11-24 13:14 ` Rasmus @ 2015-11-25 7:59 ` Alan Schmitt 2015-11-25 9:57 ` Rasmus 0 siblings, 1 reply; 15+ messages in thread From: Alan Schmitt @ 2015-11-25 7:59 UTC (permalink / raw) To: Rasmus; +Cc: emacs-orgmode [-- Attachment #1.1: Type: text/plain, Size: 93 bytes --] Hello, Here is a patch that allows the use of a format string in custom-lang-environments. [-- Warning: decoded text below may be mangled, UTF-8 assumed --] [-- Attachment #1.2: 0001-ox-latex-Extend-custom-lang-environments.patch --] [-- Type: text/x-patch, Size: 3383 bytes --] From e4731375eaf622df0187d47d949fb9f4e53330be Mon Sep 17 00:00:00 2001 From: Alan Schmitt <alan.schmitt@polytechnique.org> Date: Wed, 25 Nov 2015 08:48:58 +0100 Subject: [PATCH] ox-latex: Extend custom-lang-environments * lisp/ox-latex.el (org-latex-custom-lang-environments): extend the documentation string. * lisp/ox-latex.el (org-latex-src-block): allow a custom language environment to be a format string to be directly inserted. --- lisp/ox-latex.el | 50 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el index bbf7f41..1b0bff5 100644 --- a/lisp/ox-latex.el +++ b/lisp/ox-latex.el @@ -1025,17 +1025,40 @@ block-specific options, you may use the following syntax: "Alist mapping languages to language-specific LaTeX environments. It is used during export of src blocks by the listings and minted -latex packages. For example, +latex packages. The environment may either be a simple string, +composed of only letters and numbers. In this case, the string is +directly the name of the latex environment to use. The environment +may also be a format string. In this case the format string will be +directly exported. This format string may contain these elements: + + %c for the formatted source + %C for the caption + %f for the float attribute + %l for an appropriate label + +For example, (setq org-latex-custom-lang-environments - '((python \"pythoncode\"))) + '((python \"pythoncode\") + (ocaml \"\\\\begin{listing}\\n\\\\begin{minted}{ocaml}%c\\\\end{minted}\\n\\\\caption{%C}\\n\\\\label{%l}\"))) would have the effect that if org encounters begin_src python during latex export it will output \\begin{pythoncode} <src block body> - \\end{pythoncode}") + \\end{pythoncode} + +and if org encounters begin_src ocaml during latex export it will +output + + \\begin{listing} + \\begin{minted}{ocaml} + <src block body> + \\end{minted} + \\caption{<caption>} + \\label{<label>} + \\end{listing}") ;;;; Compilation @@ -2756,13 +2779,20 @@ contextual information." (org-export-format-code-default src-block info)))))) ;; Case 2. Custom environment. (custom-env - (let ((caption-str (org-latex--caption/label-string src-block info))) - (format "\\begin{%s}\n%s\\end{%s}\n" - custom-env - (concat (and caption-above-p caption-str) - (org-export-format-code-default src-block info) - (and (not caption-above-p) caption-str)) - custom-env))) + (let ((caption-str (org-latex--caption/label-string src-block info)) + (formatted-src (org-export-format-code-default src-block info))) + (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env) + (format "\\begin{%s}\n%s\\end{%s}\n" + custom-env + (concat (and caption-above-p caption-str) + formatted-src + (and (not caption-above-p) caption-str)) + custom-env) + (format-spec custom-env + `((?c . ,formatted-src) + (?C . ,caption) + (?f . ,float) + (?l . ,(org-latex--label src-block info))))))) ;; Case 3. Use minted package. ((eq listings 'minted) (let* ((caption-str (org-latex--caption/label-string src-block info)) -- 2.6.3 [-- Attachment #1.3: Type: text/plain, Size: 135 bytes --] Best, Alan -- OpenPGP Key ID : 040D0A3B4ED2E5C7 Athmospheric CO₂ (Updated November 22, 2015, Mauna Loa Obs.): 400.35 ppm [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 472 bytes --] ^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: interaction between org-latex-custom-lang-environments and name when exporting to latex 2015-11-25 7:59 ` Alan Schmitt @ 2015-11-25 9:57 ` Rasmus 2015-11-27 16:27 ` Alan Schmitt 0 siblings, 1 reply; 15+ messages in thread From: Rasmus @ 2015-11-25 9:57 UTC (permalink / raw) To: alan.schmitt; +Cc: emacs-orgmode Hi Alan, Alan Schmitt <alan.schmitt@polytechnique.org> writes: > Here is a patch that allows the use of a format string in > custom-lang-environments. Thanks. > * lisp/ox-latex.el (org-latex-custom-lang-environments): extend the > documentation string. Extend. You don’t need the lisp prefix. > * lisp/ox-latex.el (org-latex-src-block): allow a custom language > environment to be a format string to be directly inserted. Allow. > + %c for the formatted source > + %C for the caption > + %f for the float attribute > + %l for an appropriate label it’s a bit sadistic to use %c and %C. Can’t the first be (%s)ource-code? > +For example, > > (setq org-latex-custom-lang-environments > - '((python \"pythoncode\"))) > + '((python \"pythoncode\") > + (ocaml \"\\\\begin{listing}\\n\\\\begin{minted}{ocaml}%c\\\\end{minted}\\n\\\\caption{%C}\\n\\\\label{%l}\"))) Minor: For the sake of making the ocaml block readable you could add the actual newlines instead of "\n". > would have the effect that if org encounters begin_src python A Python source block. > during latex export it will output LaTeX export it will produce, > \\begin{pythoncode} > <src block body> > - \\end{pythoncode}") > + \\end{pythoncode} > + > +and if org encounters begin_src ocaml during latex export it will Likewise, if Org ... a Ocaml source block ... LaTeX > +output , > + \\begin{listing} > + \\begin{minted}{ocaml} > + <src block body> > + \\end{minted} > + \\caption{<caption>} > + \\label{<label>} > + \\end{listing}") Nitpick: label should be inside caption in the example to be consistent with how captions are normally formatted by ox-latex. Either way is fine though. Also, you need to denote that the defcustom was changed. :version "25.1" :package-version '(Org . "9.0") When you are at it, please add the missing tags, :group and :type. > + (let ((caption-str (org-latex--caption/label-string src-block info)) > + (formatted-src (org-export-format-code-default src-block info))) > + (if (string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env) I guess technically, you should use org-string-match-p for comparability reasons. Though I think Org-9 will drop older emacsen and xemacs support. > + (Format "\\begin{%s}\n%s\\end{%s}\n" > + custom-env > + (concat (and caption-above-p caption-str) > + formatted-src > + (and (not caption-above-p) caption-str)) > + custom-env) > + (format-spec custom-env > + `((?c . ,formatted-src) I’d use s (or anything but c) here.... > + (?C . ,caption) I’d use lowercase c. > + (?f . ,float) > + (?l . ,(org-latex--label src-block info))))))) > ;; Case 3. Use minted package. > ((eq listings 'minted) > (let* ((caption-str (org-latex--caption/label-string src-block info)) Rasmus -- Look, it is what it is because of what it was ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: interaction between org-latex-custom-lang-environments and name when exporting to latex 2015-11-25 9:57 ` Rasmus @ 2015-11-27 16:27 ` Alan Schmitt 2015-11-27 21:05 ` Rasmus 0 siblings, 1 reply; 15+ messages in thread From: Alan Schmitt @ 2015-11-27 16:27 UTC (permalink / raw) To: Rasmus; +Cc: emacs-orgmode [-- Attachment #1.1: Type: text/plain, Size: 890 bytes --] Hi Rasmus, Thanks a lot for the great comments. Here is an updated patch, with some comments on your comments for the thing I did not change. On 2015-11-25 10:57, Rasmus <rasmus@gmx.us> writes: >> + \\begin{listing} >> + \\begin{minted}{ocaml} >> + <src block body> >> + \\end{minted} >> + \\caption{<caption>} >> + \\label{<label>} >> + \\end{listing}") > > Nitpick: label should be inside caption in the example to be consistent > with how captions are normally formatted by ox-latex. Either way is fine > though. I chose this example because it's the code in the minted manual. > Also, you need to denote that the defcustom was changed. > > :version "25.1" > :package-version '(Org . "9.0") > > When you are at it, please add the missing tags, :group and :type. It's not a defcustom but a defvar. (But maybe it should be a defcustom …) [-- Warning: decoded text below may be mangled, UTF-8 assumed --] [-- Attachment #1.2: 0001-ox-latex-Extend-custom-lang-environments.patch --] [-- Type: text/x-patch, Size: 3500 bytes --] From 527635c2e674fe1f3ab9d5d3de96e238d0debf7b Mon Sep 17 00:00:00 2001 From: Alan Schmitt <alan.schmitt@polytechnique.org> Date: Wed, 25 Nov 2015 08:48:58 +0100 Subject: [PATCH] ox-latex: Extend custom-lang-environments * ox-latex.el (org-latex-custom-lang-environments): Extend the documentation string. * ox-latex.el (org-latex-src-block): Allow a custom language environment to be a format string to be directly inserted. --- lisp/ox-latex.el | 58 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el index bbf7f41..0a9f7d4 100644 --- a/lisp/ox-latex.el +++ b/lisp/ox-latex.el @@ -1025,17 +1025,44 @@ block-specific options, you may use the following syntax: "Alist mapping languages to language-specific LaTeX environments. It is used during export of src blocks by the listings and minted -latex packages. For example, +latex packages. The environment may either be a simple string, +composed of only letters and numbers. In this case, the string is +directly the name of the latex environment to use. The environment +may also be a format string. In this case the format string will be +directly exported. This format string may contain these elements: + + %s for the formatted source + %c for the caption + %f for the float attribute + %l for an appropriate label + +For example, (setq org-latex-custom-lang-environments - '((python \"pythoncode\"))) + '((python \"pythoncode\") + (ocaml \"\\\\begin{listing} +\\\\begin{minted}{ocaml} +%s\\\\end{minted} +\\\\caption{%c} +\\\\label{%l}\"))) -would have the effect that if org encounters begin_src python -during latex export it will output +would have the effect that if org encounters a Python source block +during LaTeX export it will produce \\begin{pythoncode} <src block body> - \\end{pythoncode}") + \\end{pythoncode} + +and if org encounters an Ocaml source block during LaTeX export it +will produce + + \\begin{listing} + \\begin{minted}{ocaml} + <src block body> + \\end{minted} + \\caption{<caption>} + \\label{<label>} + \\end{listing}") ;;;; Compilation @@ -2756,13 +2783,20 @@ contextual information." (org-export-format-code-default src-block info)))))) ;; Case 2. Custom environment. (custom-env - (let ((caption-str (org-latex--caption/label-string src-block info))) - (format "\\begin{%s}\n%s\\end{%s}\n" - custom-env - (concat (and caption-above-p caption-str) - (org-export-format-code-default src-block info) - (and (not caption-above-p) caption-str)) - custom-env))) + (let ((caption-str (org-latex--caption/label-string src-block info)) + (formatted-src (org-export-format-code-default src-block info))) + (if (org-string-match-p "\\`[a-zA-Z0-9]+\\'" custom-env) + (format "\\begin{%s}\n%s\\end{%s}\n" + custom-env + (concat (and caption-above-p caption-str) + formatted-src + (and (not caption-above-p) caption-str)) + custom-env) + (format-spec custom-env + `((?s . ,formatted-src) + (?c . ,caption) + (?f . ,float) + (?l . ,(org-latex--label src-block info))))))) ;; Case 3. Use minted package. ((eq listings 'minted) (let* ((caption-str (org-latex--caption/label-string src-block info)) -- 2.6.3 [-- Attachment #1.3: Type: text/plain, Size: 135 bytes --] Best, Alan -- OpenPGP Key ID : 040D0A3B4ED2E5C7 Athmospheric CO₂ (Updated November 22, 2015, Mauna Loa Obs.): 400.35 ppm [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 472 bytes --] ^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: interaction between org-latex-custom-lang-environments and name when exporting to latex 2015-11-27 16:27 ` Alan Schmitt @ 2015-11-27 21:05 ` Rasmus 2015-11-30 12:12 ` Alan Schmitt 0 siblings, 1 reply; 15+ messages in thread From: Rasmus @ 2015-11-27 21:05 UTC (permalink / raw) To: emacs-orgmode Hi Alan, Alan Schmitt <alan.schmitt@polytechnique.org> writes: >> Nitpick: label should be inside caption in the example to be consistent >> with how captions are normally formatted by ox-latex. Either way is fine >> though. > > I chose this example because it's the code in the minted manual. As you prefer. >> Also, you need to denote that the defcustom was changed. >> >> :version "25.1" >> :package-version '(Org . "9.0") >> >> When you are at it, please add the missing tags, :group and :type. > > It's not a defcustom but a defvar. (But maybe it should be a defcustom …) It should be a defcustom. Feel free to fix it, preferably in a separate patch. 11.5 Defining Global Variables A [defvar/defconst] definition is a construct that announces your intention to use a symbol as a global variable. ... To define a customizable variable, you should use defcustom (which calls defvar as a subroutine). See Variable Definitions. > From 527635c2e674fe1f3ab9d5d3de96e238d0debf7b Mon Sep 17 00:00:00 2001 > From: Alan Schmitt <alan.schmitt@polytechnique.org> > Date: Wed, 25 Nov 2015 08:48:58 +0100 > Subject: [PATCH] ox-latex: Extend custom-lang-environments > > * ox-latex.el (org-latex-custom-lang-environments): Extend the > documentation string. > * ox-latex.el (org-latex-src-block): Allow a custom language environment > to be a format string to be directly inserted. > --- > lisp/ox-latex.el | 58 ++++++++++++++++++++++++++++++++++++++++++++------------ > 1 file changed, 46 insertions(+), 12 deletions(-) > > diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el > index bbf7f41..0a9f7d4 100644 > --- a/lisp/ox-latex.el > +++ b/lisp/ox-latex.el > @@ -1025,17 +1025,44 @@ block-specific options, you may use the following syntax: > "Alist mapping languages to language-specific LaTeX environments. > > It is used during export of src blocks by the listings and minted > -latex packages. For example, > +latex packages. The environment may either be a simple string, You don’t need the ’either’ IMO, but as you please. > +composed of only letters and numbers. In this case, the string is > +directly the name of the latex environment to use. The environment > +may also be a format string. In this case the format string will be > +directly exported. This format string may contain these elements: > + > + %s for the formatted source > + %c for the caption > + %f for the float attribute > + %l for an appropriate label > + > +For example, > > (setq org-latex-custom-lang-environments > - '((python \"pythoncode\"))) > + '((python \"pythoncode\") > + (ocaml \"\\\\begin{listing} > +\\\\begin{minted}{ocaml} > +%s\\\\end{minted} > +\\\\caption{%c} > +\\\\label{%l}\"))) > > -would have the effect that if org encounters begin_src python > -during latex export it will output > +would have the effect that if org encounters a Python source block > +during LaTeX export it will produce > > \\begin{pythoncode} > <src block body> > - \\end{pythoncode}") > + \\end{pythoncode} > + > +and if org encounters an Ocaml source block during LaTeX export it Org. Perhaps the ’and’ should be capitalized. I don’t know. The rest looks good to me. Feel free to push (modulus someone else saying otherwise). Thanks, Rasmus -- m-mm-mmm-mmmm bacon! ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: interaction between org-latex-custom-lang-environments and name when exporting to latex 2015-11-27 21:05 ` Rasmus @ 2015-11-30 12:12 ` Alan Schmitt 2015-11-30 13:11 ` Rasmus 0 siblings, 1 reply; 15+ messages in thread From: Alan Schmitt @ 2015-11-30 12:12 UTC (permalink / raw) To: Rasmus; +Cc: emacs-orgmode [-- Attachment #1.1: Type: text/plain, Size: 472 bytes --] Hi, On 2015-11-27 22:05, Rasmus <rasmus@gmx.us> writes: >>> Also, you need to denote that the defcustom was changed. >>> >>> :version "25.1" >>> :package-version '(Org . "9.0") >>> >>> When you are at it, please add the missing tags, :group and :type. >> >> It's not a defcustom but a defvar. (But maybe it should be a defcustom …) > > It should be a defcustom. Feel free to fix it, preferably in a separate > patch. Here is a patch to do so. [-- Warning: decoded text below may be mangled, UTF-8 assumed --] [-- Attachment #1.2: 0001-ox-latex-Make-org-latex-custom-lang-environments-a-d.patch --] [-- Type: text/x-patch, Size: 1385 bytes --] From b94b5ba0bdb619cb9047efb19432216812eaf6a2 Mon Sep 17 00:00:00 2001 From: Alan Schmitt <alan.schmitt@polytechnique.org> Date: Mon, 30 Nov 2015 13:07:56 +0100 Subject: [PATCH] ox-latex: Make org-latex-custom-lang-environments a defcustom * ox-latex.el (org-latex-custom-lang-environments): Change from defvar into a defcustom. --- lisp/ox-latex.el | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el index eaad29f..407df6b 100644 --- a/lisp/ox-latex.el +++ b/lisp/ox-latex.el @@ -1021,7 +1021,7 @@ block-specific options, you may use the following syntax: (string :tag "Minted option name ") (string :tag "Minted option value")))) -(defvar org-latex-custom-lang-environments nil +(defcustom org-latex-custom-lang-environments nil "Alist mapping languages to language-specific LaTeX environments. It is used during export of src blocks by the listings and minted @@ -1062,7 +1062,14 @@ will produce \\end{minted} \\caption{<caption>} \\label{<label>} - \\end{listing}") + \\end{listing}" + :group 'org-export-latex + :type '(repeat + (list + (symbol :tag "Language name ") + (string :tag "Environment name or format string"))) + :version "25.1" + :package-version '(Org . "9.0")) ;;;; Compilation -- 2.6.3 [-- Attachment #1.3: Type: text/plain, Size: 260 bytes --] > The rest looks good to me. Feel free to push (modulus someone else saying > otherwise). Thanks a lot, I pushed it. Best, Alan -- OpenPGP Key ID : 040D0A3B4ED2E5C7 Athmospheric CO₂ (Updated November 29, 2015, Mauna Loa Obs.): 400.37 ppm [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 472 bytes --] ^ permalink raw reply related [flat|nested] 15+ messages in thread
* Re: interaction between org-latex-custom-lang-environments and name when exporting to latex 2015-11-30 12:12 ` Alan Schmitt @ 2015-11-30 13:11 ` Rasmus 2015-12-01 8:14 ` Alan Schmitt 0 siblings, 1 reply; 15+ messages in thread From: Rasmus @ 2015-11-30 13:11 UTC (permalink / raw) To: alan.schmitt; +Cc: emacs-orgmode Alan Schmitt <alan.schmitt@polytechnique.org> writes: >> It should be a defcustom. Feel free to fix it, preferably in a separate >> patch. > > Here is a patch to do so. > > From b94b5ba0bdb619cb9047efb19432216812eaf6a2 Mon Sep 17 00:00:00 2001 > From: Alan Schmitt <alan.schmitt@polytechnique.org> > Date: Mon, 30 Nov 2015 13:07:56 +0100 > Subject: [PATCH] ox-latex: Make org-latex-custom-lang-environments a defcustom > > * ox-latex.el (org-latex-custom-lang-environments): Change from defvar > into a defcustom. > --- > lisp/ox-latex.el | 11 +++++++++-- > 1 file changed, 9 insertions(+), 2 deletions(-) > > diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el > index eaad29f..407df6b 100644 > --- a/lisp/ox-latex.el > +++ b/lisp/ox-latex.el > @@ -1021,7 +1021,7 @@ block-specific options, you may use the following syntax: > (string :tag "Minted option name ") > (string :tag "Minted option value")))) > > -(defvar org-latex-custom-lang-environments nil > +(defcustom org-latex-custom-lang-environments nil > "Alist mapping languages to language-specific LaTeX environments. > > It is used during export of src blocks by the listings and minted > @@ -1062,7 +1062,14 @@ will produce > \\end{minted} > \\caption{<caption>} > \\label{<label>} > - \\end{listing}") > + \\end{listing}" > + :group 'org-export-latex > + :type '(repeat > + (list > + (symbol :tag "Language name ") > + (string :tag "Environment name or format string"))) I don't know if the strings have to the same width, but I assume you tested it before hand. For me at least this is the only way to evaluate Custom changes. To me it looks fine. Thanks a lot, Alan! Rasmus -- When in doubt, do it! ^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: interaction between org-latex-custom-lang-environments and name when exporting to latex 2015-11-30 13:11 ` Rasmus @ 2015-12-01 8:14 ` Alan Schmitt 0 siblings, 0 replies; 15+ messages in thread From: Alan Schmitt @ 2015-12-01 8:14 UTC (permalink / raw) To: Rasmus; +Cc: emacs-orgmode [-- Attachment #1: Type: text/plain, Size: 800 bytes --] On 2015-11-30 14:11, Rasmus <rasmus@gmx.us> writes: >> + :group 'org-export-latex >> + :type '(repeat >> + (list >> + (symbol :tag "Language name ") >> + (string :tag "Environment name or format string"))) > > I don't know if the strings have to the same width, but I assume you > tested it before hand. For me at least this is the only way to evaluate > Custom changes. I assume it has to be the same size because it looks like this: Hide org-latex-custom-lang-environments: INS DEL List: Language name : nil Environment name or format string: I'll apply the patch. Best, Alan -- OpenPGP Key ID : 040D0A3B4ED2E5C7 Athmospheric CO₂ (Updated November 29, 2015, Mauna Loa Obs.): 400.37 ppm [-- Attachment #2: signature.asc --] [-- Type: application/pgp-signature, Size: 472 bytes --] ^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2015-12-01 8:15 UTC | newest] Thread overview: 15+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2015-11-16 12:59 interaction between org-latex-custom-lang-environments and name when exporting to latex Alan Schmitt 2015-11-20 7:55 ` Alan Schmitt 2015-11-20 10:41 ` Rasmus 2015-11-20 11:06 ` Alan Schmitt 2015-11-23 11:13 ` Alan Schmitt 2015-11-23 17:54 ` Rasmus 2015-11-24 8:07 ` Alan Schmitt 2015-11-24 13:14 ` Rasmus 2015-11-25 7:59 ` Alan Schmitt 2015-11-25 9:57 ` Rasmus 2015-11-27 16:27 ` Alan Schmitt 2015-11-27 21:05 ` Rasmus 2015-11-30 12:12 ` Alan Schmitt 2015-11-30 13:11 ` Rasmus 2015-12-01 8:14 ` Alan Schmitt
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).