Creating minimal LaTeX syntax from reStructuredText with docutils

I am creating a Python script that takes multiple text files with reStructuredText syntax and creates one LaTeX file with Docutils. Everything works great except that Docutils creates a lot of extra syntax that I don't need.

For example, with a simple subsection Docutils would write

\subsection*{\phantomsection%
About%
\addcontentsline{toc}{subsection}{About}%
\label{about}%

      

when I only need

\subsection{About}

      

I saw that Pandoc doesn't generate as much extra syntax, but it doesn't support CSV tables, so I can't use it for my project.

I have looked through all docutils settings and I cannot find any options to limit the output. Is there anyway I've configured Docutils to only generate the syntax that I want?

+3


source to share


1 answer


In addition to my comment, it should be possible to subclass docutils.writers.latex2e.Writer

and docutils.writers.latex2e.LaTeXTranslator

to customize the docutils LaTeX entry output in a manner similar to this in the blog post, which describes how to customize the HTML author. However, looking docutils.writers.latex2e.LaTeXTranslator

at it looks a lot more complicated than an HTML writer.

An alternative method would be to simply change these classes. To achieve the result, you just want to do the following (note that this is for docutils 0.8.1):



  • Catalog backup path/to/docutils/writers/latex2e

  • Change path/to/docutils/writers/latex2e/__init__.py

    as follows

    • in method LaTeXTranslator.visit_title

      replace string (line 2870)

      pdfanchor = '\\phantomsection%\n  '
      
            

      from

      pdfanchor = ''
      
            

    • in method LaTeXTranslator.visit_title

      replace string (line 2878)

      self.context.append(self.bookmark(node) + '}\n')
      
            

      from

      self.context.append('}\n')
      
            

Note . It is much better to subclass if possible docutils.writers.latex2e.Writer

and docutils.writers.latex2e.LaTeXTranslator

so that you can use any changes made to these classes in future versions of docutils. The above method works, but it may need to be changed in future versions.

+2


source







All Articles