Tikz 手冊中的分層圖

Tikz 手冊中的分層圖

我想我對 vanilla Latex 有相當多的經驗(我用它來寫幾個文件),但我正在使用 tikz 邁出我的第一步。特別是,我試圖重現 Till Tantau 的 3.0.1a 版手冊第 363 頁上的圖表。我想我沒有正確設定我的環境。這是我正在使用的程式碼(從書中複製並貼上到 begin{document} 之後的部分)

 \documentclass{article} % say
 \usepackage{tikz}
 \usetikzlibrary{arrows.meta}
 \usetikzlibrary{angles}
 \usetikzlibrary{trees}
 \usetikzlibrary{arrows,decorations.pathmorphing,
 backgrounds,positioning,fit,petri}
 \usetikzlibrary{graphs}
 \usetikzlibrary{bending}
 \usetikzlibrary{automata}
 % \usetikzlibrary{graphdrawing,graphs} 
 % \usegdlibrary{layered}
 \usetikzlibrary{shapes.multipart}
 \begin{document}

\tikz [nodes={text height=.7em, text depth=.2em,
draw=black!20, thick, fill=white, font=\footnotesize},
>=spaced stealth’, rounded corners, semithick]
\graph [layered layout, level distance=1cm, sibling
sep=.5em,sibling    distance=1cm] {
"5th Edition" -> { "6th Edition", "PWB 1.0" };
"6th Edition" -> { "LSX" [>child anchor=45], "1 BSD", "Mini Unix",   
"Wollongong", "Interdata" };
"Interdata" -> { "Unix/TS 3.0", "PWB 2.0", "7th Edition" };
"7th Edition" -> { "8th Edition", "32V", "V7M", "Ultrix-11",
"Xenix",    "UniPlus+" };
"V7M" -> "Ultrix-11";
"8th Edition" -> "9th Edition";
"1 BSD" -> "2 BSD" -> "2.8 BSD" -> { "Ultrix-11", "2.9 BSD" };
"32V" -> "3 BSD" -> "4 BSD" -> "4.1 BSD" -> { "4.2 BSD", "2.8 BSD", 
"8th Edition" };
"4.2 BSD" -> { "4.3 BSD", "Ultrix-32" };
"PWB 1.0" -> { "PWB 1.2" -> "PWB 2.0", "USG 1.0" -> { "CB Unix 1",  
"USG 2.0" }};
"CB Unix 1" -> "CB Unix 2" -> "CB Unix 3" -> { "Unix/TS++", "PDP-11 
Sys V" };
{ "USG 2.0" -> "USG 3.0", "PWB 2.0", "Unix/TS 1.0" } -> "Unix/TS 3.0";
{ "Unix/TS++", "CB Unix 3", "Unix/TS 3.0" } -> "TS 4.0" -> "System  
V.0" -> "System V.2" -> "System V.3";
};

但這會導致我在 pdflatex 腳本時出現許多錯誤,特別是這個錯誤

    ! Package pgf Error: Unknown arrow tip kind 'spaced stealth’'.

但是,即使我刪除了隱身位,我仍然收到此錯誤訊息

  ! Package pgfkeys Error: I do not know the key '/tikz/graphs  
  /layered layout' and I am going to ignore it. Perhaps you  
  misspelled it.

根據我在這裡找到的

如何將 TikZ 圖形繪製包與 KTikZ+LuaLaTeX 一起使用?

這是在腳本中使用 \usegdlibrary{layered} 的問題。但在這種情況下,pdflatex 告訴我我需要求助於 lualatex。底線:我現在很困惑。有什麼建議可以讓我的生活更輕鬆嗎?

答案1

圖形繪製庫可能是更複雜的部分之一,除此之外,這個特定的圖也是一個特別複雜的圖。

透過查看您的程式碼,我可以識別出一些問題:

  • 首先,有很多多餘的函式庫。這並不是有害的,但也不完全推薦;
  • 圖形繪製演算法相當複雜,用純 TeX 實現它們會很困難(也許 LaTeX3 可能更好?),所以它們用 Lua 實現。因此,您需要使用 LuaLaTeX 進行編譯才能產生輸出。您只需要運行lualatex document.tex而不是pdflatex document.tex,或者如果您使用的是 IDE,那麼應該(希望)有一個選項可供選擇lualatex作為編譯器;
  • 箭頭有兩個問題spaced stealth
    • 首先,箭頭樣式似乎沒有在文件中的任何地方定義。有stealth和 相應的stealth',但沒有spaced stealth,所以我實際上不知道他們為什麼在文檔中使用它(編輯:看來這已被棄用,實際上應該從文檔中刪除,請參閱@cfr 的評論);
    • 其次,即使它確實存在,你也有spaced stealth’不同的spaced stealth'(注意撇號的細微差別,它們實際上是不同的unicode字元)。這是目前文件中的錯誤,在類似環境'中進行修改verbatim,就好像它是文字環境一樣。因此,複製貼上在這個階段實際上無法正常工作。 (他們還有一個問題,--會變成一個破折號。)
  • 您在錯誤的位置有某些換行符。例如,倒數第二行有"System\nV.0".這(很可能)會幹擾繪圖演算法,因為它將將該節點視為與頂點不同"System V.0"

我建議您從更簡單的範例開始。首先感受一下簡單的佈局,然後開始建立越來越複雜的佈局。例如,看看以下佈局如何變化:

a -> b -> c,
b -> d,

a -> b -> {c, d}

a -> b -> {c -> x, d}

{a, b} -> {c -> x, d}

等等。另請參閱不同的演算法如何與上述每種佈局配合使用(layered layoutspring layout等)

回到原來的範例,這裡是固定程式碼並重新格式化以使其更清晰:

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{graphs}
\usetikzlibrary{graphdrawing}
\usegdlibrary{layered}

\begin{document}
\tikz [
    nodes={
      text height=.7em,
      text depth=.2em,
      draw=black!20,
      thick, 
      fill=white, 
      font=\footnotesize
    },
    >=stealth',
    rounded corners,
    semithick
  ] 
  \graph [
    layered layout,
    level distance=1cm,
    sibling sep=.5em,
    sibling distance=1cm
  ] {
    "5th Edition" -> {"6th Edition", "PWB 1.0" };
    "6th Edition" -> { 
      "LSX" [>child anchor=45], 
      "1 BSD", 
      "Mini Unix",   
      "Wollongong", 
      "Interdata" };
    "Interdata" -> { 
      "Unix/TS 3.0", 
      "PWB 2.0", 
      "7th Edition" };
    "7th Edition" -> { 
      "8th Edition", 
      "32V", 
      "V7M", 
      "Ultrix-11",
      "Xenix",    
      "UniPlus+" };
    "V7M" -> "Ultrix-11";
    "8th Edition" -> "9th Edition";
    "1 BSD" -> "2 BSD" 
            -> "2.8 BSD" 
            -> { "Ultrix-11", "2.9 BSD" };
    "32V" -> "3 BSD" 
          -> "4 BSD" 
          -> "4.1 BSD" 
          -> { "4.2 BSD", "2.8 BSD", "8th Edition" };
    "4.2 BSD" -> { "4.3 BSD", "Ultrix-32" };
    "PWB 1.0" -> { 
      "PWB 1.2" -> "PWB 2.0", 
      "USG 1.0" -> { 
        "CB Unix 1", 
        "USG 2.0" }};
    "CB Unix 1" -> "CB Unix 2" 
                -> "CB Unix 3" 
                -> { "Unix/TS++", "PDP-11 Sys V" };
    { 
      "USG 2.0" -> "USG 3.0",
      "PWB 2.0",
      "Unix/TS 1.0"
    } -> "Unix/TS 3.0";
    { 
      "Unix/TS++", 
      "CB Unix 3", 
      "Unix/TS 3.0" 
    } -> "TS 4.0" 
      -> "System  
    V.0" -> "System V.2" 
         -> "System V.3";
};
\end{document}

這是相應的輸出:

在此輸入影像描述


注意:有一個standalone類別和 LuaLaTeX之間不相容。在standalone類別更新之前,您需要\RequirePackage{luatex85}在文檔類別聲明之前新增。

答案2

上面答案的補充:可以使用以下方式導入間隔隱形':

\usetikzlibrary{arrows.spaced}

相關內容