Tikz 图形绘制树布局:中心第二个子项

Tikz 图形绘制树布局:中心第二个子项

我创建了以下树/图:

%! TEX program = lualatex
\documentclass[]{scrartcl}
\usepackage{tikz}

\usetikzlibrary{
  graphs,
  graphdrawing,
  quotes,
}
\usegdlibrary{trees}

\begin{document}

{\footnotesize
\begin{tikzpicture}[
  every node/.style={align=center},
]

\graph[
    tree layout,
    minimum number of children=3,
    missing nodes get space=false,
    edge quotes={anchor=center, align=center},
    edges={nodes={fill=white}},
    sibling distance=28mm,
    level distance=28mm,
  ]{

  % % level 1:
  %   a/"$P4/m2/m/2/m$";
  % % level 2:
  %   b/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  % % level 3:
  %   c/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  %   d/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  % % level 4:
  %   e/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  %   f/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  %   g/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  % % level 5:
  %   h/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  %   i/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  % % level 6:
  %   j/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  %   k/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  %   l/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";
  % % level 7:
  %   m/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}";

  a ->
  b[second] ->  {  c[first] -> {e[first], , }, d[second]->{f[first] -> h[second] -> {, j[second],k[third] -> m}, ,g[third] -> i[second] -> l[second] -> m}, };
};

\end{tikzpicture}
}
\end{document} 

结果看起来就像我想要的那样:

在此处输入图片描述

但是,如果我取消注释代码块,第二个子项就不再处于居中位置:

在此处输入图片描述

我如何强制 tikz 图形绘制坚持与单字母节点标签相同的定位。

相关问题似乎是tikz 树形布局的子节点居中

唯一的答案建议切换到 forest 包。但是,如果我可以继续使用 tikz graphdrawing 库,我会很高兴。我也不确定 forrest 包是否真的允许绘制图形而不仅仅是树。

答案1

此解决方案使用大量锚定后调整来将节点定位到画布上的所需位置。这是使用regardless at键完成的(参见 Ti 的 p380Z 手册)。结果是,节点不是放置在由图形绘制算法确定的位置,而是放置在指定的坐标处。

在这种情况下,节点的位置已知,并且它们具有对称布局,我倾向于使用matrix of nodes或库的功能positioning来适当地放置节点,而无需调用任何图形绘制算法。不过,问题要求使用图形绘制语法来解决问题,所以也许这会很有用。

为了便于理解代码及其生成的图形,节点用名称(a 到 m)以及 OP 给出的标签来标识。这可以通过命令 来完成\lb。背景网格来自 如何在 TikZ 中自动按网格显示坐标?

结果如下: 在此处输入图片描述

这是代码:

%! TEX program = lualatex
\documentclass[tikz,border=3pt]{standalone}

\usetikzlibrary{
  graphs,
  graphdrawing,
}
\usegdlibrary{
layered}

\pagestyle{empty}

% https://tex.stackexchange.com/a/39698
\makeatletter
\def\grd@save@target#1{%
    \def\grd@target{#1}}
\def\grd@save@start#1{%
    \def\grd@start{#1}}
\tikzset{
    grid with coordinates/.style={
        to path={%
            \pgfextra{%
                \edef\grd@@target{(\tikztotarget)}%
                \tikz@scan@one@point\grd@save@target\grd@@target\relax
                \edef\grd@@start{(\tikztostart)}%
                \tikz@scan@one@point\grd@save@start\grd@@start\relax
                \draw[minor help lines] (\tikztostart) grid (\tikztotarget);
                \draw[major help lines] (\tikztostart) grid (\tikztotarget);
                \grd@start
                \pgfmathsetmacro{\grd@xa}{\the\pgf@x/1cm}
                \pgfmathsetmacro{\grd@ya}{\the\pgf@y/1cm}
                \grd@target
                \pgfmathsetmacro{\grd@xb}{\the\pgf@x/1cm}
                \pgfmathsetmacro{\grd@yb}{\the\pgf@y/1cm}
                \pgfmathsetmacro{\grd@xc}{\grd@xa + \pgfkeysvalueof{/tikz/grid with coordinates/major step}}
                \pgfmathsetmacro{\grd@yc}{\grd@ya + \pgfkeysvalueof{/tikz/grid with coordinates/major step}}
                \foreach \x in {\grd@xa,\grd@xc,...,\grd@xb}
                \node[anchor=north,color=blue] at ([yshift=-0.15cm]\x,\grd@ya) {\pgfmathprintnumber{\x}};
                \foreach \y in {\grd@ya,\grd@yc,...,\grd@yb}
                \node[anchor=east,color=blue] at ([xshift=-0.15cm]\grd@xa,\y) {\pgfmathprintnumber{\y}};
            }
        }
    },
    minor help lines/.style={
        help lines,
        step=\pgfkeysvalueof{/tikz/grid with coordinates/minor step},
        minor line width/.initial=0.25pt,
    },
    major help lines/.style={
        help lines,
        line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width},
        step=\pgfkeysvalueof{/tikz/grid with coordinates/major step},
        line cap=round,
        color=lightgray,
    },
    grid with coordinates/.cd,
    minor step/.initial=1, % just use major
    major step/.initial=1,
    major line width/.initial=0.5pt,
}
\makeatother
\newcommand{\lb}[3]{{\begin{tabular}{c}#1\\#2\\\fbox{#3}\end{tabular}}}

\begin{document}

\begin{tikzpicture}[
font=\normalsize\bfseries
]

\draw (-14,-20) to[grid with coordinates] (8,1);

\graph[
    layered layout,
    sibling distance=50mm,
    level distance=30mm,
  ]{
      a/\lb{a}{$P4/m2/m/2/m$}{} -> b/\lb{b}{$P4/n2_1/m2/m$}{$HT-WO_3$} -> d/\lb{d}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(0,-6)}],
%%
      d -> {f/\lb{f}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(-6,-9)}],g/\lb{g}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(6,-9)}]},
