具有列表環境的 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}

在此輸入影像描述

相關內容