旧版

旧版

我正在尝试使用 TikZ 在 LaTeX 中构建一个框。由于我想多次使用该框,因此我将其放入一个新命令中,然后将其包含在一个新节点中。

当相对放置两个节点时,立方体内文本的位置会发生偏移。我遗漏了什么?

这是我的 MWE:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, positioning}
\usepackage{etoolbox}


\newcommand{\databox}[1]{
    \begin{tikzpicture}
        % Settings
        \coordinate (CenterPoint) at (0,0);
        \def\width{25pt};
        \def\height{25pt};
        \def\textborder{2pt};
        \def\xslant{4pt};
        \def\yslant{10pt};
        \def\rounding{2pt};
        % Drawing
        \draw [rounded corners = \rounding] (CenterPoint) rectangle ($(CenterPoint) + (\width, \height)$);
        \node at ($(CenterPoint) + (\width/2., 0.5*\height)$) {#1};
        % "3D" top
        \draw [rounded corners = \rounding] %
        ($(CenterPoint) + (0, \height)$) -- %
        ($(CenterPoint) + (-\width/2. + 5*\xslant, \height + \yslant)$) -- %
        ($(CenterPoint) + (\width + 1.5*\xslant, \height + \yslant)$) -- %
        ($(CenterPoint) + (\width, \height)$) -- %
        cycle;
        % "3D" side
        \draw [rounded corners = \rounding] %
        ($(CenterPoint) + (\width + 1.5*\xslant, \height + \yslant)$) -- %
        ($(CenterPoint) + (\width + 1.5*\xslant, \yslant)$) -- %
        ($(CenterPoint) + (\width, 0)$) -- %
        ($(CenterPoint) + (\width, \height)$) -- %
        cycle;
    \end{tikzpicture}
}


\begin{document}

\begin{tikzpicture}

\node[](sip){
    \databox{SIP}
};

\node[right=of sip](sipOpen){
    \databox{SIP}
};


\end{tikzpicture}

\end{document}

结果如下:

在此处输入图片描述

答案1

这是使用(下面是第一个版本)的第二个版本pic。我从中借了一些代码cfr 对“如何使用 pic 正确制作宏?”的回答,以便更轻松地根据每张图片修改设置。

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, positioning}
\tikzset{%
  pics/databox/.style={%
    code={%
      \tikzset{%
        /databox settings,
        default,
        #1,
      }
        \node [draw,rounded corners=\rounding,minimum size=\size,inner sep=\textborder] (-txt) {\txt};
        % "3D" top
        \draw [rounded corners = \rounding]
        let
          \p1=(-txt.south west), \p2=(-txt.north east), \n1={\x2-\x1}, \n2={\y2-\y1}
        in
        (-txt.north) -- 
        ++ (-\n1/2, 0) -- coordinate [midway] (topLC) 
        ++ (1.5*\xslant, \yslant) coordinate (topUL) -- 
        ++ (\n1, 0) -- 
        ++ (-1.5*\xslant, -\yslant) -- 
        cycle;
        % "3D" side
        \draw [rounded corners = \rounding]
        let
          \p1=(-txt.south west), \p2=(-txt.north east), \n1={\x2-\x1}, \n2={\y2-\y1}
        in
        (-txt.east) -- 
        ++ (0,\n2/2) -- 
        ++ (1.5*\xslant, \yslant) -- 
        ++ (0,-\n2) -- 
        ++ (-1.5*\xslant, -\yslant) -- 
        cycle;

        \ifnum \lidopen=1
        % lid
        \draw [rounded corners = \rounding]
        let
          \p1=(topLC), \p2=(topUL), \n1={2*(\x2-\x1)}, \n2={2*(\y2-\y1)},
          \p3=(-txt.south west), \p4=(-txt.north east), \n3={\y4-\y3)}
        in
        (topLC) -- (topUL) --  
        ++ ({-\n3*cos(\lidangle)}, {\n3*sin(\lidangle)}) -- 
        ++ (-\n1,-\n2) -- 
        ++ ({\n3*cos(\lidangle)}, {-\n3*sin(\lidangle)}) -- 
        cycle;
        \fi
    }
  },
  /databox settings/.is family,
  /databox settings,
  size/.store in=\size,
  textborder/.store in=\textborder,
  xslant/.store in=\xslant,
  yslant/.store in=\yslant,
  rounding/.store in=\rounding,
  text/.store in=\txt,
  lidangle/.store in=\lidangle,
  open/.store in=\lidopen,
  default/.style={%
    textborder=0.333em,
    xslant=4pt,
    yslant=10pt,
    rounding=2pt,
    text=SIP,
    size=1.5*width("SIP"),
    lidangle=20,
    open=0
  }
}


\begin{document}

\begin{tikzpicture}

