如何在 TikZ 中的节点之间绘制矩形?

如何在 TikZ 中的节点之间绘制矩形?

我有一个如下所示的 beamer 框架。它绘制了 tikzpeople 的两个节点,然后在它们之间绘制了一个箭头。但是,我想在箭头中间绘制一个矩形,并在矩形内写一些文本。所以类似于“NODE --[some text]--> NODE”。我如何绘制一个封闭的矩形并用文本填充它?

\documentclass[xcolor=table,10pt,aspectratio=169]{beamer}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[american]{babel}
\usepackage{amsmath,amssymb,amsthm}
\usepackage[beamer,customcolors]{hf-tikz}
\usepackage{booktabs} % Tables
\usepackage{tikz}
\usetikzlibrary{
    arrows,
    calc,
    chains,
    decorations,
    decorations.text,
    decorations.pathmorphing,
    matrix,
    overlay-beamer-styles,
    positioning,
    shapes,
    tikzmark
}
\usepackage{tikzpeople}
\tikzset{hl/.style={
    set fill color=red!80!black!40,
    set border color=red!80!black,
  },
}

\begin{document}

\begin{frame}{Test}
\begin{figure}
    \begin{tikzpicture}[font=\small]
        \node[alice,minimum size=1.5cm] (A) {};
        \node[bob,right=8cm of A,minimum size=1.5cm,mirrored] (B) {};
        \draw (A.0) edge[->] node[above] {} (B.180);
    \end{tikzpicture}
\end{figure}    
\end{frame}

\end{document}

答案1

这里有两种解决方案。第一种是绘制节点并用白色填充以覆盖其后面的线条。第二种是当您想尊重背景时更好的选择。在这种情况下,您先绘制节点,然后再绘制它们之间的两条线。

\documentclass[xcolor=table,10pt,aspectratio=169]{beamer}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[american]{babel}
\usepackage{amsmath,amssymb,amsthm}
\usepackage[beamer,customcolors]{hf-tikz}
\usepackage{booktabs} % Tables
\usepackage{tikz}
\usetikzlibrary{
    arrows,
    calc,
    chains,
    decorations,
    decorations.text,
    decorations.pathmorphing,
    matrix,
    overlay-beamer-styles,
    positioning,
    shapes,
    tikzmark
}
\usepackage{tikzpeople}
\tikzset{hl/.style={
    set fill color=red!80!black!40,
    set border color=red!80!black,
  },
}

\begin{document}

\begin{frame}{Test}
\begin{figure}
    \begin{tikzpicture}[font=\small]
        \node[alice,minimum size=1.5cm] (A) {};
        \node[bob,right=8cm of A,minimum size=1.5cm,mirrored] (B) {};
        \draw (A) edge[->] node[draw, fill=white, text width=2cm, align=center] {some text inside a rectangle} (B);
    \end{tikzpicture}
\end{figure}    

\begin{figure}
    \begin{tikzpicture}[font=\small]
        \node[alice,minimum size=1.5cm] (A) {};
        \node[bob,right=8cm of A,minimum size=1.5cm,mirrored] (B) {};
        \path (A) -- node[draw, text width=2cm, align=center] (C) {some text inside a rectangle} (B);
        \draw[->] (A)--(C);
        \draw[->] (C)--(B);
    \end{tikzpicture}
\end{figure}    

\end{frame}

\end{document}

在此处输入图片描述

答案2

在此处输入图片描述

\documentclass[xcolor=table,10pt,aspectratio=169]{beamer}

%\usepackage[T1]{fontenc}
%\usepackage[utf8]{inputenc}
%\usepackage[american]{babel}
%\usepackage{amsmath,amssymb,amsthm}
%\usepackage[beamer,customcolors]{hf-tikz}
%\usepackage{booktabs} % Tables
\usepackage{tikz}
\usetikzlibrary{
    arrows,
    calc,
    chains,
    decorations,
    decorations.text,
    decorations.pathmorphing,
    matrix,
    overlay-beamer-styles,
    positioning,
    shapes,
    tikzmark
}
%\usepackage{tikzpeople}
%\tikzset{hl/.style={
%       set fill color=red!80!black!40,
%       set border color=red!80!black,
%   },
%}

\begin{document}
    
    \begin{frame}{Test}
%       \begin{figure}
            \begin{tikzpicture}[font=\small]
                \node[]at(0,0) (A) {};
                \node[]at(5,0) (B) {};
                \draw (A.0) edge[->,]node [midway, fill=white] {Label Text} (B.180);
            \end{tikzpicture}
%       \end{figure}    
    \end{frame}
    
\end{document}

相关内容