
私は 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
によって保存されます。オプションには、ノード名や位置などの必須パラメータを追加しました。私の提供したサンプル設定は、自分のニーズに合わせて簡単に調整できます。mynodeoptions
mytcboptions
\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}