在 Tikz 中向标签添加换行符

在 Tikz 中向标签添加换行符

如何在 Tikz 中向图表的标签(描述)添加换行符?这是我的代码:

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing,arrows.meta,calc,chains,shapes.geometric,backgrounds,fit,arrows,positioning}

\tikzset{
  neu/.style={
  draw,
  circle,
  minimum size=1cm,
  },
  declare function={
        sigma(\x) = 1/(1+exp(-\x));
        sigmap(\x) = sigma(\x)*(1-sigma(\x));
    },
  mem/.style={
  draw,
  circle,
  minimum size=0.5cm,
  }
input/.style={coordinate},
}

\begin{document}

\begin{figure*}
\begin{adjustbox}{width=\textwidth,height=\textheight,keepaspectratio}
\begin{tikzpicture}[auto,>=latex']
    % We start by placing the blocks
    \node [input, name=input1] {};
    %fuzzification
    \node [neu, right = 1.3 of input1, path picture={
    \draw[shift={(path picture bounding box.center)},
        domain=-0.30:0.30, samples=50,red,
    ] plot ({\x},{1*(sigmap(20*\x)-0.1)});
}][label={[xshift=0cm, yshift=0.2cm]\textcolor{blue}{\small Membership function}}] (neu) {};;

\draw [draw,->] (input1) -- node {\footnotesize{$input$}} (neu);


\end{tikzpicture}
\end{adjustbox}
\caption{My caption}\label{fig4}
\end{figure*}
\end{document}

结果如下: 在此处输入图片描述

这就是我想要的: 在此处输入图片描述

答案1


和这个类似吗?

在此处输入图片描述

编辑:
现在,当sigmap已知功能并从文档示例中删除所有错误时,MWE 可以是:

\documentclass[margin=3mm]{standalone}
%\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                %backgrounds,
                %calc, chains,
                %fit,
                %decorations.pathreplacing,
                quotes,
                positioning,
                %shapes.geometric
                }

\begin{document}
%    \begin{figure}[ht]
\begin{tikzpicture}[auto,>=Latex,
node distance = 0mm and 15mm,
   neu/.style = {circle, draw, minimum size=1cm},
every label/.append style = {font=\small, text=blue, 
                             align=center}, % added for multi lines labels
every edge quotes/.style  = {auto, font=\footnotesize\itshape, inner sep=1pt},
declare function={
        sigma(\x) = 1/(1+exp(-\x));
        sigmap(\x) = -0.25+2*sigma(\x)*(1-sigma(\x));},                    
                ]
% We start by placing the blocks
\coordinate (in);
\node[neu, right = of in,
      label=Membership\\ function] (neu) {};
\draw[red, thick] plot [domain=-6:6, samples=101] (2 + \x/20,{sigmap(\x)});
%
\draw [draw,->] (in) to["input"] (neu);
    \end{tikzpicture}
%\caption{My caption}
%\label{fig4}
%   \end{figure*}
\end{document}

附录: 更复杂一点的解决方案:

  • 函数是使用append after command节点选项绘制的节点的一部分:
  • x节点中的函数位置由从节点名称中提取的内容确定
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                calc, quotes,
                positioning,
                }
\makeatletter
\newcommand{\gettikzxy}[3]% <--- for extract x, y coordinates from node name
{%        
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \edef#2{\the\pgf@x}%
  \edef#3{\the\pgf@y}%
}
\makeatother

\begin{document}
    \begin{tikzpicture}[auto,>=Latex,
declare function={
        sigma(\x)  = 1/(1+exp(-\x));
        sigmap(\x) = -0.25+2*sigma(\x)*(1-sigma(\x));},
node distance = 0mm and 15mm,
   mem/.style = {circle, draw, minimum size=1cm,
    append after command={\pgfextra{\let\LN\tikzlastnode
                \gettikzxy{(\LN.center)}{\ax}{\ay}  %<--- for extrax x, x coordinate from \LN
                \draw[red, thick] plot [domain=-7:7, samples=101] (\ax + 1.2*\x,{sigmap(\x)});
                                   }% end \pgfextra
                         }% end append after command
                },
every label/.append style = {font=\small, text=blue, 
                             align=center}, % added for writing of multiline labels 
every edge quotes/.style  = {auto, font=\footnotesize\itshape, inner sep=1pt},
                ]
% We start by placing the blocks
\coordinate (in);
\node[mem, right = of in,
      label=Membership\\ function] (m) {};
% Input arrow
\draw [draw,->] (in) to["input"] (m);
    \end{tikzpicture}
\end{document}

结果和以前一样。

答案2

从 Zarko 的解决方案开始,但改变构造顺序,使曲线位于圆的中心(函数中的偏移量 2 被删除2 + \x/20,{sigmap(\x)}--> \x/20,{sigmap(\x)}。使用\ray命令更改此圆的大小并相应地放大曲线。

\documentclass[margin=3mm]{standalone}
%\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                %backgrounds,
                %calc, chains,
                %fit,
                %decorations.pathreplacing,
                quotes,
                positioning,
                %shapes.geometric
                }

\begin{document}
\newcommand{\ray}{2}
\begin{tikzpicture}[
    auto,>=Latex,
    node distance = 15mm,
    neu/.style = {circle, draw, minimum size=\ray cm},
    every label/.append style = {font=\small, text=blue, 
                            align=center}, % added for multi lines labels
    every edge quotes/.style  = {auto, font=\footnotesize\itshape, inner sep=1pt},
    declare function={
        sigma(\x) = 1/(1+exp(-\x));
        sigmap(\x) = -0.25+2*sigma(\x)*(1-sigma(\x));},                    
                ]
\draw[red, thick,scale=\ray] plot [domain=-6:6, samples=101] (\x/20,{sigmap(\x)});
\node[neu,label=Membership\\ function] (neu) {};
\coordinate [left = of neu] (in);
\draw [draw,->] (in) to["input"] (neu);
    \end{tikzpicture}

\end{document}

答案3

建议的代码在执行时出现了很多错误。因此,我从一个简化的代码开始:

\documentclass[10pt]{article}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}
        \draw (0,0) circle(3.5);
        \draw[red,line width=2pt] plot[line width=2pt,domain=-3:3] (\x,{1.5*cos(\x r)+1.5});
        \node[align=center,blue] at (0,4.5) {\Large\bfseries Membership\\ \Large\bfseries function};
        \draw[line width=2pt,-latex] (-8,0.5)--(-4,0.5) node[pos=.5,above] () {\large $input$};
    \end{tikzpicture}
\end{document}

输出:

在此处输入图片描述

相关内容