6

I'm super new to tikzpicture in LaTex. However here is my minimal working example:

Minimal Working Example

I basically have a graph and would like to group together both "cliques" in the graph. In other words, I want to group together (A,B) and (B,C,D). I'm not sure how to do it in a single graph (if you could do that, it would be awesome, maybe using different colors). So I ended up creating two copies of the same graph. On the left hand side I can use a rectangle to group together A and B. However, I don't know how to create a triangle to gather together B, C and D. At the moment it's a rectangle and it's grouping together also A, which I don't want.

\documentclass[hidelinks, a4paper]{article}

\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{fit}
\usetikzlibrary{backgrounds}
\usepackage{tkz-graph}

\title{Grouping Nodes with triangle backrground}

\author{\vspace{-5ex}}

\date{\vspace{-5ex}}

\begin{document}
\maketitle

\begin{center}
    \begin{tikzpicture}
        \tikzstyle{background}=[rectangle,
                                                    fill=gray!10,
                                                    inner sep=0.2cm,
                                                    rounded corners=5mm]
        \SetGraphUnit{2}
        \GraphInit[vstyle=Normal]
        \Vertex{A} \EA(A){B} \SO(A){C} \SOEA(A){D} \Edge(A)(C) \Edge(B)(C) \Edge(C)(D) \Edge(B)(D)

        \begin{pgfonlayer}{background}
        \node [background,
                    fit=(A) (C),
                    label=left:Clique 1] {};
        \end{pgfonlayer}
    \end{tikzpicture} \qquad\qquad %second picture
    \begin{tikzpicture}
        \tikzstyle{background}=[rectangle,
                                                    fill=gray!10,
                                                    inner sep=0.2cm,
                                                    rounded corners=5mm]
        \SetGraphUnit{2}
        \GraphInit[vstyle=Normal]
        \Vertex{A} \EA(A){B} \SO(A){C} \SOEA(A){D} \Edge(A)(C) \Edge(B)(C) \Edge(C)(D) \Edge(B)(D)


        \begin{pgfonlayer}{background}
        \node [background,
                    fit=(B) (C) (D),
                    label=left:Clique 2] {};
        \end{pgfonlayer}
    \end{tikzpicture}
\end{center}


\end{document}

enter image description here

  • Also, if there's an easier solution using sort of "ellipses" that group together nodes, that would also be appreciated. Basically I would like some sort of shape that gathers nodes, and being able to specify a color for it – Euler_Salter Nov 22 at 17:20
  • 1
    Hi, welcome. Please add a full compileable code. – AndréC Nov 22 at 17:28
  • @AndréC Thank you! I think this should compile now – Euler_Salter Nov 22 at 17:30
  • @AndréC True, I've just added it! – Euler_Salter Nov 22 at 17:34
  • @AndréC do you have any idea on how to go about drawing such a grouping? – Euler_Salter Nov 22 at 17:43
4

There is a plain way to draw. Options line width, line cap=round,line join=round are just good to be together in this situation. No need to use fit library.

enter image description here

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[n/.style={circle,draw,minimum size=5mm}]
\def\a{2}
\path
(0,0)    node[n] (A) {A}
(\a,0)   node[n] (B) {B}
(0,-\a)  node[n] (C) {C}
(\a,-\a) node[n] (D) {D};
\draw (A)--(C) node[midway,left=5mm]{Clique 1}
--(D)--(B)--(C);
\begin{pgfonlayer}{background}
\draw[cyan!30,line width=12mm,line cap=round] (A.center)--(C.center);
\end{pgfonlayer}

\begin{scope}[xshift=5.5cm]
\path
(0,0)    node[n] (A) {A}
(\a,0)   node[n] (B) {B}
(0,-\a)  node[n] (C) {C}
(\a,-\a) node[n] (D) {D};

\draw (A)--(C) node[midway,left=5mm]{Clique 2}
--(D)--(B)--(C);
\begin{pgfonlayer}{background}
\draw[cyan!30,fill=cyan!30,line width=12mm,line cap=round,line join=round] (B.center)--(C.center)--(D.center)--cycle;
\end{pgfonlayer}
\end{scope}
\end{tikzpicture}
\end{document}

Update: A friend suggested that figure can be drawn without using backgrounds library. Here is it.

enter image description here

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,
n/.style={circle,draw,minimum size=5mm}]
\def\a{2} \colorlet{mau}{lime}
\path
(0,0)    coordinate (A)
(\a,0)   coordinate (B)
(0,-\a)  coordinate (C)
(\a,-\a) coordinate (D);

% left part
\draw[mau,line width=12mm] (A)--(C);
\foreach \p in {A,B,C,D} \path (\p) node[n] (L\p) {\p};
\draw (LC)--(LD)--(LB)--(LC)--(LA) node[midway,left=6mm]{Clique 1};

% right part
\draw[mau,fill=mau,line width=12mm,transform canvas={shift={(0:5.5)}}] 
(B)--(C)--(D)--cycle;
\foreach \p in {A,B,C,D} \path ([shift={(0:5.5)}]\p) node[n] (R\p) {\p};
\draw (RC)--(RD)--(RB)--(RC)--(RA) node[midway,left=6mm]{Clique 2};
\end{tikzpicture}
\end{document}
  • Well, I also widened the line @AndréC, the difference being that I compute the line width from the fits of the nodes instead of hard coding it. This is what \draw[gray!10] let \p1=($(fB.north)-(fB.center)$) in [line width=2*\y1,line join=round] (B.center) -- (C.center) -- (D.center) -- cycle; in my answer does. If the nodes get bigger, my solution adjusts its size. – Schrödinger's cat Nov 22 at 19:05
  • I added an update without using background library. – Black Mild Nov 23 at 16:24
