TikZ 箭头:多色

TikZ 箭头:多色

关于此代码,我对箭头的颜色有疑问:我想制作两个箭头,其中第一个箭头以蓝色开始,以红色结束。为什么如果我编写此代码,LaTeX 会为箭头下方的空间着色?

代码

\PassOptionsToPackage{svgnames, x11names}{xcolor}
\documentclass{article}
\usepackage[a4paper,top=3cm,bottom=3cm,left=1.5cm,right=1.5cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage{amssymb}
\usepackage{extarrows}
\usepackage{stix}
\usepackage{tikz}
%\usetikzlibrary{decorations.markings}
\usepackage[bb=ams]{mathalpha}

\newcommand{\Tonde}[1]{\left(#1\right)}

\newcommand{\tonde}[1]{\bigl(#1\bigr)}

\begin{document}

\begin{center}
\begin{tikzpicture}
  \draw[thick, dashed, -latex, shorten >= 3pt, left color = blue, right color = red]
    (-2,   3.5)
      to[out=15, in=155] node[above, font=\Large]{$f$}
    ( 2.5, 3.25);
  \draw[thick, dashed, -latex, shorten >= 3pt, left color = green, right color = Silver]
    (-1.25,2.75)
      to[out=15, in=155] node[above, font=\Large]{$\bar f$} node[below, font=\Large]{$\eqqsim$}
    ( 3.00,2.25);

\end{tikzpicture}
\end{center}

\end{document}

第一次编辑:我将以前的代码编辑成

\PassOptionsToPackage{svgnames, x11names}{xcolor}
\documentclass[a4paper]{article}
\usepackage{stix}
\usepackage{tikz}
\usetikzlibrary{fadings,decorations.pathmorphing}

\makeatletter
\newif\iftikz@shading@path
\tikzset{
    % Need this for the bounding box calculation of the
    % fading and the rectangle that bounds the fading.
    % If set to zero things (e.g., arrows) may get cut off.
    fading sep/.store in=\fadingsep,
    fading sep=0.25cm,
    shaded path/.code={%
        % Prevent this stuff happning recursively.
        \iftikz@shading@path%
        \else%
            \tikz@shading@pathtrue%
            \tikz@addmode{%
                % Interrupt the picture to create a fading.
                \pgfinterruptpicture%
                    \begin{tikzfadingfrompicture}[name=.]
                        \path [shade=none,fill=none]#1;%
                        % Need to set the bounding box manually. Include the \fadingsep border.
                        \xdef\fadingboundingbox{{\noexpand\pgfpoint{\the\pgf@picminx-\fadingsep}{\the\pgf@picminy-\fadingsep}}%
                            {\noexpand\pgfpoint{\the\pgf@picmaxx+\fadingsep}{\the\pgf@picmaxy+\fadingsep}}}%
                        \expandafter\pgfpathrectanglecorners\fadingboundingbox%
                        \pgfusepath{discard}%
                    \end{tikzfadingfrompicture}%
                \endpgfinterruptpicture%
                % Install a rectangle that covers the shaded/faded path.
                \expandafter\pgfpathrectanglecorners\fadingboundingbox%
                % Make the fading happen.
                \def\tikz@path@fading{.}%
                \tikz@mode@fade@pathtrue%
                \tikz@fade@adjustfalse%
                \pgfpointscale{0.5}{\expandafter\pgfpointadd\fadingboundingbox}%
                \def\tikz@fade@transform{shift={(\the\pgf@x,\the\pgf@y)}}%
            }%
        \fi%
    }
}
\begin{document}

\begin{tikzpicture}[line cap=round, line join=round]

\path[font=\Huge]
    (-5.5,  2.5) node[blue] {$U$}
    ( 7.25, 2.5) node[red]  {$V$}
    (-3.5,  2.5) node[green] {$U'$}
    ( 5.75, 2.5) node[Silver]  {$V'$};
  \path[save path=\pathU]
    (-6, 0.5) to[out= 80, in=310, looseness=1]   (-4.5, 3)
              to[out=140, in=160, looseness=1]   (-3,   4)
              to[out=-30, in=160]                (-1,   3)
              to[out=-20, in=100]                ( 0,   2)
              to[out=-90, in= 15, looseness=1]   (-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=\pathU];
    \draw[very thin, dashed, cyan, step=0.5] (-6, 0) grid (0, 4);
  \end{scope}
  \draw[thick, blue, use path=\pathU];

  \path[save path=\pathV]
    (2.25, 1) to[out= 90, in=180, looseness=1.8]  (4.50, 3.50)
              to[out= 45, in= 90, looseness=1.25] (6.50, 2.25)
              to[out=-90, in=-20, looseness=1.75] (4.50, 0.50)
              to[out=170, in=-75, looseness=1]    (2.25, 1   );
  \begin{scope}
    \clip[use path=\pathV];
    \draw[very thin, dashed, orange]
      \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 (6.5, \y)};
  \end{scope}
  \draw[thick, red, use path=\pathV];

   \path [left color=red, right color=blue, shaded path={ 
    [draw=transparent!0, ultra thick, -stealth] (-2,   3.5)
      to[out=15, in=155] node[above, font=\Large]{$f$}
    ( 2.5, 3.25);
   }];
  
  \path [left color=green, right color=Silver, shaded path={ 
    [draw=transparent!0, ultra thick, -stealth]
    (-1.25,2.75)
      to[out=15, in=155] node[above, font=\Large]{$\bar f$} node[below, font=\Large]{$\eqqsim$}
    ( 3.00,2.25);
    }];
    
  \draw[thick, dashed, -latex, shorten >= 3pt, color = Goldenrod] (4,2.25) to[out=240, in=-60] node[below, font=\Large]{$g=f^{-1}$} (-2,2);
  
  \begin{scope}[thick]
    \draw[color = green]  (-2,2.00) circle (1cm);
    \draw[color = Silver] ( 4,2.25) circle (1cm);
    \draw[color = green] (-2,2) circle (1.5pt) node[above]{$u$};
    \draw[color = Silver] (4,2.25) circle (1.5pt) node[above]{$v$};
  \end{scope}

\end{tikzpicture}

\end{document}

第二次编辑:编译新代码,我得到了一个不想要的结果:我想要的是创建我之前描述的两个箭头。

第三次编辑:我修正了我的代码,但没有箭头。此外,节点上出现了彩色方块(为什么?)。有人可以修正我的代码吗?

太感谢了

答案1

最后我自己找到了解决方案,现在发布在这里

\PassOptionsToPackage{svgnames, x11names}{xcolor}
\documentclass[a4paper]{article}
\usepackage{stix}
\usepackage{tikz}
\usetikzlibrary{fadings,decorations.pathmorphing}

\makeatletter
\newif\iftikz@shading@path
\tikzset{
    % Need this for the bounding box calculation of the
    % fading and the rectangle that bounds the fading.
    % If set to zero things (e.g., arrows) may get cut off.
    fading sep/.store in=\fadingsep,
    fading sep=0.25cm,
    shaded path/.code={%
        % Prevent this stuff happning recursively.
        \iftikz@shading@path%
        \else%
            \tikz@shading@pathtrue%
            \tikz@addmode{%
                % Interrupt the picture to create a fading.
                \pgfinterruptpicture%
                    \begin{tikzfadingfrompicture}[name=.]
                        \path [shade=none,fill=none]#1;%
                        % Need to set the bounding box manually. Include the \fadingsep border.
                        \xdef\fadingboundingbox{{\noexpand\pgfpoint{\the\pgf@picminx-\fadingsep}{\the\pgf@picminy-\fadingsep}}%
                            {\noexpand\pgfpoint{\the\pgf@picmaxx+\fadingsep}{\the\pgf@picmaxy+\fadingsep}}}%
                        \expandafter\pgfpathrectanglecorners\fadingboundingbox%
                        \pgfusepath{discard}%
                    \end{tikzfadingfrompicture}%
                \endpgfinterruptpicture%
                % Install a rectangle that covers the shaded/faded path.
                \expandafter\pgfpathrectanglecorners\fadingboundingbox%
                % Make the fading happen.
                \def\tikz@path@fading{.}%
                \tikz@mode@fade@pathtrue%
                \tikz@fade@adjustfalse%
                \pgfpointscale{0.5}{\expandafter\pgfpointadd\fadingboundingbox}%
                \def\tikz@fade@transform{shift={(\the\pgf@x,\the\pgf@y)}}%
            }%
        \fi%
    }
}
\begin{document}

\Large


\begin{tikzpicture}[line cap=round, line join=round]

\path[font=\Huge]
    (-5.5,  2.5) node[blue] {$U$}
    ( 7.25, 2.5) node[red]  {$V$}
    (-3.5,  2.5) node[green] {$U'$}
    ( 5.75, 2.5) node[Silver]  {$V'$};
  \path[save path=\pathU]
    (-6, 0.5) to[out= 80, in=310, looseness=1]   (-4.5, 3)
              to[out=140, in=160, looseness=1]   (-3,   4)
              to[out=-30, in=160]                (-1,   3)
              to[out=-20, in=100]                ( 0,   2)
              to[out=-90, in= 15, looseness=1]   (-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=\pathU];
    \draw[very thin, dashed, cyan, step=0.5] (-6, 0) grid (0, 4);
  \end{scope}
  \draw[thick, blue, use path=\pathU];

  \path[save path=\pathV]
    (2.25, 1) to[out= 90, in=180, looseness=1.8]  (4.50, 3.50)
              to[out= 45, in= 90, looseness=1.25] (6.50, 2.25)
              to[out=-90, in=-20, looseness=1.75] (4.50, 0.50)
              to[out=170, in=-75, looseness=1]    (2.25, 1   );
  \begin{scope}
    \clip[use path=\pathV];
    \draw[very thin, dashed, orange]
      \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 (6.5, \y)};
  \end{scope}
  \draw[thick, red, use path=\pathV];

   \path [left color=blue, right color=red, shaded path={ 
    [draw=transparent!0, dashed, thick, -stealth]
    (-2,   3.5) to[out=15, in=155] ( 2.5, 3.25);
    }];
  
  \path [left color=green, right color=Silver, shaded path={
    [draw=transparent!0, thick, dashed, -stealth, -latex, shorten >= 3pt] (-1.25,2.75) to[out=15, in=155] ( 3.00,2.25);
    }];
    
  \draw[thick, dashed, -latex, shorten >= 3pt, color = Goldenrod] (4,2.25) to[out=240, in=-60] node[below, font=\Large]{$g=f^{-1}$} (-2,2);
  
  \begin{scope}[thick]
    \draw[color = green]  (-2,2.00) circle (1cm);
    \draw[color = Silver] ( 4,2.25) circle (1cm);
    \draw[color = green] (-2,2) circle (1.5pt) node[above]{$u$};
    \draw[color = Silver] (4,2.25) circle (1.5pt) node[above]{$v$};
  \end{scope}
  
  \begin{scope}
   \node at (0.25, 4.25) {$f$};
   \node at (0.75, 3.25) {$\bar f$};
   \node at (0.75, 2.50) {$\eqqsim$};
\end{scope}

\end{tikzpicture}

\end{document}

事实上,得到的输出正是我想要的,即

tikzpicture 中的多色箭头

相关内容