0

How can I horizontally center the following tikzpicture (which includes a DFA) on the whole page without changing the distance between the nodes?

\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

The natural thing to do would be to use \begin{center}...\end{center}, but you have to use a trick for content larger than the current textwidth: wrap it in \makebox[0pt]{...}, to trick LaTeX into centering it.

\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}

And indeed:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.