4

I would like to decorate the top edge of this shaded square with rounded corners by a little circle. (Hopefully, I would use the decor defined below. My trials and errors got me errors only.)

\usepackage{tikz}
\usetikzlibrary{positioning, math, decorations.markings,calc,shapes.misc}
\usepgfmodule{decorations}

\begin{document}

\begin{tikzpicture} \draw[fill=gray, rounded corners, decoration={markings,mark=at position .5 with {\draw[black, fill=white] circle[radius=2pt];}}] (0,0) -- (0,1) -- (1,1) NEED LITTLE CIRCLE HERE -- (1,0)-- cycle; \end{tikzpicture}

\end{document}

Adam
  • 791

2 Answers2

6

A simple solution with using of node in the border where needed:

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

\begin{document} \begin{tikzpicture} \draw[fill=gray, rounded corners] (0,0) -- (0,1) -- node[circle, draw, fill=white, inner sep=1.5pt] {} (1,1) -- (1,0) -- cycle; \end{tikzpicture} \end{document}

enter image description here

Addendum: Instead nodes with selected shapes, you can also use pic which can contain arrows etc. For example:

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\begin{document} \begin{tikzpicture}[ arr/.pic = {\draw[-{Straight Barb[width=3pt]}] (-1pt,0) -- (1pt,0);} ] \draw[fill=gray, rounded corners] (0,0) -- (0,1) -- pic {arr} (1,1) -- (1,0) -- cycle; \end{tikzpicture} \end{document}

enter image description here

Zarko
  • 296,517
  • This is great! Frankly, I realized that I need to decorate another edge with a simple arrowhead and perhaps that will be easier with your solution. However, replacing circle with arrow or \arrow{>} does not work. Would you know how to make it work? – Adam Feb 04 '21 at 21:08
  • Proposed solution is simple with nodes with selected shapes or contents. For using of drawing elements as are arrows, the possible solution is to draw pic images and used it similarly as nodes. – Zarko Feb 04 '21 at 21:27
  • @Adam, see addendum to answer. – Zarko Feb 04 '21 at 22:11
5

The answer to "Decorating an edge of a cycle in tikz" is to use edge[decorate].

enter image description here

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{decorations.markings}

\begin{document}

\begin{tikzpicture} \draw[fill=gray, rounded corners, decoration={markings,mark=at position .5 with {\draw[black, fill=white] circle[radius=2pt];}}] (0,0) -- (0,1) edge[decorate] (1,1) -- (1,1) -- (1,0)-- cycle; \end{tikzpicture}
\end{document}

Here is an extended comment since there is another post which points out what had been pointed out several times already, e.g. here: you can use a pic for the arrow. I'd like to complement the statement by adding that you need the sloped and allow upside down keys to make that work properly.

enter image description here

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[circ/.style={circle, draw, fill=white, inner sep=1.5pt},
    pics/arr/.style={code={\draw[-{Stealth[length=#1,fill=black]}] (-#1/2,0) -- 
    (#1/2,0); }},pics/arr/.default=1ex]
 % https://tex.stackexchange.com/a/430486   
 \draw[fill=gray, rounded corners]
        (0,0) -- (0,1) -- node[circ]{} (1,1) --
        pic[sloped,allow upside down]{arr} (1,0) -- 
        pic[sloped,allow upside down]{arr=1.2ex} cycle;
\end{tikzpicture}
\end{document}
  • @Pumucki: Thanks! I tried something similar, but I didn't realize that I need to repeat (1,1) twice. – Adam Feb 04 '21 at 20:39
  • @Adam Yes, that's a property of edge, after the edge the path will continue where it was before the edge. –  Feb 04 '21 at 20:44
  • Sorry to say this, but this site sucks. I do not understand the voting behavior here at all. There is an interesting question, and I think that I answered it correctly. Note that decorations can be much more than just decorations.markings. Then there pops up another answer, which copies things from someone else, which adds something that works only for specific decorations.markings stuff, and is incomplete. At the time of writing my first post I was well aware of the possibility, but dismissed it because it does not answer the general question. Yet users upvote this plagiate like crazy. –  Feb 05 '21 at 17:07
  • So I decided to delete my account. Have fun with answers that do not answer the question, are copied from somewhere else without attribution, and are incomplete. –  Feb 05 '21 at 17:08