With recent versions of Org-mode (yet, older than version 9 --- see edit below), you could use something like the following, that can export to LaTeX as well as to HTML. In the latter case, convert
(from the ImageMagick toolkit) is used to translate a PDF generated from the tikz code snippet into a PNG image of size 100px by 100px.
#+TITLE: Test
#+AUTHOR: Blah
#+LATEX_CLASS: article
#+LATEX_CLASS_OPTIONS: [american]
#
# Setup tikz package for both LaTeX and HTML export:
#+LATEX_HEADER: \usepackage{tikz}
#+PROPERTY: header-args:latex+ :packages '(("" "tikz"))
#
#+PROPERTY: header-args:latex+ :imagemagick (by-backend (latex nil) (t "yes"))
#+PROPERTY: header-args:latex+ :exports results :fit yes
* One Diamond
#+name: diamond
#+header: :iminoptions -density 600 -resample 100x100
#+header: :file (by-backend (latex "diamond.tikz") (t "diamond.png"))
#+begin_src latex :results raw file
\begin{tikzpicture}
\draw (1,0) -- (0,1) -- (-1,0) -- (0,-1) -- cycle;
\end{tikzpicture}
#+end_src
#+attr_latex: :float nil :width ""
#+results: diamond
* COMMENT setup
#+name: setup
#+begin_src emacs-lisp :results silent :exports none
(defmacro by-backend (&rest body)
`(case (if (boundp 'backend) (org-export-backend-name backend) nil) ,@body))
#+end_src
# Local variables:
# eval: (org-sbe "setup")
# End:
Besides, one can add a caption, and insert the picture in a floating figure
environment in LaTeX, by using:
#+caption: A diamond.
#+attr_latex: :float t :width ""
#+results: diamond
Note the :width
attribute is set to the empty string to erase the default behavior of Org-mode, which sets the width of the picture to 0.9\textwidth
when exporting to LaTeX.
According to this page, it is also possible to export pictures in SVG instead of PNG, simply by replacing diamond.png
by diamond.svg
and removing ImageMagick-related headers, as in:
#+TITLE: Test
#+AUTHOR: Blah
#+LATEX_CLASS: article
#+LATEX_CLASS_OPTIONS: [american]
#
# Setup tikz package for both LaTeX and HTML export:
#+LATEX_HEADER: \usepackage{tikz}
#+PROPERTY: header-args:latex+ :packages '(("" "tikz"))
#
#+PROPERTY: header-args:latex+ :exports results
* One Diamond
#+name: diamond
#+header: :file (by-backend (latex "diamond.tikz") (t "diamond.svg"))
#+begin_src latex :results raw file
\begin{tikzpicture}
\draw (1,0) -- (0,1) -- (-1,0) -- (0,-1) -- cycle;
% \node at (0,0) {\(x_i\)};
\end{tikzpicture}
#+end_src
#+caption: A diamond.
#+attr_latex: :float t :width ""
#+results: diamond
* COMMENT setup
#+name: setup
#+begin_src emacs-lisp :results silent :exports none
;; (setq org-babel-latex-htlatex "htlatex")
(defmacro by-backend (&rest body)
`(case (if (boundp 'backend) (org-export-backend-name backend) nil) ,@body))
#+end_src
# Local variables:
# eval: (org-sbe "setup")
# End:
Note however that this solution does not support mathematical macros in tikz code as is. htlatex
ought to support basic mathematical constructs (to be tried by un-commenting the 2 tikz and lisp lines above), but this feature needs some rework apparently, as the resulting SVG is invalid.
Edit
Since version 9, the code above becomes (with a reference to the figure, for illustrative purposes):
#+TITLE: Test
#+AUTHOR: Blah
#+LATEX_CLASS: article
#+LATEX_CLASS_OPTIONS: [american]
#
# Setup tikz package for both LaTeX and HTML export:
#
#+LATEX_HEADER: \usepackage{tikz}
#+PROPERTY: header-args:latex+ :packages '(("" "tikz"))
#+PROPERTY: header-args:latex+ :imagemagick yes :fit yes
* One Diamond
#+name: diamond
#+header: :iminoptions -density 600 -resample 100x100
#+header: :file (by-backend (latex "diamond.tikz") (t "diamond.png"))
#+begin_src latex :results raw graphics
\begin{tikzpicture}
\draw (1,0) -- (0,1) -- (-1,0) -- (0,-1) -- cycle;
\end{tikzpicture}
#+end_src
#+name: fig:diamond
#+caption: A diamond.
#+attr_latex: :float t :width ""
#+results: diamond
Figure [[fig:diamond]] is a diamond.
* Setup :noexport:
#+name: setup
#+begin_src emacs-lisp :exports none :results silent
(defmacro by-backend (&rest body)
`(case org-export-current-backend ,@body))
#+end_src
# Local variables:
# eval: (org-sbe "setup")
# End:
The main differences are in the "COMMENT" becoming a ":noexport" tag for the setup section (see this answer), the code of by-backend
macro, and the "graphics" attribute for the result of the latex code block.