Con le versioni recenti di Org-mode (ancora più vecchia della versione 9 --- vedi modifica sotto), è possibile utilizzare qualcosa di simile al seguente, che può esportare in LaTeX e in HTML. In quest'ultimo caso, convert
(dal toolkit ImageMagick) viene utilizzato per tradurre un PDF generato dallo snippet di codice tikz in un'immagine PNG di dimensioni 100 px per 100 px.
#+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:
Inoltre, si può aggiungere un sottotitolo e inserire l'immagine in un galleggiante figure
ambiente in lattice base:
#+caption: A diamond.
#+attr_latex: :float t :width ""
#+results: diamond
Nota l'attributo :width
è impostato sulla stringa vuota per cancellare la comportamento predefinito della modalità Org, che imposta la larghezza dell'immagine su 0.9\textwidth
durante l'esportazione in LaTeX.
Secondo this page, è anche possibile esportare le immagini in formato SVG, invece di PNG, semplicemente sostituendo diamond.png
da diamond.svg
e rimuovendo le intestazioni ImageMagick-correlati, come 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:
Nota, tuttavia, che questa soluzione non supporta macro matematiche nel codice tikz così com'è. htlatex
dovrebbe supportare i costrutti matematici di base (da provare annullando i commenti sulle linee 2 tikz e lisp sopra), ma questa funzione ha bisogno di qualche rielaborazione apparentemente, poiché l'SVG risultante non è valido.
Modifica
Dalla versione 9, il codice precedente diventa (con riferimento alla figura, per scopi illustrativi):
#+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:
Le principali differenze sono nella "COMMENTO "diventando un tag": noexport "per la sezione di configurazione (vedere this answer), il codice della macro by-backend
e l'attributo" graphics "per il risultato del blocco di codice latex.
Mi chiedo che cosa ti aspetti dall'esportatore html. – kindahero
@kindahero, di certo non mi aspetto che mi faccia una tazza di caffè, ma mi aspetto che renderà il grafico con dvipng e lo incorpori nell'html. –
Quindi suppongo che dovresti usare babel http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-LaTeX.html – kindahero