# Is reStructuredText compatible with the memoir class?

While I'm fine with writing "real" documents in full-blown LaTeX, I often switch to reStructuredText as a lightweight markup language for simplish documents (like meeting notes and stuff). I was quite happy with my workflow (file.txt -> rst2latex-2.6.py -> pdflatex -> file.pdf) until I recently decided that I wanted to use the memoir class, just as I do for my real documents.

The documentation for reStructuredText states that this class is supported via the --documentclass=memoir option. However, my document now fails to compile because of some command fragility issue I don't really understand :

Titles and section titles are converted to something in the lines of :

\title{\phantomsection%
This it the title%
\label{this-it-the-title}}


which article seems fine with, but which memoir chokes on (about 15 errors in the log file).

Is this an obvious but undocumented bug, or did I do something obviously wrong in the first place ?

Minimal (not)working example :

#!/bin/sh

rm -f test.*

cat >> test.txt <<EOF
This it the title
=================

this is dummy text
EOF

rst2latex-2.6.py -V
rst2latex-2.6.py --verbose --documentclass=memoir test.txt test.tex
pdflatex -interaction=nonstopmode test.tex


As for the output, I get out of it :

rst2latex-2.6.py (Docutils 0.9 [release], Python 2.6.8, on darwin)
This is pdfTeX, Version 3.1415926-2.3-1.40.12 (TeX Live 2011/MacPorts 2011_5)
...
...
!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on test.log.


In case it is useful, here is the complete (and incorrect) LateX document produced by docutils :

\documentclass[a4paper]{memoir}
% generated by Docutils <http://docutils.sourceforge.net/>
\usepackage{fixltx2e} % LaTeX patches, \textsubscript
\usepackage{cmap} % fix search and cut-and-paste in Acrobat
\usepackage{ifthen}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

%%% Custom LaTeX preamble
% PDF Standard Fonts
\usepackage{mathptmx} % Times
\usepackage[scaled=.90]{helvet}
\usepackage{courier}

%%% User specified packages and stylesheets

%%% Fallback definitions for Docutils-specific commands

\ifthenelse{\isundefined{\hypersetup}}{
\urlstyle{same} % normal text font (alternatives: tt, rm, sf)
}{}
\hypersetup{
pdftitle={This it the title},
}

%%% Title Data
\title{\phantomsection%
This it the title%
\label{this-it-the-title}}
\author{}
\date{}

%%% Body
\begin{document}
\maketitle

this is dummy text

\end{document}

-
Would you please attach also the generated .tex file? –  egreg Jun 7 '12 at 14:17
I suggest using Pandoc to process the reStructured text file. –  Sveinung Jun 8 '12 at 7:41
@Sveinung thanks heaps for telling me about pandoc. I've just dropped docutils for pandoc, and I think I won't need to look back ever (I'm not even going to report the "bug" to docutils :-) –  Gyom Jun 13 '12 at 10:23
@Gyom: Note that pandoc does not support all the features of ReST. –  Aditya Jun 13 '12 at 23:49

The problem is in the produced code

%%% Title Data
\title{\phantomsection%
This it the title%
\label{this-it-the-title}}


that works in article and other classes, but not in memoir because of untimely expansions.

A workaround would be persuading rst2latex to put \protect before \phantomsection and \label, but I really don't understand those commands inside \title should do.

-
I agree with your interpretation. My question is: why does docutils claim that it supports memoir when it generates such code (which clear purpose I don't understand either, btw) ? –  Gyom Jun 7 '12 at 15:24
Well the intention clearly is to make it possible to set a hyperlink to the title. As \title just saves its contents, it wouldn't be possible to put \phantomsection and \label outside its argument. I agree with egreg they need to be protected when put in such a place. –  Stephan Lehmke Jun 7 '12 at 15:38
@Gyom The question you pose should be directed to the developers of DocUtils. –  egreg Jun 7 '12 at 15:52
The implementation uses the same code to handle regular sectioning as well: chapters, sections and so on all get their \phantomsection and their \label. This does make some more sense, but yet it still upsets memoir. And now I wonder where I should report the bug: is that a memoir problem, or a docutils bug ? –  Gyom Jun 7 '12 at 15:53
@Gyom Definitely a problem with DocUtils. From what I can see in the primer documentation, section numbers are not automatically computed, so it would be much easier to set secnumdepth to -1000 and use \chapter, \section without *, \phantomsection and \addcontentsline. –  egreg Jun 7 '12 at 16:04

Gyom,

I suggest you the following answer, that explains that this instructions is needed when hyperref is used with section headers not included in the TOC (sectional commands with an star) or when you include manually a title in the TOC, to reference correctly the heading and not a different paragraph.

-
thanks for that. However I now use ''pandoc'' (as per suggested in the comments above) and am much happier with it than Rst. cheers. –  Gyom Feb 8 '13 at 8:53