emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Can :packages and :headers arguments in latex SRC blocks be made affect EXPORT blocks of results?
@ 2022-11-08 23:04 hugo
  2022-11-09  4:53 ` Ihor Radchenko
  2022-11-09 17:30 ` Juan Manuel Macías
  0 siblings, 2 replies; 3+ messages in thread
From: hugo @ 2022-11-08 23:04 UTC (permalink / raw)
  To: emacs-orgmode

tl;dr: can I have `:packages' and `:headers' arguments from LaTeX src
block take effect, when I export those blocks as export blocks?

I wanted a way to write tikz snippets inside org documents, preview them
in the document as images, but have the tikz /code/ appear when I
exported to LaTeX. So far I have this:

,----
| #+header: :results (if (org-export-derived-backend-p 
org-export-current-backend 'latex) "latex" "file raw")
| #+header: :file (if (org-export-derived-backend-p 
org-export-current-backend 'latex) nil "foo.png")
| #+header: :packages '((nil "tikz" t))
| #+header: :imagemagick t :fit t
| #+header: :headers '("\\usetikzlibrary{calc,positioning,patterns}")
| #+begin_src latex
| \begin{tikzpicture}[on grid,
|   font=\footnotesize,
|   level distance=2.5cm,
|   sibling distance=2.5cm,
|   every node/.style={circle,draw,fill=white},
|   touch/.style={circle,draw=red!100,fill=red!40,thick},
|   invirtueof/.style={circle,draw=red!50,fill=red!20,very thick}]
|
|   \node [invirtueof] {Car}
|   child {node {wheel}}
|   child {node [invirtueof] {body}
|     child {node {driver door}}
|     child {node [invirtueof] {passenger door}
|       child {node [touch] {region of door}}}};
| \end{tikzpicture}
`----

This works almost perfectly. It's set generate an EXPORT block of LaTeX
code when exporting, and an inline image in the results block otherwise.
When generating this inline image, it takes into account the necessary
`:packages' and extra `:headers' defined.

The only problem is that when I export to LaTeX, what is included is
/just/ what was in the results block, which is /just/ the code inside
the original src block (i.e. the export does not include the `:packages'
or `:headers' information, and thus fails to compile because the LaTeX
file doesn't load tikz).

Is there a way around this? Either to export the src block another way,
or to add metadata to the to the resulting EXPORT block (or just text,
though I'm not sure how that would work for `:packages', since they
require text to be added to the resulting exported LaTeX file in a
different place from where the text of the block goes).

Ideally, I would like to not have to change anything in the file between
working on it (previewing) and exporting. Solutions to similar problems
(e.g. <https://emacs.stackexchange.com/a/60714/34394>) seem to require
commenting and uncommenting lines. I would really like a single,
write-once solution (and am perfectly happy with it being verbose).

If there /isn't/ a way to do this at the moment but someone knows
roughly what would have to change in org for there to be, that would be
great information to have too. I would be happy to try to work up a
patch. I've looked at some of the code and it looks just about
approachable.

TIA

Hugo


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

* Re: Can :packages and :headers arguments in latex SRC blocks be made affect EXPORT blocks of results?
  2022-11-08 23:04 Can :packages and :headers arguments in latex SRC blocks be made affect EXPORT blocks of results? hugo
@ 2022-11-09  4:53 ` Ihor Radchenko
  2022-11-09 17:30 ` Juan Manuel Macías
  1 sibling, 0 replies; 3+ messages in thread
From: Ihor Radchenko @ 2022-11-09  4:53 UTC (permalink / raw)
  To: hugo, Timothy; +Cc: emacs-orgmode

hugo@heagren.com writes:

> tl;dr: can I have `:packages' and `:headers' arguments from LaTeX src
> block take effect, when I export those blocks as export blocks?
>
> I wanted a way to write tikz snippets inside org documents, preview them
> in the document as images, but have the tikz /code/ appear when I
> exported to LaTeX. So far I have this:

AFAIK, proper code integration is currently not supported by ob-latex.

Maybe something to include in Timothy's auto-preamble effort.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


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

* Re: Can :packages and :headers arguments in latex SRC blocks be made affect EXPORT blocks of results?
  2022-11-08 23:04 Can :packages and :headers arguments in latex SRC blocks be made affect EXPORT blocks of results? hugo
  2022-11-09  4:53 ` Ihor Radchenko
@ 2022-11-09 17:30 ` Juan Manuel Macías
  1 sibling, 0 replies; 3+ messages in thread
From: Juan Manuel Macías @ 2022-11-09 17:30 UTC (permalink / raw)
  To: hugo; +Cc: emacs-orgmode

hugo@heagren.com writes:

> Is there a way around this? Either to export the src block another way,
> or to add metadata to the to the resulting EXPORT block (or just text,
> though I'm not sure how that would work for `:packages', since they
> require text to be added to the resulting exported LaTeX file in a
> different place from where the text of the block goes).

If I have understood correctly what you want to do, maybe a solution
would be this. You can starting from a single block that includes
the preamble for both preview and export:

#+NAME: preamble
#+begin_src latex :exports none
\usepackage{tikz}
\usetikzlibrary{calc,positioning,patterns}
#+end_src

This block returns the above as a simple string:

#+NAME: pre
#+begin_src emacs-lisp :exports none :results silent :var x = preamble
  (with-temp-buffer
    (insert x)
    (LaTeX-mode)
    (setq preamble-prev (buffer-string)))
#+end_src

And this one adds the necessary LaTeX headers when you export the
document, so LaTeX doesn't crash when compiling.

#+begin_src latex :noweb yes :results raw
,#+LaTeX_HEADER: <<preamble>>
#+end_src

And the final block:

#+header: :results (if (org-export-derived-backend-p  org-export-current-backend 'latex) "latex" "file raw")
#+header: :file (if (org-export-derived-backend-p  org-export-current-backend 'latex) nil "foo.png")
#+header: :imagemagick t :fit t
#+header: :headers `(,(progn (org-babel-ref-resolve "pre") preamble-prev))
#+begin_src latex
 \begin{tikzpicture}[on grid,
   font=\footnotesize,
   level distance=2.5cm,
   sibling distance=2.5cm,
   every node/.style={circle,draw,fill=white},
   touch/.style={circle,draw=red!100,fill=red!40,thick},
   invirtueof/.style={circle,draw=red!50,fill=red!20,very thick}]

   \node [invirtueof] {Car}
   child {node {wheel}}
   child {node [invirtueof] {body}
     child {node {driver door}}
     child {node [invirtueof] {passenger door}
       child {node [touch] {region of door}}}};
 \end{tikzpicture}
#+end_src

#+RESULTS:
[[file:foo.png]]

Best regards,

Juan Manuel 


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

end of thread, other threads:[~2022-11-09 17:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-08 23:04 Can :packages and :headers arguments in latex SRC blocks be made affect EXPORT blocks of results? hugo
2022-11-09  4:53 ` Ihor Radchenko
2022-11-09 17:30 ` Juan Manuel Macías

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).