16

In order to complete a handout I was preparing, I wrote the code below (MWE):

\documentclass[border=10pt,tikz]{standalone}
\usetikzlibrary{calc,angles,positioning,decorations.pathreplacing}
\begin{document}
\newcounter{angle}
\setcounter{angle}{135}
\begin{tikzpicture}
    %--------------------------------------------------------------------------------------------
    %Axis
    \draw[thick,-stealth,black] (-3.7,0)--(3.7,0) node[below] {$x$}; % x axis
    \draw[thick,-stealth,black] (0,-3.7)--(0,3.7) node[left] {$y$}; % y axis
    %--------------------------------------------------------------------------------------------
    % Unit Circle
    \draw[black] (0,0) circle (2cm);
    \node[black,below] at (2.2,0) {1};
    %--------------------------------------------------------------------------------------------
    % Circle of Radius R
    \draw[black] (0,0) circle (3cm);
    \node[black,below] at (3.2,0) {$r$};
    %--------------------------------------------------------------------------------------------
    % 
    \draw[-stealth] (1,0) arc (0:\theangle:1) node at ($(\theangle/2:0.7)$) {$\theta$};
    %--------------------------------------------------------------------------------------------
    \draw[dashed] (\theangle:2cm) -- (\theangle:2cm |- 0,0) coordinate (A) node [midway, left]{$y^\prime$}; % vertical line
    \draw[thick,rotate=\theangle] (0,0) -- (2,0);%  
    \filldraw[black] (\theangle:2cm) circle(1pt);
    \filldraw[black] (A) circle(1pt);
    \draw (\theangle:2cm) node[above=2pt,xshift=2pt] {$P^\prime$};
    \node[xshift=-0.75cm, below] (A) {$x^\prime$};
    %--------------------------------------------------------------------------------------------
    \draw[thick,rotate=\theangle] (0,0) -- (3,0);
    \filldraw[black] (\theangle:3cm) circle(1pt);
    \draw (\theangle:3cm) node[above=2pt] {$P$};
    \draw[dashed] (\theangle:3cm) -- (\theangle:3cm |- 0,0) coordinate (B) node [midway, left]{$y$};
    \filldraw[black] (B) circle(1pt);
    \draw [decoration={brace,raise=17pt}, decorate,thick,rotate=\theangle] (0,0) -- (B)
        node [midway, below=4.5ex]{$x$};%
\end{tikzpicture}

\end{document}

But I feel the code is a bit messy. Can you assist me in achieving the same result but with a more compact and robust code?

Here is an image of the result above:

enter image description here

3
  • You don't need black in draw. It is default color. – Sigur Dec 21 '19 at 16:44
  • 1
    Line 24 is useless: \draw[thick,rotate=\theangle] (0,0) -- (2,0);% – AndréC Dec 21 '19 at 16:49
  • @AndréC :) You are right. – azetina Dec 21 '19 at 17:11
14

