如何才能使以下 tikzpicture(包含 DFA)在整个页面上水平居中而不改变节点之间的距离?
\documentclass{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{semantic}
\usepackage{enumerate}
\usepackage{tikz}
\usepackage[justification=centering]{caption}
\usetikzlibrary{automata,positioning,arrows,topaths}
\usepackage{tabularx}
\usetikzlibrary{positioning,backgrounds}
\setlength{\extrarowheight}{3pt}
\begin{document}
\section{LR(1) Grammars}
\begin{enumerate}
\item \leavevmode\vadjust{\vspace{\baselineskip}}\newline
\begin{tikzpicture}[>=stealth',node distance=2cm,on grid,auto]
\node[state,rectangle,initial,initial where=above] (0) {
0
$
\begin{array}{l c r | r}
{[} S & -> & \cdot aA & \$ {]}\\
{[} S' & -> & \cdot S & \$ {]}\\
{[} S & -> & \cdot e & \$ {]}
\end{array}
$
};
\node[state,rectangle,accepting] (1) [left=5cm of 0] {
1
$
\begin{array}{l c r | r}
{[}S' & -> & S \cdot & \$ {]}
\end{array}
$
};
\node[state,rectangle,accepting] (3) [right=8cm of 0] {
3
$
\begin{array}{l c r | r}
{[} S & -> & e \cdot & \$ {]}
\end{array}
$
};
\path[->]
(0) edge node {S} (1)
edge node {e} (3);
\end{tikzpicture}
\end{enumerate}
\end{document}
答案1
自然的做法是使用\begin{center}
... \end{center}
,但你得用点小伎俩对于大于当前文本宽度的内容:将其包装在\makebox[0pt]{
...中}
,以欺骗 LaTeX 将其居中。
\item \leavevmode\vadjust{\vspace{\baselineskip}}\newline
\begin{center}
\makebox[0pt]{
\begin{tikzpicture}[>=stealth',node distance=2cm,on grid,auto]
\node[state,rectangle,initial,initial where=above] (0) {
% ... rest of automaton ...
\end{tikzpicture}
}
\end{center}
\end{enumerate}
确实如此: