Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

emacs

org-mode

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?

like image 587
J. Worker Avatar asked Feb 26 '12 22:02

J. Worker


2 Answers

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.
like image 107
mpasi Avatar answered Nov 15 '22 21:11

mpasi


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.

like image 35
cm2 Avatar answered Nov 15 '22 19:11

cm2