7

I'm trying to draw a triangle using TikZ. I managed to get it completed, but now I'm having trouble with getting the node to go into the body of the triangle. I've only been using TikZ for a couple of days so this basic exercise has taken much longer than I care to admit.

How would I put the node within the body?

Although this does in fact draw a triangle, would you consider it properly done? Is there a better way?

\begin{center}
    \begin{tikzpicture} 
        \draw[gray] ++(150:2.3) -- (0,0); %hypotenuse
        \draw[teal] ++(180:2) -- (0,0); %adjacent
        \draw[orange] (-2,1.15) -- (-2,0); %opposite

        \draw[thin] (-0.5,0.25) arc (150:180:0.5) 
            node[left] {\small $30^\circ$};
    \end{tikzpicture}
\end{center}
9

Processing the original code:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture} 
\draw[gray] ++(150:2.3) -- (0,0); %hypotenuse
\draw[teal] ++(180:2) -- (0,0); %adjacent
\draw[orange] (-2,1.15) -- (-2,0); %opposite

\draw[thin] (-0.5,0.25) arc (150:180:0.5) 
  node[left] {\small $30^\circ$};
\end{tikzpicture}

\end{document}

and zooming the resulting object, reveals two additional problems: the arc doesn't touch the hypotenuse and the hypotenuse and the shorter cathetus don't intersect:

enter image description here

Both problems are due basically to the same reason: manual calculation of coordinates.

To solve the problem with the arc, I would suggest you to use clipping and an additional node to draw the circle; in this way, you solve two problems: the arc is correctly drawn in an automated way (no need to guess angles as in the other answers) and you gain finer control over the label position:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{center}
\begin{tikzpicture}
\draw[gray] ++(150:2.3) -- (0,0); %hypotenuse
\draw[teal] ++(180:2) -- (0,0); %adjacent
\draw[orange] (-2,1.15) -- (-2,0); %opposite
\path[clip] (0,0) -- (-2,0) -- (-2,1.15) -- cycle;
\node[circle,draw,minimum size=40pt] at (0,0) (circ) {};
\node[font=\footnotesize,left] at (circ.160) {$30^\circ$};
\end{tikzpicture}
\end{center}

\end{document}

enter image description here

There's still one other detail to improve: the hypotenuse and the opposite cathetus not intersecting. This can be solved by using the intersections library to automatically do the calculation of the intersection between the prolongations of the opposite cathetus and the hypotenuse:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}

\begin{center}
\begin{tikzpicture}
% place coordinates at the two initial vertices 
\coordinate (a) at (0,0);
\coordinate (b) at (-2,0);

% automatically calculate the third vertex
\path[name path=line 1] (-2,0) -- (-2,2);
\path[name path=line 2] (0,0) -- (150:2.4);
\path [name intersections={of=line 1 and line 2, by={c}}];

% draw the lines
\draw[gray] (a) -- (c); %hypotenuse
\draw[teal] (a) -- (b); %adjacent
\draw[orange] (b) -- (c); %opposite

% draw the arc clipping a circle against the triangle and place the label
\path[clip] (a) -- (b) -- (c) -- cycle;
\node[circle,draw,minimum size=40pt] at (0,0) (circ) {};
\node[font=\footnotesize,left] at (circ.160) {$30^\circ$};
\end{tikzpicture}
\end{center}

\end{document}

enter image description here

4

As Sigur points out, you can use above left for placement. Also, I would suggest you to use scale option for sizing the contents:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \begin{tikzpicture}
        \draw[gray] ++(150:2.3) -- (0,0); %hypotenuse
        \draw[teal] ++(180:2) -- (0,0); %adjacent
        \draw[orange] (-2,1.15) -- (-2,0); %opposite

        \draw[thin] (-0.45,0.255) arc (149:180:0.5)
            node[ above left, scale=.7] { $30^\circ$};
    \end{tikzpicture}
\end{tikzpicture}
\end{document}

enter image description here

  • scale= was a good suggestion. – Sigur Oct 27 '12 at 23:43
3

Use the option node[above left]

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.