6

In this example it is simple: just fit the nodes in circles of an appropriate size, and connect them, and fill the area.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{fit,calc}
\usetikzlibrary{backgrounds}
\usepackage{tkz-graph}
\begin{document}
\begin{center}
    \begin{tikzpicture}[background/.style={rectangle,
                                                    fill=gray!10,
                                                    inner sep=0.2cm,
                                                    rounded corners=5mm}]

        \SetGraphUnit{2}
        \GraphInit[vstyle=Normal]
        \Vertex{A} \EA(A){B} \SO(A){C} \SOEA(A){D} \Edge(A)(C) \Edge(B)(C) \Edge(C)(D) \Edge(B)(D)

        \begin{pgfonlayer}{background}
        \node [background,
                    fit=(A) (C),
                    label=left:Clique 1] {};
        \end{pgfonlayer}
    \end{tikzpicture} \qquad\qquad %second picture
    \begin{tikzpicture}[background/.style={rectangle,fill=gray!10,
        inner sep=0.2cm,rounded corners=5mm},
        cfit/.style={fill=gray!10,circle,inner sep={0.05cm},fit=#1}]
        \SetGraphUnit{2}
        \GraphInit[vstyle=Normal]
        \Vertex{A} \EA(A){B} \SO(A){C} \SOEA(A){D} \Edge(A)(C) \Edge(B)(C) \Edge(C)(D) \Edge(B)(D)


        \begin{pgfonlayer}{background}
        \foreach \X in {B,C,D}
        {\node[cfit=(\X)](f\X){};}
        \draw[gray!10]  let \p1=($(fB.north)-(fB.center)$)
        in  [line width=2*\y1,line join=round] (B.center) -- (C.center) -- (D.center) -- cycle; 
        \end{pgfonlayer}
    \end{tikzpicture}
\end{center}
\end{document}

enter image description here

Note that there are advanced solutions around, I will try to dig them out later. Note also that \tikzstyle is deprecated.

  • Thank you! How do I put the label Clique 2 for the second graph? – Euler_Salter Nov 22 at 18:46
  • 1
    @Euler_Salter Just add \path (fC.west) -- (fC.west|-A.center) node[midway,left]{Clique 1};. BTW, my solution adjusts its size if the nodes change their size, which is why I used fit. – Schrödinger's cat Nov 22 at 19:09
3

Just for fun with PSTricks.

http://tug.org/PSTricks/main.cgi?file=pst-node/nodes

\documentclass[border=10pt,pstricks,12pt]{standalone}
\usepackage{pst-node}
\begin{document}
    \begin{pspicture}[showgrid](2,1)(7,5)
    \pnodes(3,2){C}(6,4){B}(6,2){D}(3,4){A}
    %%%
    \psline[linewidth=1cm,linecap=1,linecolor=blue!70](A)(C)
    \pspolygon[fillstyle=solid, fillcolor=green,linewidth=.1cm,
                   linecolor=red!70,linearc=.5,  opacity=0.7]% linecolor=green!70
    (!\psGetNodeCenter{C} C.x 1.61 sub C.y .47 sub)%
    (!\psGetNodeCenter{B} B.x .47 add B.y .9 add)%
    (!\psGetNodeCenter{D} D.x .47 add D.y .47 sub)
    %%%
    \rput(C){\circlenode{C}{C}}
    \rput(B){\circlenode{B}{B}}
    \rput(D){\circlenode{D}{D}}
    \rput(A){\circlenode{A}{A}}
    \ncline{A}{C}
    \ncline{B}{D}
    \ncline{C}{D}
    \ncline{B}{C}
    \end{pspicture}
\end{document}

enter image description here

Or

\documentclass[border=10pt,pstricks,12pt]{standalone}
\usepackage{pst-node}
\begin{document}
    \begin{pspicture}[showgrid](2,1)(7,5)
    \pnodes(3,2){C}(6,4){B}(6,2){D}(3,4){A}
    %%%
        \psframe[fillstyle=solid,fillcolor=blue,linewidth=.1cm,
                   linecolor=cyan!70, framearc=.9,  opacity=0.7]
    (!\psGetNodeCenter{C} C.x .5 sub C.y .5 sub)%
    (!\psGetNodeCenter{A} A.x .5 add A.y .5 add)%
    %%%
    \pspolygon[fillstyle=solid, fillcolor=green,linewidth=.1cm,
                   linecolor=red!70,linearc=.5,  opacity=0.7]% linecolor=red!70
    (!\psGetNodeCenter{C} C.x 1.61 sub C.y .47 sub)%
    (!\psGetNodeCenter{B} B.x .47 add B.y .9 add)%
    (!\psGetNodeCenter{D} D.x .47 add D.y .47 sub)
    %%%
    \rput(C){\circlenode{C}{C}}
    \rput(B){\circlenode{B}{B}}
    \rput(D){\circlenode{D}{D}}
    \rput(A){\circlenode{A}{A}}
    \ncline{A}{C}
    \ncline{B}{D}
    \ncline{C}{D}
    \ncline{B}{C}
    \end{pspicture}
\end{document}

enter image description here

No problem if one don't care about it (See in the image)

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.