0

I'd like to set a variable to be expanded in several LaTeX code blocks. For the sake of simplicity, there is only one in this case, foo. :header-args :var is ignored: I presume because LaTeX does not support interactive evaluation? The best I could come up with is verbose. Is there a workaround, for example by using LuaTeX?

foo.org:

#+name: x-val
#+begin_src emacs-lisp :var val=1 :exports none
val
#+end_src

#+RESULTS: x-val
: 1

#+name: foo    
#+begin_src latex :noweb yes :tangle foo.tex
\documentclass{article}
\begin{document}
x = <<x-val()>>
\end{document}
#+end_src

foo.tex

\documentclass{article}
\begin{document}
x = 1
\end{document}

The first and second code blocks are evaluated with C-c C-c and C-u M-x org-babel-tangle, respectively.

UPDATE:

I tried this:

#+PROPERTY: header-args:latex :var val=1

#+name: foo
#+begin_src latex :noweb yes :tangle foo.tex
\documentclass{article}
\begin{document}
x = val
\end{document}
#+end_src

And I get this:

\documentclass{article}
\begin{document}
x = val
\end{document}

enter image description here

enter image description here

enter image description here

init.el (in full) contains config.el (in full) contains:

(org-babel-do-load-languages '(org-babel-load-languages '((latex . t) (python . t))))

2 Answers 2

0

This works:

#+name: foo    
#+begin_src latex :noweb yes :tangle foo.tex :var val=1
\documentclass{article}
\begin{document}
x = val
\end{document}
#+end_src

and so does this:

#+PROPERTY: header-args:latex :var val=1

#+name: foo    
#+begin_src latex :noweb yes :tangle foo.tex
\documentclass{article}
\begin{document}
x = val
\end{document}
#+end_src

You have misspelled the property: there should be no colons before or after its name: header-args, not :header-args (that's a frequent source of confusion: see this question) or you have added the PROPERTY and not activated it by pressing C-c C-c on it or closed and reopened the file (another frequent source of confusion).

5
  • This doesn't work for me, I get x=val. Updated the post to reflect it.
    – user19777
    May 19 at 20:27
  • Which doesn't work? The first one or the second one? Or both?
    – NickD
    May 19 at 20:40
  • OK, the property one then: did you hit C-c C-c on the #+PROPERTY line after adding it to the buffer and before tangling? Or save the file, kill the buffer and reopen the file before tangling?
    – NickD
    May 19 at 20:41
  • The pictures show C-c C-c on the #+PROPERTY, and I also tried the self contained version. Neither works.
    – user19777
    May 19 at 20:55
  • Both work here. Try starting with emacs -q and as minimal an init file as possible. Maybe some package or some setting is interfering.
    – NickD
    May 19 at 21:28
1

I noticed an Error upon launching Emacs ("Error (usepackage) org/:init:..."), and I made the following change to the config file (1) :

$ cat ~/.emacs.d/config.el | grep -n5 'org-babel-do' | tail -n5
226-   'org-babel-load-languages
227-   '((emacs-lisp . t) ;; *the change*
228-     (latex . t)
229-     (python . t)))
230-  )

(1) which begs for either of adding, rather than setting, new languages to load, or better yet lazy loading.

And this is satisfactory:

$ touch foo.tex; truncate -s 0 foo.tex
$ emacs +1 foo.org --eval "(org-babel-tangle)"
$ cat foo.tex | grep 'x = '
x = 1

I'm not certain the above change to the configuration is responsible for the error and that in turn is responsible for the seeming satisfactory resolution of the stated problem, so I'll wait for more evidence before accepting this answer.

enter image description here

2
  • 1
    That's most probably the culprit.
    – NickD
    May 20 at 1:58
  • I actually cannot reproduce the problem: doing emacs -q -l init.el foobar.org with init.el just containing the org-babel-do-load-languages to set python and latex only, I still can tangle the block or evaluate it and the val replacement happens. I think I jumped the gun here.
    – NickD
    May 20 at 16:26

Your Answer

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