0

I want to have python snippets in org thus:

#+begin_PYT
def foo(x):
# Some python code
#+end_PYT

where PYT is a latex environment for python code using (say) listings.

The problem is that while org does translate #+begin_PYT to \begin{PYT} it modifies (transcodes??) the body; so for example the python [[a]] becomes an org link a!

As a result using vanilla org I have to do

#+begin_export latex
\begin{PYT}
def foo(x):
# Some python code
\end{PYT}
#+end_export

With dozens of small snippets those two extra lines are quite an overhead

So options (I can think of!)

  1. There should be a way in vanilla org to tell org that a PYT block should be left alone (not transcoded)
    Or
  2. Using org-special-blocks-extra there should be a way but there is very scant documentation.
  3. Write a 'pre-transcode' filter that transforms #+begin_PYT to "#+begin_export latex\n\\begin{PYT}"

Can someone help out with any of these (or some other!) way to do this?

7
  • Why not have a python source block with the code exported? #+begin_src python :exports code ... #+end_src - that will use either listings or minted depending on the setting of org-latex-listings.
    – NickD
    Commented Aug 17, 2022 at 4:30
  • @NickD Does this work for non standard languages that org doesn't know about? PYT was just an example. More realistically I need a mix of python and a homegrown language for which a detailed listings parameterization has been created
    – Rusi
    Commented Aug 17, 2022 at 5:03
  • I imagine not, since it does not work with PYT.
    – NickD
    Commented Aug 17, 2022 at 5:10
  • You could also achieve it, using example blocks with the '#+ATTR_LATEX: :environment pyt' attribute. Then you need only one extra line. Of course, you could create a snippet for creating such blocks. Note that you might have to escape some characters. Despite this option, it would probably still be nice(r) to achieve this via a special block and some configuration variable. Commented Aug 17, 2022 at 6:08
  • org-special-blocks-extra does not provide an out-of-the-box solution to make the [[a]] syntax not become org-links (otherwise, the, I would say, not so scant documentation of org-special-block-extras does explain, how to achieve what you want. Commented Aug 17, 2022 at 7:20

1 Answer 1

2

The answer to your query is in the docstring of org-latex-custom-lang-environments.

Specifically, you want your ox-latex setup in such a way that when the exporter hits org-latex-src-block, the code takes this path.

  1. Create somefile.org with following contents
#+begin_src PYT
  def foo(x):
  # Some python code
#+end_src

  1. emacs -Q
  2. Copy the following snippet to *scratch* and do M-x eval-buffer
(define-derived-mode PYT-mode prog-mode "PYT"
  "Major mode for PYT files.")

(custom-set-variables
  '(org-latex-custom-lang-environments '((PYT "PYT")))
  '(org-latex-listings 'minted))

  1. C-x C-f somefile.org, followed with C-c C-e C-b l L will give you this
\begin{PYT}
def foo(x):
# Some python code
\end{PYT}

3
  • Tnx upvoted. I may need to change minted to listings I guess. Then it (should) work (on phone now)
    – Rusi
    Commented Aug 17, 2022 at 11:52
  • I just did (setq org-latex-custom-lang-environments '((PYT "PYT"))) and it worked but things inside [[...]] looked funny inside emacs (latex result ok). Adding (define-derived-mode PYT-mode prog-mode "PYT" "Major mode for PYT files.") and inside emacs is also fine. Why is the minted (or listings) needed?? Anyhow accepted
    – Rusi
    Commented Aug 17, 2022 at 12:43
  • O I see whats happening. Without the (setq org-latex-listings 'listings) org emits a \begin{verbatim}. With it, org properly makes it \begin{PYT}
    – Rusi
    Commented Aug 17, 2022 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.