如何相对于节点定位支架

如何相对于节点定位支架

我想将支架及其镜像放在矩形的左侧和右侧,并在矩形和支架之间留出一些水平间隙。我使用了以下代码,但它只在北侧留出间隙,而不在南侧留出间隙。如何在北侧和南侧的支架和矩形之间留出水平间隙。

\documentclass[12pt]{article}
\usepackage{lingmacros}
\usepackage{tree-dvips}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart,shapes,shadows,arrows.meta,arrows,decorations.markings,trees,positioning,decorations.markings,calc,fit,chains,intersections,decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}
\node[draw,text depth = 6cm, minimum width=7cm] (auth) {};
\node[draw,text depth = 3cm, minimum width=6cm] at ([yshift=-3cm]auth.north) (gen) {};
\draw [decorate, decoration={brace,amplitude=5pt}] (gen.north east)+(0.5cm, 0cm) -- (gen.south east)+(0.5cm, 0cm);
\draw [decorate, decoration={brace,amplitude=5pt,mirror}] (gen.north west)+(-0.5cm,0cm) -- (gen.south west)+(-0.5cm, 0cm);
\end{tikzpicture}
\end{document}

答案1

我不认为手动调整 像(gen.north east)+(0.5cm, 0cm)在这里是一件好事。我认为最好使用 ,(gen.north -| auth.east)它从 中获取 y 坐标gen.north,从 中获取 x 坐标auth.east。然后您可以使用raise参数来调整括号的位置。

\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
  \begin{tikzpicture}
    \path
      node[draw,minimum height = 6cm, minimum width=7cm] (auth) {}
      node[draw,minimum height = 3cm, minimum width=6cm] (gen) {};
    \begin{scope}[red, decoration={brace,amplitude=5pt,raise=3pt}]
      \draw[decorate] (gen.north -| auth.east) -- (gen.south -| auth.east);
      \draw[decorate, decoration={mirror}] (gen.north -| auth.west) -- (gen.south -| auth.west);
    \end{scope}
  \end{tikzpicture}
\end{document}

在此处输入图片描述

我还在您的代码中进行了更正:

  • 所有无用的库(您已包含decorations.markings两次!)
  • 手动调整节点的位置(gen)
  • 取而代之,text depthminimum height
  • 介绍了一种scope适用于所有常见装饰品。

答案2

它不起作用,因为(a) +(5mm,0) -- (b) +(5mm,0)意味着

  1. 将笔放在a
  2. 将笔移至a
  3. 画线到b
  4. 将笔移至b

但您还有很多选择。

  • 该库的语法calc,例如($(gen.north east)+(0.5cm, 0cm)$)
  • 关键字raise
  • shift//在最后一个坐标中,正如 percusse 在他的(现已删除的)答案中提到的那样,xshift例如。yshift(gen.south east)+(0,-0.5cm) -- ([shift={(0,-0.5cm)}]gen.south west)
  • 如果你的目的是让括号沿着外矩形的边缘,那么你可以使用垂直坐标,如下所示

在此处输入图片描述

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,calc}
\begin{document}
\begin{tikzpicture}
\node[draw,text depth = 6cm, minimum width=7cm] (auth) {};
\node[draw,text depth = 3cm, minimum width=6cm] at ([yshift=-3cm]auth.north) (gen) {};

% calc library
\draw [decorate, decoration={brace,amplitude=5pt}] 
        ($(gen.north east)+(0.8cm, 0cm)$) -- ($(gen.south east)+(0.8cm, 0cm)$);

% raise key
\draw [decorate,
       decoration={
          brace,
          amplitude=5pt,
          mirror,
          raise=8mm} % <--- raise key to move brace away
       ] (gen.north west) -- (gen.south west);


% shift one coordinate
\draw [decorate, decoration={brace,amplitude=5pt}] (gen.south east)+(0,-0.5cm) -- ([shift={(0,-0.5cm)}]gen.south west);


% if the brace should be along the outer rectangle
% perpendicular coordinates
\draw [decorate, decoration={brace,amplitude=5pt}]
     (gen.north east-|auth.east) -- (gen.south east-|auth.east);

\draw [decorate, decoration={brace,amplitude=5pt,mirror}]
     (gen.north west-|auth.west) -- (gen.south west-|auth.west);

\end{tikzpicture}
\end{document}

相关内容