5

I'm trying to create a c++ class hierarchy. Each node in the hierarchy is a class description with code listing in it and multiple nodes are connected by lines and arrows depicting the hierarchy. I'm trying to create the node using tcolorbox with code listing in it and encompass the box in tikz node so that I can draw lines between nodes.

So I created CodeNode environment, and trying to call node related stuff in the environment. The MWE is here

\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}

I'm getting error in 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}

What am I missing here ?

  • Have you tried loading tcolorbox with option all: \usepackage[all]{tcolorbox} and compile the file with -shell-escape? – Herr K. Dec 23 '13 at 3:21
  • I did. I still get the same error. – Surya Dec 23 '13 at 3:38
6

The main problem seems to be the capture of the verbatim text in interaction with tikz and environ.

My solution proposal skips the environ package, which is a great tool, but is not needed here. The CodeNode options are saved by mynodeoptions and mytcboptions. I've added mandatory parameters like node name and position to the options. You can easily adapt my given example settings to your own needs.

\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}

enter image description here

  • This works fine. But I've one more request, this is about tcolorbox. The tcbinputlisting is, if i'm correct, based on tcolorbox and not tcbox (which shrink fits the contents). To make the listing box shrink fit, I'm using /tcb/width option, but this is not accurate. I do not find any macro for listing box based on tcbox. Is one available ? – Surya Dec 23 '13 at 12:50
  • Got it. It is /tcb/capture=hbox – Surya Dec 23 '13 at 12:54
  • @Surya Yes, exactly :-) – Thomas F. Sturm Dec 23 '13 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.