5

This question is related to Draw curly braces in TikZ.

Is there a way of making the solution more general for when you wish to use node names to define the end points? To illustrate, I've made a slight change to the code given by @Jake on the earlier question. In my figure below, the brace on the left is drawn to the nodes I've specified (rather than coordinates), but the result is drawn to touch the boxes. I would prefer to have a small space so that the brace doesn't touch the boxes, however, the xshift and yshift options don't seem to have any effect on how the brace is drawn. Is there a way of modifying the brace so that it looks more like the one on the right of the figure (has a small space between brace and boxes), without having to specify additional nodes to enforce the space I require?

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}
\begin{tikzpicture}[scale=1]
\draw[thick] (-1,0) rectangle +(6,7.5);
\filldraw[thick, top color=white,bottom color=red!50!] (0.5,0) rectangle node{$N_S$} +(1.3,0.5);
\filldraw[thick, top color=white,bottom color=red!50!] (2.2,0) rectangle node{$N_L$} +(1.3,0.65);
\filldraw[top color=white,bottom color=blue!50!] (0.5,0.5) rectangle node{$P_{1}$} +(1.3,0.5);
\filldraw[top color=white,bottom color=blue!50!] (2.2,0.65) rectangle node{$P_{2}$} +(1.3,1.0);
\filldraw[top color=white,bottom color=blue!50!] (0.5,1.0) rectangle node{$P_{3}$} +(1.3,1.5);
\filldraw[top color=white,bottom color=blue!50!] (2.2,1.65) rectangle node{$P_{4}$} +(1.3,2.0);
\filldraw[top color=white,bottom color=blue!50!] (0.5,2.5) rectangle node{$P_{5}$} +(1.3,2.5);
\filldraw[top color=white,bottom color=blue!50!] (2.2,3.65) rectangle node{$P_{6}$} +(1.3,3.0);

% defining nodes at the points that are the ends of the left brace on the figure
\node[draw] (TopLeftPoint) at (0.5,5.0){box 1};
\node[draw] (BottomLeftPoint) at (0.5,0.5){box 2};

%changing xshift and yshift values on next line doesn't seem to affect the brace at all
\draw [decorate,decoration={brace,amplitude=10pt},xshift=-400pt,yshift=1000pt]
(BottomLeftPoint.west) -- (TopLeftPoint.west) node [black,midway,xshift=-0.6cm] 
{\footnotesize $P_1^*$};
\draw [decorate,decoration={brace,amplitude=10pt,mirror,raise=4pt},yshift=0pt]
(3.5,0.65) -- (3.5,6.5) node [black,midway,xshift=0.8cm] {\footnotesize
$P_2$};
\end{tikzpicture}
\end{document}

enter image description here

Jason Whyte
  • 1,408
  • @percusse Thanks for being nice about my faux pas. It took a little while to remember how to relate to past questions. – Jason Whyte Nov 13 '13 at 23:41
  • Try anchoring the brace at the west anchor of the nodes: (BottomLeftPoint.west) -- (TopLeftPoint.west) – Herr K. Nov 14 '13 at 00:08
  • @KevinC Hi, good point and it works in this simple case where the node is an empty point. This doesn't work in my more complex motivating example where the node has a rectangle with text around it and putting node.west still draws the brace against the west side of the rectangle. – Jason Whyte Nov 14 '13 at 00:17
  • I will make minor edits to the question to show the problem more clearly and generally. – Jason Whyte Nov 14 '13 at 00:23
  • @HarishKumar No, all that does is change the point at which the brace meets the box. It has no effect on the spacing. I think the failure of xshift and yshift to have any effect must mean that something special is happening…. – Jason Whyte Nov 14 '13 at 00:34
  • What about ($(node.south west) + (-.3,0)$) with \usetikzlibrary{calc} –  Nov 14 '13 at 00:39
  • @HarishKumar Thanks, this does fix the problem in one sense. I was hoping that manual fiddling wouldn't be necessary, that something like xshift would be able to move the whole brace. However, a working solution is a good solution! – Jason Whyte Nov 14 '13 at 00:46
  • xshift works. See my answer. :-) –  Nov 14 '13 at 00:49
  • 1
    @HarishKumar adjusting your fix which uses coordinates to use a node name, e.g. ([xshift=-.2cm]BottomLeftPoint.west) then this works nicely. However, I'm still puzzled as to why the xshift that occurs in the first brace drawing command in my example seems to have no effect whatsoever. It would be much easier to use one command to move the whole brace, especially in my case where I'm drawing a brace between two boxes of different widths. – Jason Whyte Nov 14 '13 at 00:59
  • Where did you use xshift? –  Nov 14 '13 at 01:04
  • @HarishKumar xshift is used as you suggest. This does the job: \draw [decorate,decoration={brace,amplitude=10pt},xshift=-400pt,yshift=1000pt] ([xshift=-.2cm]BottomLeftPoint.center) -- ([xshift=-.2cm]TopLeftPoint.center) node [black,midway,xshift=-0.6cm] {\footnotesize $P_1^*$}; I'm now drawing the brace relative to the centre of the two nodes, rather than the left edge of the rectangle. As long as the xshift value is the same in each case, the brace will be as desired. The xshift command before the specification of the brace ends seems to make no difference to the brace. – Jason Whyte Nov 14 '13 at 01:11

