# Pre Process Org mode export

This is the emacs side of a question I asked on the Latex SE.

This other emacs question is also related but for now I am assuming its not an issue

## Short version

I want the best of two worlds:

• The org-tab behavior of *-ed headlines
• The Latex typesetting nuance of \newtheorem applied on these headlines

IOW I want headlines -- maybe slightly doctored -- to behave as headlines in emacs; and to export as math in Latex. Except that instead of Equation Lemma Theorem etc I want Law Fact Action

## This table summarizes

Idea Emacs code Latex Behavior
Law * Text :L: $\odot$ Law nn Text
Fact * Text :F: $\boxdot$ Fact nn Text
Action * Text :A: \checkmark Action nn Text

nn's are the auto-numbering of org-latex as it normally works

## Longer Version

Say I have a heading like

* Recursion before Iteration  :L:


I want it to become on the latex side (something like)

\textbf{$\odot$ Law nn. Recursion before Iteration}


One thing I (guess) I want is a full scale preprocessor of org-export

So something like a sed modification on the buffer before export

After writing the above I found this question with this answer.

That gives me greater clarity.

So there is the function org-export-ignore-headlines in ox-extra.el

How do I change it so that on Latex export

* Lorem Ipsum  :L:


is converted to

#+BEGIN_LAW
Lorem Ipsum
#+END_LAW


[Assume that a suitable law environment has been created in the style file using newtheorem

## In response to TMar

No your attempt does not work.

If I give:

* A
** Recursion before Iteration                :LAW:
Lorem Ipsum


I get

\begin{document}

\tableofcontents

\#+BEGIN\textsubscript{LAW}

\section{A}
\label{sec:org196f793}
\#+END\textsubscript{LAW}

Recursion before Iteration                :LAW:
Lorem Ipsum
\end{document}


IOW

• sequencing of fragments is wrong
• timing is wrong -- its happening after begin blocks are processed

The second point can be obviated by simply replacing the #+BEGIN_LAW by \begin{law}. But what should go in the block is going outside the block.

Two alternatives

1. The simpler one

*** Recursion before Iteration :L:
# nothing allowed here inside


becomes

#+begin_law
Recursion before Iteration
#+end_law

2. More fiddly but hopefully not too hairy! Assume that :ignore: headlines are ignored though their contents are exported according to org-export-ignore headlines. So

*** Recursion before Iteration  :L:
Lorem ipsum etc


becomes

*** Dummy :ignore:
#+begin_law
Recursion before Iteration
#+end_law
Lorem Ipsum etc

• I don't really understand what you are trying to do (but I haven't read your related questions yet), but you seem focused on pre-processing: I wonder if you can do what you want with post-processing, e,g, using filters. BTW, the answer you link to is not mine: I just edited it. Apr 19 at 12:53
• @NickD Yes I guess filters would fit. But the API is quite arcane so I was hoping someone who is familiar would show how
– Rusi
Apr 19 at 13:50
• It's not clear to me how you want to map things: in the "normal" exporter, the hierarchical structure of an org document's headlines is mapped to the hierarchical structure of an article/report/book in LaTeX: parts, chapters, sections, subsections etc. You show first and second level headlines above, some with tags, some without, with presumably some tags being relevant to you and some not. How is each such case and combinations thereof to be mapped? Until you specify the mapping exactly, it's impossible to propose a general mechanism (and it might be that such a mechanism does not exist)... Apr 20 at 5:30
• ... in which case, you are going to have to spell out what limitations in the structure you are willing to accept. Apr 20 at 5:30
• @NickD Spelt out in new addition
– Rusi
Apr 20 at 15:22

Another approach is, for example, to have a pre-processing using org-export-before-parsing-hook. Here is a small code with this idea.


(interactive)
(save-excursion
(goto-char (point-min))
(save-match-data
;; test that the tag LAW is in the headline.
(if  (member "LAW" (org-get-tags))
;; remove headline format (e.g. remove the stars)
(save-excursion
(end-of-line 0)
(open-line 1)
(forward-line 1)
(insert text1)
)
(save-excursion
(end-of-line)
(open-line 1)
(forward-line 1)
(insert text2))))))))

(defun insert-beginning-end (backend)
" insert beginning and end"


• Thanks for the efforts. But I guess this is some kind of skeleton that I have to fill according to my needs?? Because I am not seeing above where/how a headline starting with L becomes \begin{law} etc. O second thought that L etc can be tags and then one must search for the tag
– Rusi
Apr 19 at 10:45
• Changed the question to be tag based
– Rusi
Apr 19 at 10:49
• I added a line to test for a tag. This can be adapted to work with other tags. I guess this is not the optimal code but it should work.
– TMat
Apr 19 at 12:12
• Ive added a footer to the question showing how it does not work. Thanks though for trying
– Rusi
Apr 19 at 13:50
• I changed it, just an error in the if. I also added backslashes to #+BEGIN_LAW and #+END_LAW for it to work.
– TMat
Apr 19 at 15:24