2

The context

I know how to display a cross inside a rectangle (see minimal working example and image below)

\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}

enter image description here

I also know how to display a rectangle inside a rectangle (see minimal working example and image below)

\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}

enter image description here

The problem

I wanted to merge the two previously explained concepts, so that I can draw a node that contains both the cross and the small rectangle. For this reason, I wrote the following document (mystyle1 and mystyle2 are the same styles that were showed above)

\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}

However, when compiling the document using pdflatex from TeX Live 2020. I get the following error

! 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};
                                      
? 

The question

In a tikzpicture environment, how can I draw a node that contains a cross and a smaller rectangle (similar to the ones showed in the images above)?

2 Answers 2

1

Instead of creating a new node in mystyle1, you can draw the cross using edges:

enter image description here

\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}
0

You need append commands inside the previous node. So you only need one style for that:

\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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .