
我正在寫一篇關於模態邏輯的論文,其中我經常需要繪製「世界」的圖表。每個世界都用一個點來表示,它有兩個標籤:點的一邊是世界的名稱,另一邊是一些命題公式。箭頭連結不同的世界,有些世界可能與它們本身相連。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}[scale=2]
\coordinate (1) at (0,0);
\coordinate (2) at (1,0);
\coordinate (3) at (-60:1);
\foreach\x in {1,2,3}{
\node[fill,circle,inner sep=1pt, label=left:$w_\x$] (world\x) at (\x) {};
}
\node[right] at (1) {$\neg p, q$};
\node[right] at (2) {$p, q$};
\node[right] at (3) {$p, q$};
\draw[->] (world2) to (world3);
\draw[->] (world3) to (world1);
\draw[->,min distance=10,in=60,out=120] (world2) to (world2);
\end{tikzpicture}
\end{document}
作為一個 TikZ 新手,我認為我的第一次嘗試還不錯,但肯定可以改進。例如,我認為標籤離箭頭太近,點和箭頭的起點/終點之間應該有一個小「間隙」。
有沒有一些技巧可以解決我的圖表的缺點?我的程式碼設定是否足夠好,可以用於將來的類似圖表?
答案1
實際上有是世界和箭頭之間已經存在間隙,因為我認為您正在填充但沒有繪製節點,因此線寬仍然存在,但沒有填充。如果您不想要間隙,可以透過新增draw
節點輕鬆修復。
另一方面,如果您想要一個更大間隙,那麼您可以使用shorten >
and/orshorten <
表示圖片(或特定箭頭,但大概您希望此處保持一致)。
例如,我們可以嘗試
\begin{tikzpicture}[scale=2, shorten >=.5pt, shorten <=.5pt]
加入.5pt
世界和箭頭兩端之間的距離。
對於標籤距離,我傾向於將所有標籤建立為標籤,然後在全域範圍內控制它們的距離用於label distance
圖片。這樣,您就可以確保一致性和靈活性,因為如果需要,可以輕鬆更改。
為此,我們可以使用循環中的兩個變數一步來建立節點和標籤:一個用於位置,一個用於標籤。我們可以使用它來計算世界count
並使用它來創建標準世界標籤。
例如:
\foreach \x/\j [count=\xno] in {(0,0)/{$\lnot p, q$},(1,0)/{$p, q$},(-60:1)/{$p, q$}}{
\node [fill, circle, inner sep=1pt, label=left:$w_\xno$, label=right:\j] (world\xno) at \x {};
};
這裡,\x
是位置((0,0)
對於第一個世界),\j
是 wffs 的集合($\lnot p, q$
對於第一個世界)。\xno
記錄世界的數量(1
對於第一個世界)。然後該\node...
命令創建世界,對其進行適當的命名(world1
對於第一個世界),並將世界的名稱放在左側($w_1$
對於第一個世界),將 wff 集放在右側($\lnot p, q$
)。
我們可以透過修改以下配置來設定標籤與世界的距離tikzpicture
:
\begin{tikzpicture}[scale=2, label distance=2pt, shorten >=.5pt, shorten <=.5pt]
scope
或者我們可以根據需要使用 a 對圖片的特定部分執行此操作
\begin{scope}[label distance=5pt]
...
\end{scope}
然後結果如下所示:
完整程式碼:
\documentclass[tikz,multi,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}[scale=2, label distance=2pt, shorten >=.5pt, shorten <=.5pt]
\foreach \x/\j [count=\xno] in {(0,0)/{$\lnot p, q$},(1,0)/{$p, q$},(-60:1)/{$p, q$}}{
\node [fill, circle, inner sep=1pt, label=left:$w_\xno$, label=right:\j] (world\xno) at \x {};
};
\draw [->] (world2) to (world3);
\draw [->] (world3) to (world1);
\draw [->, min distance=10,in=60,out=120] (world2) to (world2);
\end{tikzpicture}
\end{document}