I would like to remove the frames and set a light grey shade background to my code blocks.

I know I could use colorbox such as \begin{tcolorbox}, but I have no idea where to place it.

Sphinx uses this environment:

  % first, let's check if there is a caption
      % there was no caption. Check if nevertheless a label was set.
      % we require some space to be sure hyperlink target from \phantomsection
      % will not be separated from upcoming verbatim by a page break
     \if t\spx@opt@literalblockcappos
                 % if no frame (code-blocks inside table cells), remove
                 % the "verbatimsep" whitespace from the top (better visually)
                 % caption package adds \abovecaptionskip vspace, remove it
     % \sphinxVerbatimTitle must reset color
     % caption package may detect wrongly if top or bottom, so we help it
  \fboxsep\sphinxverbatimsep \fboxrule\sphinxverbatimborder
  \let\FrameCommand     \sphinxVerbatim@FrameCommand
  \let\MidFrameCommand  \sphinxVerbatim@MidFrameCommand
  \let\LastFrameCommand \sphinxVerbatim@LastFrameCommand
  % 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).
  % Allow breaks at special characters using \PYG... macros.
  % Breaks at punctuation characters . , ; ? ! and / (needs catcode activation)
  \else % end of conditional code for wrapping long code lines
  % 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
  \parindent  \z@% becomes \itemindent. Default zero, but perhaps overwritten.
     % use a minipage if we are already inside a framed environment
     \MakeFramed {% adapted over from framed.sty's snugshade environment
     % For grid placement from \strut's in \FancyVerbFormatLine
     % active comma should not be overwritten by \@noligs
       \let\verbatim@nolig@list \sphinx@verbatim@nolig@list
     % will fetch its optional arguments if any

1 Answer 1


It looks like you should be able to edit the frames and background color of code blocks through Sphinx configuration rather than messing around with the TeX files. This link has some good information about this:


Specifically, the "verbatimwithframe," the "VerbatimColor," and the "VerbatimBorderColor" elements seem to be relevant to your issue.


After playing around with this a little bit, adding:

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

into the latex_elements field of your conf.py file in the Sphinx project will allow you to change the background color of verbatim blocks.

  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jul 5, 2022 at 15:41
  • 1
    I updated according to this suggestion, works perfect. Updated the answer with complete code sample.
    – larrycai
    Commented Aug 12, 2023 at 15:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .