0

I have the following automaton.

enter image description here

\begin{tikzpicture}[>=latex, >=stealth', auto, node distance=0.5cm and 1.5cm, semithick, initial text=]
  \node[state, initial] (q0) {$q_{0}$};
  \node[state] (q1) [above right = of q0] {$q_{1}$};
  \node[state] (q2) [below right = of q0] {$q_{2}$};

  \path[->, shorten >=1pt]
  (q0) edge node {$a$} (q1)
  (q0) edge [dashed] node {$a$} (q2)
  ;
\end{tikzpicture}

And I want to cross out one transition, so it will look something like this.

enter image description here

How do I add one bold red cross on the middle of the transition?

1
  • You can add the calc library and: \draw[red,thick] ($(q0)!0.5!(q2)$) ++ (45:0.2) --++ (225:0.4); \draw[red,thick] ($(q0)!0.5!(q2)$) ++ (135:0.2) --++ (315:0.4); Mar 20 at 14:00

1 Answer 1

2

You've got multiple ways to add something to a path/straight line (apart from figuring out where that point is manually).

As you know, you can add a node (like those $a$s) along an edge.

You could just use a node with a red X, i.e.

\tikz\path[->, shorten >=1pt] (q0) edge node {$a$} (q1)
  edge [dashed] node {$a$}
    node[red, marking, sloped] {\Huge$\times$} (q2);

I've set up the key marking so that it is the same as auto = false since you don't want that node to be put to the left or right of the line.

You can also use the cross out shape of the shapes.misc library where you can control the size of the cross by manipulating the size of the node with the usual means: inner xsep, inner ysep, minimum width, minimum height.

\tikz\path[->, shorten >=1pt] (q0) edge node {$a$} (q1)
  edge [dashed] node {$a$}
    node[cross out, draw=red, marking, sloped, solid,
         shorten >=0pt, inner ysep=.5em] {} (q2);

Or, with maybe a bit more control from the user-interface side of things, a pic that draws that cross manually. Here, I've set up a pic named cross that takes one optional argument that specifies (half) the length of the lines and uses the value of the key cross angle to tighten that angle of that cross.

\tikz\path[->, shorten >=1pt] (q0) edge node {$a$} (q1)
  edge [dashed] node {$a$}
    pic[red, marking, sloped, cross angle=10] {cross} (q2);

Since you're using shorten > and -> for the path with the edges these get passed down to the nodes and pics which is why I've had to add a few counter-corrections on these.


In your actual document, you best put all this in a nice style like

\tikzset{
  Cross/.style={
    sloped, auto=false, shorten >=+0pt, shorten >=+0pt, arrows=-, solid, …}}

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{automata, positioning, arrows.meta, shapes.misc}
\tikzset{
  marking/.style={auto=false},
  cross angle/.initial=0,
  cross/.pic={\draw[solid, arrows=-, shorten <=+0pt, shorten >=+0pt]
                   (225+\pgfkeysvalueof{/tikz/cross angle}:{#1})
                 --( 45+\pgfkeysvalueof{/tikz/cross angle}:{#1})
                   (135-\pgfkeysvalueof{/tikz/cross angle}:{#1})
                 --(315-\pgfkeysvalueof{/tikz/cross angle}:{#1});},
  pics/cross/.default=2.5mm}
\tikzset{% just for this answer
  every picture/.append style={
    >=Stealth, auto, node distance=0.5cm and 1.5cm, semithick, initial text=},
  every picture/.append code={
    \node[state, initial] (q0) {$q_{0}$};
    \node[state] (q1) [above right = of q0] {$q_{1}$};
    \node[state] (q2) [below right = of q0] {$q_{2}$};}}
\begin{document}
\tikz\path[->, shorten >=1pt] (q0) edge node {$a$} (q1)
  edge [dashed] node {$a$}
    node[red, marking, sloped] {\Huge$\times$} (q2);

\tikz\path[->, shorten >=1pt] (q0) edge node {$a$} (q1)
  edge [dashed] node {$a$}
    node[cross out, draw=red, marking, sloped, solid,
         shorten >=0pt, inner ysep=.5em] {} (q2);

\tikz\path[->, shorten >=1pt] (q0) edge node {$a$} (q1)
  edge [dashed] node {$a$}
    pic[red, marking, sloped, cross angle=10] {cross} (q2);
\end{document}

Output

enter image description here enter image description here enter image description here

1
  • This works very well, thank you!
    – McDuck
    Mar 20 at 14:40

You must log in to answer this question.

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