I'm trying to get this latex class to work with org, anyone had any luck?

  (add-to-list 'org-latex-classes
"\\documentclass[preprint,a4paper, amsfonts, amssymb, amsmath, showkeys, nofootinbib, fleqn]{revtex4-1}
             ("\\section{%s}" . "\\section*{%s}")
             ("\\subsection{%s}" . "\\subsection*{%s}")
             ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
             ("\\paragraph{%s}" . "\\paragraph*{%s}")
             ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))

the org exporter puts the title and author commands in the preamble, while the revtex class requires them to be put in the document section, is there a way to tweak org-exporter to work with it?

  • 1
    Are you having a problem with it? If so, can you describe the problem? Otherwise, the question is likely to be closed as ill-defined.
    – NickD
    Nov 2 '20 at 16:24
  • You can add #+OPTIONS: title:nil such that org-mode will not add the \maketitle in the exported tex. Then you can put \maketitle wherever you want. You can also set author:nil, date:nil, etc.
    – darcamo
    Nov 2 '20 at 21:04
  • 1
    I had forgotten about this peculiarity of revtex but I had to deal with it about 10 years ago, Things have changed a bit in Org mode (the understatement of the century) but the basic idea of the hack still works. See my answer. And thanks for the memories :-)
    – NickD
    Nov 2 '20 at 23:29

A long time ago, I had worked on a hack to deal with this peculiarity of revtex and reconstructed it for this answer (with some differences to accommodate things that have changed in Org mode, in particular the handling of titles).

The main problem as you note is that revtex wants the title, author and date in the body, not in the preamble where just about every other package wants it. That in itself is not too difficult to accomplish:

#+LATEX: \title{Foo}

will put it in the body and similarly for the others. Unfortunately, it will add it after the \maketitle and \tableofcontents constructs in the body, which is too late: we want it before the \maketitle.

The LaTeX backend (uniquely among all the backends) defines a variable org-latex-title-command whose default value is the string \maketitle. Immediately after it outputs the \begin{document}, it will output the value of org-latex-title-command. In fact, the string can be a template with placeholders like %t which will get substituted with the value of the #+TITLE keyword. Do C-h v org-latex-title-command to see all the placeholders that it can deal with.

So if we could redefine org-latex-title-command to the string \title{%t} \maketitle, we'd be all set: the LaTeX exporter would insert the \title{...} part and the \maketitle part in the body of the document, as required by revtex.

The way to do that is to use the #+BIND: keyword mechanism which allows you to define local variables in the temp buffer where the LaTeX output is constructed. The format is

#+BIND: variable value

This mechanism is considered to be something of a safety risk, so you need to explicitly allow it in your Org mode configuration:

(setq org-export-allow-bind-keywords t)

Of course, in this case, you are the one setting the local variable, so you can judge whether it is unsafe, but in general, you might pick up a malicious Org mode file, so Org mode tries to be conservative in this area.

So the Org mode file would look like this at this point:

#+LATEX_CLASS: revtex

#+TITLE: This is the title
#+DATE: November 2, 2020

#+BIND: org-latex-title-command "\\title{%t} \\date{%D} \\maketitle"

* h1

Lorem ipsum etc.

Exporting to LaTeX should work and should do what you expect. Note that backslashes are doubled in the string: that's required by elisp.

There is one more wrinkle having to do with \author constructs. You would expect that you could add \author{%a} to the #+BIND: line and add a keyword line like this: #+AUTHOR: A.U. Thor, and everything would work, but it does not: the #+TITLE: keyword makes the exporter produce a title{...} construct in the preamble; the #+AUTHOR: keyword behaves similarly. But while revtex seems unfazed by the existence of the \title{...}, it barfs on seeing \author{...} in the preamble. So to deal with that wrinkle, we explicitly turn off the effect of the #AUTHOR: keyword by turning off the author option in the file:

#+LATEX_CLASS: revtex
#+OPTIONS: author:nil

#+AUTHOR: A.U. Thor
#+TITLE: This is the title
#+DATE: November 2, 2020

#+BIND: org-latex-title-command "\\title{%t} \\author{%a} \\date{%D} \\maketitle"

* h1

Lorem ipsum etc.

A small extract of the resulting LaTeX file looks like this:

\date{November 2, 2020}
\title{This is the title}


\title{This is the title} \author{A.U. Thor} \date{(November 2, 2020)} \maketitle


Lorem ipsum etc.

There are \title{} and \date{} constructs in the preamble but they do not matter: Org mode has already substituted their value to the places that do matter: the \title{} and \date{} constructs in the body, just before the \maketitle. Note also that even though there is no \author{} in the preamble (which was accomplished through the #+OPTIONS: setting), the one in the body also has the correct value, substituted in from the value of #+AUTHOR: in the Org mode file. And the proof of the pudding is that revtex likes the result.

  • Thank you!! this helps a lot Nov 3 '20 at 16:28
  • Glad it helped!
    – NickD
    Nov 3 '20 at 19:01

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.