The source block most certainly doesn't get executed when using org-babel-tangle
. Consequently i
and fig-file
are void.
To get around this you can (starting with the cleanest solution):
- use the result of a named block
- execute the source block when opening the file
- use file local variables
Note: Newer emacs versions use cl-incf
instead of incf
.
1. Use the result of a named block
Change your first block into this to. Notice that I added #+name: fig-file
to it.
#+name: fig-file
#+begin_src emacs-lisp :exports none
(defvar-local i 0)
(concat "f" (number-to-string (incf i)) ".pdf")
#+end_src
Use the result of that block as your file name by using org-sbe
:
#+begin_src R :file (org-sbe "fig-file") :results output graphics :exports results
(plot (rnorm(10))
#+end_src
2. Execute the source block when opening the file
Add this to the top of the org file:
# -*- eval: (org-sbe "fig-file") -*-
Add #+name: fig-file
to your first source block:
#+name: fig-file
#+begin_src emacs-lisp :exports none
(set (make-local-variable 'i) 0)
(fset (make-local-variable 'fig-file)
#'(lambda () (concat "f" (number-to-string (cl-incf i)) ".pdf")))
#+end_src
This executes the source block when opening the file and then fig-file
is defined.
As file local variables can be a security risk, you will be prompted when you open the file if the values should be applied.
3. Use file local variables
Add those lines at the top of your file to make file local variables:
# -*- i: 0; -*-
# -*- fig-file: nil; -*-
# -*- eval: (fset fig-file (lambda () (concat "f" (number-to-string (incf i)) ".pdf"))); -*-
or this at the end:
* COMMENT Footer
# Local Variables:
# i: 0
# fig-file:
# eval: (fset fig-file (lambda () (concat "f" (number-to-string (incf i)) ".pdf")))
# End:
Your first source block is not needed anymore with this solution.
The security risk applies again.