1

The context

When editing Org mode files, I like editing code snippets in isolated buffers because that way I can use snippets and language dependent features in those buffers (something which is not possible out of the box in an Org mode buffer).

Because of this, I've decided to enclose every LaTeX environment within a #+BEGIN_EXPORT block because that ensures that when executing org-edit-export-block within that code block, I will be able to edit that code block in an isolated buffer and have all those features that are available on those buffers with latex-mode as major mode.

The problem

The problem with this approach is that executing org-latex-preview (i.e. pressing C-c C-x C-l) does not show the preview #+BEGIN_EXPORT blocks as would occur with embedded LaTeX environments.

Here's a minimal working example. Let's assume we have the following Org mode file

* My heading

\begin{equation}
  x = \frac{10}{10}
\end{equation}

#+begin_export
\begin{equation}
  x = \frac{10}{10}
\end{equation}
#+end_export

Executing org-latex-preview within the equation environment which is outside the #+BEGIN_EXPORT block does show the preview, while executing org-latex-preview within the equation environment which is inside the #+BEGIN_EXPORT block does not show the preview of that equation environment but previews the other equation environment.

The question

How can I make org-latex-preview preview the content of #+BEGIN_EXPORT code blocks?

If that's not possible, then how can I preview a given #+BEGIN_EXPORT code block whose language is LaTeX without exiting the current buffer? This behavior must be similar to previewing LaTeX environments with the org-latex-preview function.

  • Unfortunately that does not seem to be possible currently: the code uses 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 in export-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. – NickD Sep 17 at 15:18
  • In the meantime, you can use the somewhat awkward workflow of adding the export markers when needed, editing the block in the sub-editing buffer, and after exiting the sub-editing buffer delete the markers. Adding the markers is easy: select the region and then say C-c C-, l to add export 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. – NickD Sep 17 at 16:01
  • Thank you for the workaround. I think I'll be using that until something better pops up. I've answered my question with relevant information. – doltes Sep 17 at 16:15
1

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 #+PROPERTYs.

Related information

Related discussion can be found in

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.