
Jupyter Notebook의 코드와 유사한 코드를 표시하기 위해 다음 솔루션을 사용하고 있습니다. 나는 여기에서 그것을 얻었다:목록이 포함된 IPython 노트북 셀
가능성을 추가할 수 있는 방법이 있나요?캡션 및 라벨상자에?
최소 작업 예:
\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}
답변1
와 함께 옵션을 사용할 수 있습니다 blend into
(이 경우 listings
정의를 에 넣어야 함을 의미합니다. 이유는 매뉴얼을 \AtBeginDocument{...}
참조하세요 ) . 해당 플로트 카운터를 선택하고 이에 따라 제목을 설정합니다.tcolorbox
title
blend into
이러한 코드 변경으로
\AtBeginDocument{
\newtcblisting[blend into=listings]{ipythonnb}[2][\theipythcntr]{
title=#2,
enlarge left by=\inwd,
...
}
}
그리고 글쓰기
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}
우리는 얻는다
캡션을 제목 대신 외부에 표시하려면 예를 들어 다음을 사용할 수 있습니다.comment above* listing
대신에listing only
분리된 제목과 함께 :
\AtBeginDocument{
\newtcblisting[blend into=listings]{ipythonnb}[2][\theipythcntr]{
title=#2,
detach title,
coltitle=black,
comment above* listing,
comment=\centering\tcbtitle,
enlarge left by=\inwd,
...
}
}
코드를 다음과 같이 변경하면
\AtBeginDocument{
\newtcblisting[blend into=listings]{ipythonnb}[2][lst:\thelstlisting]{
label=#1,
title=#2,
...
{\stepcounter{ipythcntr}In [\theipythcntr]:};
...
}
}
그리고 유형
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}
라벨을 사용할 수도 있습니다.
마지막 예제의 전체 코드는 다음과 같습니다.
\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}