9
votes

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?

3
Are you sure you used /section{%s} and /section%s rather than \section{%s} and \section%s?N.N.
Used right slashes in actual emacs! Thus, not the problem. (although use of LaTex IDEs does create occasional problem in "finger memory as my stupid goof in comment box shows ;-) ). Of course, %s is escaping the curly brackets...given how much hacking is done on org, is there an alternative (e.g. %foo) that does not escape the content? Is there a better hack for the the core problem? Seems to me I am not the only person likely to have long section titles that wants to use the short variant for TOCs and other navigation.J. Worker

3 Answers

8
votes

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.
1
votes

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.

0
votes

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.