Tikz 路径上的方框文本

Tikz 路径上的方框文本

需要将第一张图片上的文字框起来(实线,不是虚线),但线条要用虚线……这是 MWE:

            \documentclass[10.5pt,b5paper]{memoir}
            \usepackage[english]{babel}
            \usepackage[utf8]{inputenc}
            \usepackage[T1]{fontenc}
            \usepackage{graphicx}% http://ctan.org/pkg/graphicx
            \usepackage{xcolor} %
            \definecolor{gr}{RGB}{200,250,200}
            \usepackage{tikz}
            \usetikzlibrary{shapes,arrows,automata}
             \usetikzlibrary{arrows,decorations.pathmorphing,decorations.pathreplacing,
            decorations.shapes,shadows,backgrounds,decorations.markings}

            \begin{document}
            \begin{figure}[!h]
            \begin{center}
            \begin{tikzpicture}
            [auto,
            block/.style ={rectangle, draw=gr!10!black, thick, fill=gr,
            text width=7.1em,align=center, minimum height=4em, decorate, decoration={random steps,segment length=3pt,amplitude=1pt}},
            line/.style ={draw, thick, densely dashed, -latex',shorten >=2pt},
            cloud/.style ={rectangle, rounded corners, fill=blue!20, minimum height=7em, text width=7.1em, align=center}]
            \matrix [column sep=14mm,row sep=11mm]
            %draw,text width=100pt, text centered, line width=1pt, fill=gr, minimum height=2cm, minimum width=2.5cm, decorate, decoration={random steps,segment length=3pt,amplitude=1.5pt}
            {
            &\node [block] (p1) {Programm 1}; &\\
            \node [cloud] (keyboard) {Keyboard}; &
            \node [block] (p2) {Programm 2}; &
            \node [cloud] (display) {Terminal}; &\\
            &\node [block] (p3) {Programm 3}; &\\
            };
            \begin{scope}[every path/.style=line]
            \path (keyboard) |- node {\small{\hspace{2cm} test1 }} (p1);
            \path (p1) -- node[sloped,above] {\small{test1}} (p2);
            \path (p1)  edge [bend left=35] node[sloped,above,red] {\small{test 2}} (display);
            \path (p2) -- node[sloped,above,red] {\small{test1}} (display);
            \path (p3) -- node[sloped,above,red] {\small{test1}} (display);
            \path (p2) -- node[sloped,above] {\small{test1}} (p3);
            \path (p3) edge [bend left=-35] node[sloped,above] {\small{test 2}} (display);
            \end{scope}
            \end{tikzpicture}
            \vspace{-\baselineskip}
            \end{center}
            \caption{I want texts on lines to be in box with solid line like below}
            \end{figure}



            \begin{tikzpicture}
            [auto,
            block/.style ={rectangle, draw=gr!10!black, thick, fill=gr,
            text width=7.1em,align=center, minimum height=4em, decorate, decoration={random steps,segment length=3pt,amplitude=1pt}},
            line/.style ={draw, thick, densely dashed,  -latex',shorten >=2pt},
            cloud/.style ={rectangle, rounded corners, fill=blue!20, minimum height=7em, text width=7.1em, align=center}]
            \matrix [column sep=34mm,row sep=11mm]
            {
            \node [block] (p) {Programm}; &
            \node [cloud] (display) {Terminal}; &\\
            };
            \begin{scope}[every path/.style=line]
            \path decorate[decoration={markings, mark connection node=my node, mark=at position .5 with
            {\node [draw,red,transform shape] (my node) {How to make this box,but not line, solid?};} }] {(p) -- (display)};
            \end{scope}
            \end{tikzpicture}

            \end{document}

答案1

这是一个可能的解决方案。

在此处输入图片描述

基本上,重要的部分是:

node[draw,solid,above=3pt,near end]在直角箭头的末端附近插入文本节点。

node[draw,solid,above=3pt]以实心样式在节点周围稍微向上绘制框架,这样框架就不会覆盖箭头。

\documentclass{standalone}
\usepackage{xcolor} %
\definecolor{gr}{RGB}{200,250,200}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,automata}
\usetikzlibrary{arrows,decorations.pathmorphing,decorations.pathreplacing,
decorations.shapes,shadows,backgrounds,decorations.markings}

\begin{document}
\begin{tikzpicture}[%
    auto,
    block/.style={%
        rectangle,
        draw=gr!10!black,
        thick,
        fill=gr,
        text width=7.1em,
        align=center,
        minimum height=4em,
        decorate,
        decoration={random steps,segment length=3pt,amplitude=1pt}
    },
    line/.style={%
        draw,
        thick,
        densely dashed,
        -latex',
        shorten >=2pt
    },
    cloud/.style={%
        rectangle,
        rounded corners,
        fill=blue!20,
        minimum height=7em,
        text width=7.1em,
        align=center
    }
]
\matrix[column sep=14mm,row sep=11mm]
{
    &   \node [block] (p1) {Programm 1};            & \\
        \node [cloud] (keyboard) {Keyboard};    &
        \node [block] (p2) {Programm 2};            &
        \node [cloud] (display) {Terminal};     & \\
    &   \node [block] (p3) {Programm 3};            & \\
};
\begin{scope}[every path/.style=line]
    \path (keyboard) |- node[draw,solid,above=3pt,near end] 
                {\small{test 1}} (p1);
    \path (p1) -- node[sloped,above=3pt,draw,solid]
                {\small{test 1}} (p2);
    \path (p1)  edge [bend left=35] node[sloped,above=3pt,red,draw,solid] 
                {\small{test 2}} (display);
    \path (p2) -- node[sloped,above=3pt,red,draw,solid] 
                {\small{test 1}} (display);
    \path (p3) -- node[sloped,above=3pt,red,draw,solid] 
                {\small{test 1}} (display);
    \path (p2) -- node[sloped,above=3pt,draw,solid] 
                {\small{test 1}} (p3);
    \path (p3) edge [bend left=-35] node[sloped,below=3pt,draw,solid] 
                {\small{test 2}} (display);
\end{scope}
\end{tikzpicture}

\end{document}

相关内容