2

How can I write a program that names each picture and so I can use tikzpicture environment once.

I am happy with the output but not the program

\begin{figure}[h]
\begin{center}
\begin{tikzpicture}
\node at (-5,1) {$
\begin{tikzpicture}[scale=.75]
\draw[<->,thick]
    (4,0) coordinate (A) node[right] {}
    -- (0,0) coordinate (O) node[left] {}
    -- (0,4) coordinate (B) node[above right] {}
    pic["\textcolor{red}{\ \ $90^{\circ}$}", draw=red, ->, angle eccentricity=1.2, angle radius=1cm]
    {angle=A--O--B};
\node at (0,-.5) [right] {Right angle};
\draw[thick] (.25,0)--(.25,.25)--(0,.25);
\draw[fill] (0,0) circle (2pt);
\end{tikzpicture}$};

\node at (0,0) {$\begin{tikzpicture}[scale=.75]
\draw[<->,thick]
    (4,0) coordinate (A) node[right] {}
    -- (0,0) coordinate (O) node[left] {}
    -- (-4,0) coordinate (B) node[above right] {}
    pic["\textcolor{red}{\ \ $180^{\circ}$}", draw=red, ->, angle eccentricity=1.2, angle radius=1cm]
    {angle=A--O--B};
\node at (0,-.5) [] {Straight angle};
%\draw[thick] (.25,0)--(.25,.25)--(0,.25);
\draw[fill] (0,0) circle (2pt);
\end{tikzpicture}$};

\node at (6,0) {$\begin{tikzpicture}[scale=.75]
\draw[<->,thick]
    (4,0) coordinate (A) node[right] {}
    -- (0,0) coordinate (O) node[left] {}
    -- (3,2) coordinate (B) node[above right] {}
    pic["\textcolor{red}{$\theta$}", draw=red, ->, angle eccentricity=1.2, angle radius=1cm]
    {angle=A--O--B};
\node at (-.5,-.5) [right] {Acute angle $0 < \theta < 90^{\circ}$};
%\draw[thick] (.25,0)--(.25,.25)--(0,.25);
\draw[fill] (0,0) circle (2pt);
\end{tikzpicture}$};

\node at (0,-3) {$
\begin{tikzpicture}[scale=.75]
\draw[<->,thick]
    (3,0) coordinate (A) node[right] {}
    -- (0,0) coordinate (O) node[left] {}
    -- (-3,2) coordinate (B) node[above right] {}
    pic["\textcolor{red}{$\theta$}", draw=red, ->, angle eccentricity=1.2, angle radius=1cm]
    {angle=A--O--B};
\node at (-1.5,-.5) [right] {Obtuse angle\ $90^{\circ} < \theta < 180^{\circ}$};
%\draw[thick] (.25,0)--(.25,.25)--(0,.25);
\draw[fill] (0,0) circle (2pt);
\end{tikzpicture}$};
\end{tikzpicture} 
\end{center}
\caption{}
\end{figure}

enter image description here

2
  • Search for args in the pgfmanual. // As a strategy such considerations and refactoring is better to do, once it‘s clear, there will be at least two objects of this kind.
    – MS-SPO
    Dec 9, 2023 at 5:13
  • 1
    @MS-SPO I think that might still leave rather a lot of hay to find the needle in. (That is, you'll get an awful lot of results for args.) Searching for pic might be better but, as I remember it, it wasn't very obvious how to do this from the manual. (Well, admittedly it didn't help that the manual's documented method didn't work at the time, but still.)
    – cfr
    Dec 9, 2023 at 6:19

1 Answer 1

4

Here's a way to do it. Keys:

  1. Provide a complete MWE. Use this checklist to arrive there.

  2. Define and pass parameters to the new \pic, diagr in this case. How many you pass and what they shall mean, is up to you. Here's one way to do it:

        pics/diagr/.style args={#1/#2/#3} {
            code={
...
            }}
  1. Place this new \pic just like nodes as often as you want, while passing both name and parameters in the "text field" {}.
    \pic at (0,0)   {diagr={90/90^{\circ}/Right angle}};
  1. Better use polar coordinates at least for point (B).

  2. IF you need the square for the rectangle, define and pass a new parameter OR use the ifthen package inside of diagr.

result

\documentclass[10pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{angles,quotes,arrows.meta}

\begin{document}

 \begin{figure}[h]
%   \begin{center}
    \centering
    \begin{tikzpicture}[
        % IF you need to scale it; alternative: adjust drawing itself
        scale=0.8,transform shape,
        % nicer arrow tips
        >={Stealth},
        % #1 = angle value
        % #2 = anle symbol
        % #3 = "caption"
        pics/diagr/.style args={#1/#2/#3} {
            code={
                \draw[<->,thick]
                    ( 0:3) coordinate (A) node[right] {} -- 
                    ( 0,0) coordinate (O) node[left] {} -- 
                    (#1:3) coordinate (B) node[above right] {}
                    pic[
                        "\textcolor{red}{$#2$}", 
                        draw=red,
                        ->, 
                        angle eccentricity=1.3,
                        angle radius=1cm
                    ]
                    {angle=A--O--B};
                \node at (-1.5,-.5) [right] {#3};
                \draw[fill] (0,0) circle (2pt);     
            }
        }
    ]
    
    \pic at (0,0)   {diagr={90/90^{\circ}/Right angle}};
    \pic at (7,0)   {diagr={180/180^{\circ}/Straight angle}};
    \pic at (12,0)  {diagr={40/\theta/Acute angle\ $90^{\circ} < \theta < 90^{\circ}$}};
    \pic at (7,-5)  {diagr={120/\theta/Obtuse angle\ $90^{\circ} < \theta < 180^{\circ}$}};

     \end{tikzpicture}

%   \end{center}
 \caption{Captain caption}
 \end{figure}

\end{document}
1
  • 1
    @cfr, right + thanks. That, the h , is one of the remaining things to clean up.
    – MS-SPO
    Dec 9, 2023 at 6:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .