4

I'm trying to implement quarter turn style arguments in TikZ, in such a way that

\begin{tikzpicture}
\path (2cm,1cm) coordinate (A);
\draw[blue,->]  (A) [northwestqturn=3cm];
\draw[green,->]  (A) [northeastqturn=3cm];
\draw[red,->]  (A) [southwestqturn=3cm];
\draw[yellow,->]  (A) [southeastqturn=3cm];
\draw (A) node {$\bullet$};
\end{tikzpicture}

is equivalent to

\begin{tikzpicture}
\path (2cm,1cm) coordinate (A);
\draw[blue,->]  (A)   arc[start angle=0, delta angle=90, radius=3cm];
\draw[green,->]  (A) arc[start angle=180, delta angle=(-90), radius=3cm];
\draw[red,->]  (A) arc[start angle=0, delta angle=(-90), radius=3cm];
\draw[yellow,->]  (A)   arc[start angle=180, delta angle=90, radius=3cm];
\draw (A) node {$\bullet$};
\end{tikzpicture}

Below is my failed attempt. I feel that it was a near miss and a minor typo is probably all that's keeping my code from working. Any help appreciated.

\documentclass{article}
\usepackage{tikz}


\tikzset{
  nwchord/.style args={#1}{
    /utils/exec=%
   radius=#1, start angle=0, delta angle=90},
  northwestqturn/.style={insert path={arc[nwchord={#1}]}},
 nwchord r/.initial=,
  nwchord do/.style={nwchord={\pgfkeysvalueof{/tikz/nwchord r}}},
}

\tikzset{
  swchord/.style args={#1}{
    /utils/exec=%
  radius=#1, start angle=270, delta angle=(-90)},
  southwestqturn/.style={insert path={arc[swchord={#1}]}},
  swchord r/.initial=,
  swchord do/.style={swchord={\pgfkeysvalueof{/tikz/swchord r}}},
}

\tikzset{
  sechord/.style args={#1}{
    /utils/exec=%
  radius=#1, start angle=180, delta angle=90},
  southeastqturn/.style={insert path={arc[sechord={#1}]}},
  sechord r/.initial=,
  sechord do/.style={sechord={\pgfkeysvalueof{/tikz/sechord r}}},
}

\tikzset{
  nechord/.style args={#1}{
    /utils/exec=%
   radius=#1, start angle=180, delta angle=(-90)},
  northeastqturn/.style={insert path={arc[nechord={#1}]}},
  nechord r/.initial=,
  nechord do/.style={nechord={\pgfkeysvalueof{/tikz/nechord r}}},
}



\begin{document}

\begin{tikzpicture}
\path (2cm,1cm) coordinate (A);
\draw[blue,->]  (A) [northwestqturn=3cm];
\draw[green,->]  (A) [northeastqturn=3cm];
\draw[red,->]  (A) [southwestqturn=3cm];
\draw[yellow,->]  (A) [southeastqturn=3cm];
\draw (A) node {$\bullet$};
\end{tikzpicture}

\begin{tikzpicture}
\path (2cm,1cm) coordinate (A);
\draw[blue,->]  (A)   arc[start angle=0, delta angle=90, radius=3cm];
\draw[green,->]  (A) arc[start angle=180, delta angle=(-90), radius=3cm];
\draw[red,->]  (A) arc[start angle=0, delta angle=(-90), radius=3cm];
\draw[yellow,->]  (A)   arc[start angle=180, delta angle=90, radius=3cm];
\draw (A) node {$\bullet$};
\end{tikzpicture}

\end{document}
  • I think the typo is the first two lines of each style for example nwchord/.style={radius=#1, start angle=0, delta angle=90}, draws. – percusse Jun 14 '16 at 8:27
  • @percusse draws and produces the expected output. If you turn your comment into an answer, I'll accept it (unless someone else explains further and says what the correct syntax with /utils/exec and style args should have been – user40960 Jun 14 '16 at 8:42
2

First, /utils/exec is meant to run a particular code through the keys to do auxiliary operations in TeX. It doesn't run keys, doesn't execute key code (unless you use pgfkeys commands). So here it has nothing to do with the purpose.

/.style args is a handler for creating custom keys with particular syntax such as mykey/.style args={I want an argument#1and#2with#3} then you can call this key with

mykey= I want an argument 3cm and green with onion

then #1 will be 3cm and so on.

In your case you are defining a style args but the argument is going to be a simple argument meaning there is no extra pattern hence a /.style would be sufficient.

And you can simplify things a bit

\documentclass[tikz]{standalone}
\tikzset{
  qturn/.style args ={#1with#2}{insert path={arc[#1=#2]}},
  ne/.style={radius=#1, start angle=180, delta angle=-90},
  nw/.style={radius=#1, start angle=0,   delta angle=90},
  se/.style={radius=#1, start angle=180, delta angle=90},
  sw/.style={radius=#1, start angle=0,   delta angle=(-90)},
}
\begin{document}
\begin{tikzpicture}
\path (2cm,1cm) coordinate (A);
\draw[blue,->]   (A) [qturn= ne with 3cm];
\draw[green,->]  (A) [qturn= nw with 3cm];
\draw[red,->]    (A) [qturn= se with 3cm];
\draw[yellow,->] (A) [qturn= sw with 3cm];
\draw (A) node {$\bullet$};
\end{tikzpicture}
\end{document}

enter image description here

  • I have a question about the arc[#1=#2] part in your code. Are those the same #1 and #2 from {#1with#2} ? – user40960 Jun 14 '16 at 10:12
  • @user40960 Yes, the first part defines the custom input pattern and the second part defines how they are used. So in effect it becomes ne=3cm for example. – percusse Jun 14 '16 at 11:24

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.