3

I am looking to improve my programming skills with this question. I believe I am making a mistake placing a tikzpicture within a tikzpicture? Any suggestions how to make this better and more flexible so one can vary the output according to ones needs? This is exactly what I want but I want to improve the programming.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{center}
\begin{tikzpicture}
\node at (-3,0) {\begin{tikzpicture}[scale=2]%,cap=round,>=latex]
\coordinate [label=left:$B$] (B) at (0:0cm);
\coordinate [label=above:$A$] (A) at (60:1.5cm);
\coordinate [label=right:$C$] (C) at (20:1.8cm);
\filldraw[fill=orange!50!white, draw=black] (B) -- node[above left] {c} (A) -- 
node[above right] {b} (C) -- node[below] {a} (B);
\end{tikzpicture}};
\node at (3,0) {\begin{tikzpicture}[scale=2]%,cap=round,>=latex]
\coordinate [label=left:$Y$] (Y) at (0:0cm);
\coordinate [label=above:$X$] (X) at (60:.5cm);
\coordinate [label=right:$Z$] (Z) at (20:.6cm);
\filldraw[fill=orange!50!white, draw=black] (Y) -- node[above left] {z} (X) -- 
node[above right] {y} (Z) -- node[below] {x} (Y);
\end{tikzpicture}};
\end{tikzpicture}
\end{center}

\end{document}

This outputs:

enter image description here

  • Yes, you should not nest tikzpictures, and all you need is to use pics. – user121799 Oct 20 '18 at 16:20
  • I remember you saying this. How can I give them names? Should I use the \def command ? – MathScholar Oct 20 '18 at 16:23
  • I appended two pics examples to my answer. – user121799 Oct 20 '18 at 16:35
5

In this case, the simplest way is to replace the inner tikzpictures by scopes and implement the position by simple shifts.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{scope}[xshift=-3cm,scale=2]%,cap=round,>=latex]
\coordinate [label=left:$B$] (B) at (0:0cm);
\coordinate [label=above:$A$] (A) at (60:1.5cm);
\coordinate [label=right:$C$] (C) at (20:1.8cm);
\filldraw[fill=orange!50!white, draw=black] (B) -- node[above left] {c} (A) -- 
node[above right] {b} (C) -- node[below] {a} (B);
\end{scope}
\begin{scope}[xshift=3cm,scale=2]%,cap=round,>=latex]
\coordinate [label=left:$Y$] (Y) at (0:0cm);
\coordinate [label=above:$X$] (X) at (60:.5cm);
\coordinate [label=right:$Z$] (Z) at (20:.6cm);
\filldraw[fill=orange!50!white, draw=black] (Y) -- node[above left] {z} (X) -- 
node[above right] {y} (Z) -- node[below] {x} (Y);
\end{scope}
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

If you have the same thing and want to place it all over, use pics, but since these are only two triangles here with different labels, it is perhaps easier to use scopes. However, as you asked in the comments, here is the pics variant.

\documentclass{article}
\usepackage{tikz}
\tikzset{pics/.cd,
3angle/.style n args={6}{code={\coordinate [label=left:$#2$] (B) at (0:0cm);
\coordinate [label=above:$#1$] (A) at (60:1.5cm);
\coordinate [label=right:$#3$] (C) at (20:1.8cm);
\filldraw[fill=orange!50!white, draw=black] (B) -- node[midway,above left] {#6} (A) -- 
node[midway,above right] {#5} (C) -- node[midway,below] {#4} (B);}}}
\begin{document}
\begin{center}
\begin{tikzpicture}
\pic[scale=2] at (-3,0) {3angle={A}{B}{C}{a}{b}{c}};
\pic[scale=0.75] at (3,0) {3angle={X}{Y}{Z}{x}{y}{z}};
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

Notice that one might be able to cook down the number of parameters to 3 if you always label the edge opposite a given corner by the lowercase letter.

\documentclass{article}
\usepackage{tikz}
\tikzset{pics/.cd,
3angle/.style n args={3}{code={\coordinate [label=left:$\MakeUppercase{#2}$] (B) at (0:0cm);
\coordinate [label=above:$\MakeUppercase{#1}$] (A) at (60:1.5cm);
\coordinate [label=right:$\MakeUppercase{#3}$] (C) at (20:1.8cm);
\filldraw[fill=orange!50!white, draw=black] (B) -- node[midway,above left] {#3} (A) -- 
node[midway,above right] {#2} (C) -- node[midway,below] {#1} (B);}}}
\begin{document}
\begin{center}
\begin{tikzpicture}
\pic[scale=2] at (-3,0) {3angle={a}{b}{c}};
\pic[scale=0.75] at (3,0) {3angle={x}{y}{z}};
\end{tikzpicture}
\end{center}
\end{document}

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.