简化 TikZ 代码

简化 TikZ 代码

有没有办法简化这段代码并获得相同的效果tikzpicture

简化:我的意思是减少代码长度,例如添加更多\begin{scope}...\end{scope},...)

在此处输入图片描述

\documentclass{article}
\usepackage[a4paper,top=3cm,bottom=3cm,left=1.5cm,right=1.5cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage{amssymb}
\usepackage{tikz}

\begin{document}

\begin{center}
\begin{tikzpicture}

\begin{scope}[thick, blue]
\draw (-6,0.5) to[out=80,in=310,looseness=1] (-4.5,3);
\draw (-4.5,3) to[out=140,in=160,looseness=1] (-3,4);
\draw (-3,4) to[out=-30,in=160] (-1,3);
\draw (-1,3) to[out=-20,in=100] (0,2);
\draw (-1,0.5) to[out=15,in=-90,looseness=1] (0,2);
\draw (-2,0.75) to[out=-10,in=190] (-1,0.5);
\draw (-6,0.5) to[out=270,in=190,looseness=1.5] (-5,0);
\draw (-5,0) to[out=10,in=170,looseness=1.5] (-2,0.75);
\end{scope}

\begin{scope}[very thin, dashed]

%Per lo spazio topologico V

\draw (-5.5,0.05) -- (-5.5,1.2);
\draw (-5,0.2) -- (-5,1.65);
\draw (-4.5,0.3) -- (-4.5,2);
\draw (-4,0.5) -- (-4,3.75);
\draw (-3.5,0.7) -- (-3.5,3.95);
\draw (-3,0.9) -- (-3,3.9);
\draw (-2.5,0.9) -- (-2.5,3.6);
\draw (-2,0.85) -- (-2,3.35);
\draw (-1.5,0.7) -- (-1.5,3.1);
\draw (-1,0.65) -- (-1,2.9);
\draw (-0.5,0.85) -- (-0.5,2.65);

\draw (-5.75,0.5) -- (-4,0.5);
\draw (-5.5,1) -- (-.5,1);
\draw (-5,1.5) -- (-0.25,1.5);
\draw (-4.5,2) -- (-0.25,2);
\draw (-4.25,2.5) -- (-.3,2.5);
\draw (-4.25,3) -- (-1.25,3);
\draw (-4.25,3.5) -- (-2.5,3.5);

%Per lo spazio topologico U

\draw (2.5,3.25) to[out=280,in=80,looseness=1.5] (2.5,2.25);
\draw (2.5,2.25) to[out=280,in=80,looseness=1.5] (2.5,1.25);
\draw (2.5,1.25) to[out=280,in=80,looseness=1.5] (2.5,0.75);
\draw (3,3.25) to[out=280,in=80,looseness=1.5] (3,2.25);
\draw (3,2.25) to[out=280,in=80,looseness=1.5] (3,1.25);
\draw (3,1.25) to[out=280,in=80,looseness=1.5] (3,0.75);
\draw (3.5,3.25) to[out=280,in=80,looseness=1.5] (3.5,2.25);
\draw (3.5,2.25) to[out=280,in=80,looseness=1.5] (3.5,1.25);
\draw (3.5,1.25) to[out=280,in=80,looseness=1.5] (3.5,0.75);
\draw (4,3.25) to[out=280,in=80,looseness=1.5] (4,2.25);
\draw (4,2.25) to[out=280,in=80,looseness=1.5] (4,1.25);
\draw (4,1.25) to[out=280,in=80,looseness=1.5] (4,0.75);
\draw (4.5,3.25) to[out=280,in=80,looseness=1.5] (4.5,2.25);
\draw (4.5,2.25) to[out=280,in=80,looseness=1.5] (4.5,1.25);
\draw (4.5,1.25) to[out=280,in=80,looseness=1.5] (4.5,0.75);
\draw (5,3.5) to[out=280,in=80,looseness=1.5] (5,3.25);
\draw (5,3.25) to[out=280,in=80,looseness=1.5] (5,2.25);
\draw (5,2.25) to[out=280,in=80,looseness=1.5] (5,1.25);
\draw (5,1.25) to[out=280,in=80,looseness=1.5] (5,0.75);
\draw (5,0.75) to[out=280,in=80,looseness=1.5] (5,0.5);
\draw (5.5,3.5) to[out=280,in=80,looseness=1.5] (5.5,3.25);
\draw (5.5,3.25) to[out=280,in=80,looseness=1.5] (5.5,2.25);
\draw (5.5,2.25) to[out=280,in=80,looseness=1.5] (5.5,1.25);
\draw (5.5,1.25) to[out=280,in=80,looseness=1.5] (5.5,0.75);
\draw (5.5,0.75) to[out=280,in=80,looseness=1.5] (5.5,0.5);
%\draw (6,3.50) to[out=280,in=80,looseness=1.5] (6,3.25);
\draw (6,3.25) to[out=280,in=80,looseness=1.5] (6,2.25);
\draw (6,2.25) to[out=280,in=80,looseness=1.5] (6,1.25);
\draw (6,1.25) to[out=280,in=80,looseness=1.5] (6,0.75);
\draw (6,0.75) to[out=280,in=80,looseness=1.5] (6,0.5);
%\draw (6.5,2.25) to[out=280,in=80,looseness=1.5] (6.5,1.25);
%\draw (6.5,1.25) to[out=280,in=80,looseness=1.5] (6.5,0.75);

\draw (2.5,2.75) to[out=45,in=135,looseness=0.5] (6,2.75);
\draw (2.5,2.25) to[out=45,in=135,looseness=0.5] (6,2.25);
\draw (2.5,1.75) to[out=45,in=135,looseness=0.5] (6,1.75);
\draw (2.5,1.25) to[out=45,in=135,looseness=0.5] (6,1.25);
\draw (2.5,0.75) to[out=45,in=135,looseness=0.5] (6,0.75);

\end{scope}

\node at (-5.5,2.5) {\textcolor{blue}{\Huge{\ensuremath V}}};
\draw[thick,dashed,-latex,shorten >= 3pt] (-2,3.5) to[out=15,in=155] (2.5,3.25); \node at (0.75,4.15) {\Large{$\Lambda_p$}};
\node at (7.25,2.5) {\textcolor{red}{\Huge{\ensuremath U}}};

\draw[thick,red] (2,1) to[out=90,in=180,looseness=1.8] (4.5,3.5);
\draw[thick,red] (4.5,3.5) to[out=45,in=90,looseness=1.25] (6.5,2.5);
%\draw[thick,red] (6.5,2.5) to[out=-45,in=100,looseness=1] (7.5,1.5);
%\draw[thick,red] (6.5,2.5) to[out=-45,in=10,looseness=1.5] (6.5,0);
%\draw[thick,red] (4.5,0.5) to[out=-10,in=170,looseness=2] (6.5,0);
\draw[thick,red] (6.5,2.5) to[out=-90,in=-20,looseness=1.75] (4.5,0.5);
%\draw[thick,red] (2,1) to[out=-60,in=160,looseness=2] (4.5,0.5);
%\draw[thick,red] (2.5,3.5) to[out=190,in=190,looseness=2] (4.5,0.5);
\draw[thick,red] (2,1) to[out=-75,in=170,looseness=1] (4.5,0.5);

\end{tikzpicture}
\end{center}

\end{document}

答案1

以下并不完全相同:拓扑结构中的网格接触边界(如果您不想要这样,您可以用bluewhite, double=bluered替换white, double=red以获得一点距离,这个绝妙的想法不是我的,所有赞美都归功于Torbjørn T.),并将其$\Lambda_p$稍微向左移动。

这利用了保存路径(只需要让拓扑的边界覆盖网格的虚线)、选项clip、将多个\draw指令组合成单个路径、\foreach用于波浪形网格和grid用于矩形网格、将$\Lambda_p$节点自动放置在箭头路径的中心、将$U$和组合$V$在同一路径上以便只需要font=\Huge一次,也许还有一些其他小的更改。哦,我从序言中删除了不相关的包并使用了standalone

\documentclass[border=3.14,tikz]{standalone}

\begin{document}
\begin{tikzpicture}
  \path[font=\Huge]
    (-5.5,  2.5) node[blue] {$V$}
    ( 7.25, 2.5) node[red]  {$U$};

  \path[save path=\pathV]
    (-6, 0.5) to[out= 80, in=310]                (-4.5, 3)
              to[out=140, in=160]                (-3,   4)
              to[out=-30, in=160]                (-1,   3)
              to[out=-20, in=100]                ( 0,   2)
              to[out=-90, in= 15]                (-1,   0.5)
              to[out=190, in=-10]                (-2,   0.75)
              to[out=170, in= 10, looseness=1.5] (-5,   0)
              to[out=190, in=270, looseness=1.5] (-6,   0.5);
  \begin{scope}
    \clip[use path=\pathV];
    \draw[very thin, dashed, step=0.5] (-6, 0) grid (0, 4);
  \end{scope}
  \draw[thick, blue, use path=\pathV];

  \path[save path=\pathU]
    (2, 1) to[out= 90, in=180, looseness=1.8]  (4.5, 3.5)
           to[out= 45, in= 90, looseness=1.25] (6.5, 2.5)
           to[out=-90, in=-20, looseness=1.75] (4.5, 0.5)
           to[out=170, in=-75]                 (2,   1);
  \begin{scope}
    \clip[use path=\pathU];
    \draw[very thin, dashed, out=280, in=80, looseness=1.5]
      \foreach\x in{2, 2.5, ..., 6}
        {
          (\x, 4) to (\x, 3.5)
                  to (\x, 3.25)
                  to (\x, 2.25)
                  to (\x, 1.25)
                  to (\x, 0.75)
                  to (\x, 0.25)
        }
      \foreach\y in{3.25, 2.75, ..., 0.25}
        {(2, \y) to[out=45, in=135, looseness=0.5] (6.5, \y)};
  \end{scope}
  \draw[thick, red, use path=\pathU];

  \draw[thick, dashed, -latex, shorten >= 3pt]
    (-2,   3.5)
      to[out=15, in=155] node[above, font=\Large]{$\Lambda_p$}
    ( 2.5, 3.25);
\end{tikzpicture}
\end{document}

比较

新输出:

在此处输入图片描述

您的原始输出:

在此处输入图片描述

代码大小

行(无空行)

  • 您的:86 或 76(另外没有注释行)

  • 我的:47

字符(不含空格和换行符)

  • 你的:3381

  • 我的:1198(这个号码已经过时了,因为也looseness=1被删除了,谢谢黑色温和

相关内容