Tikz 移位问题

Tikz 移位问题

我对 tikz 还很陌生,几天前我完成了手册上的教程。我试图为我正在编写的一个项目绘制一个非常简单的图表。这就是我所做的:

\begin{tikzpicture}
    [
    node distance=5cm,
    cyl/.style={cylinder, aspect=0.3, shape border rotate=90, minimum height=2cm, minimum width=2.5cm, draw}]

    % \newcommand\file[3][]{\draw[xshift=#1, yshift=#2, scale=#3] (0,0) -- (0.8, 0) -- (1, -0.2) -- (1, -1.414) -- (0, -1.414) -- cycle;}
    \node[rectangle, rounded corners, draw, inner sep=8pt] (Application) {Application};
    \node[below left of=Application, cyl] (Filesystem) {Filesystem};
    \node[below right of=Application, cyl] (Database) {Database};
    \begin{scope}[xshift=-5mm, yshift=1cm]
        \draw[scale=0.75]   (Filesystem.east) -- ++(0.8, 0) -- ++(0.2, -0.2) -- ++(0, -1.214) -- ++(-1, 0) -- ++(0, 1.414) -- cycle;
    \end{scope}
    \draw[<->] (Application) -- (Filesystem);
    \draw[<->] (Application) -- (Database);

    \begin{scope}[on background layer]
        \node[fill=bluish, rounded corners, inner sep=4mm, fit=(Application) (Filesystem) (Database)] {};
        \node[draw=black!70,thick, dashed, fit=(Filesystem)(Database)] {};
    \end{scope}

\end{tikzpicture}

结果是这样的:

在此处输入图片描述

如您所见,我想要放在文件系统节点上的小“纸张”图标不受我对其应用的移动的影响。我也尝试将其应用于路径,但结果没有改变。

此外,我尝试定义一个新的命令来绘制它,但是编译失败,提示该\file命令未定义。

您能帮我找出我犯的错误吗?

提前致谢。

答案1

符号坐标不会发生偏移。您可以使用

\draw[scale=0.75]   ([xshift=-5mm, yshift=1cm]Filesystem.east) -- ++(0.8, 0) -- ++(0.2, -0.2) -- ++(0, -1.214) -- ++(-1, 0) -- ++(0, 1.414) -- cycle;

但是,pgfmanual 中有一个非常相似的形状,您也可以使用。我建议使用positioning

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{backgrounds,fit,positioning,shapes.geometric}
\makeatletter
\pgfdeclareshape{document}{% from p. 1147 of pgfmanual v3.1.5
\inheritsavedanchors[from=rectangle] % this is nearly a rectangle 
\inheritanchorborder[from=rectangle] 
\inheritanchor[from=rectangle]{center} 
\inheritanchor[from=rectangle]{north} 
\inheritanchor[from=rectangle]{south} 
\inheritanchor[from=rectangle]{west} 
\inheritanchor[from=rectangle]{east}
% ... and possibly more 
\backgroundpath{% this is new
    % 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
% compute corner of ``flipped page''
\pgf@xc=\pgf@xb \advance\pgf@xc by-5pt % this should be a parameter \pgf@yc=\pgf@yb \advance\pgf@yc by-5pt
    % construct main path
    \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}
    \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yb}}
    \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yc}}
    \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
    \pgfpathclose
    % add little corner
    \pgfpathmoveto{\pgfpoint{\pgf@xc}{\pgf@yb}}
    \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
    \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yc}}
    \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
}
}
\makeatother
\colorlet{bluish}{blue!10}
\begin{document}
\begin{tikzpicture}
    [
    node distance=4cm,
    cyl/.style={cylinder, aspect=0.3, shape border rotate=90, minimum height=2cm, minimum width=2.5cm, draw}]

    \node[rectangle, rounded corners, draw, inner sep=8pt] (Application) {Application};
    \node[below left=of Application, cyl] (Filesystem) {Filesystem};
    \node[below right=of Application, cyl] (Database) {Database};
    \node[right=-3mm of Filesystem,yshift=1mm,document,draw,
        minimum width=2em,minimum height=3em] {};
    \draw[<->] (Application) -- (Filesystem);
    \draw[<->] (Application) -- (Database);

    \begin{scope}[on background layer]
        \node[fill=bluish, rounded corners, inner sep=4mm, fit=(Application) (Filesystem) (Database)] {};
        \node[draw=black!70,thick, dashed, fit=(Filesystem)(Database)] {};
    \end{scope}

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容