0

I use org-babel-tangle to generate latex documents from org files.

These org files have the following first line:

#+PROPERTY: header-args :latex :comments org :tangle PATH/FILE_NAME.tex

I built a function to tangle to tex, compile the tex, and open the generated PDF:

  (defun mda/tangle-tex-compile-open-pdf()
    "Generate and open a PDF from an org file containing tex blocks."
    (interactive)
    (org-babel-tangle)
    (setq full-path-tex (cdr (assq :tangle (caddr (org-babel-get-src-block-info)))))
    (current-buffer)
    (cd (file-name-directory full-path-tex))
    (shell-command (concat "xelatex -interaction=nonstopmode " (file-name-nondirectory full-path-tex)))
    (shell-command (concat "evince " (file-name-base full-path-tex) ".pdf")))

I put this function in my init. When I execute it, it produces the following error :

cd: Wrong type argument: stringp, nil

What I don't understand is that when I run

    (setq full-path-tex (cdr (assq :tangle (caddr (org-babel-get-src-block-info)))))
    (current-buffer)
    (cd (file-name-directory full-path-tex))
    (shell-command (concat "xelatex -interaction=nonstopmode " (file-name-nondirectory full-path-tex)))
    (shell-command (concat "evince " (file-name-base full-path-tex) ".pdf"))

from org files, I have no errors and everything works fine...

6
  • The error msg is telling you that (cdr (assq :tangle (caddr (org-babel-get-src-block-info)))) return nil, not an absolute file name. Why this is the case when you call the function and not when you eval the code from an Org buffer, I don't know. A guess is that in the context of Ords (org-babel-get-src-block-info) gives you something useful and outside that context it doesn't. Try M-x debug-on-entry RET mda/tangle-tex-compile-open-pdf RET, and walk through the debugger with d, to see what goes wrong.
    – Drew
    Sep 15, 2023 at 20:25
  • debug-on-entry RET mda/tangle-tex-compile-open-pdf RET doesn't do anything. I don't even have an error message. And when I press d, I just see the letter d wirrtten down in the buffer.
    – crocefisso
    Sep 15, 2023 at 23:56
  • If the debugger (buffer *Backtrace* doesn't open after you've done M-x debug-on-entry... and retried your code, then mda/tangle-tex-compile-open-pdf isn't getting called, for some reason (?). If the debugger does open, the d in the debugger window steps through the evaluation of that function call.
    – Drew
    Sep 16, 2023 at 2:30
  • I managed to get the *Backtrace* buffer. After doing M-x debug-on-entry RET mda/tangle-tex-compile-open-pdf RET once, the debugger appears each time I do M-x mda/tangle-tex-compile-open-pdf RET. I now have to understand how the debugger works, I'll try to find some tutorial.
    – crocefisso
    Sep 16, 2023 at 5:51
  • You should probably not use cd in a function: it is meant for interactively changing the currenct directory of a buffer. You are better off let-binding default-directory. See my answer to your related question which indirectly also answers this question.
    – NickD
    Sep 18, 2023 at 1:33

0

Your Answer

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

Browse other questions tagged or ask your own question.