2 Answers2

7

Thanks to input from @HKumar here's a fix that uses two xshift commands - one xshift for each of the centres of the nodes which are joined by the brace. It still would be nice to know why one xshift doesn't move the whole brace as experimentation with the first xshift value in the \draw [decorate,decoration= below seems to have no effect.

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{decorations.pathreplacing,calc}

\begin{document}
\begin{tikzpicture}[scale=1]
\draw[thick] (-1,0) rectangle +(6,7.5);
\filldraw[thick, top color=white,bottom color=red!50!] (0.5,0) rectangle node{$N_S$} +(1.3,0.5);
\filldraw[thick, top color=white,bottom color=red!50!] (2.2,0) rectangle node{$N_L$} +(1.3,0.65);
\filldraw[top color=white,bottom color=blue!50!] (0.5,0.5) rectangle node{$P_{1}$} +(1.3,0.5);
\filldraw[top color=white,bottom color=blue!50!] (2.2,0.65) rectangle node{$P_{2}$} +(1.3,1.0);
\filldraw[top color=white,bottom color=blue!50!] (0.5,1.0) rectangle node{$P_{3}$} +(1.3,1.5);
\filldraw[top color=white,bottom color=blue!50!] (2.2,1.65) rectangle node{$P_{4}$} +(1.3,2.0);
\filldraw[top color=white,bottom color=blue!50!] (0.5,2.5) rectangle node{$P_{5}$} +(1.3,2.5);
\filldraw[top color=white,bottom color=blue!50!] (2.2,3.65) rectangle node{$P_{6}$} +(1.3,3.0);

% defining nodes at the points that are the ends of the left brace on the figure
\node[draw] (TopLeftPoint) at (0.5,5.0){box1};
\node[draw] (BottomLeftPoint) at (0.5,0.5){box2};

%changing xshift and yshift values on next line doesn't seem to affect the brace at all

\draw [decorate,decoration={brace,amplitude=10pt},xshift=-400pt,yshift=1000pt]
([xshift=-.7cm]BottomLeftPoint.center)   -- ([xshift=-.7cm]TopLeftPoint.center) node [black,midway,xshift=-0.6cm] 
{\footnotesize $P_1^*$};


\draw [decorate,decoration={brace,amplitude=10pt,mirror,raise=4pt},yshift=0pt]
(3.5,0.65) -- (3.5,6.5) node [black,midway,xshift=0.8cm] {\footnotesize
$P_2$};

\end{tikzpicture}
\end{document}

enter image description here

Jason Whyte
  • 1,408
3

May be I don't understand, but xshift works for me.

\draw [decorate,decoration={brace,amplitude=10pt},xshift=-0.5cm,yshift=0pt]
(0.5,0.5) -- (0.5,5.0) node [black,midway,xshift=-0.6cm]
{\footnotesize $P_1$};

Full code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}
\begin{tikzpicture}[scale=1]
\draw[thick] (-1,0) rectangle +(6,7.5);
\filldraw[thick, top color=white,bottom color=red!50!] (0.5,0) rectangle node{$N_S$} +(1.3,0.5);
\filldraw[thick, top color=white,bottom color=red!50!] (2.2,0) rectangle node{$N_L$} +(1.3,0.65);
\filldraw[top color=white,bottom color=blue!50!] (0.5,0.5) rectangle node{$P_{1}$} +(1.3,0.5);
\filldraw[top color=white,bottom color=blue!50!] (2.2,0.65) rectangle node{$P_{2}$} +(1.3,1.0);
\filldraw[top color=white,bottom color=blue!50!] (0.5,1.0) rectangle node{$P_{3}$} +(1.3,1.5);
\filldraw[top color=white,bottom color=blue!50!] (2.2,1.65) rectangle node{$P_{4}$} +(1.3,2.0);
\filldraw[top color=white,bottom color=blue!50!] (0.5,2.5) rectangle node{$P_{5}$} +(1.3,2.5);
\filldraw[top color=white,bottom color=blue!50!] (2.2,3.65) rectangle node{$P_{6}$} +(1.3,3.0);
\draw [decorate,decoration={brace,amplitude=10pt},xshift=-0.5cm,yshift=0pt]
(0.5,0.5) -- (0.5,5.0) node [black,midway,xshift=-0.6cm]
{\footnotesize $P_1$};
\draw [decorate,decoration={brace,amplitude=10pt,mirror,raise=4pt},yshift=0pt]
(3.5,0.65) -- (3.5,6.5) node [black,midway,xshift=0.8cm] {\footnotesize
$P_2$};
\end{tikzpicture}
\end{document}

enter image description here