A feature request was published asking this in the mailing list in 2017, but it seems that it is still not possible to do this.
However, there is something you need to know: The reason why you have decided to enclose every LaTeX fragment in a #+BEGIN_EXPORT
block is the fact that you can edit it the #+BEGIN_EXPORT
block in an isolated buffer by executing org-edit-export-block
. If this is the only reason, then you can accomplish this similar behavior without the usage of #+BEGIN_EXPORT
code blocks.
As stated in the description of org-edit-special
(i.e. the function bound to C-c '
)
When in a LaTeX environment, call ‘org-edit-latex-environment’.
executing org-edit-latex-environment
opens the LaTeX fragment (if and only if it is a complete environment) in a new buffer.
Limitations
This has some limitations which are described in this section
org-edit-latex-environment
is not "intelligent" enough
Consider this minimal working example
* My heading
\begin{tcolorbox}
\begin{tcolorbox}
\begin{tcolorbox}
a
\end{tcolorbox}
\end{tcolorbox}
\end{tcolorbox}'
executing org-edit-latex-environment
within the parent tcolorbox
environment shows the following in the opened buffer
\begin{tcolorbox}
\begin{tcolorbox}
\begin{tcolorbox}
a
\end{tcolorbox}
This doesn't occur when executing org-edit-export-block
within a #+BEGIN_EXPORT
code block with the same content.
#+begin_export latex
\begin{tcolorbox}
\begin{tcolorbox}
\begin{tcolorboox}
a
\end{tcolorbox}
\end{tcolorbox}
\end{tcolorbox}
#+end_export
Not being able to edit multiple environments in a single buffer
Let's suppose you want to edit two tabular
environments. What you would need to do is to execute org-edit-latex-environment
in each environment to make the changes and then execute org-latex-preview
in each of them.
\begin{tabular}
...
\end{tabular}
\begin{tabular}
...
\end{tabular}
This would be easily performed with a #+BEGIN_EXPORT
block since you only need to insert both environments within a #+BEGIN_EXPORT
block and then run org-edit-export-block
(see below).
#+begin_export latex
\begin{tabular}
...
\end{tabular}
\begin{tabular}
...
\end{tabular}
#+end_export
As stated above, the problem is that you will not be able to preview the #+BEGIN_EXPORT
block by executing org-latex-preview
.
The perfect solution
Here's a minimal working example (more details below the source code block)
#+LATEX_HEADER: \usepackage{tcolorbox}
#+BEGIN_SRC latex :results file raw :exports results :file tmp.png
\begin{tcolorbox}
\begin{tcolorbox}
\begin{tcolorbox}
a
\end{tcolorbox}
\end{tcolorbox}
\end{tcolorbox}
#+END_SRC
#+RESULTS:
[[file:tmp.png]]
Some details
- Make sure that
latex
has been passed as an argument to org-babel-do-load-languages
. Thus, you will be able to evaluate #+BEGIN_SRC
blocks whose language is LaTeX.
- In order to display the actual image, you need to toggle inline images. I toggled it by executing
org-toggle-inline-images
.
- Since you are exporting it as an image, the box will be displayed as an image in the resulting PDF. This is not what we want. The solution for this is explained below.
You can solve #+BEGIN_SRC
LaTeX code blocks being exported as images in PDFs by modifying the header arguments for your LaTeX code blocks accordingly to your needs. For example, I don't usually export my Org files as PDFs; most of the time I edit my Org files and read them within an Emacs buffer. Because of this, I would set ((1))
on top of my file. Thus, each #+BEGIN_SRC
LaTeX block would be shown within the Org buffer. Now, if I needed to export the Org file as a PDF, then I would comment ((1))
and comment out ((2))
. Thus, #+BEGIN_SRC
latex code blocks would be shown in higher quality than embedded images.
# ((1)) Property for previewing LaTeX as images within an Org buffer
#+PROPERTY: header-args:latex :results file raw :exports results :file tmp.png
# ((2)) Property for exporting #+BEGIN_SRC blocks as actual LaTeX environments and not as images. This is what you might want when exporting your Org file as a PDF.
# #+PROPERTY: header-args:latex :exports results
#+LATEX_HEADER: \usepackage{tcolorbox}
#+BEGIN_SRC latex
\begin{tcolorbox}
\begin{tcolorbox}
\begin{tcolorbox}
a
\end{tcolorbox}
\end{tcolorbox}
\end{tcolorbox}
#+END_SRC
You can also bind a key to modify LaTeX header arguments by modifying the value of the org-babel-default-header-args:latex
variable. This way, you avoid going to the top of the file in order to modify the #+PROPERTY
s.
Related information
Related discussion can be found in
org-element-context
to figure out whether it's in (or near) a LaTeX fragment, but the#+begin_export
interferes and it decides it is inexport-block
context. At that point, the value of the context is the LaTeX fragment in-between, but that's a featureless blob to Org mode: it does not try to recognize it at all. So it would require code changes at the very least. You might want to propose it as an enhancement on the mailing list, but I'm not sure whether it would be acted upon.C-c C-, l
to addexport latex
markers around it. I don't know of a simpler way to delete, but killing the begin/end lines certainly works - and can be automated if necessary.