2

I am trying to present some code as follows:

\documentclass[11pt]{beamer}
\usepackage{tcolorbox}
\tcbuselibrary{listings,skins}
\lstdefinestyle{compactstyle}{
    numbers=left, 
    numberstyle=\small, 
    numbersep=8pt, 
    language=Python,
    basicstyle=\tiny
}
\newtcblisting{compactlisting}[2][]{
    arc=0pt, outer arc=0pt,
    listing only, 
    listing style=compactstyle,
    title=#2,
    #1
}
\usepackage{listings,newtxtt}
\lstset{basicstyle=\ttfamily, keywordstyle=\bfseries}
\begin{document}
\begin{frame}[fragile]
    \frametitle{Title}  
    \begin{compactlisting}[hbox]{}
def peak_recursive(A):
    n = len(A)
    if n == 1:
        return 0
    if n == 2:
        return 0 if A[0]>=A[1] else 1
    if A[n//2]>=A[n//2 + 1] and A[n//2]>=A[n//2 - 1]:
        return n//2
    elif A[n//2 - 1]>=A[n//2]:
        return peak_recursive(A[0:n//2])
    else:
        return n//2+1+peak_recursive(A[n//2+1:])
    \end{compactlisting}
\end{frame}
\end{document}

However the numbers on the left overlap the enclosing box:

listings

How can one fix this?

(As a secondary question, is it possible to control how far the tab indents so that line 2 is less indented, for example?)

1 Answer 1

5
You can set the margin with tcolorbox keys:

\documentclass[11pt]{beamer}
\usepackage{tcolorbox}
\tcbuselibrary{listings,skins}
\lstdefinestyle{compactstyle}{
    numbers=left,
    numberstyle=\small,
    numbersep=8pt,
    language=Python,
    basicstyle=\tiny,
}
\newtcblisting{compactlisting}[2][]{
    arc=0pt, outer arc=0pt,
    listing only,
    listing style=compactstyle,
    title=#2,
    left=1cm,%<------------ adapt
    #1
}
\usepackage{listings,newtxtt}
\lstset{basicstyle=\ttfamily, keywordstyle=\bfseries}
\begin{document}
\begin{frame}[fragile]
    \frametitle{Title}
    \begin{compactlisting}[hbox]{}
def peak_recursive(A):
    n = len(A)
    if n == 1:
        return 0
    if n == 2:
        return 0 if A[0]>=A[1] else 1
    if A[n//2]>=A[n//2 + 1] and A[n//2]>=A[n//2 - 1]:
        return n//2
    elif A[n//2 - 1]>=A[n//2]:
        return peak_recursive(A[0:n//2])
    else:
        return n//2+1+peak_recursive(A[n//2+1:])
    \end{compactlisting}
\end{frame}
\end{document}

enter image description here

1
  • Thank you very much!
    – Simd
    Aug 14, 2019 at 10:32

You must log in to answer this question.

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