3

I have code for a pie chart that is not giving the display that I expected. There are two aspects of this display that I want changed. First, I want to have 5% on the line below Clothing. I have tried the commands

\path (O) -- node[pos=0.75,pin=225:Clothing \\ 5\%] {} (225:3); 

and

\path (O) -- node[align=left,pin=225:Clothing \\ 5\%] {} (225:5);

but the \\ seems to be ignored. Second, I want the pin to be in the direction of 225 degrees. I thought (225:3) in these commands would get this in the display.

What part of the code has the pin coming from the above right point on the node? What does pos=0.75 instruct TikZ to draw?

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{positioning}   


\begin{document}

\begin{tikzpicture}[pin distance=5mm]

\coordinate (O) at (0,0);
\draw (O) circle (3);
\draw (O) -- (0:3);
\draw (O) -- (90:3);
\draw (O) -- (216:3);
\draw (O) -- (234:3);
\draw (O) -- (288:3);

\path (O) -- node[align=left]{Food \\ 25\%} (45:3);
\path (O) -- node[align=left]{Rent and \\ Utilities \\ 35\%} (153:3);
%\path (O) -- node[align=left,pin=225:Clothing \\ 5\%] {} (225:5);
\path (O) -- node[pos=0.75,pin=225:Clothing\\ 5\%] {} (225:3);
\path (O) -- node[align=left]{Other \\ 5\%} (261:3);
\path (O) -- node[align=left]{Car \\ 20\%} (324:3);

