Search code examples
latexpython-sphinxrestructuredtext

Adding background color of notes to Sphinx-generated PDF files?


I am able to generate notes using the Sphinx directive .. notes::. However, the notes in html file have a background color while those from the generated PDF don’t.

How can I add color to Sphinx-generated PDF files?


Solution

  • You can add something like this in your conf.py file (see the doc for the options for the LaTeX output):

    latex_custom = r'''
    \definecolor{Admonition}{RGB}{221,233,239}
    
    \makeatletter
      \newenvironment{admonitionbox}{
        \begin{lrbox}{\@tempboxa}\begin{minipage}{\columnwidth}
      }{
        \end{minipage}\end{lrbox}
        \colorbox{Admonition}{\usebox{\@tempboxa}}
      }
    
      \renewenvironment{notice}[2]{
        \begin{admonitionbox}
      }{
        \end{admonitionbox}
      }
    \makeatother
    '''
    
    latex_elements = {'preamble': latex_custom}
    

    This is a basic example, it will change the background color of all the admonitions boxes (note, warning, tip, etc.).