![在 tikzpicture 中划掉链中的节点,保留边界](https://linux22.com/image/427299/%E5%9C%A8%20tikzpicture%20%E4%B8%AD%E5%88%92%E6%8E%89%E9%93%BE%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9%EF%BC%8C%E4%BF%9D%E7%95%99%E8%BE%B9%E7%95%8C.png)
您能帮我看看如何在一个带有矩形框的节点的图表中划掉一个节点吗?该节点从左到右排列,用箭头连接。使用时cross out
,框线/边界线会消失。
例如,在下图中,我想划掉第二个节点。
\documentclass[]{beamer}
\usepackage{tikz}
\usetikzlibrary
{
arrows.meta,
chains,
shapes
}
\begin{document}
\begin{frame}[t]
\begin{tikzpicture}
[
node distance = 15mm,
start chain = going right,
default/.style = {draw, minimum height = 10mm, minimum width = 15mm, align=center, font=\linespread{0.8}\selectfont}
]
\begin{scope}[every node/.append style={on chain, join=by -Stealth}]
\node (n1) [default] {1};
\node (n2) [default, cross out] {2};
\node (n3) [default] {3};
\node (n4) [default] {4};
\end{scope}
\end{tikzpicture}
\end{frame}
\end{document}
答案1
只是为了好玩。下面的示例提供了一个新的形状,它将绘制一个十字和它的矩形框架。只需复制形状的声明并添加线条framed cross out
即可实现。cross out
\inheritbackgroundpath[from=rectangle]
\documentclass[]{beamer}
\usepackage{tikz}
\usetikzlibrary
{
arrows.meta,
chains,
shapes
}
\makeatletter
\pgfdeclareshape{framed cross out}
{%
% codes from shepe "cross out"
\inheritsavedanchors[from=rectangle]% % this is nearly a rectangle
\inheritanchorborder[from=rectangle]%
\inheritanchor[from=rectangle]{north}%
\inheritanchor[from=rectangle]{north west}%
\inheritanchor[from=rectangle]{north east}%
\inheritanchor[from=rectangle]{center}%
\inheritanchor[from=rectangle]{west}%
\inheritanchor[from=rectangle]{east}%
\inheritanchor[from=rectangle]{mid}%
\inheritanchor[from=rectangle]{mid west}%
\inheritanchor[from=rectangle]{mid east}%
\inheritanchor[from=rectangle]{base}%
\inheritanchor[from=rectangle]{base west}%
\inheritanchor[from=rectangle]{base east}%
\inheritanchor[from=rectangle]{south}%
\inheritanchor[from=rectangle]{south west}%
\inheritanchor[from=rectangle]{south east}%
\inheritbackgroundpath[from=rectangle] %% <<< added
\foregroundpath{
% store lower right in xa/ya and upper right in xb/yb
\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
\pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}}
\pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}}
\pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
\pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}}
\pgfsetarrowsstart{}
\pgfsetarrowsend{}
}%
}%
\makeatother
\begin{document}
\begin{frame}[t]
\begin{tikzpicture}
[
node distance = 15mm,
start chain = going right,
default/.style = {draw, minimum height = 10mm, minimum width = 15mm, align=center, font=\linespread{0.8}\selectfont}
]
\begin{scope}[every node/.append style={on chain, join=by -Stealth}]
\node (n1) [default] {1};
\node (n2) [default, framed cross out] {2};
\node (n3) [default] {3};
\node (n4) [default] {4};
\end{scope}
\end{tikzpicture}
\end{frame}
\end{document}
答案2
使用绘制主节点后的fit
库进行绘制:cross out
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary
{
arrows.meta,
chains,
fit,
shapes
}
\begin{document}
\begin{frame}[fragile]%, t
\begin{tikzpicture}
[
node distance = 15mm,
start chain = going right,
default/.style = {draw, minimum height = 10mm, minimum width = 15mm,
align=center, font=\linespread{0.8}\selectfont},
fco/.style = {draw, inner sep=0pt, cross out, fit=#1}, %Fit, Cross Out
]
\begin{scope}[nodes={default, on chain, join=by -Stealth}]
\node (n1) {1};
\node (n2) {2};
\node (n3) {3};
\node (n4) {4};
\end{scope}
\node[fco=(n2)] {};
\end{tikzpicture}
\end{frame}
\end{document}
附录:解决方案与使用path picture bounding box
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary
{
arrows.meta,
chains,
}
\newcommand\ppbb{path picture bounding box} % <---
\begin{document}
\begin{frame}[fragile]%, t
\begin{tikzpicture}
[
node distance = 15mm,
start chain = going right,
box/.style = {draw, minimum height = 10mm, minimum width = 15mm,
align=center, font=\linespread{0.8}\selectfont},
co/.style = {box, % crossed out
path picture={\draw (\ppbb.north west) -- (\ppbb.south east)
(\ppbb.south west) -- (\ppbb.north east);} % <---
}
]
\begin{scope}[nodes={box, on chain, join=by -Stealth}]
\node (n1) {1};
\node (n2) [co] {2}; % <---
\node (n3) {3};
\node (n4) {4};
\end{scope}
\end{tikzpicture}
\end{frame}
\end{document}
结果和以前一样。