\node[above=33mm of O] {\textbf{David's Monthly Expenses}};
\end{tikzpicture}

\end{document}
user74973
  • 4,071
  • Other is probably meant to be 15%. –  May 28 '15 at 14:31
  • I hope that now you are satisfied with received answers. Except that they for writing of shares use siunitx (what is good idea) are the same as I give you in previous your questions ... Sometimes help to formulate question again that you see, what you omit from received answer in your further use. – Zarko May 28 '15 at 16:10

2 Answers2

3

Line breaks are enabled, if option align is given:

\path (O) -- node[pos=0.75,pin={[align=center]225:Clothing\ 5\%}] {} (225:3);

Instead of nested nodes, a node can be placed and a line drawn:

\draw (225:4) node[align=center] (tmp) {Clothing\\5\%} (tmp) -- (225:2.5);

Full example:

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{siunitx}

\begin{document}
\begin{tikzpicture}[pin distance=5mm]

\draw
  (0, 0) coordinate (O)
  circle (3)
  \foreach \i in {0, 90, 216, 234, 288} {
    (O) -- (\i:3)
  }
;

\path[every node/.style={align=center}]
  (O) -- node {Food \\ \SI{25}{\percent}} (45:3)
  (O) -- node {Rent and \\ Utilities \\ \SI{35}{\percent}} (153:3)
  (O) -- node {Other \\ \SI{5}{\percent}} (261:3)
  (O) -- node {Car \\ \SI{20}{\percent}} (324:3)
  (225:4) node (Clothing) {Clothing\\ \SI{5}{\percent}}
;
\draw (225:2.5) -- (Clothing);

\node[above=33mm of O] {\textbf{David's Monthly Expenses}};
\end{tikzpicture}
\end{document}

Result

In the former case, pos=.75 is used as starting point for the line of the pin option.

Heiko Oberdiek
  • 271,626
  • pos=0.75 starts from what point? – user74973 May 28 '15 at 14:24
  • @user74973 The node with the position is put on the line from (O) to (225:3). – Heiko Oberdiek May 28 '15 at 14:31
  • You told me that to get text in a node on different lines, I have to use align. I used align=left in the command \path (O) -- node[align=left,pin=225:Clothing \\ 5\%] {} (225:5); and I do not get 5% on a separate line. (This was in my original code.) – user74973 May 28 '15 at 14:44
  • @user74973 The align=left is applied to the outer node with the empty text. In my answer you can see, that I applied the align option to the pin node. – Heiko Oberdiek May 28 '15 at 14:58
  • I am not familiar with your foreach command and \SI{25}{\percent}. How do I edit the command the \path (O) -- node[pos=0.75,pin=225:Clothing \\ 5\%] {} (225:3); to get 5% below Clothing? – user74973 May 28 '15 at 15:32
  • @user74973 Package siunitx is the state of the art package for typesetting units and numbers. The package has documentation. Of course, you are free to set the percent numbers the way you like. But, AFAIK, a space is missing between the number and the percent sign in the question. – Heiko Oberdiek May 28 '15 at 15:44
  • @user74973 \foreach is documented in the manual for TikZ. TikZ is so powerful, that things can be done in many different ways. The example in my answer is just an example to show such a different way. – Heiko Oberdiek May 28 '15 at 15:47
  • @user74973 I do not like the left aligned entries in the image of the question, thus I have used align=center. Replacing center by left should not be to difficult? I have written my example in such a way, that this can be changed in one place instead of going through all nodes. – Heiko Oberdiek May 28 '15 at 15:50
  • I agree. I was just imitating what I saw in a book. I decided to center the text in the nodes. After do so, I still do not get 5% under Clothing. How do I edit the command the \path (O) -- node[align=center,pos=0.75,pin=225:Clothing \\ 5\%] {} (225:3); to get 5% below Clothing? – user74973 May 28 '15 at 15:56
  • @user74973 Look at the second line of my answer. – Heiko Oberdiek May 28 '15 at 16:05
  • How do I "apply the align option to the pin node" in the command \path (O) -- node[align=center,pos=0.75,pin=225:Clothing \\ 5\%] {} (225:3); to get 5% below Clothing? – user74973 May 28 '15 at 16:24
  • @user74973 What's the difficulty reading the second line of my answer? I have even made the relevant part bold. – Heiko Oberdiek May 28 '15 at 17:36
  • Is this your "second line"? "The align=left is applied to the outer node with the empty text. In my answer you can see, that I applied the align option to the pin node." As you can see from the command that I have sent you twice now, I have put align=center in it and I am not getting 5% on its own line. – user74973 May 28 '15 at 19:21
  • @user74973 The first line of the answer is "Line breaks are enabled, if option align is given:" and the second line is "\path (O) -- node[pos=0.75,pin={[align=center]225:Clothing\ 5\%}] {} (225:3);". – Heiko Oberdiek May 28 '15 at 19:36
  • I was looking at "comments." OK. I see that you have the pin option within the node, and within the pin, you have the option [align=center]. By the way, you need to have "Clothing \ 5%"; you have "Clothing \ 5%." – user74973 May 28 '15 at 21:24
2

It's probably easier if you use the actual node and then pin "nothing" to it so that it looks like the opposite. As far as I know you cannot apply a line break unless it's a node.

Output

enter image description here

Code

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{positioning}   


\begin{document}

\begin{tikzpicture}[pin distance=1cm]

\coordinate (O) at (0,0);
\draw (O) circle (3);
\draw (O) -- (0:3);
\draw (O) -- (90:3);
\draw (O) -- (216:3);
\draw (O) -- (234:3);
\draw (O) -- (288:3);

\path (O) -- node[align=left]{Food \\ 25\%} (45:3);
\path (O) -- node[align=left]{Rent and \\ Utilities \\ 35\%} (153:3);
\path (O) -- node[pos=.80,text width=2cm,align=left,pin=45:] {Clothing\\ 5\%} (225:5);
\path (O) -- node[align=left]{Other \\ 5\%} (261:3);
\path (O) -- node[align=left]{Car \\ 20\%} (324:3);

\node[above=33mm of O] {\textbf{David's Monthly Expenses}};
\end{tikzpicture}
\end{document}
Alenanno
  • 37,338
  • I see that you have pin=45:{}. Why do you say this is easier? I guess that since you are "pinning nothing," you have to have text width = 2cm. This is guessing that I would prefer to avoid. – user74973 May 28 '15 at 14:27
  • What part of the code has the pin coming from the above right point on the node? – user74973 May 28 '15 at 14:28
  • @user74973 Sorry, I was not here. Where do you see guessing? The text width command is needed to set a maximum so you can go to a new line. pin=45 is the direction of the pin, in this case, 45 degrees (up right). – Alenanno May 28 '15 at 17:56
  • I used the command \path (O) -- node[pos=0.75, pin={[align=center]225:Clothing \\ 5\%}] {} (225:3.5); to get the display that I wanted. (Heiko Oberdiek used it in his code.) There is not tex width command there. – user74973 May 28 '15 at 19:39
  • 1
    @user74973 Yes I saw it. However, although Heiko's solution is better because he managed to apply the line break in a pin, his solution is not that different from mine. You have only a few ways to use the \linebreak or similar commands: apply text width, align left and some other. In other words, they're equivalent. See this question: Manual/automatic line breaks and text alignment in TikZ nodes – Alenanno May 28 '15 at 21:59
  • I just read it. Thanks for forwarding it to me. That was a convenient post to the members of tex.stackexchange. – user74973 May 28 '15 at 22:06
  • 1
    @user74973 No problem! Glad you appreciated. :D – Alenanno May 28 '15 at 22:07
  • I did ... and I would appreciate your help with two aspects for specifying a pin. I edited my code in the post to include the command \path (O) -- node[pos=0.75, pin={[align=center]225:Clothing \\ 5\%}] {} (225:3.5);. It gives me what I want, but I do not know why the pin emanates from the above right part of the node, and I do not know how to TikZ determines how far the pin is to go into the slice (or how far the node is from the slice). – user74973 May 28 '15 at 22:19