
在使用 tikz 建立狀態機圖時,我注意到它似乎偏離中心。這個結果對我來說似乎違反直覺,因為該圖的創建方式是使用 foreach 構造,所有節點都有相同的半徑和不同的角度。我還在坐標 (0,0) 處放置了一個節點,但它似乎沒有居中。我在下面提供了一個顯示該問題的工作範例。我確信一定有一個我錯過的合乎邏輯的解釋,也許這裡有人能夠解釋發生了什麼
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{cmbright}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,automata,positioning,calc}
\begin{document}
Indented text
\noindent Unindented text
\begin{figure}[h]
\centering
\begin{tikzpicture}[-Stealth,shorten >=1pt,semithick]
\tikzstyle{every state}=[fill=blue!20,draw=blue!50,very thick,text=black,minimum size=1cm]
\def \n {8}
\def \radius {6cm}
\node[state] (c) at (0,0) {};
\foreach \s/\stext in {1/1,2/2\_1,3/3,4/4,5/5,6/6,7/7,8/8}
{
\node[state] (\s) at ({-360/\n * (\s - 1)+90}:\radius) {\stext};
}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
如果我編譯您的程式碼,我會收到以下警告:Overfull \hbox (26.08575pt too wide)
這意味著您的圖像比文字寬度更寬,因此看起來沒有相對於邊距居中。要使圖像更小,您可以將scale=0.9
選項新增至\begin{tikzpicture}
.
您還應該將該\tikzstyle
命令替換為, \tikzset{every state/.append style={fill=blue!20,draw=blue!50,very thick,text=black,minimum size=1cm}}
因為前者已被棄用。