在序言中概括我的 tikzpicture 环境的主体

在序言中概括我的 tikzpicture 环境的主体

我想概括这个图表并将其放在命令之前\begin{environment},这样我就可以简单/轻松地在旋转正方形时更改数字。如果能在这方面得到帮助,我将不胜感激。

在此处输入图片描述

\documentclass[a4paper, 12pt, openany]{book}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{patterns, matrix,3d,arrows.meta,bending,perspective, positioning, calc, math,arrows,shapes,fit}
\usepackage{tikz}

\begin{document}

\begin{center}
\begin{tikzpicture}

    % draw the background
    \draw [line width=1.5pt, fill=gray!2] (0,0) -- (4,0) -- (4,4) -- (0,4) -- cycle;

    \coordinate[label=left:$2$]  (A) at (0,0);
    \coordinate[label=right:$3$] (B) at (4,0);
    \coordinate[label=above:$1$] (C) at (0,4);
    \coordinate[label=above:$4$] (D) at (4,4);

  
\draw[fill=yellow!30] (4,4) -- (3.5,4) -- (3.5,3.5) -- (4, 3.5);

\draw[fill=yellow!30] (4,0) -- (3.5,0) -- (3.5,0.5) -- (4, 0.5);

\draw[fill=yellow!30] (0,4) -- (0.5,4) -- (0.5,3.5) -- (0, 3.5);

\draw[fill=yellow!30] (0,0) -- (0.5,0) -- (0.5,0.5) -- (0, 0.5);

\draw [line width=1.5pt] (A) -- (B) -- (D) -- (C) -- cycle;

\coordinate (A) at (5,2);
\coordinate (B) at (7,2);
\coordinate (C) at (6,1.2);
\draw[red, ultra thick, ->] (A) to [bend right=50] (B);
\node at (C) {$r_{90^\circ}=r$};

  \end{tikzpicture}
\hspace{0.5cm}
\begin{tikzpicture}
   \draw [line width=1.5pt, fill=gray!2] (0,0) -- (4,0) -- (4,4) -- (0,4) -- cycle;

    \coordinate[label=left:$1$]  (A) at (0,0);
    \coordinate[label=right:$2$] (B) at (4,0);
    \coordinate[label=above:$4$] (C) at (0,4);
    \coordinate[label=above:$3$] (D) at (4,4);

  
\draw[fill=yellow!30] (4,4) -- (3.5,4) -- (3.5,3.5) -- (4, 3.5);

\draw[fill=yellow!30] (4,0) -- (3.5,0) -- (3.5,0.5) -- (4, 0.5);

\draw[fill=yellow!30] (0,4) -- (0.5,4) -- (0.5,3.5) -- (0, 3.5);

\draw[fill=yellow!30] (0,0) -- (0.5,0) -- (0.5,0.5) -- (0, 0.5);

\draw [line width=1.5pt] (A) -- (B) -- (D) -- (C) -- cycle;
  \end{tikzpicture}
\end{center}
\end{document}

答案1

这是众多解决方案之一,其中包括\newcommand

概括一幅图景

\documentclass[border=3.14mm]{standalone}
\usepackage{tikz}

\newcommand{\mysquare}[4]%
    {%
    \def\myradius{4cm}
    \begin{tikzpicture}
    \foreach \l [count=\i from 0] in {#1,#2,#3,#4}
        {
        \draw[fill=yellow!20] (45+90*\i:\myradius) rectangle (45+90*\i:0.8*\myradius);
        \node at (45+\i*90:1.1*\myradius) {\l};
        }
    \draw[line width=1.5pt] (45:\myradius) rectangle (225:\myradius);
    \end{tikzpicture}
    }
    
\begin{document}
    \mysquare{4}{1}{2}{3}
    \hspace*{5mm}
    \raisebox{2cm}
        {   
        \begin{tikzpicture}
            \draw[red, ultra thick, ->] (0,0) to [bend right=50] (2,0);
            \node at (1,-.75) {$r_{90^\circ}=r$};
        \end{tikzpicture}
        }
    \hspace*{5mm}
    \mysquare{1}{2}{3}{4}
\end{document}

相关内容