计算两个节点之间的 x 距离

计算两个节点之间的 x 距离

有没有办法定义一个宏来评估为两个节点之间的距离的 x 部分?我知道有点寄存器\p和相应的\x命令,但我觉得它们相当麻烦。

\documentclass{article}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{positioning}

\newcommand{\xdist}[2]{%
  % Should compute a parseable representation of the
  % x part of the distance between two nodes
  1cm
}

\begin{document}
    \begin{tikzpicture}
        \node[rectangle,draw] (a) {a\strut};
        \node[rectangle,draw,below right=0.2cm of a] (b) {b\strut};
        \node[rectangle,draw,below=0.2cm of b.south east,
              anchor=north east,
              minimum width=\xdist{b.east}{a.west}] (ab) {ab\strut};
    \end{tikzpicture}
\end{document}

编译结果

在上面的 MWE 中,\xdist命令应该计算ab节点的最小宽度,但该值当前是硬编码的。

我正在寻找更好的方法创建一个适合其他两个节点水平宽度的节点

答案1

新版本

来自打击乐的想法这里可以应用于这种情况并且代码更好。

 \documentclass{article}
 \pagestyle{empty}
 \usepackage{tikz}
 \usetikzlibrary{positioning}

 \makeatletter  
 \tikzset{minimum dist/.code 2 args={%
      \path (#1);
      \pgfgetlastxy{\xa}{\ya} 
       \path (#2);
      \pgfgetlastxy{\xb}{\yb}   
       \pgfpointdiff{\pgfpoint{\xa}{\ya}}%
                    {\pgfpoint{\xb}{\yb}}%
       \pgf@xa=\pgf@x}
    ,
   minimum width=\pgf@xa
   } 

 \begin{document}
     \begin{tikzpicture}
         \node[rectangle,draw] (a) {a\strut};
         \node[rectangle,draw,below right=2.2cm of a] (b) {b\strut};
         \node[rectangle,draw,below=0.2cm of b.south east,
               anchor=north east,minimum dist={a.west}{b.east}] (ab) {ab\strut};   
     \end{tikzpicture}
 \end{document} 

宏不是函数(数学术语)。使用时minimum width需要给出长度。我不是一个伟大的(TeX)专家,但我认为很难做到你想要的(调用宏并只获取长度)。

一个潜在可能

 \documentclass{article}
 \pagestyle{empty}
 \usepackage{tikz}
 \usetikzlibrary{positioning}

 \makeatletter  
 \tikzset{minimum dist/.style n args={4}{%
   insert path={% 
     \pgfextra{%
       \pgfpointdiff{\pgfpointanchor{#1}{#2}}%
                    {\pgfpointanchor{#3}{#4}}%
       \pgf@xa=\pgf@x}
       },
   minimum width=\pgf@xa}
  } 

 \begin{document}
     \begin{tikzpicture}
         \node[rectangle,draw] (a) {a\strut};
         \node[rectangle,draw,below right=2.2cm of a] (b) {b\strut};
         \node[rectangle,draw,below=0.2cm of b.south east,
               anchor=north east,minimum dist={a}{west}{b}{east}] (ab) {ab\strut};   
     \end{tikzpicture}
 \end{document}    

更新

仅使用 2 个参数的变体。

 \documentclass{article}
 \pagestyle{empty}
 \usepackage{tikz}
 \usetikzlibrary{positioning}

 \makeatletter  
 \tikzset{minimum dist/.style 2 args={%
   insert path={% 
     \pgfextra{% 
      \path (#1);
      \pgfgetlastxy{\xa}{\ya} 
       \path (#2);
      \pgfgetlastxy{\xb}{\yb}   
      \pgfpointdiff{\pgfpoint{\xa}{\ya}}%
                    {\pgfpoint{\xb}{\yb}}%
      \pgf@xa=\pgf@x}
       },
   minimum width=\pgf@xa}
   } 

 \begin{document}
     \begin{tikzpicture}
         \node[rectangle,draw] (a) {a\strut};
         \node[rectangle,draw,below right=2.2cm of a] (b) {b\strut};
         \node[rectangle,draw,below=0.2cm of b.south east,
               anchor=north east,minimum dist={a.west}{b.east}] (ab) {ab\strut};   
     \end{tikzpicture}
 \end{document}

在此处输入图片描述

如果需要获取最小高度,下面的代码给出了两个尺寸

\documentclass{article}
 \pagestyle{empty}
 \usepackage{tikz}
 \usetikzlibrary{positioning}

 \makeatletter
 \newdimen\y@min@dim 
  \newdimen\x@min@dim  
 \tikzset{h minimum dist/.code 2 args={%
      \path (#1);
      \pgfgetlastxy{\xa}{\ya} 
       \path (#2);
      \pgfgetlastxy{\xb}{\yb}   
       \pgfpointdiff{\pgfpoint{\xa}{\ya}}%
                    {\pgfpoint{\xb}{\yb}}%
       \y@min@dim=\pgf@y}
    ,
   minimum height=\y@min@dim
   } 
  \tikzset{w minimum dist/.code 2 args={%
       \path (#1);
       \pgfgetlastxy{\xa}{\ya} 
        \path (#2);
       \pgfgetlastxy{\xb}{\yb}   
        \pgfpointdiff{\pgfpoint{\xa}{\ya}}%
                     {\pgfpoint{\xb}{\yb}}%
        \x@min@dim=\pgf@x}
     ,
    minimum width=\x@min@dim
    } 
    \makeatother

 \begin{document}
     \begin{tikzpicture}
         \node[rectangle,draw] (a) {a\strut};
         \node[rectangle,draw,below right=2.2cm of a] (b) {b\strut};
         \node[rectangle,draw,below=0.2cm of b.south east,
               anchor=north east,
               w minimum dist={a.west}{b.east}] (hab) {h ab\strut}; 
        \node[rectangle,draw,right=0.2cm of b.south east,
              anchor=south west,
              h minimum dist={b.south}{a.north}
              ] (wab) {w ab\strut}; 

              \node[rectangle,draw=red, 
                    anchor=north west,
                    h minimum dist={b.south}{a.north},
                    w minimum dist={a.west}{b.east},
                    ] (test) at (a.north west) {test};  
     \end{tikzpicture}
 \end{document}

在此处输入图片描述

答案2

编辑:这里有一个通过let操作更好的解决方案:

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

\begin{document}
\begin{tikzpicture}
  \node[rectangle,draw] (a) {a\strut};
  \node[rectangle,draw,below right=0.2cm of a] (b) {b\strut};

  \path let \p{1}=(a.west), \p{2}=(b.east), \n{x dist}={abs(\x{2}-\x{1})} in
  node[rectangle,draw,below=0.2cm of b.south east,anchor=north east,minimum width=\n{x dist}]
  (ab) {ab\strut};
\end{tikzpicture}
\end{document}

第一个答案:这是您修改后使用的示例\setxveclength

\documentclass{article}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{positioning}

\makeatletter
\newcommand\setxveclength[5]{% newmacro, node1, anchor1, node2, anchor2
  \pgfpointdiff{\pgfpointanchor{#2}{#3}}{\pgfpointanchor{#4}{#5}}
  \edef#1{\the\pgf@x}
}
\makeatother

\begin{document}
    \begin{tikzpicture}
        \node[rectangle,draw] (a) {a\strut};
        \node[rectangle,draw,below right=0.2cm of a] (b) {b\strut};
        \setxveclength{\mydist}{a}{west}{b}{east}
        \node[rectangle,draw,below=0.2cm of b.south east,
              anchor=north east,
              minimum width=\mydist] (ab) {ab\strut};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容