\pic (sip) {databox};
\pic [right=1.5cm of sip-txt] (sipOpen) {databox={text=SAAAP,open=1}};
\pic [right=of sipOpen-txt,blue,thick] (bluesip) {databox={rounding=4pt}};
\pic [right=of  bluesip-txt,red] {databox={text=SUUP,size=40pt,xslant=8pt}};

\end{tikzpicture}
\end{document}

旧版

你也许可以使用pic。但这确实有它自己的问题,但似乎在https://tex.stackexchange.com/a/302562/586在这里也有效。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, positioning}

\tikzset{
databox/.pic={
        % Settings
        \def\width{25pt};
        \def\height{25pt};
        \def\textborder{2pt};
        \def\xslant{4pt};
        \def\yslant{10pt};
        \def\rounding{2pt};
        \coordinate (CenterPoint) at (-\width/2,-\height/2); 
        % Drawing
        \draw [rounded corners = \rounding] (CenterPoint) rectangle ($(CenterPoint) + (\width, \height)$);
        % note anchor=center
        \node [anchor=center] (-txt) at ($(CenterPoint) + (\width/2., 0.5*\height)$) {#1};
        % "3D" top
        \draw [rounded corners = \rounding] %
        ($(CenterPoint) + (0, \height)$) -- %
        ($(CenterPoint) + (-\width/2. + 5*\xslant, \height + \yslant)$) -- %
        ($(CenterPoint) + (\width + 1.5*\xslant, \height + \yslant)$) -- %
        ($(CenterPoint) + (\width, \height)$) -- %
        cycle;
        % "3D" side
        \draw [rounded corners = \rounding] %
        ($(CenterPoint) + (\width + 1.5*\xslant, \height + \yslant)$) -- %
        ($(CenterPoint) + (\width + 1.5*\xslant, \yslant)$) -- %
        ($(CenterPoint) + (\width, 0)$) -- %
        ($(CenterPoint) + (\width, \height)$) -- %
        cycle;
  }
}

\begin{document}

\begin{tikzpicture}

\pic (sip) {databox=SIP};

\pic [right=of sip-txt] (sipOpen) {databox=SIP};

\pic [below=1.4cm of sipOpen-txt,blue] {databox=SIP};

\pic [above=1.4cm of sipOpen-txt,red] {databox=SIP};

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

我改编了 Torbjørn T. 的解决方案以允许第二个论点:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, positioning}
\usepackage{etoolbox}



\tikzset{
    pics/databox/.style args={#1/#2}{
        code = {
        % Settings
        \def\width{25pt};
        \def\height{25pt};
        \def\textborder{2pt};
        \def\xslant{4pt};
        \def\yslant{10pt};
        \def\rounding{2pt};
        \coordinate (CenterPoint) at (-\width/2,-\height/2); 
        % Drawing
        \draw [rounded corners = \rounding] (CenterPoint) rectangle ($(CenterPoint) + (\width, \height)$);
        % note anchor=center
        \node [anchor=center] (-txt) at ($(CenterPoint) + (\width/2., 0.5*\height)$) {#1};
        % "3D" top
        \draw [rounded corners = \rounding] %
        ($(CenterPoint) + (0, \height)$) -- %
        ($(CenterPoint) + (-\width/2. + 5*\xslant, \height + \yslant)$) -- %
        ($(CenterPoint) + (\width + 1.5*\xslant, \height + \yslant)$) -- %
        ($(CenterPoint) + (\width, \height)$) -- %
        cycle;
        % "3D" side
        \draw [rounded corners = \rounding] %
        ($(CenterPoint) + (\width + 1.5*\xslant, \height + \yslant)$) -- %
        ($(CenterPoint) + (\width + 1.5*\xslant, \yslant)$) -- %
        ($(CenterPoint) + (\width, 0)$) -- %
        ($(CenterPoint) + (\width, \height)$) -- %
        cycle;
        \ifstrequal{#2}{open}%
        {% "3D" lid
            \draw[rounded corners = \rounding] %
            ($(CenterPoint) + (0, \height)$) -- %
            ($(CenterPoint) + (-\width/2. + 5*\xslant, \height + \yslant)$) -- %
            ($(CenterPoint) + (-\width/2. - 0.2*\xslant, \height + 2.2*\yslant)$) -- %
            ($(CenterPoint) + (-\width/2. - 2.2*\xslant , \height + \yslant)$) -- %
            cycle;
        }{}
    }
}}


\begin{document}

\begin{tikzpicture}

\pic (sip) {databox=SIP/closed};
\pic [right=1.5cm of sip-txt] (sipOpen) {databox=SIP/open};
\pic [below=1.4cm of sipOpen-txt,blue] {databox=AIP/closed};
\pic [above=1.4cm of sipOpen-txt,red] {databox=DIP/open};

\end{tikzpicture}

\end{document}

这将给出带有不同标签的打开或关闭的盒子: 在此处输入图片描述

相关内容