6

I've got the following code:

\documentclass[a4paper]{article} \usepackage{tikz} \usetikzlibrary{intersections, calc, spath3} \begin{document} \begin{tikzpicture} \coordinate (P) at (110:1.5); \draw[spath/save=g, thick] (200:3) -- (20:3) node[below] {$g$}; \draw[thick] plot[mark=x, mark size=3pt] coordinates {(P)} node[above right] {$P$}; \path[spath/save=CircleP] (P) circle (2); \path[name intersections={of=g and CircleP, name=g}]; \path[spath/save=CA] (g-1) circle (4mm); \draw[blue] plot[mark=x, mark size=3pt] coordinates {(g-1)} node[below left] {$A$}; \path[spath/save=CB] (g-2) circle (4mm); \draw[blue] plot[mark=x, mark size=3pt] coordinates {(g-2)} node[below right] {$B$}; \tikzset{ spath/split at intersections={CircleP}{CA}, spath/split at intersections={CircleP}{CB}, CircleP component 2/.style={blue, draw}, CircleP component 4/.style={blue, draw}, spath/render components=CircleP, } \path[spath/save=CircleA] (g-1) circle (2.3); \path[spath/save=CircleB] (g-2) circle (2.3); \path[name intersections={of=CircleA and CircleB, name=m}]; \path[spath/save=CM1] (m-1) circle (4mm); \path[spath/save=CM2] (m-2) circle (4mm); \tikzset{ spath/split at intersections={CircleA}{CM1}, spath/split at intersections={CircleA}{CM2}, CircleA component 2/.style={blue, draw}, CircleA component 4/.style={blue, draw}, spath/render components=CircleA, spath/split at intersections={CircleB}{CM1}, spath/split at intersections={CircleB}{CM2}, CircleB component 2/.style={blue, draw}, CircleB component 4/.style={blue, draw}, spath/render components=CircleB, } \draw[thick, blue] ($(m-1)!1.1!(m-2)$) -- ($(m-2)!1.1!(m-1)$); \end{tikzpicture} \end{document} 

Since I use the same construction to draw the sections of a circle around the intersection point of that circle with another path, which I want to use in other tikzpictures as well, I wanted to make that part into a new TikZ-element to which I can pass the path name of the circle, the path name of the other path and the name by which the intersections schould be named.

Sure, I can always make a newcommand achieving this, but I was hoping to find another way using tikzset or something along those lines which allows for a syntax that blends in with the construction of the other elements. Also I hope that such a way would allow for an easy change of aspects like the line thickness and color.

If possible I'd like to be able to write something like \draw circleintersect[circle=CircleA, path=g, name=g, radius=4mm]; to achive this.

I think I understood most of what was done in the answer to this question: TikZ: Easy drawing of cuboids. Sadly I wasn't able to get some code running for what I have in mind. My biggest problem is how to implement the keyval part I think. I tried to read the pgf/tikz manual on this but haven't gotten far.

Any help or hints are very much appreciated.

4
  • 2
    \draw circleintersect[circle=CircleA, path=g, name=g, radius=4mm]; would be much trickier to implement than, say, \draw pic [circleintersect={circle=CircleA, path=g, name=g, radius=4mm}];. that's not quite right as I've forgotten the syntax, but the point is that having pic in the path is expected - and then you can do what you want, more-or-less, whereas getting the parser to pic up an entirely new element involves more heavy lifting. Commented Dec 14, 2024 at 19:34
  • can you provide code for a single example of the construction you want to capture? as I understand it, your current example includes several examples of that construction, but I'm not sure whether I've understood correctly. Commented Dec 14, 2024 at 19:43
  • 1
    The construction I want to capture is the following: \path[name intersections={of=CircleP and g, name=g}]; \path[spath/save=CA] (g-1) circle (4mm); \path[spath/save=CB] (g-2) circle (4mm); \tikzset{ spath/split at intersections={CircleP}{CA}, spath/split at intersections={CircleP}{CB}, CircleP component 2/.style={blue, draw}, CircleP component 4/.style={blue, draw}, spath/render components=CircleP, } So for a given circle and path the intersections are calculated and then only the part of the circle within the specified radius is drawn. Commented Dec 14, 2024 at 20:21
  • @cfr even a pic solution would be much better than the newcommand solution I could cook up myself. Commented Dec 14, 2024 at 20:24

1 Answer 1

5

You can parametize and/or customise this further, but this should give you an idea of how to define and use a pic for this kind of construction. It isn't the only - or necessarily the best - way to do it, but it is fairly simple to implement and straightforward to extend or adapt to other constructions.

