Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to make a drawing using tikz. In this drawing, I put some shapes in specific angles. What I want to do is "highlight" those angles using simple dashed lines. My code is the following

\documentclass{article}

\usepackage{kerkis}
\usepackage{tikz}

\begin{document}
 \begin{tikzpicture}
  % Incident Beam
  \draw[->, very thick] (-5,0) -- (2,0);
  % Target : Boron + Au
  \shade[top color=gray!60, bottom color=gray!20] (6,1) rectangle (6.25,-1);% Au
  \shade[top color=gray!80, bottom color=gray!40] (6.25,1) rectangle (7.25,-1);% B
  % Scattering Chamber
  %\draw[dotted, red, very thick] (6.6125,0) circle (8cm);
  %\draw[dotted, red, very thick] (6.6125,0) circle (9cm);
  % Telescopes
  \path(6.6125,0)+(170:8)node[right color=black!70, left color=black!20,minimum width=1.2cm,minimum height=.4cm,rotate=80]{$\Delta E$};
  \path(6.6125,0)+(-120:8)node[top color=black!70, bottom color=black!20,minimum width=1.2cm,minimum height=.4cm, rotate=-210]{$\Delta E$};
  \path(6.6125,0)+(170:9)node[right color=black!80, left color=black!40,minimum width=1.2cm,minimum height=1.4cm,rotate=80]{$\mathbf{E}$};
  \path(6.6125,0)+(-120:9)node[top color=black!80, bottom color=black!40,minimum width=1.2cm,minimum height=1.4cm, rotate=-210]{$\mathbf{E}$};
  % Angles
  \draw[dashed, gray, thick] (4,0) -- (6,0);
  \draw[dashed, gray, thick,->] (6,0) -- (170:2);% <-------- This is where it's not working as expected
 \end{tikzpicture}

\end{document}

The output of the above code is

enter image description here

You can see that I am creating a horizontal line \draw[dashed, gray, thick] (4,0) -- (6,0);. I want to create other lines that will point to the grouped rectangles(which are placed in specific angles) so I used \draw[dashed, gray, thick,->] (6,0) -- (170:2); in order to point to the right angle and have the same length as the horizontal line, but it seems that there is something weird going on when using polar coordinates...

Any idea on why is this happening and how to achieve the desired drawing?

