I'm following the Latex howto of org-mode babel. Here's the snippet from the howto I've got in a separate .org file (see bottom of howto page):
#+LATEX_HEADER: \usepackage{tikz}
First execute the second code block, to define the convenience macro
and to set the required new variables in ob-latex.el. Then export to
HTML and to pdf to see the tree exported as an SVG image and as
embedded tikz respectively.
* Tikz test
Here's a tree, exported to both html and pdf.
#+header: :file (by-backend (html "tree.svg") (t 'nil))
#+header: :imagemagick
#+header: :results (by-backend (pdf "latex") (t "raw"))
#+header: :tangle yes
#+begin_src latex
\usetikzlibrary{trees}
\begin{tikzpicture}
\node [circle, draw, fill=red!20] at (0,0) {1}
child { node [circle, draw, fill=blue!30] {2}
child { node [circle, draw, fill=green!30] {3} }
child { node [circle, draw, fill=yellow!30] {4} }};
\end{tikzpicture}
#+end_src
* COMMENT setup
#+header: :tangle yes
#+begin_src emacs-lisp :results silent
(setq org-babel-latex-htlatex "htlatex")
(defmacro by-backend (&rest body)
`(case (if (boundp 'backend) (org-export-backend-name backend) nil) ,@body))
#+end_src
This doesn't really produce a .svg of the tree as advertised, but exporting to Latex does produce it just fine.
My real confusion starts when I try to tangle the babel code blocks. The C-c C-v t command produces two separate files just fine, a .tex and .el, but then if I try to Run Latex on the .tex file just by itself it gives an error. Here's what the org-mode tangle produces:
\usetikzlibrary{trees}
\begin{tikzpicture}
\node [circle, draw, fill=red!20] at (0,0) {1}
child { node [circle, draw, fill=blue!30] {2}
child { node [circle, draw, fill=green!30] {3} }
child { node [circle, draw, fill=yellow!30] {4} }};
\end{tikzpicture}
And here's the error log after I try to run it by itself in Emacs:
. . .entering extended mode
restricted \write18 enabled.
file:line:error style messages enabled.
%&-line parsing enabled.
**\input prac2.tex
(./prac2.tex
./prac2.tex:2: Undefined control sequence.
l.2 \usetikzlibrary
{trees}
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.
Another version (from C-c `) produces this error message:
ERROR: Undefined control sequence.
--- TeX said ---
l.2 \usetikzlibrary
{trees}
--- HELP ---
TeX encountered an unknown command name. You probably misspelled the
name. If this message occurs when a LaTeX command is being processed,
the command is probably in the wrong place---for example, the error
can be produced by an \item command that's not inside a list-making
environment. The error can also be caused by a missing \documentclass
command.
What am I missing here? I'd like to be able to keep my org stuff separate from the Latex source, which org-mode seems to do nicely. But then it has to run properly too. . . .
LB