`tikzpicture`와 `lslisting` 결합

`tikzpicture`와 `lslisting` 결합

lstlisting저는 TikZ를 사용하여 프레임과 제목을 추가하는 사용자 정의 환경에서 작업하고 있습니다 . 널리 사용되는 중괄호 때문에 를 node사용할 수 없으므로 newenvironment를 사용해야 합니다 NewEnviron.

MWE와 아래 그림을 보면 내가 달성하려는 목표가 무엇인지 분명해집니다.

\documentclass[a4paper,11pt]{article}

\usepackage{listings}
\lstset{breaklines=true, postbreak={\mbox{$\hookrightarrow\space$}}, language=Mathematica}

\usepackage{tikz}
\usetikzlibrary{shapes,decorations}

\tikzstyle{boxround} = [draw=black, thick, rectangle, rounded corners, inner sep=10pt, inner ysep=20pt]
\tikzstyle{head} =[draw=black, fill=white, thick, rectangle]

\usepackage{environ}

\NewEnviron{code}{%
\bigskip
\noindent\begin{tikzpicture}
\node [boxround] (box){%
\begin{minipage}{.94\textwidth}
\BODY
\end{minipage}};
\node[head, right=10pt] at (box.north west) {\textbf{Code listing}};
\end{tikzpicture}
\bigskip
}

\begin{document}

\begin{code}
How to get some \textsc{Mathematica} code (shown below) in this box...
\end{code}

\footnotesize
\begin{lstlisting}
B11[x_, y_] = Piecewise[{{1, 0 <= x <= 1 && 0 <= y <= 1}}];
Plot3D[B11[x, y], {x, -.5, 1.5}, {y, -.5, 1.5}, PlotRange -> Full]; 
\end{lstlisting}
\normalsize

\end{document}

그 결과

여기에 이미지 설명을 입력하세요

가 아니었다면 tikzpicture그냥 을 사용했을 것입니다 lstnewenvironment. 그러나 널리 퍼져 있는 중괄호를 고려하면 node어떻게 할 수 있는지 모르겠습니다. 어떤 아이디어가 있나요?

답변1

다음과 같은 제안이 있습니다 mdframed.

\documentclass{article}

\usepackage{libertine}
\usepackage[T1]{fontenc}
\usepackage{listings}
\lstset{basicstyle=\footnotesize\ttfamily,breaklines=true, postbreak={\mbox{$\hookrightarrow\space$}}, language=Mathematica}

\usepackage[framemethod=TikZ]{mdframed}

\mdfdefinestyle{lstlisting}{%
 innertopmargin=5pt,
 middlelinewidth=1pt,
 outerlinewidth=9pt,outerlinecolor=white,
 innerleftmargin=10pt,
 innerrightmargin=10pt,
 leftmargin=-9pt,rightmargin=-9pt,
 skipabove=\topskip,
 skipbelow=\topskip,
 roundcorner=5pt,
 singleextra={\node[draw, fill=white,anchor=west, xshift=10pt+1pt,font=\bfseries] at (O|-P) {Code Listings};},
 firstextra={\node[draw, fill=white,anchor=west, xshift=10pt+1pt,font=\bfseries] at (O|-P) {Code Listings};}
}

\lstnewenvironment{code}{%
  \mdframed[style=lstlisting]%
}{\endmdframed}

\begin{document}
Text
\begin{code}
B11[x_, y_] = Piecewise[{{1, 0 <= x <= 1 && 0 <= y <= 1}}];
Plot3D[B11[x, y], {x, -.5, 1.5}, {y, -.5, 1.5}, PlotRange -> Full]; 
\end{code}
\end{document}

여기에 이미지 설명을 입력하세요


업데이트

아래 예에는 code하나의 선택 인수와 하나의 필수 인수가 있는 새 환경이라는 이름이 있습니다. 선택적 인수가 전달되고 \lstset필수 인수는 제목입니다.

\documentclass{article}

\usepackage{libertine}
\usepackage[T1]{fontenc}
\usepackage{listings}
\lstset{basicstyle=\footnotesize\ttfamily,breaklines=true, postbreak={\mbox{$\hookrightarrow\space$}}, language=Mathematica}

\usepackage[framemethod=TikZ]{mdframed}

\makeatletter
\def\mdf@@codeheading{Code Listings}%new mdframed key:
\define@key{mdf}{title}{%
   \def\mdf@@codeheading{#1}
}

\mdfdefinestyle{lstlisting}{%
 innertopmargin=5pt,
 middlelinewidth=1pt,
 outerlinewidth=9pt,outerlinecolor=white,
 innerleftmargin=10pt,
 innerrightmargin=10pt,
 leftmargin=-9pt,rightmargin=-9pt,
 skipabove=\topskip,
 skipbelow=\topskip,
 roundcorner=5pt,
 singleextra={\node[draw, fill=white,anchor=west, xshift=10pt+1pt,font=\bfseries] at (O|-P) {\csname mdf@@codeheading\endcsname};},
 firstextra={\node[draw, fill=white,anchor=west, xshift=10pt+1pt,font=\bfseries] at (O|-P) {\csname mdf@@codeheading\endcsname};}
}

\lstnewenvironment{code}[2][]{%
  \lstset{#1}%
  \mdframed[style=lstlisting,title={#2}]%
}{\endmdframed}

\begin{document}
Text

\begin{code}{Mathematice Listings}
B11[x_, y_] = Piecewise[{{1, 0 <= x <= 1 && 0 <= y <= 1}}];
Plot3D[B11[x, y], {x, -.5, 1.5}, {y, -.5, 1.5}, PlotRange -> Full]; 
\end{code}

Text

\begin{code}{Code Listings}
B11[x_, y_] = Piecewise[{{1, 0 <= x <= 1 && 0 <= y <= 1}}];
Plot3D[B11[x, y], {x, -.5, 1.5}, {y, -.5, 1.5}, PlotRange -> Full]; 
\end{code}
\end{document}

답변2

여기에 다음을 사용하는 제안이 있습니다.tcolorbox

\documentclass{article}
\usepackage{libertine}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{tcolorbox}
\tcbuselibrary{listings}
\tcbuselibrary{skins}

\newenvironment{code}{%
  \tcblisting{listing only,colback=white,enlarge top by=5.5mm,enhanced,%
     overlay={\node[draw,fill=white,xshift=10pt,right,font=\bfseries] at (frame.north west) {Code Listings};},%
     listing options={basicstyle=\footnotesize\ttfamily,breaklines=true,%
                      postbreak={\mbox{$\hookrightarrow\space$}},language=Mathematica},%
  }%
 }%
 {\endtcblisting}

\begin{document}
\begin{code}
B11[x_, y_] = Piecewise[{{1, 0 <= x <= 1 && 0 <= y <= 1}}];
Plot3D[B11[x, y], {x, -.5, 1.5}, {y, -.5, 1.5}, PlotRange -> Full]; 
\end{code}

\end{document}

여기에 이미지 설명을 입력하세요

관련 정보