¿Agregar títulos y etiquetas a la lista personalizada de tcolorbox?

¿Agregar títulos y etiquetas a la lista personalizada de tcolorbox?

Estoy usando la siguiente solución para mostrar el código que debería parecerse al código de Jupyter Notebook. Lo saqué de aquí:Celdas de IPython Notebook con listados

El resultado se ve así:https://i.stack.imgur.com/C18GA.png (Fuente de la publicación vinculada)

¿Hay alguna manera de agregar la posibilidad desubtítulos y etiquetasa las cajas?

Ejemplo de trabajo mínimo:

\documentclass{article}
\usepackage{listings}

\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}

\definecolor{light-gray}{gray}{0.95}


\newlength\inwd
\setlength\inwd{1.3cm}

\newcounter{ipythcntr}

\newtcblisting{ipythonnb}[1][\theipythcntr]{
    enlarge left by=\inwd,
    width=\linewidth-\inwd,
    enhanced,
    boxrule=0.4pt,
    colback=light-gray,
    listing only,
    top=0pt,
    bottom=0pt,
    overlay={
        \node[
        anchor=north east,
        text width=\inwd,
        font=\footnotesize\ttfamily\color{blue!50!black},
        inner ysep=2mm,
        inner xsep=0pt,
        outer sep=0pt
        ] 
        at (frame.north west)
        {\stepcounter{ipythcntr}In [#1]:};
    }
    listing options={
        basicstyle=\footnotesize\ttfamily,
        language=python,
        escapechar=¢,
        showstringspaces=false,
    },
}

\lstset{numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt}


\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Here are two IPython cells:
\begin{ipythonnb}
n = 10
\end{ipythonnb}

\begin{ipythonnb}
for i in range(n):
    print('i = ', i)
\end{ipythonnb}

\begin{ipythonnb}[13]
n = 10
\end{ipythonnb}

\end{document}

Respuesta1

Podrías usar la blend intoopción (en el caso de listingsesto también significa que tienes que poner la definición en \AtBeginDocument{...}, consulta el tcolorboxmanual para conocer los motivos) junto con title. blend intorecoge el contador flotante correspondiente y establece el título en consecuencia.

Con estos cambios en tu código

\AtBeginDocument{
  \newtcblisting[blend into=listings]{ipythonnb}[2][\theipythcntr]{
    title=#2,
    enlarge left by=\inwd,
    ...
  }
}

y escribiendo

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.

Here are two IPython cells:
\begin{ipythonnb}{My caption}
n = 10
\end{ipythonnb}

\begin{ipythonnb}{Another caption}
for i in range(n):
    print('i = ', i)
\end{ipythonnb}

\begin{ipythonnb}[13]{And another one}
n = 10
\end{ipythonnb}

obtenemos

ingrese la descripción de la imagen aquí


Si desea que el título esté afuera en lugar de ser un título, puede usar, por ejemplocomment above* listing en cambiode listing onlyjunto con un título separado:

\AtBeginDocument{
  \newtcblisting[blend into=listings]{ipythonnb}[2][\theipythcntr]{
    title=#2,
    detach title,
    coltitle=black,
    comment above* listing,
    comment=\centering\tcbtitle,
    enlarge left by=\inwd,
    ...
  }
}

ingrese la descripción de la imagen aquí


Si cambiamos el código a

\AtBeginDocument{
  \newtcblisting[blend into=listings]{ipythonnb}[2][lst:\thelstlisting]{
    label=#1,
    title=#2,
    ...
    {\stepcounter{ipythcntr}In [\theipythcntr]:};
    ...
  }
}

y escribe

Here are two IPython cells:
\begin{ipythonnb}{My caption}
n = 10
\end{ipythonnb}

\begin{ipythonnb}[lst:another]{Another caption}
for i in range(n):
    print('i = ', i)
\end{ipythonnb}

\setcounter{ipythcntr}{12}
\begin{ipythonnb}{And another one}
n = 10
\end{ipythonnb}

see listing~\ref{lst:another}

También podemos usar etiquetas:

ingrese la descripción de la imagen aquí


El código completo para el último ejemplo:

\documentclass{article}
\usepackage{listings}

\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}

\definecolor{light-gray}{gray}{0.95}


\newlength\inwd
\setlength\inwd{1.3cm}

\newcounter{ipythcntr}

\AtBeginDocument{
  \newtcblisting[blend into=listings]{ipythonnb}[2][lst:\thelstlisting]{
    label=#1,
    title=#2,
    detach title,
    coltitle=black,
    comment above* listing,
    comment=\centering\tcbtitle,
    enlarge left by=\inwd,
    width=\linewidth-\inwd,
    enhanced,
    boxrule=0.4pt,
    colback=light-gray,
    top=0pt,
    bottom=0pt,
    overlay={
        \node[
        anchor=north east,
        text width=\inwd,
        font=\footnotesize\ttfamily\color{blue!50!black},
        inner ysep=2mm,
        inner xsep=0pt,
        outer sep=0pt
        ] 
        at (frame.north west)
        {\stepcounter{ipythcntr}In [\theipythcntr]:};
    }
    listing options={
        basicstyle=\footnotesize\ttfamily,
        language=python,
        escapechar=¢,
        showstringspaces=false
    }
  }
}
\lstset{numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt}


\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.

Here are two IPython cells:
\begin{ipythonnb}{My caption}
n = 10
\end{ipythonnb}

\begin{ipythonnb}[lst:another]{Another caption}
for i in range(n):
    print('i = ', i)
\end{ipythonnb}

\setcounter{ipythcntr}{12}
\begin{ipythonnb}{And another one}
n = 10
\end{ipythonnb}

see listing~\ref{lst:another}

\end{document}

información relacionada