2

I want to include part of the following file, foo.py, using \inputminted in a listing environment.

def foo(a, b):
    return a + b

def bar(a, b)
    return a - b

However, if this listing environment occurs in some other environment that increases the indentation of the left margin, \inputminted does not seem to respect this.

(Note that you'll need to save the contents of the first code block as foo.py in the same directory as this MWE in order for it to compile.)

\documentclass{article}
\usepackage{minted}

\begin{document}
\begin{enumerate}

\item

  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  Suspendisse euismod, orci ac iaculis posuere, felis augue ultricies augue.

  \begin{listing}[H]
    \inputminted[firstline=1,lastline=2]{python}{foo.py}
    \caption{The function foo}
  \end{listing}

  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  Suspendisse euismod, orci ac iaculis posuere, felis augue ultricies augue. 

  \begin{listing}[H]
    \begin{minted}{python}
    def foo(a, b):
        return a + b
    \end{minted}
    \caption{The function foo}
  \end{listing}

  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  Suspendisse euismod, orci ac iaculis posuere, felis augue ultricies augue.  

\end{enumerate}
\end{document}

Picture showing the result of compiling the MWE provided here

I believe this is related to Centering \inputminted inside a figure?, but I'm not totally sure what the solution is. According to the fancyvrb documentation, resetmargins is false by default, so the margin should not have been reset, but it nonetheless appears to have been reset. How can I make sure that the margin respects other indented environments when using \inputminted inside of a listing environment? That is, I want to be able to use \inputminted but have the indentation of the code block that I get for the second listing environment when just using \begin{minted}...\end{minted}.

Adam Liter
  • 12,567
  • Please, where can I find foo.py? Your code have an error: Package minted Error: Missing Pygments output; \inputminted was [...ed[firstline=1,lastline=2]{python}{foo.py}]. – Sebastiano Oct 26 '19 at 20:13
  • 1
    @Sebastiano Sorry, the first code block was intended to be saved as foo.py. I'll edit the question to make that clearer. – Adam Liter Oct 26 '19 at 20:37
  • There's no need, at least for me, to apologize to me. I just wanted to help you :-) – Sebastiano Oct 26 '19 at 20:40

1 Answers1

3

Use a different approach with \captionof:

\begin{filecontents*}{\jobname.py}
def foo(a, b):
    return a + b

def bar(a, b)
    return a - b
\end{filecontents*}

\documentclass{article}
\usepackage{minted}
\usepackage{caption}

\usepackage{showframe}% just for the example

\newenvironment{pseudolisting}
 {\begin{minipage}{\linewidth}\vspace*{\topsep}}
 {\vspace*{\topsep}\end{minipage}}

\begin{document}

Some text before to check the spacing
\begin{listing}[H]
  \inputminted[firstline=1,lastline=2]{python}{\jobname.py}
  \caption{The function foo}
\end{listing}
Some text after to check the spacing


\begin{enumerate}

\item Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Suspendisse euismod, orci ac iaculis posuere, felis augue ultricies augue.

\begin{pseudolisting}
\inputminted[firstline=1,lastline=2]{python}{\jobname.py}
\captionof{listing}{The function foo}
\end{pseudolisting}

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Suspendisse euismod, orci ac iaculis posuere, felis augue ultricies augue. 

\begin{pseudolisting}
\begin{minted}{python}
def foo(a, b):
    return a + b
\end{minted}
\captionof{listing}{The function foo}
\end{pseudolisting}

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Suspendisse euismod, orci ac iaculis posuere, felis augue ultricies augue.  

\end{enumerate}

\end{document}

enter image description here

Production notes. I used showframe to see the page margins; the file is saved as \jobname.py just not to clobber my files. You may want to change \topsep to something else.

egreg
  • 1,121,712
  • +1 So I take it the underlying issue has to do with how the Verbatim environment interacts with floats? Anyway, I'm happy with this solution, so I've gone ahead and accepted the answer. Thanks! – Adam Liter Oct 28 '19 at 15:37