This makes the code a bit shorter.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{decorations.pathreplacing,arrows.meta,bending}
\begin{document}
\begin{tikzpicture}[declare function={angle=135;},bullet/.style={inner
    sep=1pt,fill,draw,circle,solid}]
    %--------------------------------------------------------------------------------------------
    %Axis
    \draw[thick,-stealth,black] (-3.7,0)--(3.7,0) node[below] {$x$}; % x axis
    \draw[thick,-stealth,black] (0,-3.7)--(0,3.7) node[left] {$y$}; % y axis
    %--------------------------------------------------------------------------------------------
    % Rest
    \path (0,0) coordinate (O);
    \draw foreach \rad/\lab in {2/1,3/r}
    { (O) circle [radius=\rad*1cm]  (\rad,0) node[below right]{$\lab$}};
    \draw[thick] (O) -- (angle:3);
    \draw[dashed] foreach \rad/\lab in {2/',3/}
    {(angle:\rad) node[bullet,label=above:$P\lab$](P\lab){} -- 
     node[left]{$y\lab$}    (P\lab|-O)node[bullet]{} };
    \draw[-{Stealth[bend]}] (angle/2:0.6) node{$\theta$} (0:1) arc(0:angle:1) ;
    \path (O) -- node[below] {$x'$}(P'|-O); 
    \draw [decoration={brace}, decorate,thick] 
    (0,-0.5) -- (P|-0,-0.5) node[midway,below=0.1ex]{$x$};
\end{tikzpicture}
\end{document}

enter image description here

Let me mention that overall your code is fine. I feel that the things that you really should do is to avoid positioning the brace with raise and to use styles for repeating things like the bullet here. The \foreach thingy does make the code shorter but for two repetitions it is not that big of a deal.

Here is an annotated version:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{decorations.pathreplacing,arrows.meta,bending}
\begin{document}
\begin{tikzpicture}[declare function={angle=135;}, % This defines a 
                    % parameterless function which as opposed to
                    % \newcounter{angle}\setcounter{angle}{135}
                    % also allows to fractional values and is local.
                    % Of course there is nothing wrong with counters.
    bullet/.style={inner sep=1pt,fill,draw,circle,solid} % This 
    ] % defines a style, which you may think of as short cut.
    %  Instead of "inner sep=1pt,fill,draw,circle,solid" you only need to
    % say "bullet". And if you universally want to change something,
    % such as the radius which is controlled by inner sep, you only
    % need to change it once instead of several times.
    %--------------------------------------------------------------------------------------------
    %Axis
    \draw[thick,-stealth,black] (-3.7,0)--(3.7,0) node[below] {$x$}; % x axis
    \draw[thick,-stealth,black] (0,-3.7)--(0,3.7) node[left] {$y$}; % y axis
    %--------------------------------------------------------------------------------------------
    % Rest
    \path (0,0) coordinate (O);
    % You repeat the circles, which differ in their radii and labels, 
    % so one can do a loop that runs over the latter. 
    \draw foreach \rad/\lab in {2/1,3/r}
    { (O) circle [radius=\rad*1cm]  (\rad,0) node[below right]{$\lab$}};
    % Just a line. 
    \draw[thick] (O) -- (angle:3);
    % The P-type nodes and dashed lines are repetitive, too, so we do 
    % another loop (in which we name the P nodes for later use).
    \draw[dashed] foreach \rad/\lab in {2/',3/}
    {(angle:\rad) node[bullet,label=above:$P\lab$](P\lab){} -- 
     node[left]{$y\lab$}    (P\lab|-O)node[bullet]{} };
    % This is just an arc but it looks IMHO better if the arrow head
    % is bent. In any case, loading the bending library improves the 
    % angles of the arrow heads regardless of whether you use it explicitly
    % by saying "bent".
    \draw[-{Stealth[bend]}] (angle/2:0.6) node{$\theta$} (0:1) arc(0:angle:1) ;
    % add the x' label
    \path (O) -- node[below] {$x'$}(P'|-O); 
    % Add the brace. Rather than raising it we just connect the appropriate
    % points. Here and above we use the |- notation, e.g. 
    % (P|-0,-0.5) means "take the x value of (P) and the y value of (0,-0.5)",
    % see https://tex.stackexchange.com/a/401429 for more details.
    \draw [decoration={brace}, decorate,thick] 
     (0,-0.5) -- (P|-0,-0.5) node[midway,below=0.1ex]{$x$};
\end{tikzpicture}
\end{document}
4
  • Interesting! Let me look at your code more closely to understand what you did. Maybe comment some sections to explain what they do? Thanks. – azetina Dec 21 '19 at 17:35
  • 2
    @azetina I added an annotated version. – user194703 Dec 21 '19 at 17:44
  • Could you please annotate the \begin{tikzpicture} line for newbies? – mavzolej Dec 22 '19 at 22:45
  • 1
    @mavzolej I tried to do so, please see the update. – user194703 Dec 22 '19 at 22:55
12

Good exercice to test the new version of tkz-euclide. This version is beta v 2.49b

Firstly, you ask for a more compact and robust code. I understand "robust" but not compact. I prefer "easy to read", "to use" or "to change" ...

\documentclass{standalone}
\usepackage{tkz-euclide}

\begin{document} 
\begin{tikzpicture}[>=latex]
% Definiton
  \tkzInit[xmin=-1.4,xmax=1.4,xstep=.2,ymin=-1.4,ymax=1.4,ystep=.2]
  \tkzDefPoint(0,0){O}\tkzDefPoint(1,0){A}
  \tkzDefPoint(1.3,0){B}  \tkzDefPoint(.4,0){C}
  \tkzDefPoint({cos(2.5)},{sin(2.5)}){P'}
  \tkzDefPoint({1.3*cos(2.5)},{1.3*sin(2.5)}){P}
  \tkzDefPointsBy[projection=onto O--A](P,P'){p,p'}
  % Drawing  No measures after the first part
  \tkzDrawXY[noticks] 
  \tkzDrawSegments[dashed](P,p P',p')
  \tkzDrawSegment[line width=1.2 pt](O,P)
  \tkzDrawCircles(O,A O,B)
  \tkzDrawArc(O,A)(P')
  \tkzDrawArc[->](O,C)(P)
  \tkzDrawPoints(P,P',p,p')
  % Label
  \tkzLabelPoints[above=8pt](P,P')
  \tkzLabelPoint(A){$1$}
  \tkzLabelPoint(B){$r$}
  \tkzLabelSegment(p,P){$y$}
  \tkzLabelSegment(p',P'){$y'$}
  \tkzLabelSegment(O,p'){$x'$}
  \tkzLabelAngle[dist=1.8](A,O,P){$\theta$}
  % some tikz's code
  \begin{scope}[/pgf/decoration/raise=15pt,
      decoration={brace,mirror,amplitude=10pt}]
  \draw [decorate](p) -- (O) node [black,midway,yshift=-30pt]  {$x$};
  \end{scope}
\end{tikzpicture} 

\end{document}

enter image description here

A complement for the unit circle :

\documentclass{standalone}
\usepackage{tkz-euclide}

\begin{document} 
\begin{tikzpicture}
  \tkzInit[xmin=-1,xmax=1.1,xstep=.2,ymin=-1,ymax=1.1,ystep=.2]
  \tkzAxeXY

  \tkzDefPoint(0,0){O}
  \tkzDefPoint(1,0){A}
  \tkzDefPoint(1,1){T}
  \tkzDefPoint({cos(1)},{sin(1)}){M}
  \tkzDefPoint(40:1){LM}  

  \begin{scope} %le clip est ainsi local !
    \tkzClipCircle(O,A)
    \tkzGrid[color=yellow!20](-1,-1)(1,1)
  \end{scope}

  \tkzFillAngle[fill=blue!20,size=5cm,opacity=.2](A,O,M) 
  \tkzDrawArc[color=blue](O,A)(M)
  \tkzDrawArc[red](O,M)(A)
  \tkzDrawSegments[color=blue](A,T O,M)
  \tkzDrawPoints(M,T)  \tkzPointShowCoord(M) 

  \tkzLabelSegment[right](A,T){\textcolor{blue}{1\,u}}
  \tkzLabelSegment[pos=.2,left](O,M){\textcolor{blue}{1\,u}} 
  \tkzLabelPoint[above right](LM){\textcolor{blue}{1\,u}}

  \tkzText[color= blue,draw,fill=yellow](0.3,0.15){$1$\,rad}
  \tkzText[draw,color= red,below=20pt,fill=yellow]({cos(1)},0){$\cos(1)$}
  \tkzText[draw,color= red, left=20pt,fill=yellow](0,{sin(1)}){$\sin(1)$}
\end{tikzpicture}  
\end{document}

enter image description here

1
  • God level coding! Nice. Will surely consider looking in to this code. Thanks. – azetina Dec 21 '19 at 22:51
7

To make the code more compact and robust, you can draw many paths in one \path command. This is how I would draw that figure.

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[>=stealth]
  \draw[thick] (3.7,0) edge[<-] (-3.7,0) node[below] {$x$} (0,3.7) edge[<-] (0,-3.7) node[left] {$y$} 
    (0,0) -- (135:3);
  \draw (0,0) circle (2) circle (3);
  \draw[->] (1,0) arc (0:135:1) node[midway,anchor=67.5] {$\theta$};
  \fill (135:3) coordinate (p) circle (1pt) node[above] {$P$}
    (135:2) coordinate (p') circle (1pt) node[above=1ex] {$P'$}
    (p |- 0,0) circle (1pt) (p' |- 0,0) circle (1pt);
  \draw[dashed] (p) -- (p |- 0,0) node[midway,left] {$y$} (p') -- (p' |- 0,0) node[midway,left] {$y'$};
  \path (p' |- 0,0) -- (0,0) node[midway,below] {$x'$} 
    (2,0) node[below right] {1} (3,0) node[below right] {$r$};
  \draw[decorate,decoration={brace,mirror},thick] (p |- 0,0) ++ (0,-.7) -- (0,-.7)
    node[midway,below=1ex] {$x$}; 
\end{tikzpicture}
\end{document}

enter image description here

1
  • Only one library! Nice code. – azetina Dec 21 '19 at 17:37
6

How I would've drawn this (most likely not the most elegant solution...). I've changed the names of y, y', x and x' as two of those are already taken as axis names.

\documentclass[border=3.14,tikz]{standalone}

\newcommand*\myangle{135}
\newcommand*\myrad{1.5}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}
\begin{tikzpicture}[x=2cm,y=2cm,>=stealth, thick]
  \coordinate (O);
  \draw[->] (-1.2*\myrad,0) -- (1.2*\myrad,0) node[below]{$x$};
  \draw[->] (0,-1.2*\myrad) -- (0,1.2*\myrad) node[left]{$y$};
  \draw[thin]
    circle[radius=1]
    circle[radius=\myrad]
    ;
  \draw
    (1,0) node[below right]{$1$}
    (\myrad,0) node[below right]{$\vphantom{1}r$}
    (O) -- (\myangle:1) coordinate(Pp) node[above]{$P'$}
        -- (\myangle:\myrad) coordinate(P) node[above]{$P$}
    ;
  \draw[thin,dashed]
    (P)  --node[left,inner sep=1pt]{$y_P$}    (P|-O)
    (Pp) --node[right,inner sep=1pt]{$y_{P'}$} (Pp|-O)
    ;
  \foreach\p in {P,Pp,P|-O,Pp|-O}
    \filldraw (\p) circle[radius=1pt];
  \path
    (O) --node[below]{$x_{P'}$} (Pp|-O)
    ;
  \draw[->,thin]
    (.5,0) arc[start angle=0, end angle=\myangle, radius=.5]
    node[midway,below]{$\theta$}
    ;
  \draw[decoration={brace,raise=17pt}, decorate]
    (O) -- (P|-O) node[midway,below=4.5ex] {$x_P$}
    ;
\end{tikzpicture}
\end{document}

enter image description here

1
  • Nice idea of renaming the points and vertical phantom function. – azetina Dec 21 '19 at 17:36
5

Since you asked for "compact" code, I present you compact code. Don't complain.

(This is built from Schrödinger's cat's answer)

\documentclass[tikz,border=3]{standalone}\let~\def~\'{decorat}~\*{tikzpicture}
\usetikzlibrary{\'ions.pathreplacing}\begin{document}\begin{\*}[b/.style={inner
sep=1,fill,circle},>=stealth]~\1{135:}~\2{\draw[}~\3{\2thick,}~\4{node[}~
\6{midway}~\7{\foreach\'/}~~{\4below }\3->]coordinate(O)(-3.7,0)--(3.7,0)~]
{$x$};\3->](0,-3.7)--(0,3.7)\4left]{$y$};\7\"in{2/1,3/r}\2]circle(\')(\',0)
~right]{$\"$};\3](O)--(\13);\7~in{2/',3/}\2dashed](\1\')\4b,label=above:$P~$]
(P~){}--(P~|-O)\4left,\6]{$y~$}\4b]{};\2->]--(P'|-O)~,\6]{$x'$}(0:1)arc(0:\11)
~,\6]{$\theta$};\3\'ion={brace},\'e](0,-.5)--(P|-0,-.5)~=.1ex,\6]{$x$};\end{\*}
\end{document}

enter image description here

1 byte more, but it looks compacter:

\documentclass[tikz,border=3]{standalone}\let~\def~\'{decorat}~\*{tikzpicture}%
\usetikzlibrary{\'ions.pathreplacing}\begin{document}\begin{\*}[b/.style={inner
sep=1,fill,circle},>=stealth]~\1{135:}~\2{\draw[}~\3{\2thick,}~\4{node[}~\6{mi%
dway}~\7{\foreach\'/}~~{\4below }\3->]coordinate(O)(-3.7,0)--(3.7,0)~]{$x$};\3-
>](0,-3.7)--(0,3.7)\4left]{$y$};\7\"in{2/1,3/r}\2]circle(\')(\',0)~right]{$\"$}
;\3](O)--(\13);\7~in{2/',3/}\2dashed](\1\')\4b,label=above:$P~$](P~){}--(P~|-O)
\4left,\6]{$y~$}\4b]{};\2->]--(P'|-O)~,\6]{$x'$}(0:1)arc(0:\11)~,\6]{$\theta$};
\3\'ion={brace},\'e](0,-.5)--(P|-0,-.5)~=.1ex,\6]{$x$};\end{\*}\end{document}
1
  • Incredible! :) Nicely done I suppose. – azetina Dec 22 '19 at 20:15

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.