7

How can I draw only parts of a rectangle? I want to omit a small part of the rectangle to put some text in. As I'm using various complex rectangles, I do not want to construct own "open rectangles".

Is there a way to do this? Maybe similarly to using an arc?


Currently, I'm using

\begin{tikzpicture}
        \draw[rounded corners=3pt] (-5,-5) rectangle (5,5);
        \node at (0,5) {text with line in the middle from rectangle};
\end{tikzpicture}

I have already tried

to clip the rectangle using an arc or leaving out some part of the rectangle using How can I invert a 'clip' selection within TikZ?, but both got too complex, fragile and did not work. There must be an easy way!?


Update:

Since there are other drawings beneath, using filling color beneath the text is no solution. The following example is a simple one, in other pictures there is even more "lively a background" behind the text... exemplary rectangles

DaveBall aka user750378
  • 1,098
  • 1
  • 12
  • 29

3 Answers3

2

Your question is not very clear.

Maybe the following code will help you:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  % first solution
  \node (label) at (0,5) {text with line in the middle from rectangle};
  \draw[rounded corners=3pt]
  (label) -| (-5,-5) -| (5,5) -- (label);

  % second solution (with filled rectangle and node)
  \node (label) at (0,4)
  {\phantom{text with line in the middle from rectangle}};
  \draw[rounded corners=3pt,fill=red!10]
  (label) -| (-4,-4) -| (4,4) -- (label);
  \node[draw,fill=lime!10,rounded corners=3pt] (label) at (0,4)
  {text with line in the middle from rectangle};
\end{tikzpicture}
\end{document}

enter image description here

Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283
  • I think (label) -| (-5,-5) -| (5,5) -- (label); would suffice for input labor. But I think the idea is to have a nonclosed rectangle as arc to a circle though I think best is to make a node shape. – percusse Sep 04 '12 at 21:10
  • Using `||' and '-|' is a good suggestion. But I update my answer. I don't understand your second sentence... – Paul Gaborit Sep 04 '12 at 21:14
  • 1
    I meant when a circle is not completed as a closed path it's an arc and there is a command for that. As far as I understand OP is looking for that analogy for the rectangle. – percusse Sep 04 '12 at 21:18
  • @PaulGaborit with your solution, using fill for the rectangle. the text will be partially covered; I think the OP wants some easy equivalent for something like the following code produces `\documentclass{article} \usepackage{tikz}

    \begin{document} \begin{tikzpicture} \node (label) at (0,5) {\phantom{text with line in the middle from rectangle}}; \draw[rounded corners=3pt,fill=red!20] (label) -| (-5,-5) -| (5,5) -- (label); \node at (0,5) {text with line in the middle from rectangle}; \end{tikzpicture} \end{document}`

    – Gonzalo Medina Sep 04 '12 at 21:26
  • Maybe... I add your suggestion. – Paul Gaborit Sep 04 '12 at 21:28
2

May be append after command option could help you. It allows to place all nodes and draw only some part of the border. It's only a partial solution because If you want to fill them you need to use several overlays. I hope somebody will provide a better solution.

In any case here you have an example:

\documentclass[tikz,border=3mm]{standalone}

\begin{document}
\begin{tikzpicture}
\draw node[label={[name=mylabel,label distance=-2mm]above:label},
    append after command={[rounded corners]
(b.north-|mylabel.west)-|(b.west)|-(b.south)-|(b.east)|-(b.north-|mylabel.east)},
] 
(b) {My first rectangle};

\draw node[minimum width=3cm,minimum height=3cm,align=center,
    append after command={(mylabel.north-|c.west)|-(c.north east)|-(c.south west)--(b.south-|c.west)},
] 
(c) at (2,1) {My second rectangle};
\end{tikzpicture}
\end{document}

enter image description here

Ignasi
  • 136,588
1

My final solution

I accepted Paul Gaborit suggestion and that I need to draw rectangles by hand, instead of using something like (0,0) rectangleArc (30:360:1cm). So I ended up with:

\documentclass[tikz,border=3mm]{standalone}

\begin{document}

\newcommand{\labeledRectangle}[7]{
        \node[text=black, opacity=0.45, anchor=west] (label) at (#2+0.01, #3-0.01) {#4};%
        \node[text=#1, anchor=west] at (#2,#3) {#4};%
        \draw[#1, rounded corners=3pt]
        (label) -| (#5,#6) -| (#7,#3) -- (label);
}         

\begin{tikzpicture}
  \labeledRectangle{red}{-.4}{2}{B\"uchi}{-1}{-3.2}{3.5}{2};
\end{tikzpicture}
\end{document}

The whole picture now looks like this:

enter image description here

DaveBall aka user750378
  • 1,098
  • 1
  • 12
  • 29