I'm working on a piece of software that outputs IPython Notebooks as tex documents. One of my requirements is to be able to draw code input output blocks that look like the Notebook's UI.

Disclaimer:
For a long time I was using MDFRAMED to draw the cells. Unfortunately the vertical spacing of the MDFRAMED differs between versions, and I absolutely cannot force users to use a specific version of MDFRAMED. I looked into distributing MDFRAMED along with the software and found that it could not be done without modifications to MDFRAMED

So I bit the bullet and started writing my own custom framing logic in TIKz. My only requirements were to have rounded edges, a small border, support for multiple pages, and support for pygments output (fancyvrb Verbatim).

I can't run you code but I'd be suspicious of def\notebookcellframe#1{ and similar definitions missing a % after the { which will typically offset the contents by one word space.
– David CarlisleSep 3 '13 at 1:10

@DavidCarlisle That helped! It removed the horizontal offset however I still have a vertical offset in my output, I'll upload a new screen capture.
– JonathanSep 3 '13 at 1:14

1

is it not possible to create a reproducible example (that preferably doesn't require running sphinx (I don't know if you can capture the tex it generates?)
– David CarlisleSep 3 '13 at 1:17

The sphinx latex code itself is very complex. The best I could do is package the sphinx dependencies in a zip and upload that somewhere. I'm almost certain I would hit some sort of character limit trying to upload all of the sphinx code in my post.
– JonathanSep 3 '13 at 1:23

I don't see the definition for addmargin but I assume the content starts in vertical mode.

The \needsspace test uses the outer (larger) value of baselineskip.

\smaller is a font change so changes the font size (immediately) and the baselineskip
will change immediately, although the effective value that will be used for text depends on whether the paragraph ends in the current scope (ie the definition of \endaddmargin)

\smmaller is

\def\smaller{\fontsize{9.5pt}{9.5pt}\selectfont}

which sets 9.5pt font to a 9.5pt target baseline so normally this is not possible and so \baselineskip in fact will not be used and \lineskip will be used instead, normally the baseline space is around 10-12% larger than the font size.

Assuming addmargin does start in vertical mode then

\vspace{-1\baselineskip}%
\vspace{-1\borderthickness}%

will now add -ve space (using the 9.5pt for \baselineskip)/ If addmargin starts in horizontal mode then these spaces will be held in a \vadjust node and added following the next line of text. You may way to put \par in front of them to make sure you are in vmode.

Then the text is set in the small font then

\vspace{-1\baselineskip}%

is interpreted in horizontal mode so held in a vadjust node until the paragraph ends, presumably in the definition of \enddaddmargin somewhere. Again you may want to put \par before the \vskip.

If you are getting different layout around these spaces the causes are probably one definition of addmargin or notebookcell

adds a box or write node or special of some kind at the start

or

redefines things so that addmargin starts in horizontal mode

or

redefines things so there is an extra group layer and so the group ends before the paragraph of small text, so it is set with the original larger baseline.

The main remaining cause of your issue is that Sphinx Verbatim uses itself framed.sty and framed.sty induces vertical spaces which your code did not at all try to control. It is quite complicated if furthermore the framed is started in horizontal mode. I gave a shot to your problem with current Sphinx.

Removing all the various vertical spaces accumulated in each use of the notebookcell environments, plus drawing the In[1]: without entering paragraph mode (and without using TikZ which was confusing there), there only remains a \smallskip which is always inserted by Sphinx on top of frame. Then we use a conditional to tell the verbatim it is already inside a frame environment (it then must be done inside a minipage); this conditional is only in recent versions of Sphinx (there was no reason for it in older times as no Verbatim would end up inside another Verbatim... but more recent Sphinx uses framed for other things).