Search code examples
pythonlatexpython-sphinxrestructuredtextdocutils

Prevent escaping of special characters in LaTeX output


By default the LaTeX output generated by Sphinx escapes the LaTeX markup. So if I write:

Normal text

.. only:: latex

   \textbf{latex only text}

After a make latexpdf call it will be

Normal text

textbf\{latex only text\}

in LaTeX. So all chars are escaped, and it is not a LaTeX code anymore. Is there a way to pass LaTeX code directly to the tex source generated by Sphinx?

MWE is to do a sphinx-quickstart accepting defaults, then inputing the above ReST code somewhere in index.rst and finally building the thing with make latexpdf (make.bat latexpdf on Windows).


Solution

  • What you want is the raw directive:

    .. raw:: latex
    
       \textbf{latex only text}
    

    If you only want this text in LaTeX output, you will need to combine it with the only directive.