目标

目标

目标

我正在尝试使用水平-垂直-水平线连接图形的节点。

平均能量损失

请考虑以下示例:

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

\begin{document}
\begin{tikzpicture}
    \graph[left anchor=east, right anchor=west] {
        a -- { b, c }
    };
\end{tikzpicture}
\end{document}   

我需要传递什么参数来\graph定制我的边缘,以及如何从这里开始:

当前图表,使用上述代码生成

至此(未按比例):

在此处输入图片描述

我的想法

我尝试指定密钥/tikz/graphs/new --,但我不确定该怎么做。我的主要想法是将以下内容添加到前面的代码中:

...
\graph[..., new --={\draw[#3] (#1) -| ($(#1)!.5!(#2)$) |- (#2) #4;}] {
...

及其变体,尝试以各种方式指定我的功能:

\graph[..., new --/.code={...}] { % or /.code 4 args or /.style
---------------------------------------------------------------------------
\graph[..., every new --={...}] { % or /.code or /.code 4 args or /.style or nothing
---------------------------------------------------------------------------
\tikzset{ % This seemed to be a good lead too
    manhattan/.code 4 args={\draw[#3] (#1) -| ($(#1)!.5!(#2)$) |- (#2) #4;},
}

...
\graph[..., new --=manhattan] { % or /tikz/manhattan
---------------------------------------------------------------------------
\pgfkeys{/user/manhattan/.code 4 args={...}} % Same use as above

我也测试了这些想法,具体说明/tikz/graphs/default edge kind键,而不是/tikz/graphs/new --

以下是我在执行任务过程中遇到的两个反复出现的错误消息:

// Trying to define a key with /.code 4 args
! Illegal parameter number in definition of \pgfkeys@temp.
<to be read again> 
                   3
l.7 ...[#3] (#1) -| ($(#1)!.5!(#2)$) |- (#2) #4;}]
                                                   {

--------------------------------------------------------

// Directly in \graph
! Argument of \tikz@parse@calculator has an extra }.
<inserted text> 
                \par 
l.11 ...#3] (#1) -| ($(#1)!.5!(#2)$) |- (#2) #4;}]
                                                   {
? 

无论如何,我迷路了,所以感谢你们的帮助!

答案1

new --钥匙将使用四个参数进行调用:{⟨left node⟩}{⟨right node⟩}{⟨edge options⟩}{⟨edge nodes⟩}

这意味着您必须以相同的方式覆盖它。PGFKeys 提供了四个代码处理程序:

  1. .code对于零个或一个参数,
  2. .code 2 args对于两个参数,
  3. .code args对于自定义参数规范和
  4. .code n args对于可变数量的参数(零到九)

以及一堆变体(.ecode,,.add code等等)。

您将需要使用code n args

new --/.code n args={4}{\draw[#3] (#1\tikzgraphleftanchor) -| ($(#1)!.5!(#2)$)
                                               |- #4 (#2\tikzgraphrightanchor);}

我擅自包含了锚点规范(尽管这些连接不需要它们,因为 TikZ 会自动使用相同的锚点)。


您还可以调整/tikz/every new --样式以使用自定义样式to path,例如

\tikzset{
  every new --/.style={
    to path={
      -|($(\tikztostart)!.5!\tikztotarget)$) -| (\tikztotarget) \tikztonodes
    }
  }
}

\tikztostart但是当/\tikztotarget包含它们时,您将无法访问纯节点名称(或者您需要再次将其解析出来) 。

edges将所有边设置为相同样式的键也是如此。

有趣的是,您可以将new --键设置为语法选项的一部分--[<opt>]。不过,使用它->不会改变任何东西。

代码

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, graphs, calc}
\tikzgraphsset{
  |-| connection/.style={
    new --/.code n args={4}{\draw[##3] (##1\tikzgraphleftanchor) -| ($(##1)!.5!(##2)$) |- ##4 (##2\tikzgraphrightanchor);}}}
\begin{document}
\tikz
\graph[left anchor=east, right anchor=west,
  new --/.code n args={4}{\draw[#3] (#1\tikzgraphleftanchor) -| ($(#1)!.5!(#2)$) |- #4 (#2\tikzgraphrightanchor);}
] {
    a -- { b, c } --[complete bipartite] {d, e, f}
  };

\tikz[>=Stealth]
  \graph[left anchor=east, right anchor=west] {
    a -- { b, c }
      --[|-| connection, complete bipartite] {d, e, f}
      ->[|-| connection, complete bipartite] {g, h}
  };
\end{document}

输出

在此处输入图片描述

相关内容