emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* shorter syntax for both latex and html export of a figure
@ 2016-03-07 10:35 Alan Schmitt
  2016-03-07 16:34 ` Charles C. Berry
  0 siblings, 1 reply; 4+ messages in thread
From: Alan Schmitt @ 2016-03-07 10:35 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 177 bytes --]

Hello,

I'm trying to adapt the example that allows to export a figure both to
tikz and to png so that I don't have to manually add all the header
lines.

Here is what I tried:

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: orgtikz.org --]
[-- Type: text/x-org, Size: 1632 bytes --]

#+LATEX_HEADER: \usepackage{tikz}

* Tikz test
Here's a tree, exported to both html and pdf.

#+header: :file (by-backend (latex "tree.tikz") (t "tree.png"))
#+header: :imagemagick yes :iminoptions -density 600 :imoutoptions -geometry 800
#+header: :results file raw
#+header: :fit yes
#+header: :headers '("\\usepackage{tikz}")
#+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

The same, with shorter syntax

#+header: :file (by-backend (latex "tree2.tikz") (t "tree2.png"))
#+begin_src tikz
  \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 evaluate this first
#+begin_src emacs-lisp :results silent
(defmacro by-backend (&rest body)
  `(case org-export-current-backend ,@body))

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

(defalias 'org-babel-execute:tikz 'org-babel-execute:latex)

(add-to-list 'org-src-lang-modes '("tikz" . latex))

(defvar org-babel-default-header-args:tikz
  '((:imagemagick . "yes")
    (:iminoptions . "-density 600")
    (:imoutoptions . "-geometry 300")
    (:results . "file raw")
    (:fit . "yes")
    (:headers . "'(\"\\\\usepackage{tikz}\")")))
#+end_src

