4

Consider this simple tikz tree:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{trees,shapes,snakes}

\begin{document}

\tikzstyle{mynode} = [text width=4em, text centered]
\begin{tikzpicture}[]

\node{My Root}
child{node[mynode]{Good Morning}}
child{node[mynode, cross out]{Good Afternoon}}
child{node[mynode]{Good Evening}}
\end{tikzpicture}

\end{document}

This code produces the following:

enter image description here

However, I was expecting that the "Good afternoon" node should be crossed out, since there was something similar here.

How can I make a cancelling 'X' appear over that particular node?

geo909
  • 790

3 Answers3

7

You need to put ,draw along with cross out:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{trees,shapes,decorations}

\begin{document}

\tikzstyle{mynode} = [text width=4em, text centered]
\begin{tikzpicture}

\node{My Root}
child{node[mynode]{Good Morning}}
child{node[mynode, cross out,draw]{Good Afternoon}}
child{node[mynode]{Good Evening}};

\end{tikzpicture}

\end{document}

enter image description here

If you want colour, use draw=red or any colour.

  • Thanks, that was exactly what I needed, and the most simple solution. Could you provide a quick explanation why the 'draw' had to be added? – geo909 Sep 05 '14 at 04:43
  • 1
    @geo909 A node needs draw to have its borders drawn. Try \node {ABC}; and \node[draw]{ABC};. You will see the difference. Without draw only the contents are displayed. Otherwise, it is only a \path, a line without ink. –  Sep 05 '14 at 04:46
2

This is based on Jesse's answer but uses a style definition rather than nesting tikzpicture environments:

\documentclass[tikz]{standalone}

\usetikzlibrary{trees,shapes.misc}
\begin{document}

  \begin{tikzpicture}[
      mynode/.append style={text width=4em, align=center},
      tcancel/.append style={draw=#1, cross out, inner sep=1pt},
    ]

    \node{My Root}
    child{node[mynode]{Good Morning}}
    child{node[mynode,tcancel=red]{Good Afternoon}}
    child{node[mynode]{Good Evening}};
  \end{tikzpicture}

\end{document}

crossing out with styles

cfr
  • 198,882
1

This is one possible solution where \tcancel macro was defined. #1 = color preferred. #2 is the content.

\newcommand{\tcancel}[2][black]{%
\begin{tikzpicture}
\node[draw=#1,cross out,inner sep=1pt] (a){#2};
\end{tikzpicture}%
}

enter image description here

Code

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{trees,shapes,snakes}
\usetikzlibrary{shapes.misc}
\newcommand{\tcancel}[2][black]{%
\begin{tikzpicture}
\node[draw=#1,cross out,inner sep=1pt] (a){#2};
\end{tikzpicture}%
}
\begin{document}

\tikzstyle{mynode} = [text width=4em, text centered]
\begin{tikzpicture}[]

\node{My Root}
child{node[mynode]{Good Morning}}
child{node[mynode]{\tcancel[red]{Good Afternoon}}}
child{node[mynode]{Good Evening}};
\end{tikzpicture}

\end{document}
Jesse
  • 29,686
  • Isn't it problematic to use a tikzpicture within another one? That is, I've seen this said though I don't understand TikZ well enough to know. (On the other hand, this seems to be what happens if you use symbols from tikzsymbols in a tikzpicture and that seems to work OK.) – cfr Sep 03 '14 at 12:14
  • Hi @cfr, I am aware of your remark and this link http://tex.stackexchange.com/q/51193/34618. In addtion, I also tried (but failed just now) to find a 200 or 500 bounty question that I saw a couple of days ago in this site using nested tikz and got bounty. Personally, I do use nested tikz and did not get me errors. – Jesse Sep 03 '14 at 12:33