0

When tangling, I want to use/refer to code-blocks from different file.


Suppose /tmp/1.org file below that contains code block named code_block_in_1_org (that I want to use later).

* This is ~1.org~ file:

#+name: code_block_in_1_org
#+begin_src python :noweb yes
import pandas as pd
#+end_src

And then, I want to use code_block_in_1_org block in different file (/tmp/2.org - the_final_code_block):

#+INCLUDE: "/tmp/1.org"           

* This is the ~/tmp/2.org~ file
#+caption: Local Code Block
#+name: code_block_local
#+begin_src python :noweb yes
import numpy as np
#+end_src

#+caption: The Final Code Block
#+name: the_final_code_block
#+begin_src python :results output :tangle output.py :noweb yes
<<code_block_in_1_org>>      # referencing from 1.org
<<code_block_local>>         # referencing from within 2.org
#+end_src

When I export 2.org to LaTeX PDF everything works fine: enter image description here


Problem: when I org-babel-tangle in 2.org the output output.py file doesn't contain the code_block_in_1_org part:

# referencing from 1.org
import numpy as np         # referencing from within 2.org

Question:

  • How to refer to code-block from different file when tangling the code?
  • And, why does it work when Exporting PDF, and not when tangling?
4
  • 1. I don't know for sure, but I don't see any evidence that it is possible; 2. because #+INCLUDEs are processed on export, but are not processed on tangle - I'd suggest that you send an RFE to the mailing list. One possible hack is to proceed as in export: copy the file contents into a temp buffer, process the includes in the temp buffer and then tangle from that buffer to the output file.
    – NickD
    Apr 22, 2023 at 13:35
  • Sorry, what's a RFE? (is this a tag for Request-Feature?)
    – Garid
    Apr 22, 2023 at 16:10
  • 1
    Sorry -RFE stands for "Request For Enhancement".
    – NickD
    Apr 22, 2023 at 17:52
  • 1
    I've sent it, If anyone interested, you can see it here
    – Garid
    Apr 22, 2023 at 19:42

1 Answer 1

0

Apparently this can be done via org-babel-lob-ingest functions. (see the mail reply).

From manual-16.12:

For any user to add code to the library, first save the code in regular code blocks of an Org file, and then load the Org file with org-babel-lob-ingest, which is bound to C-c C-v i.

Just adding /tmp/1.org via M-x org-babel-lob-ingest, or (org-babel-lob-ingest "/tmp/1.org") will load code-blocks in that file. And then, tangling /tmp/2.org does include code-blocks from the other file.

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.