TikZ:沿路径的文本

TikZ:沿路径的文本

有一个节点到节点的路径,我想让文本与它对齐。此外,[xshift=5mm] 和 [xshift=-5mm] 应该确保路径末端不接触节点边界,但似乎没有任何效果。我读了手册的第 338-340 页。

%
%
%
\documentclass{standalone}

\usepackage{tikz,pgf}

\usetikzlibrary{decorations.markings,%
    snakes,fit,scopes,arrows,calc,shapes.misc,%
    shapes.arrows,chains,matrix,positioning,decorations.pathmorphing,shapes,backgrounds,%
    decorations.text}

\begin{document}

\tikzset{
    fuzzy/.style = {decorate, decoration = {random steps, segment length = 0.5mm, amplitude = 0.15pt}},
    ns/.style = {
        rectangle,
        minimum size = 6mm, 
        rounded corners = 3mm,
        very thick,
        draw,
        font = \ttfamily,
        fuzzy
    }
}

\newcommand{\bbrect}[3]{\node[rectangle,fuzzy,draw,fit=(B-#1-1.north west) (B-#2-1.south east), inner sep = 0pt] (block#3) {}}
\newcommand{\cbl}[2]{\node[rectangle,fuzzy,draw,fit=(C-#1-1.north west) (C-#1-1.south east), inner sep = 0pt] (cblock#2) {}}

\begin{tikzpicture} [
        >=latex, thick, 
        /pgf/every decoration/.style = {/tikz/sharp corners}, minimum size = 6mm
    ]
    \ttfamily
    \begin{scope}
            \matrix (B) [matrix of nodes, ampersand replacement = \&] {
                \hline
                {B}\\
                \hline
                {12}\\
                {7}\\
                    {2}\\
                {2}\\
                {2}\\
                {2}\\
                 {2}\\
                 {2}\\
                 {7}\\
                 {7}\\
                 {2}\\
                 {2}\\
                 {2}\\
                 {8}\\
                 {4}\\
                 {4}\\
                 {4}\\
                 {4}\\
                 {4}\\
                 {4}\\
                 {9}\\
                 {11}\\
                 {1}\\
                 {1}\\
                 {1}\\
                 {1}\\
                 {1}\\
                 {1}\\
                 {1}\\
                 {1}\\
                 {1}\\
                 {5}\\
                 {5}\\
                 {5}\\
                 {6}\\
                 {6}\\
                 {6}\\
                 {10}\\
                 {3}\\
                 {3}\\
                 {3}\\
                 {0}\\
                 {0}\\
                 {0}\\
                 {0}\\
                 {0}\\
                 {0}\\
                 {0}\\
                 {0}\\
                 {0}\\
            };
            \bbrect{2}{2}{1};
            \bbrect{3}{3}{2};
            \bbrect{4}{9}{3};
            \bbrect{10}{11}{4};
            \bbrect{12}{14}{5};
            \bbrect{15}{15}{6};
            \bbrect{16}{21}{7};
            \bbrect{22}{22}{8};
            \bbrect{23}{23}{9};
            \bbrect{24}{32}{10};
            \bbrect{33}{35}{11};
            \bbrect{36}{38}{12};
            \bbrect{39}{39}{13};
            \bbrect{40}{42}{14};
            \bbrect{43}{51}{15};
        \end{scope}

        \begin{scope}[xshift = 5cm]
            \matrix (C) at ([xshift = 5cm]B-1-1.north east) [anchor=C-1-1.north west, matrix of nodes, ampersand replacement = \&] {
                \hline
                {C}\\
                \hline
                {12}\\
                {7}\\
                {2}\\
                {7}\\
                {2}\\
                {8}\\
                {4}\\
                {9}\\
                {11}\\
                {1}\\
                {5}\\
                {6}\\
                {10}\\
                {3}\\       
                {0}\\
            };  
            \cbl{2}{1};
            \cbl{3}{2};
            \cbl{4}{3};
            \cbl{5}{4};
            \cbl{6}{5};
            \cbl{7}{6};
            \cbl{8}{7};
            \cbl{9}{8};
            \cbl{10}{9};
            \cbl{11}{10};
            \cbl{12}{11};
            \cbl{13}{12};
            \cbl{14}{13};
            \cbl{15}{14};
            \cbl{16}{15};
    \end{scope}

    \draw[postaction={decoration={text along path,text = {merging a contiguous run},text align=fit to path},decorate}]
        ([xshift=5mm]block7) -- ([xshift=-5mm]$(cblock7.north)!.5!(cblock7.south)$);

    \begin{pgfonlayer}{background}
        \foreach \i in {1,...,15} {
            \path[draw,dotted] (block\i.north east) -- (cblock\i.north west);
            \path[draw,dotted] (block\i.south east) -- (cblock\i.south west);
        }
    \end{pgfonlayer}

\end{tikzpicture}

\end{document}

答案1

为了避免触碰路径的末端,您可以添加pre length选项post length进行装饰,例如:

\draw[postaction={decoration={text along path,
  text = {merging a contiguous run}, text align=fit to path,
  pre length=2em, post length=2em},decorate}]
    (block7) -- ($(cblock7.north)!.5!(cblock7.south)$);

相关内容