[-- Attachment #1.3: Type: text/plain, Size: 547 bytes --]


The first part, where all the headers line are there, works great, but
the second source block for a newly defined language tikz (an alias to
latex with some default headers) does not work unfortunately. In the
exported version, I get a minted source block and not a link.

Am I doing something wrong? Is it possible to have a shorter syntax for
these blocks, or do I have to put all the header lines all the time?

Thanks,

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7
Monthly Athmospheric CO₂ (2016-01, Mauna Loa Obs.): 402.52

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: shorter syntax for both latex and html export of a figure
  2016-03-07 10:35 shorter syntax for both latex and html export of a figure Alan Schmitt
@ 2016-03-07 16:34 ` Charles C. Berry
  2016-03-08 10:33   ` Alan Schmitt
  0 siblings, 1 reply; 4+ messages in thread
From: Charles C. Berry @ 2016-03-07 16:34 UTC (permalink / raw)
  To: Alan Schmitt; +Cc: emacs-orgmode

On Mon, 7 Mar 2016, Alan Schmitt wrote:

> Hello,
>
> I'm trying to adapt the example that allows to export a figure both to
> tikz and to png so that I don't have to manually add all the header
> lines.
>

Maybe this gets you a step closer

#+begin_src emacs-lisp :results silent
     (defmacro by-backend (&rest body)
       `(case (org-bound-and-true-p org-export-current-backend)
 	 ,@body))

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

     (defalias 'org-babel-execute:tikz 'org-babel-execute:latex)
     (defalias 'org-babel-expand-body:tikz 'org-babel-expand-body:latex)

     (add-to-list 'org-src-lang-modes '("tikz" . latex))

   (defvar org-babel-default-header-args:tikz
     '((:imagemagick . "yes")
       (:iminoptions . "-density 600")
       (:imoutoptions . "-geometry 800")
       (:results . "file raw")
       (:fit . "yes")
       (:headers . "(\\usepackage{tikz})")
       (:exports . "results")))
#+end_src


When I run your tikz src block  interactively I get two files created. Not 
sure why and I haven't time to edebug step thru the code. But maybe you 
can handle that bit.

HTH,

Chuck

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: shorter syntax for both latex and html export of a figure
  2016-03-07 16:34 ` Charles C. Berry
@ 2016-03-08 10:33   ` Alan Schmitt
  2016-03-08 13:46     ` Alan Schmitt
  0 siblings, 1 reply; 4+ messages in thread
From: Alan Schmitt @ 2016-03-08 10:33 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1642 bytes --]

On 2016-03-07 17:34, "Charles C. Berry" <ccberry@ucsd.edu> writes:

> On Mon, 7 Mar 2016, Alan Schmitt wrote:
>
>> Hello,
>>
>> I'm trying to adapt the example that allows to export a figure both to
>> tikz and to png so that I don't have to manually add all the header
>> lines.
>>
>
> Maybe this gets you a step closer
>
> #+begin_src emacs-lisp :results silent
>      (defmacro by-backend (&rest body)
>        `(case (org-bound-and-true-p org-export-current-backend)
>  	 ,@body))
>
>      (org-babel-do-load-languages
>       'org-babel-load-languages
>       '((latex . t)))
>
>      (defalias 'org-babel-execute:tikz 'org-babel-execute:latex)
>      (defalias 'org-babel-expand-body:tikz 'org-babel-expand-body:latex)
>
>      (add-to-list 'org-src-lang-modes '("tikz" . latex))
>
>    (defvar org-babel-default-header-args:tikz
>      '((:imagemagick . "yes")
>        (:iminoptions . "-density 600")
>        (:imoutoptions . "-geometry 800")
>        (:results . "file raw")
>        (:fit . "yes")
>        (:headers . "(\\usepackage{tikz})")
>        (:exports . "results")))
> #+end_src
>
>
> When I run your tikz src block  interactively I get two files created. Not 
> sure why and I haven't time to edebug step thru the code. But maybe you 
> can handle that bit.

Thank you for the suggestion. At the moment I use a macro (because I
need to put the resulting figure in a figure environment, as I want a
label and caption), but I will give this a try.

Thanks again,

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7
Monthly Athmospheric CO₂ (2016-02, Mauna Loa Obs.): 404.02

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: shorter syntax for both latex and html export of a figure
  2016-03-08 10:33   ` Alan Schmitt
@ 2016-03-08 13:46     ` Alan Schmitt
  0 siblings, 0 replies; 4+ messages in thread
From: Alan Schmitt @ 2016-03-08 13:46 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1841 bytes --]

On 2016-03-08 11:33, Alan Schmitt <alan.schmitt@polytechnique.org> writes:

> Thank you for the suggestion. At the moment I use a macro (because I
> need to put the resulting figure in a figure environment, as I want a
> label and caption), but I will give this a try.

I'm fairly happy with what I ended up doing (using a macro as I need to
pass some extra arguments, and I don't know how to extend a source block
with custom arguments), so here is my solution.

I define this utility function that is lob-ingested:

#+name: wrap-res
#+BEGIN_SRC emacs-lisp :var link="" :var caption="" :var label="" :results raw :exports none
(concat "#+LABEL: " label "\n#+CAPTION: " caption "\n" link)
#+END_SRC

I also define a macro (it's on one line but for this email I'm wrapping
it)

#+macro: tikzfig #+header: :file (by-backend (latex "$1.tikz") (t
 "$1.png")) :imagemagick yes :iminoptions -density 600 :imoutoptions
 -geometry 300 :results file raw :fit yes :headers
 '("\\usepackage{tikz}") :post
 wrap-res(link=*this*,label="fig:$1",caption="$2")

I can then have the following that is exported both in tikz and png (I
do not use svg as I found htlatex lacking for text nodes):

{{{tikzfig(cnf-example,An ordinal in Cantor normal form)}}}
#+begin_src latex
\begin{tikzpicture}[scale=2, every node/.style={transform shape}]
\node[color=blue]{$\omega^{(\omega^\omega\,+\, \omega^2 \times 8 \,+\, \omega)}+ \omega^\omega + \omega^4+ 6$};
\end{tikzpicture}
#+end_src

The first argument of the macro is the label of the figure (with "fig:"
added automatically) as well as the file used to export the figure, the
second argument is the caption.

Hopefully this may be useful to others.

Best,

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7
Monthly Athmospheric CO₂ (2016-02, Mauna Loa Obs.): 404.02

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-03-08 13:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-03-07 10:35 shorter syntax for both latex and html export of a figure Alan Schmitt
2016-03-07 16:34 ` Charles C. Berry
2016-03-08 10:33   ` Alan Schmitt
2016-03-08 13:46     ` Alan Schmitt

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).