16

I want to draw this thing many a time:

\node[circle,draw,thick,inner sep=0pt,minimum size=2ex] (sum) [left = of myobject] {};
\draw[semithick] (sum.north west) -- (sum.south east);
\draw[semithick] (sum.south west) -- (sum.north east);
\fill (sum.center) -- (sum.south west) arc (-135:-45:1ex) -- cycle;

Is it possible to make a one command that will execute all this code? And [left = of myobject] should be a variable.

0
14

New TiKZ 3.0 pics can help you:

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning}

\tikzset{mysymbol/.pic={
    \node[circle,draw,thick,inner sep=0pt, minimum size=2ex] (-sum) {};
    \draw[semithick] (-sum.north west)--(-sum.south east);
    \draw[semithick] (-sum.south west)--(-sum.north east);
    \fill (-sum.center)--(-sum.south west) arc (-135:-35):1ex)--cycle;
}}

\begin{document}
\begin{tikzpicture}

\node (a) {A};

\path pic (symbol_a) at ([xshift=1cm]a) {mysymbol};

\pic[above right=5mm and 8mm of a, red] (symbol_b) {mysymbol};

\pic[left=of symbol_b-sum, blue] {mysymbol};
\end{tikzpicture}

\end{document}

enter image description here

Some pic examples here:

Update: Include filled sectors as parameter

Next code show one possibility of including which sectors will be filled as a parameter to the pic definition.

To use parameters with a pic a new syntax is needed, the original mysymbol/.pic changes to pics/mysymbol/.style={code={.... This is enough with only one parameters but it's also possible to use 2 (pics/mysymbol/.style 2 args={code={...) or more (pics/mysymbol/.style n args={n}{code={...). Take a look at TikZ 3.0---Multiple arguments for `pic` for some other examples.

In this case you can pass a sector list (from 0 to 3, 0=east sector, 1=north, ...).

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning}

\tikzset{
    pics/mysymbol/.style={
        code={
    \node[circle,draw,thick,inner sep=0pt, minimum size=2ex] (-sum) {};
    \draw[semithick] (-sum.north west)--(-sum.south east);
    \draw[semithick] (-sum.south west)--(-sum.north east);
    \foreach \i in {#1}
    \fill[rotate=90*\i] (-sum.center)--++(-45:1ex) arc[start angle=-45,delta angle=90,radius=1ex]--cycle;
}}} 

\begin{document}
\begin{tikzpicture}
\node (a) {A};

\path pic (symbol_a) at ([xshift=1cm]a) {mysymbol={2,0}};

\pic[above right=5mm and 8mm of a, red] (symbol_b) {mysymbol={0}};

\pic[left=of symbol_b-sum, blue] {mysymbol={0,2,3}};
\end{tikzpicture}

\end{document}

enter image description here

2
  • How can I parametrize the sector that will be filled? Or several sectors.
    – dm-kiselev
    Apr 23 '14 at 15:43
  • 1
    @ignasi thank you so much for this brilliant and extensive example. I learned so much from it.
    – Jan
    Apr 3 '18 at 9:48
6

Based on your question, [left = of myobject] is translated into 3 arguments [#1 = #2 of #3]

The user-defined command mydraw is to draw the repeated lines, which takes a style mylocation that takes #1 = above, below, left or right, #2 = x unit and #3 = myobject. Here A=myobject in this example.

enter image description here

Code

\documentclass[border=10pt]{standalone}
%\usepackage[margin=1cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc,positioning}

\tikzset{mylocation/.style args={#1 and  #2 of #3}{
#1 = #2 of #3}
}

\newcommand\mydraw[3]{
\node[circle,draw,thick,inner sep=0pt,minimum size=2ex] (sum) [mylocation= #1 and #2 of #3] {};
\draw[semithick] (sum.north west) -- (sum.south east);
\draw[semithick] (sum.south west) -- (sum.north east);
\fill (sum.center) -- (sum.south west) arc (-135:-45:1ex) -- cycle;
}


\begin{document}
\begin{tikzpicture}
\node(A) at (0,0) {A};

\mydraw{above}{2}{A}
\mydraw{left}{2}{A}
\mydraw{right}{2}{A}
\mydraw{below}{2}{A}
\end{tikzpicture}
\end{document}
0

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.