Emacs org: Promote all headings of a subtree during export? - emacs

I use emacs org a lot to export parts of org documents to latex/pdf. I was wondering whether there is a way to promote all headings of the selected parts during the export process. For instance, suppose the file looks like this:
* Project 1
** Task 1 :export:
*** Introduction
Text text text.
*** Results
Text text text.
* Project 2
The emacs org export to latex would produce a tex file of the following structure:
\section{Project 1}
\subsection{Task 1}
\subsubsection{Introduction}
Text text text.
\subsubsection{Results}
Text text text.
But because there is not highest level in the part to be exported, it would make more sense to have the following structure:
\section{Task 1}
\subsection{Introduction}
Text text text.
\subsection{Results}
Text text text.
Or, even better:
\title{Task 1}
\maketitle
\section{Introduction}
Text text text.
\section{Results}
Text text text.
I was wondering whether anyone has an idea how to go about this? My lisp skills are unfortunately very rudimentary, seems like it should not be too hard.
Thanks!
Stephan

The first behavior you describe can be achieved by adding the following to your .emacs:
;; Define a function for turning a single subtree into a top-level tree
;; (:export: headings might be located at an arbitrary nesting level,
;; so a single call to "org-promote-subtree" is not enough):
(defun org-promote-to-top-level ()
"Promote a single subtree to top-level."
(let ((cur-level (org-current-level)))
(loop repeat (/ (- cur-level 1) (org-level-increment))
do (org-promote-subtree))))
;; Define a function that applies "org-promote-to-top-level"
;; to each :export: subtree:
(defun org-export-trees-to-top-level (backend)
"Promote all subtrees tagged :export: to top-level.
BACKEND is the export back-end being used, as a symbol."
(org-map-entries 'org-promote-to-top-level "+export"))
;; Make org-mode run "org-export-subtrees-to-top-level" as part of the export
;; process:
(add-hook 'org-export-before-parsing-hook 'org-export-trees-to-top-level)
Implementing the second behavior is a bit trickier but you can use theorg-export-trees-to-top-level function as a starting point if that's what you ultimately need. I'd like to point out, however, that this will not work for files with more than one :export: subtree (unless you also come up with a way to decide which headline would become the \title in these cases).
Sources:
Logic of org-promote-to-top-level based on source code of org-cycle-level command
Using the org-mode mapping API
Relevant information about export hooks

Related

LaTeX math mode ($...$) font color in org mode

I have just started using org-mode and it looks awesome. The only issue that I have so far is that when I write a text in mathmode ($...$) it appears in the standard-text font color.
So, I would like to make org-mode to identify the mathmode text and be able to present it in some other color. Note that I don't need to change the color of the actual equation, just the source text in org-mode.
Here is an example of how the text is currently presented
A paper by Rohnert, titled "Moving a disc between polygons" introduces
a structure using which one can generate a solution (path) for
a given query in $O(\log n) + k$ time.
and how I would like it to look
A paper by Rohnert, titled "Moving a disc between polygons" introduces
a structure using which one can generate a solution (path) for
a given query in $O(\log n) + k$ time.
(Note that I would prefer to display in some given color, e.g. red, and not bold face.)
In Emacs version 24.4 and later, this is controlled via the variable org-highlight-latex-and-related:
Non-nil means highlight LaTeX related syntax in the buffer. When non
nil, the value should be a list containing any of the following
symbols:
`latex' Highlight LaTeX snippets and environments.
`script' Highlight subscript and superscript.
`entities' Highlight entities.
So something like
(eval-after-load 'org
'(setf org-highlight-latex-and-related '(latex)))
in your init should help. Such code is formatted according to the face org-latex-and-related.
In earlier versions, the variable org-highlight-latex-fragments-and-specials, which is a simpler nil / non-nil variable:
(eval-after-load 'org
'(setf org-highlight-latex-fragments-and-specials t))
In this case, the face org-latex-and-export-specials is used.

How to get literal forward slashes in org-mode?

In text, can I make org-mode ignore forward slashes somehow? Phonetics uses /s/ to denote a certain level of analysis.
I assume that you do not want the text to appear emphasized in the buffer, nor in the output. This is a slightly more complex answer which will achieve that result:
There is a variable defined by Org-mode called org-emphasis-alist which defines the different emphasis modes, what their plain-text syntaxes are, and how they are exported to HTML. You can achieve the result you want by changing the value of this variable before Org-mode has been loaded. That last part is critical so note it well—Org-mode reads the value of org-emphasis-alist when it is loaded and uses that value to generate a regular expression for highlighting ("font-lock") purposes.
Here are two routes to that:
Add the following lines to your .emacs file above the lines that load Org-mode:
(setq org-emphasis-alist
`(("*" bold "<b>" "</b>")
;; ("/" italic "<i>" "</i>")
("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
("=" org-code "<code>" "</code>" verbatim)
("~" org-verbatim "<code>" "</code>" verbatim)
("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
"<del>" "</del>")))
(Notice the commented out line.)
Make the change through Emacs' customization facility:
M-x customize RET
In the search box enter Org Emphasis and click Search.
Click the down arrow next to Org Emphasis Alist to reveal its value.
Find and click the second DEL button—corresponding to the italic list item.
Click the Save for future sessions button at the top of the buffer.
You can use
#+OPTIONS: *:nil
to turn off text-emphasis (bold,italics,underline). This will however only work on export itself, the emphasis will still be visible.
See the manual for other export options.
If you like the standard emphasis functionality of the forward slashes in org-mode, you could also just define a new environment. I put something like the following in the preamble whenever I do phonetics/phonology typesetting:
\newcommand\uRep[1]{$/$\textipa{#1}$/$}

How can I use other heading styles like twiki ---+ or mediawiki == h2 == in org-mode?

I really want to use org-mode.
But, I want to use org-mode to understand structured documents that have already been written using different heading syntax,
e.g. using twiki's ---+
---+ H1
Top level
---++ H2
Nested
---+ H1 #2
Second top level
Or mediawiki like
= H1 =
Top level
== H2 ==
Nested
= H1 #2 =
Second top level
I'd like to have all of the goodness of org-mode folding, etc., just using these different heading styles.
Actually, worse that that:
I would like, say, the twiki or mediawaiki headings to take priority over org mode asterisk headings. But I would like to have both in use.
= H1 =
Top level
* this is a list
** nested
* list
** nested
== H2 ==
Nested
= H1 #2 =
Second top level
--+ What I have tried so far
I have been able to use outline mode to handle twiki,
for example via
---+ Emacs stuff
# try (defvar twiki-outline-regexp "---+\\++ \\|\\(\\(?: \\)+\\)[0-9*] ")
Local Variables: ***
outline-regexp: "^---\\++" ***
org-outline-regexp: "^---\\++" ***
End: ***
However, org-outline-regexp doesn't do hwat I would hope.
emacs' outline-mode's out-level function looks almost exactly like what I want.
(defvar outline-level 'outline-level
"*Function of no args to compute a header's nesting level in an outline.
It can assume point is at the beginning of a header line and that the match
data reflects the `outline-regexp'.")
i.e. instead of regexps, a generic function.
But I have not managed to make it work with org-mode. It looks like org-mode does not really use this, or, rather, has other stuff.
;; In Org buffers, the value of `outline-regexp' is that of
;; `org-outline-regexp'. The only function still directly relying on
;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
;; job when `orgstruct-mode' is active.
(defvar org-outline-regexp "\\*+ "
"Regexp to match Org headlines.")
(defconst org-outline-regexp-bol "^\\*+ "
"Regexp to match Org headlines.
This is similar to `org-outline-regexp' but additionally makes
sure that we are at the beginning of the line.")
(defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
"Matches an headline, putting stars and text into groups.
Stars are put in group 1 and the trimmed body in group 2.")
Failing this, well, the main thing that I want from org-mode is links, Asking another question here
How can I "linkify" a non-org-mode buffer in emacs
My frustration was simply that org-mode has different rules for what constitutes a new outline section than outline-mode does. It requires a space after the asterisks, so it doesn't work on my extensive collection of notes, which forgo those spaces.
I resolved that by removing the trailing space in the not-well-documented org-outline-regexp variable, which is used to initialize the buffer-local variable outline-regexp, and that seems to be working for me. E.g. (set-variable 'org-outline-regexp "\\*+")
As to your actual question, my guess is that other regexp's and code would have to change to handle radically different stuff like twiki or mediawiki headings.

FlySpell in Org-Mode recognize latex syntax like auctex

Original Response:
I was trying to figure out how in auctex mode latex doesn't seem to highlight any latex functions with flyspell turned on. Is this a custom dictionary file or how is this implemented? Can this be easily incorporated into an org-mode file so it doesn't highlight inserted latex code that will get exported.
Edit:
Simple example taken from top of file and in the text. Basically so latex syntax like ref or label inside {} won't be spell checked (this has been fixed by using (setq ispell-parser tex). Then also setting up a function that specific labels with #+ as the first text on the line won't be checked. I would want the caption checked but not #+LABEL: or #+TYP_TODO: (not shown. Also a way to add TODO keywords to not get checked. I can think of a way to do this is on startup add these to the LOCALWORDS: ispell places at the bottom of the file if not already there but is there an easier or better way to do this.
#+TAGS: NOTE REPORT export noexport MEETING
#+TYP_TODO: TODO Weekly WAITING NEXT | Meeting DONE
#+STARTUP: hidestars content hideblocks
The exponential running mean is shown for various alpha values in Figure \ref{fig:saturation_varying_alphaval_00f6set2}.
#+CAPTION: Plot of varying alpha values for the exponential running mean ($EM$) with $S_{min} = 0.0008 \text{ and } P_m = 0.20$
#+LABEL: fig:saturation_varying_alphaval_00f6set2
#+ATTR_LaTeX: width=0.4\textwidth placement=[h!tb]
flyspell internally uses ispell-mode. To change the way that ispell parses files set the variable ispell-parser to 'tex.
(add-hook 'org-mode-hook (lambda () (setq ispell-parser 'tex)))
Most likely auctex is using flyspell-mode-predicate to define what portions of the buffer shouldn't be spellchecked. (This answer shows how to do something similar for MoinMoinWiki.) org-mode already has such a function, org-mode-flyspell-verify, which apparently doesn't work properly for you... A quick look at the source suggests that org-remove-flyspell-overlays-in should be called for buffer portions containing code samples etc.
Sorry about the handwavy answer; marking this as community wiki to invite improvements.

Org-mode & Latex export: Any way to put short & long names in sectioning commands? Workarounds?

In standard Latex, one can use something like...
\section[short head]{A longer and more meaningful heading version for the section}
...that gives both a long and short version of a section (or other sectioning command) Thus, allowing for both meaningful sectioning 'titles' and, also, reasonable-looking running heads, TOCs, beamer navigation, etc..
Is there any way to easily achieve this in org mode? (That is without hard coding the sectioning commands in LATEX snippets and, thus, defeating most of the flexibility of changing sectioning levels and repurposing content for beamer, book, and article classes that is my reason for wanting to try orgmode, in first place?)
I tried a "workaround" that did not work. I tried editing the possible latex export classes by adding another class to org-export-latex-classes. This new class changes sectioning commands from \section{%s} to \section%s(EDIT-Fixed typo in slashes). Then I tested using [short]{longer version} in orgmode sections of the file. It worked, except it acted as if the longer version section heading was just "{" and "longer version" was body text! What is up with that?
Since version 8.0 the "org-export-latex-classes" strategy won't work anymore.
Instead, and dare I say much more elegantly, you can set the ALT_TITLE property for the heading.
See http://orgmode.org/manual/Table-of-contents.html.
The following org code:
* The Long Title of Section 1
:PROPERTIES:
:ALT_TITLE: Section 1
:END:
Lorem ipsum.
** The Long Title of Subsection 1-1
:PROPERTIES:
:ALT_TITLE: Subsection 1-1
:END:
Dolor sit amet.
will export to LaTeX as:
[...]
\section[Section 1]{The Long Title of Section 1}
\label{sec-1}
Lorem ipsum.
\subsection[Subsection 1-1]{The Long Title of Subsection 1-1}
\label{sec-1-1}
Dolor sit amet.
You had the right idea with creating your own LaTeX class. The problem lies with the way the templates are filled by the default org-fill-template function. I'm not so great with Lisp, but this this hack will do the trick. Add the following to your .emacs file:
(defun my-section (level text)
(let* ((in "") (out "")
(short-title (if (string-match "\\[.*\\]" text)
(substring text (match-beginning 0)
(match-end 0))
nil)))
(if short-title (setq text (substring text (match-end 0) -1)))
(setq in (org-fill-template
"\\section%S{%s}"
(list (cons "S" (or short-title ""))
(cons "s" (or text ""))))
out (copy-sequence "\\end{section}"))
(cons text (list in out in out))))
(add-to-list 'org-export-latex-classes
'("test"
"\\documentclass{article}"
my-section))
This declares a new latex class by adding a "test" class to the org-export-latex-classes. Here we declare, instead of the normal \\section{%s} stuff a function that takes two parameters --- the current level and the headline text --- and returns a modified cons cell. Some details of this information can be found in org-latex-export.el.
Above the adding to the list is where we actually define the function. This is honestly a hacky version, and I pulled a lot from the org-beamer-sectioning function in org-beamer.el file. This function basically searches the headline for anything that is like a LaTeX short label (i.e. [....]) removes it from the headline and sticks it before the actual section label. Right now this hack will only generate \section statements, no matter how deep the level - if you want something more intelligent like \chapter or \subsection or even unnumbered items, you'll need to do some more Lisping; again, see org-beamer.el for some help.
This bit of org-mode code
#+latex_class: test
* [short 1] this is 1 star
test
** this is a 2 star
test
*** [short 3] this is a 3 star
test
**** what happens
exports to LaTeX as (only relevant sections shown here):
\section[short 1]{ this is 1 star}
\label{sec-1}
test
\section{ this is a 2 star }
\label{sec-1-1}
test
\section[short 3]{ this is a 3 star}
\label{sec-1-1-1}
test
\section{ what happens }
\label{sec-1-1-1-1}
\end{section}
\end{section}
\end{section}
\end{section}
Although it's not a straight org-mode solution, it seems to work and can be a starting point for you. One of these days I might try to write it up properly and get it folded into the org-mode distribution.
It is possible to use the following commands in latex to define the text that should appear in the header to replace section names. But the TOC will still contain the original names.
\chaptermarks
\sectionmarks
\subsectionmarks
...
So, in org-mode you can write
* Long section title
#+LaTeX: \sectionmark{Short title}
edit: it actually doesn't work on the very page where the section name appears. On this one only, the full name is still put in the header.