\documentclass[a4paper]{article} \usepackage{tikz} \usetikzlibrary{intersections, calc, spath3} % ateb: https://tex.stackexchange.com/a/732904/ % addaswyd o gwestiwn Peter Maier: https://tex.stackexchange.com/q/732895/ % goal: % \path[name intersections={of=CircleP and g, name=g}]; % \path[spath/save=CA] (g-1) circle (4mm); % \path[spath/save=CB] (g-2) circle (4mm); % \tikzset{ % spath/split at intersections={CircleP}{CA}, % spath/split at intersections={CircleP}{CB}, % CircleP component 2/.style={blue, draw}, % CircleP component 4/.style={blue, draw}, % spath/render components=CircleP, % } \tikzset{% circleintersect/.search also={/tikz,/pgf}, circleintersect/.cd, error/.code={\errmessage{Ouch! Undefined!}}, circle/.initial=circleintersect/error, path/.initial=circleintersect/error, name/.initial=MyName, radius/.initial=50mm, /tikz/.cd, pics/circleintersect/.style={% code={% \tikzset{% circleintersect/.cd, #1, /tikz/.cd, }% \path[name intersections={of/.expanded=\pgfkeysvalueof{/tikz/circleintersect/path} and \pgfkeysvalueof{/tikz/circleintersect/circle},name/.expanded=\pgfkeysvalueof{/tikz/circleintersect/name}}]; \path[spath/save=CA] ({\pgfkeysvalueof{/tikz/circleintersect/path}-1}) circle (\pgfkeysvalueof{/tikz/circleintersect/radius}); \path[spath/save=CB] (\pgfkeysvalueof{/tikz/circleintersect/path}-2) circle (\pgfkeysvalueof{/tikz/circleintersect/radius}); \tikzset{% spath/split at intersections/.expanded={\pgfkeysvalueof{/tikz/circleintersect/circle}}{CA}, spath/split at intersections/.expanded={\pgfkeysvalueof{/tikz/circleintersect/circle}}{CB}, \pgfkeysvalueof{/tikz/circleintersect/circle} component 2/.style={blue, draw}, \pgfkeysvalueof{/tikz/circleintersect/circle} component 4/.style={blue, draw}, spath/render components/.expanded=\pgfkeysvalueof{/tikz/circleintersect/circle}, }% }% }, } \begin{document} \begin{tikzpicture} \coordinate (P) at (110:1.5); \draw[spath/save=g, thick] (200:3) -- (20:3) node[below] {$g$}; \draw[thick] plot[mark=x, mark size=3pt] coordinates {(P)} node[above right] {$P$}; \path[spath/save=CircleP] (P) circle (2); \pic {circleintersect={circle=CircleP,name=g,path=g,radius=4mm}}; \draw[blue] plot[mark=x, mark size=3pt] coordinates {(g-1)} node[below left] {$A$}; \draw[blue] plot[mark=x, mark size=3pt] coordinates {(g-2)} node[below right] {$B$}; \path[spath/save=CircleA] (g-1) circle (2.3); \path[spath/save=CircleB] (g-2) circle (2.3); \path[name intersections={of=CircleA and CircleB, name=m}]; \path[spath/save=CM1] (m-1) circle (4mm); \path[spath/save=CM2] (m-2) circle (4mm); \tikzset{ spath/split at intersections={CircleA}{CM1}, spath/split at intersections={CircleA}{CM2}, CircleA component 2/.style={blue, draw}, CircleA component 4/.style={blue, draw}, spath/render components=CircleA, spath/split at intersections={CircleB}{CM1}, spath/split at intersections={CircleB}{CM2}, CircleB component 2/.style={blue, draw}, CircleB component 4/.style={blue, draw}, spath/render components=CircleB, } \draw[thick, blue] ($(m-1)!1.1!(m-2)$) -- ($(m-2)!1.1!(m-1)$); \end{tikzpicture} \end{document} 

The output is the same as your original code. The only difference is that the picture now uses a pic defined in the preamble, which can therefore be reused elsewhere.

2
  • 1
    Thank you very much. After changing \pgfkeysvalueof{/tikz/circleintersect/path}-1 to \pgfkeysvalueof{/tikz/circleintersect/name}-1 it works perfectly for the other two occurrences in my original example - I guess I should be careful to use unique names in my examples. I'm sure I'll be able to further customize it to my needs from here on. Commented Dec 14, 2024 at 22:57
  • 1
    @JeT thank you so much - I truly appreciate your saying that. Commented Dec 15, 2024 at 1:29

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.