목록 환경을 갖춘 tikz 노드

목록 환경을 갖춘 tikz 노드

C++ 클래스 계층 구조를 만들려고 합니다. 계층 구조의 각 노드는 코드 목록이 포함된 클래스 설명이며 여러 노드는 계층 구조를 나타내는 선과 화살표로 연결됩니다. 코드 목록을 사용하여 노드를 만들고 노드 사이에 선을 그릴 수 있도록 tcolorbox상자를 포함 하려고 합니다.tikz node

그래서 CodeNode 환경을 만들고, node그 환경에서 관련된 것들을 호출해 보았습니다. MWE가 여기 있습니다

\documentclass{book}

\usepackage{tcolorbox}
\usepackage{tikz}
\usepackage{environ}

\tcbuselibrary{listings}


\NewEnviron{CodeNode}{
  \node{%
    \begin{minipage}{0.8\textwidth}
      \begin{tcbwritetemp}
        {\BODY}
      \end{tcbwritetemp}
      \tcbox[arc=0pt,outer arc=0pt,top=1mm,bottom=1mm,left=1mm,right=1mm,
             boxrule=0.6pt,title=#1]{\tcbusetemplisting}
    \end{minipage}
  };
}

\begin{document}

\begin{figure}
  \begin{tcolorbox}
   \begin{tikzpicture}
     \begin{CodeNode}[class name]
       virtual void draw() = 0;
       virtual void some_other_function() = 0;
     \end{CodeNode}
   \end{tikzpicture}
  \end{tcolorbox}
\end{figure}

\end{document}

tcbwritetemp에서 오류가 발생했습니다.

Library (tcolorbox): 'tcblistingscore.code.tex' version '2.60'
)) (./mwe.aux) ABD: EveryShipout initializing macros
! Argument of \next has an extra }.
<inserted text> 
                \par 
l.30      \end{CodeNode}

내가 여기서 무엇을 놓치고 있습니까?

답변1

tikz주요 문제는 및 과 의 상호 작용에서 텍스트를 그대로 캡처하는 것 같습니다 environ.

내 솔루션 제안에서는 environ훌륭한 도구이지만 여기서는 필요하지 않은 패키지를 건너뜁니다. 옵션 은 및 에 CodeNode의해 저장됩니다 . 옵션에 노드 이름, 위치 등 필수 매개변수를 추가했습니다. 주어진 예제 설정을 자신의 필요에 맞게 쉽게 조정할 수 있습니다.mynodeoptionsmytcboptions

\documentclass{book}

\usepackage{tcolorbox}
\usepackage{tikz}

\tcbuselibrary{listings}

\newenvironment{CodeNode}[4][]{
  \tikzset{mynodeoptions/.style={at={(#2)},name=#3,#1}}%
  \tcbset{mytcboptions/.style={title=#4}}%
  \tcboutputlisting%
}{\endtcboutputlisting%
  \node[inner sep=0pt,outer sep=0pt,draw=none,fill=none,mynodeoptions]{%
    \tcbinputlisting{listing only,width=0.8\textwidth,colback=white,
      arc=0pt,outer arc=0pt,top=1mm,bottom=1mm,left=1mm,right=1mm,
      boxrule=0.6pt,mytcboptions}};%
}

\begin{document}

\begin{figure}
\begin{tcolorbox}[center upper,colframe=blue!50!black,colback=blue!5!white]
\begin{tikzpicture}
  \begin{CodeNode}{0,2}{anode}{class name}
virtual void draw() = 0;
virtual void some_other_function() = 0;
  \end{CodeNode}
  \begin{CodeNode}{0,-2}{bnode}{other class name}
virtual void other_draw() = 0;
virtual void yet_some_other_function() = 0;
  \end{CodeNode}
  \draw[red,very thick,->] (anode)--(bnode);
  \draw[red,very thick,->] (anode.east)-- ++(1,0);
  \draw[red,very thick,->] (anode.west)-- ++(-1,0);
\end{tikzpicture}
\end{tcolorbox}
\end{figure}

\end{document}

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

관련 정보