为Sphinx生成的PDF文件添加注释的背景颜色?

时间:2012-11-23 13:53:16

标签: latex python-sphinx restructuredtext

我可以使用Sphinx指令.. notes::生成笔记。但是,html文件中的注释具有背景颜色,而生成的PDF中的注释则没有。

如何为Sphinx生成的PDF文件添加颜色?

4 个答案:

答案 0 :(得分:6)

您可以在conf.py文件中添加类似的内容(有关LaTeX输出的选项,请参阅doc):

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}

这是一个基本的例子,它会改变所有告诫框的背景颜色(注意,警告,小费等)。

答案 1 :(得分:1)

我的解决方案的灵感来自Nicolas'回答。它建立在./_build/latex/sphinx.sty中的原始sphinx代码之上。与尼古拉斯相反。解决方案,这个保留原始的Sphinx布局(间距,框架等)并添加背景颜色。此外,它会不同地处理lightbox(注意,提示,提示,重要)和heavybox(警告,警告,注意,危险,错误)警告框样式。

conf.py文件中添加以下代码 (最终输出应该类似于this):

latex_custom = r'''
\definecolor{AdmonitionHeavyColor}{RGB}{255,204,204}
\definecolor{AdmonitionLightColor}{RGB}{238,238,238}

\makeatletter

  \renewcommand{\py@heavybox}{
    \setlength{\fboxrule}{1pt}
    \setlength{\fboxsep}{6pt}
    \setlength{\py@noticelength}{\linewidth}
    \addtolength{\py@noticelength}{-4\fboxsep}
    \addtolength{\py@noticelength}{-2\fboxrule}
    %\setlength{\shadowsize}{3pt}
    \Sbox
    \minipage{\py@noticelength}
  }

  \renewcommand{\py@endheavybox}{
    \endminipage
    \endSbox
    \savebox{\@tempboxa}{\fbox{\TheSbox}}
    \colorbox{AdmonitionHeavyColor}{\usebox{\@tempboxa}}
  }

  \renewcommand{\py@lightbox}{
    {%
      \setlength\parskip{0pt}\par
      \noindent\rule[0ex]{\linewidth}{0.5pt}%
      %\par\noindent\vspace{-0.2ex}%
    }
    \setlength{\py@noticelength}{\linewidth}
    \setlength{\fboxrule}{0pt}
    \setlength{\fboxsep}{2pt}
    %\setlength{\py@noticelength}{\linewidth}
    \addtolength{\py@noticelength}{-4\fboxsep}
    \addtolength{\py@noticelength}{-2\fboxrule}
    \Sbox
    \minipage{\py@noticelength}
  }

  \renewcommand{\py@endlightbox}{
    \endminipage
    \endSbox
    \savebox{\@tempboxa}{\fbox{\TheSbox}}
    \colorbox{AdmonitionLightColor}{\usebox{\@tempboxa}}
    {%
      \setlength{\parskip}{0pt}%
      \par\noindent\rule[0.5ex]{\linewidth}{0.5pt}%
      \par\vspace{-0.5ex}%
    }
  }


\makeatother
'''


latex_elements = {'preamble': latex_custom}

答案 2 :(得分:0)

在较新的版本中,您可以在sphinx_setup中使用latex_elements的{​​{1}}键,以很容易地实现 some 的建议,例如:

conf.py

将警告背景颜色更改为红色。查看documentation了解更多信息。

撰写latex_elements = { 'sphinxsetup': 'warningBgColor={RGB}{255,204,204}' } 似乎不是一种选择,因此这对OP中的内容没有帮助,但可能对其他建议有所帮助。

答案 3 :(得分:0)

如果不使用乳胶,则可以为不同的警告创建不同的彩色框。将此添加到conf.py

latex_custom = r'''
    \makeatletter
    \usepackage{ifthen}
    \usepackage{tcolorbox}
    \tcbuselibrary{skins}
    \renewenvironment{sphinxadmonition}[2]
    {
       %Green colored box for Conditions
       \ifthenelse{\equal{#2}{Conditions}}{
                         \medskip
                         \begin{tcolorbox}[before={}, enhanced, colback=green!10, 
                                           colframe=green!65!black,fonttitle=\bfseries,
                                           title=\sphinxstrong{#2}, arc=0mm, drop fuzzy shadow=blue!50!black!50!white]}{

           %Blue colored box for Notes
           \ifthenelse{\equal{#2}{Note:}}{
                             \medskip
                             \begin{tcolorbox}[before={}, enhanced, colback=blue!5!white, 
                                               colframe=blue!75!black,fonttitle=\bfseries,
                                               title=\sphinxstrong{#2}, arc=0mm, drop fuzzy shadow=blue!50!black!50!white]}{
               %Orange colored box for Warnings 
               \ifthenelse{\equal{#2}{Warning:}}{
                             \medskip
                             \begin{tcolorbox}[before={}, enhanced, colback=orange!5!white, 
                                               colframe=orange!75!black,fonttitle=\bfseries,
                                               title=\sphinxstrong{#2}, arc=0mm, drop fuzzy shadow=blue!50!black!50!white]}{

                   %Red colored box for everthing else
                   \medskip
                   \begin{tcolorbox}[before={}, enhanced, colback=red!5!white, 
                                 colframe=red!75!black, fonttitle=\bfseries,
                                 title=\sphinxstrong{#2}, arc=0mm, drop fuzzy shadow=blue!50!black!50!white]}
           }
       }
    }
    {
       \end{tcolorbox}\par\bigskip
    } 

    \makeatother
'''

latex_elements['preamble'] += latex_custom

输出类似enter image description here enter image description here