如何默认在图形节点周围绘制框

如何默认在图形节点周围绘制框

我正在用该包制作一些不那么简单的流程图graphdrawing,并且我想在每个节点周围画一个框。

这可能吗,同时保留简单的语法?如果可以,怎么做?

平均能量损失

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{graphs}
\usegdlibrary{trees}

\begin{document}

\begin{tikzpicture}
\graph[tree layout, grow=down] % somehow make each node have a box around it by putting an option here?
{
Enter the tavern -> Like each other -> Go adventuring,
Enter the tavern -> Hate each other -> Fight,
Go adventuring -> Utter defeat., 
Go adventuring -> Find treasure -> Utter defeat.,
Fight -> Utter defeat.,
Find treasure <-> Sell treasure,
};
\end{tikzpicture}
\end{document}

图表示例

答案1

更换线路

\begin{tikzpicture}

经过

\begin{tikzpicture}[every node/.style={draw}]

在此处输入图片描述

通过运行获得lualatex

\RequirePackage{luatex85}
\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{graphs}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}[every node/.style={draw}] % <<<<<<<<
\graph[tree layout, grow=down]
{
Enter the tavern -> Like each other -> Go adventuring,
Enter the tavern -> Hate each other -> Fight,
Go adventuring -> Utter defeat., 
Go adventuring -> Find treasure -> Utter defeat.,
Fight -> Utter defeat.,
Find treasure <-> Sell treasure,
};
\end{tikzpicture}
\end{document}

答案2

设置图形中节点样式的标准方法是使用nodes图形的键。

例如,要绘制此图中所有节点的边框,请添加nodes=draw

\IfFileExists{luatex85.sty}{\RequirePackage{luatex85}}{}
\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{graphdrawing,graphs}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}
  \graph[tree layout, grow=down, nodes=draw] % somehow make each node have a box around it by putting an option here?
  {
    Enter the tavern -> Like each other -> Go adventuring,
    Enter the tavern -> Hate each other -> Fight,
    Go adventuring -> Utter defeat.,
    Go adventuring -> Find treasure -> Utter defeat.,
    Fight -> Utter defeat.,
    Find treasure <-> Sell treasure,
  };
\end{tikzpicture}
\end{document}

边界节点

根据您使用every nodegraphs/nodes,受影响的节点将更多或更少。此外,将选项传递给\graphtikzpicture也会影响结果。

以下是三种可能性:

  1. 添加nodes=draw\graph->此图中受影响的节点。
  2. 添加graphs/nodes=drawtikzpicture->所有受影响的图中的节点。
  3. 添加every node/.append style=drawtikzpicture->此图片中所有受影响的常规节点(但不包括标签等)。

区别如下:

节点影响

代码:

\IfFileExists{luatex85.sty}{\RequirePackage{luatex85}}{}
\documentclass[border=10pt,multi,tikz]{standalone}
\usetikzlibrary{graphdrawing,graphs}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}
  \graph[tree layout, grow=down, nodes=draw] % somehow make each node have a box around it by putting an option here?
  {
    Enter the tavern -> Like each other -> Go adventuring,
    Enter the tavern -> Hate each other -> Fight,
    Go adventuring -> Utter defeat.,
    Go adventuring -> Find treasure -> Utter defeat.,
    Fight -> Utter defeat.,
    Find treasure <-> Sell treasure,
  };
  \begin{scope}[yshift=-50mm]
  \graph[tree layout, grow=down] % somehow make each node have a box around it by putting an option here?
  {
    Fly a kite -> Enjoy the sunshine -> Learn Mandarin,
    Fly a kite -> Hate the rain -> Hibernate,
    Learn Mandarin -> Impress the Empress.,
    Learn Mandarin -> Insult the Emperor -> Abandoned in jail.,
    Hibernate -> Horrendous nightmares.,
    Hibernate <-> Sweet dreams,
  };
  \end{scope}
  \node [font=\ttfamily, anchor=north, label=below:{as option to \ttfamily\textbackslash graph}] at ([yshift=-5mm]current bounding box.south) {nodes};
\end{tikzpicture}
\begin{tikzpicture}[graphs/nodes=draw]
  \graph[tree layout, grow=down] % somehow make each node have a box around it by putting an option here?
  {
    Enter the tavern -> Like each other -> Go adventuring,
    Enter the tavern -> Hate each other -> Fight,
    Go adventuring -> Utter defeat.,
    Go adventuring -> Find treasure -> Utter defeat.,
    Fight -> Utter defeat.,
    Find treasure <-> Sell treasure,
  };
  \begin{scope}[yshift=-50mm]
  \graph[tree layout, grow=down] % somehow make each node have a box around it by putting an option here?
  {
    Fly a kite -> Enjoy the sunshine -> Learn Mandarin,
    Fly a kite -> Hate the rain -> Hibernate,
    Learn Mandarin -> Impress the Empress.,
    Learn Mandarin -> Insult the Emperor -> Abandoned in jail.,
    Hibernate -> Horrendous nightmares.,
    Hibernate <-> Sweet dreams,
  };
  \end{scope}
  \node [font=\ttfamily, anchor=north, label=below:as global option] at ([yshift=-5mm]current bounding box.south) {graphs/nodes};
\end{tikzpicture}
\begin{tikzpicture}[every node/.append style=draw]
  \graph[tree layout, grow=down] % somehow make each node have a box around it by putting an option here?
  {
    Enter the tavern -> Like each other -> Go adventuring,
    Enter the tavern -> Hate each other -> Fight,
    Go adventuring -> Utter defeat.,
    Go adventuring -> Find treasure -> Utter defeat.,
    Fight -> Utter defeat.,
    Find treasure <-> Sell treasure,
  };
  \begin{scope}[yshift=-50mm]
  \graph[tree layout, grow=down] % somehow make each node have a box around it by putting an option here?
  {
    Fly a kite -> Enjoy the sunshine -> Learn Mandarin,
    Fly a kite -> Hate the rain -> Hibernate,
    Learn Mandarin -> Impress the Empress.,
    Learn Mandarin -> Insult the Emperor -> Abandoned in jail.,
    Hibernate -> Horrendous nightmares.,
    Hibernate <-> Sweet dreams,
  };
  \end{scope}
  \node [font=\ttfamily, anchor=north, label=below:but not labels] at ([yshift=-5mm]current bounding box.south) {every node};
\end{tikzpicture}
\end{document}

相关内容