我希望画一个十字菱形,但是十字线却在菱形里面,就像图中描述的那样。
但是,我尝试了下面的代码,结果是
\node[draw, diamond, below=of aux] (cross diamond) {};
\draw (cross diamond.north) -- (cross diamond.south)
(cross diamond.west) -- (cross diamond.east);
答案1
您可以使用[shift=<length>]
选项(显示为红色),或者使用tikz
的calc
库执行坐标计算(显示为蓝色)。此外,如果您希望按照原始图像设置圆角,您可以添加 ,rounded corners=<length>
就像我对红色版本所做的那样:
代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,shapes,positioning}
\newcommand*{\Shift}{0.6ex}%
\begin{document}
\begin{tikzpicture}[thick,red]
\node[draw, diamond, rounded corners=1.5pt] (cross diamond) {};
\draw ([yshift=-\Shift]cross diamond.north)
-- ([yshift=+\Shift]cross diamond.south)
([xshift=+\Shift]cross diamond.west)
-- ([xshift=-\Shift]cross diamond.east);
\end{tikzpicture}
\hspace*{1.0cm}
\begin{tikzpicture}[thick,blue]
\node[draw, diamond] (cross diamond) {};
\draw ($(cross diamond.north) - (0,\Shift)$)
-- ($(cross diamond.south) + (0,\Shift)$)
($(cross diamond.west) + (\Shift,0)$)
-- ($(cross diamond.east) - (\Shift,0)$);
\end{tikzpicture}
\end{document}
答案2
我们也可以定义一个形状。大部分工作已经由菱形完成,因此通过简单的 hack(有一天我会发布一个没有 hack 的答案),我们可以使用如下代码:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\pgfkeys{/pgf/crossed diamond shorten/.initial=0pt}
\makeatletter
\pgfdeclareshape{crossed diamond}{
\inheritsavedanchors[from=diamond]
\inheritanchorborder[from=diamond]
\inheritanchor[from=diamond]{center}
\inheritanchor[from=diamond]{text}
\inheritanchor[from=diamond]{base}
\inheritanchor[from=diamond]{base west}
\inheritanchor[from=diamond]{base east}
\inheritanchor[from=diamond]{mid}
\inheritanchor[from=diamond]{mid west}
\inheritanchor[from=diamond]{mid east}
\inheritanchor[from=diamond]{north}
\inheritanchor[from=diamond]{south}
\inheritanchor[from=diamond]{west}
\inheritanchor[from=diamond]{east}
\inheritanchor[from=diamond]{north east}
\inheritanchor[from=diamond]{south west}
\inheritanchor[from=diamond]{north west}
\inheritanchor[from=diamond]{south east}
\backgroundpath{%
\pgf@sh@bg@diamond% Steal the diamond background path
\pgfmathparse{\pgfkeysvalueof{/pgf/crossed diamond shorten}}
\ifpgfmathunitsdeclared%
\outernortheast%
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
\advance\pgf@xa by-\pgfmathresult pt\relax%
\advance\pgf@ya by-\pgfmathresult pt\relax%
\else
\outernortheast%
\pgfmathparse{1-\pgfmathresult}%
\pgf@xa=\pgfmathresult\pgf@x%
\pgf@ya=\pgfmathresult\pgf@y%
\fi%
\ifdim\pgf@xa>0pt%
\pgfpathmoveto{\pgfqpoint{-\pgf@xa}{0pt}}%
\pgfpathlineto{\pgfqpoint{\pgf@xa}{0pt}}%
\fi%
\ifdim\pgf@ya>0pt\relax%
\pgfpathmoveto{\pgfqpoint{0pt}{\pgf@ya}}%
\pgfpathlineto{\pgfqpoint{0pt}{-\pgf@ya}}%
\fi%
}
}
\makeatother
\begin{document}
\begin{tikzpicture}[diamond with cross/.style={
crossed diamond,
crossed diamond shorten=#1,
draw,
rounded corners=0.0625cm,
minimum size=1cm,
very thick,
}]
\foreach \s [count=\i from 0]in {0, 0.125, 0.5, 0.75, 0cm, 0.125cm, 0.25cm, 0.375cm}
\node [diamond with cross=\s] at ({int(\i/4)*2}, {mod(\i, 4)*1.5}) {};
\end{tikzpicture}
\end{document}
由此产生了如下结果:
当键crossed diamond shorten
包含单位时,这被视为缩短交叉“臂”的绝对距离;当没有给出单位时,臂将按全臂长度的分数缩短。底部形状演示了使用全长时可能出现的不良结果。
答案3
我认为仍有利用魔法的答案的空间append after command
。此解决方案创建两种样式,crossed diamond
并rounded crossed diamond
在绘制菱形后自动附加十字形,从节点的锚点开始绘制必要的路径。
为了自定义十字的长度,两种样式都接受它作为参数,但是当线宽未自定义时,为了使十字恰好接触菱形的边界,存在默认值(这是因为默认值是基于设置的\pgflinewidth
)。
基本用法非常简单:
\tikz\node[minimum size=1cm,crossed diamond=2ex]{};
给出:
尽管
\tikz\node[minimum size=1cm,rounded crossed diamond=2ex]{};
给出:
minimum size
建议使用的规范。
以下是示例和所需代码的详细列表:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\tikzset{
crossed diamond/.style={
diamond,
draw,
append after command={
[every edge/.append style={
shorten >=#1,
shorten <=#1,
}]
(\tikzlastnode.north) edge (\tikzlastnode.south)
(\tikzlastnode.east) edge (\tikzlastnode.west)
},
},
crossed diamond/.default={\pgflinewidth},
rounded crossed diamond/.style={
rounded corners,
crossed diamond={#1},
},
rounded crossed diamond/.default={4\pgflinewidth}
}
\begin{document}
\begin{tikzpicture}[minimum size=1cm, node distance=2cm]
\begin{scope}[blue]
\node[crossed diamond] (first) {};
\node[right of=first, crossed diamond={2ex}] (second) {};
\node[right of=second, crossed diamond={0.25cm}] (third) {};
\node[right of=third, crossed diamond={0.4cm}] (fourth) {};
\end{scope}
\begin{scope}[red]
\node[below of=first, rounded crossed diamond] {};
\node[below of=second, rounded crossed diamond={2ex}] {};
\node[below of=third,rounded crossed diamond={0.25cm}] {};
\node[below of=fourth, rounded crossed diamond={0.4cm}] {};
\end{scope}
\end{tikzpicture}
\vspace*{1.5\baselineskip}
\begin{tikzpicture}[line width=1mm,minimum size=1.5cm, node distance=3cm]
\begin{scope}[blue]
\node[crossed diamond] (first) {};
\node[right of=first, crossed diamond={2ex}] (second) {};
\node[right of=second, crossed diamond={0.4cm}] (third) {};
\node[right of=third, crossed diamond={0.6cm}] (fourth) {};
\end{scope}
\begin{scope}[red]
\node[below of=first, rounded crossed diamond] {};
\node[below of=second, rounded crossed diamond={2ex}] {};
\node[below of=third,rounded crossed diamond={0.4cm}] {};
\node[below of=fourth, rounded crossed diamond={0.6cm}] {};
\end{scope}
\end{tikzpicture}
\vspace*{1.5\baselineskip}
\begin{tikzpicture}[very thick,minimum size=2cm, node distance=3cm]
\begin{scope}[blue]
\node[crossed diamond] (first) {};
\node[right of=first, crossed diamond={2ex}] (second) {};
\node[right of=second, crossed diamond={0.5cm}] (third) {};
\node[right of=third, crossed diamond={0.75cm}] (fourth) {};
\end{scope}
\begin{scope}[red]
\node[below of=first, rounded crossed diamond] {};
\node[below of=second, rounded crossed diamond={2ex}] {};
\node[below of=third,rounded crossed diamond={0.5cm}] {};
\node[below of=fourth, rounded crossed diamond={0.75cm}] {};
\end{scope}
\end{tikzpicture}
\end{document}
结果: