上下文

上下文

上下文

我知道如何在矩形内显示一个十字(见下面的最小工作示例和图像)

\documentclass[border = 2]{standalone}

\usepackage{tikz}

\usetikzlibrary{shapes.misc, fit}

\tikzset{
  mystyle1/.style = {
    rectangle,
    draw,
    append after command={
      node [
        fit = (\tikzlastnode),
        inner sep = -\pgflinewidth,
        cross out,
        draw = blue,
      ] {}
    }
  }
}

\begin{document}
\begin{tikzpicture}
  \node [mystyle1] {a};
\end{tikzpicture}
\end{document}

在此处输入图片描述

我还知道如何在矩形内显示一个矩形(参见下面的最小工作示例和图像)

\documentclass[border = 2]{standalone}

\usepackage{tikz}

\usetikzlibrary{shapes.misc, fit}

\tikzset{
  mystyle2/.style = {
    rectangle,
    draw,
    append after command={
      node [
        fit = (\tikzlastnode),
        inner sep = -\pgflinewidth,
        draw = blue,
        scale = 0.6
      ] {}
    }
  }
}

\begin{document}
\begin{tikzpicture}
  \node [mystyle2] {a};
\end{tikzpicture}
\end{document}

在此处输入图片描述

问题

我想把前面解释的两个概念合并起来,这样我就可以画一个既包含十字又包含小矩形的节点。为此,我写了下面的文档(mystyle1mystyle2上面展示的样式相同)

\documentclass[border = 2]{standalone}

\usepackage{tikz}

\usetikzlibrary{shapes.misc, fit}

\tikzset{
  mystyle1/.style = {
    rectangle,
    draw,
    append after command={
      node [
        fit = (\tikzlastnode),
        inner sep = -\pgflinewidth,
        cross out,
        draw = blue,
      ] {}
    }
  },
  mystyle2/.style = {
    rectangle,
    draw,
    append after command={
      node [
        fit = (\tikzlastnode),
        inner sep = -\pgflinewidth,
        draw = blue,
        scale = 0.6
      ] {}
    }
  }
}

\begin{document}
\begin{tikzpicture}
  \node [mystyle1, mystyle2] {a};
\end{tikzpicture}
\end{document}

但是,使用 TeX Live 2020 编译文档时,pdflatex出现以下错误

! Package pgf Error: No shape named `' is known.

See the pgf package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.40   \node [mystyle1, mystyle2] {a};
                                      
? 

问题

tikzpicture环境中,如何绘制一个包含十字和较小矩形的节点(类似于上图所示的节点)?

答案1

您不需要在 中创建新节点mystyle1,而是可以使用边绘制十字:

在此处输入图片描述

\documentclass[border = 2]{standalone}
\usepackage{tikz}
\usetikzlibrary{fit}

\tikzset{
  mystyle1/.style = {
    rectangle,
    draw,
    append after command={
      (\tikzlastnode.north west) edge[blue,shorten >=\pgflinewidth,,shorten <=\pgflinewidth] (\tikzlastnode.south east)
      (\tikzlastnode.north east) edge[blue,shorten >=\pgflinewidth,,shorten <=\pgflinewidth] (\tikzlastnode.south west)
    }
  },
  mystyle2/.style = {
    rectangle,
    draw,
    append after command={
      node [
        fit = (\tikzlastnode),
        inner sep = -\pgflinewidth,
        draw = blue,
        scale = 0.6
      ] {}
    }
  }
}

\begin{document}
\begin{tikzpicture}
  \node [mystyle1,mystyle2] {a};
\end{tikzpicture}
\end{document}

答案2

您需要在前一个节点内附加命令。因此,您只需要一种样式:

\documentclass[border = 2]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.misc, fit}

\tikzset{
mystyle/.style = 
{
rectangle,
draw,
append after command=
    {
    node [
        fit = (\tikzlastnode),
        inner sep = -\pgflinewidth,
        cross out,
        draw = blue,
        append after command={
        node [
            fit = (\tikzlastnode),
            inner sep = -\pgflinewidth,
            draw = blue,
            scale = 0.6
            ] {}
        }
        ] {}
    }
}
}

\begin{document}
\begin{tikzpicture}
\node [mystyle] {a};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容