EDIT I also tried(after suggestion's) to use \draw[dashed, gray, thick,->] (6,0) -- +(170:2); but the lines aren't perfectly centered with the rectangles, as shown below

enter image description here

The code for the above image is the following

\documentclass{article}

\usepackage{kerkis}
\include{tikz}

\begin{document}
 \begin{tikzpicture}
  % Incident Beam
  \draw[->, very thick] (-5,0) -- (2,0);
  \node[above] at (-4.5,0) (proton) {$p^+$};
  % Target : Boron + Au
  \shade[top color=gray!60, bottom color=gray!20] (6,1) rectangle (6.25,-1);% Au
  \node[left] at (6.1,1.2) (Au) {$Au$};
  \shade[top color=gray!80, bottom color=gray!40] (6.25,1) rectangle (7.25,-1);% B
  \node[right] at (6.75,1.2) (B) {$^{nat}B$};
  % Telescopes
  \path(6.6125,0)+(170:8)node[right color=black!70, left color=black!20,minimum width=1.2cm,minimum height=.4cm,rotate=80]{$\Delta E$};
  \path(6.6125,0)+(170:8)node[above=0.75cm, right=0cm]{$6\mu m$};
  \path(6.6125,0)+(-120:8)node[top color=black!70, bottom color=black!20,minimum width=1.2cm,minimum height=.4cm, rotate=-210]{$\Delta E$};
  \path(6.6125,0)+(-120:8)node[below=0.35cm,right=0.5cm]{$22\mu m$};
  \path(6.6125,0)+(170:9)node[right color=black!80, left color=black!40,minimum width=1.2cm,minimum height=1.4cm,rotate=80]{$\mathbf{E}$};
  \path(6.6125,0)+(170:9)node[above=0.9cm, left=0cm]{$1000\mu m$};
  \path(6.6125,0)+(-120:9)node[top color=black!80, bottom color=black!40,minimum width=1.2cm,minimum height=1.4cm, rotate=-210]{$\mathbf{E}$};
  \path(6.6125,0)+(-120:9)node[below=0.35cm,right=0.5cm]{$1000\mu m$};
  % Angles
  \draw[dashed, gray, thick] (2,0) -- (6,0);
  \draw[dashed, gray, thick] (6,0) -- +(170:7.5);
  \draw[dashed, gray, thick] (6,0) -- +(-120:7.5);
  % 
 \end{tikzpicture}
\end{document}
share|improve this question
    
I don't understand the problem. It does what it is supposed to do. And why don't you refer to the delta node instead with calc library, say, ($(6,0)!2cm!(delta1)$)? –  percusse Jul 8 at 9:38
    
@percusse : Thank you very much for your comment! \draw[dashed, gray, thick,->] (6,0) -- (170:2); ---> Draws a line from point (6,0) to point (170:2), where 170 is the polar angle and 2 is the polar radius. \draw[dashed, gray, thick] (4,0) -- (6,0); This is a line with length 2. Those two line don't have the same length...Why? Also \path(6.6125,0)+(170:8)node[]{};` draws a rectangle at 170 degrees. Why the line doesn't point at the center of this rectangle? –  Thanos Jul 8 at 9:43
    
I'm no expert with TikZ, but the following seems to draw what I think you are trying to achieve: \draw[dashed, gray, thick,->] (6,0) -- +(170:2); –  mvkorpel Jul 8 at 9:44
    
@mvkorpel : Although it does, somehow the lines aren't perfectly centered with the rectangles. I'll edit my question, to see my point. –  Thanos Jul 8 at 9:48
    
I think you need 6 instead of 6.6125. –  mvkorpel Jul 8 at 9:58

1 Answer 1

up vote 6 down vote accepted

Polar coordinates are by default relative to the origin, so when you simply write:

  \draw[dashed, gray, thick,->] (6,0) -- (170:2);

you draw a line from (6,0) to the point which is at distance 2, angle 170 from the origin (0,0), as the following example demonstrates:

\begin{tikzpicture}
      \fill[red] (0,0) circle(2pt);  % Dot at origin
      \fill[blue] (6,0) circle(2pt); % Dot at (6,0)
      \draw[dashed, gray, thick,->] (6,0) -- (170:2); % Your line
      \draw[red] (0,0) -- (170:2);                    % Help line
\end{tikzpicture}

Example 1

If you put a + in front of any Tikz coordinate, then that coordinate is not considered relative to the origin, but relative to the last absolute coordinate used in that path. So, if you write instead:

  \draw[dashed, gray, thick,->] (6,0) -- +(170:2);

Then the angle 170 and distance 2 are relative to the point (6,0). The following drawing can help to understand:

\begin{tikzpicture}
      \fill[red] (0,0) circle(2pt);  % Dot at origin
      \fill[blue] (6,0) circle(2pt); % Dot at (6,0)
      \draw[gray] (6,0) -- (170:2);                     % Your bad line
      \draw[dashed, gray, thick,->] (6,0) -- +(170:2);  % Your "good" line
      \draw[red] (0,0) -- (170:2);                      % Help line
\end{tikzpicture}

Example 2

So this is the right approach.

However, your figure has another problem. You put the right box at point (6.6125,0), and place the rotated boxes relative to that origin (6.6125,0), but later you draw the dashed lines using (6,0) as origin. This explains the offset. If you draw the dashed lines from the correct origin, you get:

  \draw[dashed, gray, thick] (2,0) -- (6.6125,0);
  \draw[dashed, gray, thick] (6.6125,0) -- +(170:7.5);
  \draw[dashed, gray, thick] (6.6125,0) -- +(-120:7.5);

Result

Which has the correct alignment.

I understand that you used (6.6125,0) to place the right box because you wanted there its center, but later draw the lines from its edge that you somehow computed to be at (6,0). However you didn't use (6,0) as origin when placing the rotated boxes.

All this can be simplified if you put the right box at (6,0) using anchor=west, so you don't have to compute the extra 0.6125. In addition, the code can be simplified by defining some styles and relative coordinates, which avoid the use of most "hardcoded" numbers.

Update

Some ideas to improve the readability and maintainability of the code.

  1. Avoid "magic numbers". Tikz has the ability of give names to the coordinates. Those names are much more meaningful than the numbers.
  2. Use relative coordinates. Once some nodes are placed, their names and anchors can be used to define new coordinates and to place other nodes relative to those. positioning library is useful here too.
  3. Use label to add text next to the nodes. This option allows to place short text "out of" the node, at the desired position which can be expressed using words such as above or angles. The general syntax is: label=angle:$text$. This can be used to place most of the text in your figure, relative to the rectangular boxes (which are other nodes).
  4. Define styles for the shades, sizes, etc which can be reused at different parts of the figure. This way, you can change the whole look of the figure simply modifying these styles.

Putting all these ideas in action:

\usetikzlibrary{positioning}

\tikzset{
  gold/.style = {
    top color=gray!60, 
    bottom color=gray!20, 
    minimum width=0.25cm, 
    minimum height=2cm, 
    anchor=west,
  },
  boron/.style = {
    top color=gray!80, 
    bottom color=gray!40,
    minimum width=1cm, 
    minimum height=2cm, 
    anchor=west,
  },
  telescope/.style = {
    right color=black!70, 
    left color=black!20,
    minimum width=1.2cm,
    minimum height=.4cm,
    sloped,
    pos=1,
    rotate=90,
  },
  E/.style = {
     top color=black!80, 
     bottom color=black!40,
     minimum width=1.2cm,
     minimum height=1.4cm, 
     sloped,
     pos=1,
     rotate=90,
   },
}

 \begin{tikzpicture}

  \coordinate (beam left) at (-5,0);
  \coordinate (beam right) at (2,0);

  % Incident Beam
  \draw[->, very thick] (beam left) -- (beam right);
  \node[above right] at (beam left) (proton) {$p^+$};

  % Target : Boron + Au
  \node[gold, label=95:$Au$] at (6,0) (gold) {};% Au
  \node[boron, right=0mm of gold, label=80:$^{nat}B$] (boron) {};

  \coordinate (hit) at (gold.west);

  % Telescopes
  \path (hit) -- +(170:8) 
        node[telescope, label=right:$6\mu m$] (telescope1) {$\Delta E$};
  \path (hit) -- +(-120:8)
        node[telescope, label=left:$22\mu m$] (telescope2) {$\Delta E$};
  \path (hit) -- +(170:9)
        node[E, label=20:$1000\mu m$] {$\mathbf{E}$};
  \path (hit) -- +(-120:9)
        node[E, label=left:$1000\mu m$]  {$\mathbf{E}$};

  % Angles
  \draw[dashed, gray, thick] (beam right) -- (hit);
  \draw[dashed, gray, thick] (hit) -- (telescope1);
  \draw[dashed, gray, thick] (hit) -- (telescope2);
 \end{tikzpicture}

Note that using node names the final dashed lines can be drawn directly between nodes. No polar coordinates are needed again.

Ah, and a final note. To avoid specifying the rotation angle for the nodes of the telescopes, I used the sloped option. This options automatically rotates the node so that the text is aligned with the path in which the node appears. This requires to have a true path, hence the -- in \path (hit) -- +(polar coord). This is the line along which the node will be sloped. Finally, a rotate=90 is needed to have the node perpendicular to that line, instead of aligned with it.

Result:

Result

share|improve this answer
    
Thank you very much for your educational answer! Great information!!! –  Thanos Jul 8 at 10:41
    
@Thanos You are welcome. See my updated answer for more educational tips :-) –  JLDiaz Jul 8 at 11:11
    
It was really educational too!!! I want to create some arcs to illustrate the angles and I used \draw[<->,thick] (4cm,0mm) arc (180:170:2cm); \node[above=0.2cm,left] at (4,0) (170) {$170^\circ$}; \draw[<->,thick] (4cm,0mm) arc (-180:-120:2cm); \node[below=1cm,left] at (4,0) (120) {$120^\circ$}; I get the output that I want, but is there a more convenient way? –  Thanos Jul 8 at 12:41
1  
@Thanos, yes, I would use again polar coordinates from (hit) point, as in the following (btw, the angle quoted was wrong, wasn't it? I fixed it): \draw[<->,thick] (hit) +(180:2) arc (180:170:2); \path (hit) +(175:2) node[left] {$10^\circ$}; \draw[<->,thick] (hit) +(180:2) arc (-180:-120:2); \path (hit) +(210:2) node[left] {$60^\circ$}; –  JLDiaz Jul 8 at 14:13
    
Thank's a lot for your help! I also noticed sth weird... Take a look at the output imgur.com/dF8kPZu You can see that there is a blank space at the left side. I tried to see where can I fix it tin the code but I didn't see anything... Any idea? –  Thanos Jul 10 at 7:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.