如何在 TikZ 中在文本周围绘制形状?

如何在 TikZ 中在文本周围绘制形状?

请看下图。右箭头由 组成&。我只想&通过在其周围绘制一个圆形、正方形或其他形状来突出显示它。该形状将填充灰色。我该怎么做? 在此处输入图片描述 此外,如果您有更好的想法来强调这一点&,那么请毫不犹豫地分享。

平均能量损失

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz, }
\usetikzlibrary{matrix, arrows.meta}
\begin{document}
    \begin{tikzpicture}
        [mat/.style={matrix of nodes,
            nodes={draw, minimum size=10mm, minimum width=15mm, fill=gray!10,   anchor=north, },
            column sep=-\pgflinewidth, 
            row sep=0.5mm, 
            nodes in empty cells, 
            row 1 column 1/.style={nodes={draw=none}},
            row 2 column 1/.style={nodes={draw=none, fill=none}},
            row 2 column 2/.style={nodes={draw=none, fill=none}},
            row 2 column 8/.style={nodes={draw=none, fill=none}},
            row 2 column 9/.style={nodes={draw=none, fill=none}},
            row 5 column 4/.style={nodes={draw=none, fill=none}},
            row 5 column 5/.style={nodes={draw=none, fill=none}},
            row 5 column 6/.style={nodes={draw=none, fill=none}},
            row 1/.style={nodes={font=\ttfamily, draw=none, fill=none, minimum size=5mm}}, 
            row 3/.style={nodes={font=\ttfamily, draw=none, fill=none, minimum size=5mm}}, 
            row 4/.style={nodes={font=\ttfamily, draw=none, fill=none, minimum size=5mm}}, 
        }]
        
        \matrix[mat] (array) 
        { 
            &   &   &   &   dhaka   &   &   &   &       \\
            &   &   &   &   19      &   &   &   &       \\
            &   &   & 20021 &   20022 & 20023 & &   &       \\
            &   sylhet  &   &   &           &   &   &   khulna  &       \\
            & 19    &   &   &           &   &   &   20022 &     \\
        }; 
        
        
        \draw [-{Latex[length=3mm]}]  (array-2-5.south)--(array-5-8.north) node [near end, above, sloped] (TextNode) {\&}; 
        \draw [-{Latex[length=3mm]}]  (array-2-5.south)--(array-5-2.north); 
        
    \end{tikzpicture}
\end{document}

答案1

draw, circle只需在 的选项中使用node,以及fill=gray!10来填充它:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz, }
\usetikzlibrary{matrix, arrows.meta}
\begin{document}
    \begin{tikzpicture}
        [mat/.style={matrix of nodes,
            nodes={draw, minimum size=10mm, minimum width=15mm, fill=gray!10,   anchor=north, },
            column sep=-\pgflinewidth, 
            row sep=0.5mm, 
            nodes in empty cells, 
            row 1 column 1/.style={nodes={draw=none}},
            row 2 column 1/.style={nodes={draw=none, fill=none}},
            row 2 column 2/.style={nodes={draw=none, fill=none}},
            row 2 column 8/.style={nodes={draw=none, fill=none}},
            row 2 column 9/.style={nodes={draw=none, fill=none}},
            row 5 column 4/.style={nodes={draw=none, fill=none}},
            row 5 column 5/.style={nodes={draw=none, fill=none}},
            row 5 column 6/.style={nodes={draw=none, fill=none}},
            row 1/.style={nodes={font=\ttfamily, draw=none, fill=none, minimum size=5mm}}, 
            row 3/.style={nodes={font=\ttfamily, draw=none, fill=none, minimum size=5mm}}, 
            row 4/.style={nodes={font=\ttfamily, draw=none, fill=none, minimum size=5mm}}, 
        }]
        
        \matrix[mat] (array) 
        { 
            &   &   &   &   dhaka   &   &   &   &       \\
            &   &   &   &   19      &   &   &   &       \\
            &   &   & 20021 &   20022 & 20023 & &   &       \\
            &   sylhet  &   &   &           &   &   &   khulna  &       \\
            & 19    &   &   &           &   &   &   20022 &     \\
        }; 
        
        
        \draw [-{Latex[length=3mm]}]  (array-2-5.south)--(array-5-8.north)
          node [near end, above, sloped, draw, circle, inner sep=1pt,
          fill=gray!10] (TextNode) {\&}; 
        \draw [-{Latex[length=3mm]}]  (array-2-5.south)--(array-5-2.north); 
        
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容