# How do I create a floating tikz figure when exporting to PDF in org-mode?

With org-mode 8.2.10 on Emacs 24.5.1, the following will produce a ditaa figure in the exported PDF (LaTeX):

#+BEGIN_SRC ditaa :file foo.png :cmdline -E -s 4 -S
+---------+
| testing |
+---------+
#+END_SRC
#+CAPTION: ditaa caption
#+LABEL: fig:ditaa
#+attr_latex: :width 1in
#+RESULTS:
[[file:foo.png]]


If I try to do the same thing but using tikz to generate a figure, it does not work. The following example (copied from the org-mode documentation) simply produces a PDF containing the string "[[file:fsa.pdf]]".

#+begin_src latex :file fsa.pdf :packages '(("" "tikz")) :border 1em
% Define block styles
\usetikzlibrary{shapes,arrows}
\tikzstyle{astate} = [circle, draw, text centered, font=\footnotesize, fill=blue!25]
\tikzstyle{rstate} = [circle, draw, text centered, font=\footnotesize, fill=red!25]

\begin{tikzpicture}[->,>=stealth', shorten >=1pt, auto, node distance=2.8cm, semithick]
\node [astate] (1) at (0,0) {1};
\node [astate] (2) at (1,0) {2};
\node [rstate] (3) at (2,0) {3};
\path (1) edge [bend left] node {b} (2)
(2) edge node {b} (3)
(2) edge [bend left] node {a} (1)
(3) edge [loop above] node {(a, b)} (3);
\end{tikzpicture}
#+end_src
#+CAPTION: tikz caption
#+LABEL: fig:tikz
#+RESULTS:
[[file:fsa.pdf]]


How do I generate a floating tikz figure without manually using \begin{figure}...\end{figure}?

• The problem seems to be that the result file is put between #+BEGIN/END_LaTeX marks (eg type C-c C-c inside the src block.) – JeanPierre Feb 28 '16 at 20:34

They key is to add a :results raw header argument (documentation) to the #+BEGIN_SRC line to prevent org-mode from wrapping the results in #+BEGIN_LaTeX/#+END_LaTeX:

#+begin_src latex :file fsa.pdf :packages '(("" "tikz")) :border 1em :results raw
% Define block styles
\usetikzlibrary{shapes,arrows}
\tikzstyle{astate} = [circle, draw, text centered, font=\footnotesize, fill=blue!25]
\tikzstyle{rstate} = [circle, draw, text centered, font=\footnotesize, fill=red!25]

\begin{tikzpicture}[->,>=stealth', shorten >=1pt, auto, node distance=2.8cm, semithick]
\node [astate] (1) at (0,0) {1};
\node [astate] (2) at (1,0) {2};
\node [rstate] (3) at (2,0) {3};
\path (1) edge [bend left] node {b} (2)
(2) edge node {b} (3)
(2) edge [bend left] node {a} (1)
(3) edge [loop above] node {(a, b)} (3);
\end{tikzpicture}
#+end_src
#+CAPTION: tikz caption
#+LABEL: fig:tikz
#+RESULTS:
[[file:fsa.pdf]]

• Is this still working for folks? For me, the pdf file produced is in the local directory and has a random hash file name, org seems to look for it in the temporary files (but it's not there), and it can't be found. The :file fsa.pdf seems to have no effect, but I think that's because the org system gets confused about where the temp file went (it's in the local directory instead of the temp directory). – Rico Picone Feb 9 '19 at 2:00