如何在 tikz 中产生划掉的矩形?

如何在 tikz 中产生划掉的矩形?

我有以下 tikz 风格

  \tikzset{actor/.style={ rectangle, minimum size=6mm, very thick,
    draw=red!50!black!50, top color=white, bottom
    color=red!50!black!20 }
}

我想要另一种样式,与上面相同,但矩形中有一个叉号。我该如何实现?

答案1

您可以append after command与库结合使用fit,将cross out节点(来自shapes.misc库)放置在actor节点上方:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.misc, fit}

\begin{document}
\begin{tikzpicture}
\tikzset{
    actor/.style={
        rectangle, minimum size=6mm, very thick,
        draw=red!50!black!50, top color=white, bottom
        color=red!50!black!20
    },
    actor crossed out/.style={
        actor, 
        append after command={
            node [
                fit=(\tikzlastnode),
                draw=red!50!black!50,
                thick,
                inner sep=-\pgflinewidth,
                cross out
            ] {}
        }
    }
}

\node [actor] (a) {A};
\node [actor crossed out, right of = a] {B};

\end{tikzpicture}
\end{document} 

或者,您可以使用edge中的 s 来append after command绘制线条。这不需要任何库:

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\tikzset{
    actor/.style={
        rectangle, minimum size=6mm, very thick,
        draw=red!50!black!50, top color=white, bottom
        color=red!50!black!20
    },
    actor crossed out/.style={
        actor, 
        append after command={
            [every edge/.append style={
                thick,
                red!50!black!50,
                shorten >=\pgflinewidth,
                shorten <=\pgflinewidth,
            }]
           (\tikzlastnode.north west) edge (\tikzlastnode.south east)
           (\tikzlastnode.north east) edge (\tikzlastnode.south west)
        }
    }
}

\node [actor] (a) {A};
\node [actor crossed out, right of = a] {B};

\end{tikzpicture}
\end{document} 

相关内容