Sphinx Verbatim(程式碼區塊)與背景顏色

Sphinx Verbatim(程式碼區塊)與背景顏色

我想刪除框架並為我的程式碼區塊設定淺灰色陰影背景。

我知道我可以使用colorbox例如\begin{tcolorbox},但我不知道要把它放在哪裡。

Sphinx 使用此環境:

\newenvironment{sphinxVerbatim}{%
  % first, let's check if there is a caption
  \ifx\sphinxVerbatimTitle\empty
      \sphinxvspacefixafterfrenchlists
      \parskip\z@skip
      \vskip\sphinxverbatimsmallskipamount
      % there was no caption. Check if nevertheless a label was set.
      \ifx\sphinxLiteralBlockLabel\empty\else
      % we require some space to be sure hyperlink target from \phantomsection
      % will not be separated from upcoming verbatim by a page break
          \needspace{\sphinxliteralblockwithoutcaptionneedspace}%
          \phantomsection\sphinxLiteralBlockLabel
      \fi
  \else
     \parskip\z@skip
     \if t\spx@opt@literalblockcappos
       \vskip\spx@abovecaptionskip
       \def\sphinxVerbatim@Before
           {\sphinxVerbatim@Title\nointerlineskip
            \kern\dimexpr-\dp\strutbox+\sphinxbelowcaptionspace
                 % if no frame (code-blocks inside table cells), remove
                 % the "verbatimsep" whitespace from the top (better visually)
                 \ifspx@opt@verbatimwithframe\else-\sphinxverbatimsep\fi
                 % caption package adds \abovecaptionskip vspace, remove it
                 \spx@ifcaptionpackage{-\abovecaptionskip}{}\relax}%
     \else
       \vskip\sphinxverbatimsmallskipamount
       \def\sphinxVerbatim@After
          {\nointerlineskip\kern\dimexpr\dp\strutbox
            \ifspx@opt@verbatimwithframe\else-\sphinxverbatimsep\fi
            \spx@ifcaptionpackage{-\abovecaptionskip}{}\relax
           \sphinxVerbatim@Title}%
     \fi
     \def\@captype{literalblock}%
     \capstart
     % \sphinxVerbatimTitle must reset color
     \setbox\sphinxVerbatim@TitleBox
            \hbox{\begin{minipage}{\linewidth}%
     % caption package may detect wrongly if top or bottom, so we help it
                    \spx@ifcaptionpackage
                      {\caption@setposition{\spx@opt@literalblockcappos}}{}%
                    \sphinxVerbatimTitle
                  \end{minipage}}%
  \fi
  \global\let\sphinxLiteralBlockLabel\empty
  \global\let\sphinxVerbatimTitle\empty
  \fboxsep\sphinxverbatimsep \fboxrule\sphinxverbatimborder
  \ifspx@opt@verbatimwithframe\else\fboxrule\z@\fi
  \let\FrameCommand     \sphinxVerbatim@FrameCommand
  \let\FirstFrameCommand\sphinxVerbatim@FirstFrameCommand
  \let\MidFrameCommand  \sphinxVerbatim@MidFrameCommand
  \let\LastFrameCommand \sphinxVerbatim@LastFrameCommand
  \ifspx@opt@verbatimhintsturnover\else
      \let\sphinxVerbatim@Continued\@empty
      \let\sphinxVerbatim@Continues\@empty
  \fi
  \ifspx@opt@verbatimwrapslines
  % fancyvrb's Verbatim puts each input line in (unbreakable) horizontal boxes.
  % This customization wraps each line from the input in a \vtop, thus
  % allowing it to wrap and display on two or more lines in the latex output.
  %     - The codeline counter will be increased only once.
  %     - The wrapped material will not break across pages, it is impossible
  %       to achieve this without extensive rewrite of fancyvrb.
  %     - The (not used in sphinx) obeytabs option to Verbatim is
  %       broken by this change (showtabs and tabspace work).
    \let\sphinxVerbatimFormatLine\sphinxVerbatimFormatLineWrap
    \let\FV@Space\spx@verbatim@space
  % Allow breaks at special characters using \PYG... macros.
    \sphinxbreaksatspecials
  % Breaks at punctuation characters . , ; ? ! and / (needs catcode activation)
    \fvset{codes*=\sphinxbreaksviaactive}%
  \else % end of conditional code for wrapping long code lines
    \let\sphinxVerbatimFormatLine\sphinxVerbatimFormatLineNoWrap
  \fi
  \let\FancyVerbFormatLine\sphinxFancyVerbFormatLine
  \VerbatimEnvironment
  % workaround to fancyvrb's check of current list depth
  \def\@toodeep {\advance\@listdepth\@ne}%
  % The list environment is needed to control perfectly the vertical space.
  % Note: \OuterFrameSep used by framed.sty is later set to \topsep hence 0pt.
  % - if caption: distance from last text baseline to caption baseline is
  %   A+(B-F)+\ht\strutbox, A = \abovecaptionskip (default 10pt), B =
  %   \baselineskip, F is the framed.sty \FrameHeightAdjust macro, default 6pt.
  %   Formula valid for F < 10pt.
  % - distance of baseline of caption to top of frame is like for tables:
  %   \sphinxbelowcaptionspace (=0.5\baselineskip)
  % - if no caption: distance of last text baseline to code frame is S+(B-F),
  %   with S = \sphinxverbatimtopskip (=\smallskip)
  % - and distance from bottom of frame to next text baseline is
  %   \baselineskip+\parskip.
  % The \trivlist is used to avoid possible "too deeply nested" error.
  \itemsep   \z@skip
  \topsep    \z@skip
  \partopsep \z@skip
  % trivlist will set \parsep to \parskip = zero
  % \leftmargin will be set to zero by trivlist
  \rightmargin\z@
  \parindent  \z@% becomes \itemindent. Default zero, but perhaps overwritten.
  \trivlist\item\relax
     \ifsphinxverbatimwithminipage\spx@inframedtrue\fi
     % use a minipage if we are already inside a framed environment
     \ifspx@inframed\noindent\begin{minipage}{\linewidth}\fi
     \MakeFramed {% adapted over from framed.sty's snugshade environment
        \advance\hsize-\width\@totalleftmargin\z@\linewidth\hsize\@setminipage
        }%
     % For grid placement from \strut's in \FancyVerbFormatLine
     \lineskip\z@skip
     % active comma should not be overwritten by \@noligs
     \ifspx@opt@verbatimwrapslines
       \let\verbatim@nolig@list \sphinx@verbatim@nolig@list
     \fi
     % will fetch its optional arguments if any
     \OriginalVerbatim
}

答案1

看起來您應該能夠透過 Sphinx 配置來編輯程式碼區塊的框架和背景顏色,而不是亂搞 TeX 檔案。這個連結有一些關於這方面的好資訊:

https://www.sphinx-doc.org/en/master/latex.html

具體來說,「verbatimwithframe」、「VerbatimColor」和「VerbatimBorderColor」元素似乎與您的問題相關。

編輯:

經過一番嘗試後,添加:

# conf.py
latex_elements = {
   "sphinxsetup": 'verbatimwithframe=false, VerbatimColor={gray}{0.95}',
}

進入Sphinx 項目中latex_elementsconf.py文件欄位將允許您更改逐字塊的背景顏色。

相關內容