%%
      f -> h/\lb{h}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(-6,-12)}] -> j/\lb{j}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(-6,-15)}],
      g -> i/\lb{i}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(6,-12)}] -> l/\lb{l}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(6,-15)}] -> m/\lb{m}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(6,-18)}],
      h -> k/\lb{k}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(0,-15)}],
      k -> m,
%%
      b -> c/\lb{c}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(-6,-6)}] -> e/\lb{e}{$P4/n2_1/m2/m$}{$HT-WO_3$}[regardless at={(-12,-9)}]
  };

\end{tikzpicture}

\end{document} 

答案2

使用第一次运行计算出的坐标

%! TEX program = lualatex
\documentclass[]{scrartcl}
\usepackage{tikz}

\usetikzlibrary{
  calc,
  graphs,
  graphdrawing,
  quotes,
}
\usegdlibrary{trees}

\makeatletter
\def\extractcoord#1#2#3{
  \path let \p1=(#3) in \pgfextra{
    \pgfmathsetmacro#1{\x{1}/\pgf@xx}
    \pgfmathsetmacro#2{\y{1}/\pgf@yy}
    \xdef#1{#1} \xdef#2{#2}
  };
}
\makeatother

\begin{document}

{\footnotesize
\begin{tikzpicture}[
  every node/.style={align=center},
]

\graph[
    tree layout,
    minimum number of children=3,
    missing nodes get space=false,
    edge quotes={anchor=center, align=center},
    edges={nodes={fill=white}},
    sibling distance=28mm,
    level distance=28mm,
  ]{

    % % copy `nail at` from log file
    % a/"$P4/m2/m/2/m$"                    [nail at={(TO BE DETERMINED)}];
    % b/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % c/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % d/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % e/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % f/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % g/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % h/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % i/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % j/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % k/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % l/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];
    % m/"$P4/n2_1/m2/m$\\\fbox{$HT-WO_3$}" [nail at={(TO BE DETERMINED)}];

  a ->
    b[second] ->  {
    c[first] -> { e[first], , },
    d[second]->{
      f[first] -> h[second] -> {,
        j[second],
        k[third] -> m
      }, ,
      g[third] -> i[second] -> l[second] -> m
    },
  };
};

\extractcoord\xa\ya{a}\typeout{[nail at={(\xa,\ya)}];}
\extractcoord\xb\yb{b}\typeout{[nail at={(\xb,\yb)}];}
\extractcoord\xc\yc{c}\typeout{[nail at={(\xc,\yc)}];}
\extractcoord\xd\yd{d}\typeout{[nail at={(\xd,\yd)}];}
\extractcoord\xe\ye{e}\typeout{[nail at={(\xe,\ye)}];}
\extractcoord\xf\yf{f}\typeout{[nail at={(\xf,\yf)}];}
\extractcoord\xg\yg{g}\typeout{[nail at={(\xg,\yg)}];}
\extractcoord\xh\yh{h}\typeout{[nail at={(\xh,\yh)}];}
\extractcoord\xi\yi{i}\typeout{[nail at={(\xi,\yi)}];}
\extractcoord\xj\yj{j}\typeout{[nail at={(\xj,\yj)}];}
\extractcoord\xk\yk{k}\typeout{[nail at={(\xk,\yk)}];}
\extractcoord\xl\yl{l}\typeout{[nail at={(\xl,\yl)}];}
\extractcoord\xm\ym{m}\typeout{[nail at={(\xm,\ym)}];}

\end{tikzpicture}
}

\end{document} 

现在您可以在日志文件中找到以下代码片段

[nail at={(0.0,0.0)}];
[nail at={(0.0,-2.8)}];
[nail at={(-2.8,-5.6)}];
[nail at={(0.0,-5.6)}];
[nail at={(-5.6,-8.40001)}];
[nail at={(-2.8,-8.40001)}];
[nail at={(2.8,-8.40001)}];
[nail at={(-2.8,-11.2)}];
[nail at={(2.8,-11.2)}];
[nail at={(-2.8,-14.0)}];
[nail at={(0.0,-14.0)}];
[nail at={(2.8,-14.0)}];
[nail at={(2.8,-16.8)}];

您可以将其插入到主文档中并取消注释完整的顶点标签。

在此处输入图片描述

相关内容