1

In an article environment, how do I bring the tikzpicture to the center of the page. \begin{center} and \end{center} do not work.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows}
\usetikzlibrary{backgrounds}
\usetikzlibrary{fit}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{calc}

\tikzset{squarenode/.style = {
    shape  = rectangle,
    fill   = gray!50,
    draw   = black,
    thick,
    minimum height = 1cm,
    minimum width  = 1cm
}}

\tikzset{circlenode/.style = {
    shape  = circle,
    fill   = blue!20,
    draw   = blue,
    thick,
    minimum size = 1cm
}}

\tikzset{redcirclenode/.style = {
    shape  = circle,
    fill   = red!20,
    draw   = red,
    thick,
    minimum size = 1cm
}}

\tikzset{help lines/.style=very thin}
\tikzset{My Grid/.style={help lines,color=blue!50}}

\begin{document}
\begin{center}
\begin{tikzpicture}
  [>=stealth']
  %\draw[My Grid] (-5,-5) grid (15,15);
  \node (r1) at (-4,4) [squarenode] {};
  \node[right=4cm of r1] (r2) [squarenode] {};
  \node[below=3cm of r1] (r3) [squarenode] {};
  \node[right=4cm of r3] (r4) [squarenode] {};
  \node[right=1.5cm of r1] (s2) [circlenode] {};
  \node[above=1cm of s2] (s1) [circlenode] {}; %s1
  \node[below=1cm of s2] (s3) [circlenode] {}; %s3
  \node[above=1ex of s3,red] {$s \leq 3$};
  \node[right=1.5cm of r3] (s4) [circlenode] {};
  \node[below=1cm of s4] (s5) [circlenode] {}; %s5
  \draw[fill=black] (s1.center) circle [radius=0.1cm];
  \draw[fill=black] (s5.center) circle [radius=0.1cm];
  \draw[->] (r1) -- (s2);
  \draw[->] (s2) -- (r2);
  \draw[->] (r3) -- (s4);
  \draw[->] (s4) -- (r4);
  \draw[->] (s1) to [out=180,in=90] (r1);
  \draw[->] (r1) to [out=-90,in=180] (s3);
  \draw[->] (s3) to [out=0,in=-90] (r2);
  \draw[->] (r2) to [bend right=45] node[auto,swap] {2} (s1);
  \draw[->] (r3) to [out=90,in=180] (s3);
  \draw[->] (s3) to [out=0,in=90] (r4);
  \draw[->] (r4) to [bend left=45] node[auto] {2} (s5);
  \draw[->] (s5) to [bend left=45] (r3);
  \begin{scope}[on background layer]
    \node [fill=gray!15,rounded corners,fit=(s1) (s3) (s5) (r1) (r2)] (R1) {};
  \end{scope}

  \begin{scope}[xshift=10cm]
    \node (r1) at (-4,4) [squarenode] {};
    \node[right=4cm of r1] (r2) [squarenode] {};
    \node[below=3cm of r1] (r3) [squarenode] {};
    \node[right=4cm of r3] (r4) [squarenode] {};
    \node[right=1.5cm of r1] (s2) [circlenode] {};
    \node[above=1cm of s2] (s1) [circlenode] {}; %s1
    \node[below left=1.35cm and 0cm of s2,] (s3) [redcirclenode] {}; %s3
    \node[right=1.5cm of r3] (s4) [circlenode] {};
    \node[below=1cm of s4] (s5) [circlenode] {}; %s5
    \node[right=0.5cm of s3] (s6) [redcirclenode] {}; %s6
    \node[left=1ex of s3,red] {$s$};
    \node[right=1ex of s6,red] {$\bar{s}$};
    \draw[fill=black] (s1.center) circle [radius=0.1cm];
    \draw[fill=black] (s5.center) circle [radius=0.1cm];
    \draw[fill=black] ($(s6.center)-(0.23,0.15)$) circle [radius=0.1cm];
    \draw[fill=black] ($(s6.center)-(0,-0.23)$) circle [radius=0.1cm];
    \draw[fill=black] ($(s6.center)+(0.23,-0.15)$) circle [radius=0.1cm];
    \draw[->] (r1) -- (s2);
    \draw[->] (s2) -- (r2);
    \draw[->] (r3) -- (s4);
    \draw[->] (s4) -- (r4);
    \draw[->] (s1) to [out=180,in=90] (r1);
    \draw[->] (r1) to (s3);
    \draw[->] (s6) to (r1);
    \draw[->] (s3) to (r2);
    \draw[->] (r2) to (s6);
    \draw[->] (r2) to [bend right=45] node[auto,swap] {2} (s1);
    \draw[->] (r3) to (s3);
    \draw[->] (s6) to (r3);
    \draw[->] (s3) to (r4);
    \draw[->] (r4) to (s6);
    \draw[->] (r4) to [bend left=45] node[auto] {2} (s5);
    \draw[->] (s5) to [bend left=45] (r3);
        \begin{scope}[on background layer]
            \node [fill=gray!15,rounded corners,fit=(s1) (s3) (s5) (r1) (r2)] (R2) {};
        \end{scope}
  \end{scope}

  \draw[shorten >=1mm,-to,thick,decorate,decoration={snake,amplitude=.4mm,segment length=2mm,pre=moveto,pre length=1mm,post length=2mm}] (R1) -- (R2) node [above=1mm,midway,text width=3cm,align=center] {replacement of the \textcolor{red}{capacity} by \textcolor{red}{two places}};

\end{tikzpicture}  
\end{center}
\end{document}
  • @JouleV - can you elaborate on reset the margin – subham soni Mar 6 at 5:42
  • The geometry package allows you to modify the margins. \usepackage [margin=1cm] {geometry} – AndréC Mar 6 at 5:45
3

Your graphics are too large to be centered. To see this, simply load the showframe package that highlights the page and its margins.

It is therefore necessary to reduce the graph! I know two different ways to reduce it:

  • with Tikz scale=.5, transform shape
  • with the macro \scalebox
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows}
\usetikzlibrary{backgrounds}
\usetikzlibrary{fit}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{calc}
\usepackage{showframe}

\tikzset{squarenode/.style = {
    shape  = rectangle,
    fill   = gray!50,
    draw   = black,
    thick,
    minimum height = 1cm,
    minimum width  = 1cm
}}

\tikzset{circlenode/.style = {
    shape  = circle,
    fill   = blue!20,
    draw   = blue,
    thick,
    minimum size = 1cm
}}

\tikzset{redcirclenode/.style = {
    shape  = circle,
    fill   = red!20,
    draw   = red,
    thick,
    minimum size = 1cm
}}

\tikzset{help lines/.style=very thin}
\tikzset{My Grid/.style={help lines,color=blue!50}}

\begin{document}
\begin{center}
\scalebox{.5}{
\begin{tikzpicture}%[scale=.6,transform shape]
  [>=stealth']
  %\draw[My Grid] (-5,-5) grid (15,15);
  \node (r1) at (-4,4) [squarenode] {};
  \node[right=4cm of r1] (r2) [squarenode] {};
  \node[below=3cm of r1] (r3) [squarenode] {};
  \node[right=4cm of r3] (r4) [squarenode] {};
  \node[right=1.5cm of r1] (s2) [circlenode] {};
  \node[above=1cm of s2] (s1) [circlenode] {}; %s1
  \node[below=1cm of s2] (s3) [circlenode] {}; %s3
  \node[above=1ex of s3,red] {$s \leq 3$};
  \node[right=1.5cm of r3] (s4) [circlenode] {};
  \node[below=1cm of s4] (s5) [circlenode] {}; %s5
  \draw[fill=black] (s1.center) circle [radius=0.1cm];
  \draw[fill=black] (s5.center) circle [radius=0.1cm];
  \draw[->] (r1) -- (s2);
  \draw[->] (s2) -- (r2);
  \draw[->] (r3) -- (s4);
  \draw[->] (s4) -- (r4);
  \draw[->] (s1) to [out=180,in=90] (r1);
  \draw[->] (r1) to [out=-90,in=180] (s3);
  \draw[->] (s3) to [out=0,in=-90] (r2);
  \draw[->] (r2) to [bend right=45] node[auto,swap] {2} (s1);
  \draw[->] (r3) to [out=90,in=180] (s3);
  \draw[->] (s3) to [out=0,in=90] (r4);
  \draw[->] (r4) to [bend left=45] node[auto] {2} (s5);
  \draw[->] (s5) to [bend left=45] (r3);
  \begin{scope}[on background layer]
    \node [fill=gray!15,rounded corners,fit=(s1) (s3) (s5) (r1) (r2)] (R1) {};
  \end{scope}

  \begin{scope}[xshift=10cm]
    \node (r1) at (-4,4) [squarenode] {};
    \node[right=4cm of r1] (r2) [squarenode] {};
    \node[below=3cm of r1] (r3) [squarenode] {};
    \node[right=4cm of r3] (r4) [squarenode] {};
    \node[right=1.5cm of r1] (s2) [circlenode] {};
    \node[above=1cm of s2] (s1) [circlenode] {}; %s1
    \node[below left=1.35cm and 0cm of s2,] (s3) [redcirclenode] {}; %s3
    \node[right=1.5cm of r3] (s4) [circlenode] {};
    \node[below=1cm of s4] (s5) [circlenode] {}; %s5
    \node[right=0.5cm of s3] (s6) [redcirclenode] {}; %s6
    \node[left=1ex of s3,red] {$s$};
    \node[right=1ex of s6,red] {$\bar{s}$};
    \draw[fill=black] (s1.center) circle [radius=0.1cm];
    \draw[fill=black] (s5.center) circle [radius=0.1cm];
    \draw[fill=black] ($(s6.center)-(0.23,0.15)$) circle [radius=0.1cm];
    \draw[fill=black] ($(s6.center)-(0,-0.23)$) circle [radius=0.1cm];
    \draw[fill=black] ($(s6.center)+(0.23,-0.15)$) circle [radius=0.1cm];
    \draw[->] (r1) -- (s2);
    \draw[->] (s2) -- (r2);
    \draw[->] (r3) -- (s4);
    \draw[->] (s4) -- (r4);
    \draw[->] (s1) to [out=180,in=90] (r1);
    \draw[->] (r1) to (s3);
    \draw[->] (s6) to (r1);
    \draw[->] (s3) to (r2);
    \draw[->] (r2) to (s6);
    \draw[->] (r2) to [bend right=45] node[auto,swap] {2} (s1);
    \draw[->] (r3) to (s3);
    \draw[->] (s6) to (r3);
    \draw[->] (s3) to (r4);
    \draw[->] (r4) to (s6);
    \draw[->] (r4) to [bend left=45] node[auto] {2} (s5);
    \draw[->] (s5) to [bend left=45] (r3);
        \begin{scope}[on background layer]
            \node [fill=gray!15,rounded corners,fit=(s1) (s3) (s5) (r1) (r2)] (R2) {};
        \end{scope}
  \end{scope}

  \draw[shorten >=1mm,-to,thick,decorate,decoration={snake,amplitude=.4mm,segment length=2mm,pre=moveto,pre length=1mm,post length=2mm}] (R1) -- (R2) node [above=1mm,midway,text width=3cm,align=center] {replacement of the \textcolor{red}{capacity} by \textcolor{red}{two places}};

\end{tikzpicture} 
} 
\end{center}
\end{document}

screenshot

  • I understand the key scale. What is the purpose of transform shape in Tikz – subham soni Mar 6 at 5:39
  • 2
    By default, enlargements and reductions do not affect the nodes, they only affect the coordinates. However, your graph is built with nodes, transform shape also allows you to make scale act on the nodes. – AndréC Mar 6 at 5:43
2

You might just use an adjustbox.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows}
\usetikzlibrary{backgrounds}
\usetikzlibrary{fit}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{calc}

\tikzset{squarenode/.style = {
    shape  = rectangle,
    fill   = gray!50,
    draw   = black,
    thick,
    minimum height = 1cm,
    minimum width  = 1cm
}}

\tikzset{circlenode/.style = {
    shape  = circle,
    fill   = blue!20,
    draw   = blue,
    thick,
    minimum size = 1cm
}}

\tikzset{redcirclenode/.style = {
    shape  = circle,
    fill   = red!20,
    draw   = red,
    thick,
    minimum size = 1cm
}}

\tikzset{help lines/.style=very thin}
\tikzset{My Grid/.style={help lines,color=blue!50}}
\usepackage{adjustbox}
\begin{document}
\begin{center}
\adjustbox{width=\textwidth}{\begin{tikzpicture}
  [>=stealth']
  %\draw[My Grid] (-5,-5) grid (15,15);
  \node (r1) at (-4,4) [squarenode] {};
  \node[right=4cm of r1] (r2) [squarenode] {};
  \node[below=3cm of r1] (r3) [squarenode] {};
  \node[right=4cm of r3] (r4) [squarenode] {};
  \node[right=1.5cm of r1] (s2) [circlenode] {};
  \node[above=1cm of s2] (s1) [circlenode] {}; %s1
  \node[below=1cm of s2] (s3) [circlenode] {}; %s3
  \node[above=1ex of s3,red] {$s \leq 3$};
  \node[right=1.5cm of r3] (s4) [circlenode] {};
  \node[below=1cm of s4] (s5) [circlenode] {}; %s5
  \draw[fill=black] (s1.center) circle [radius=0.1cm];
  \draw[fill=black] (s5.center) circle [radius=0.1cm];
  \draw[->] (r1) -- (s2);
  \draw[->] (s2) -- (r2);
  \draw[->] (r3) -- (s4);
  \draw[->] (s4) -- (r4);
  \draw[->] (s1) to [out=180,in=90] (r1);
  \draw[->] (r1) to [out=-90,in=180] (s3);
  \draw[->] (s3) to [out=0,in=-90] (r2);
  \draw[->] (r2) to [bend right=45] node[auto,swap] {2} (s1);
  \draw[->] (r3) to [out=90,in=180] (s3);
  \draw[->] (s3) to [out=0,in=90] (r4);
  \draw[->] (r4) to [bend left=45] node[auto] {2} (s5);
  \draw[->] (s5) to [bend left=45] (r3);
  \begin{scope}[on background layer]
    \node [fill=gray!15,rounded corners,fit=(s1) (s3) (s5) (r1) (r2)] (R1) {};
  \end{scope}

  \begin{scope}[xshift=10cm]
    \node (r1) at (-4,4) [squarenode] {};
    \node[right=4cm of r1] (r2) [squarenode] {};
    \node[below=3cm of r1] (r3) [squarenode] {};
    \node[right=4cm of r3] (r4) [squarenode] {};
    \node[right=1.5cm of r1] (s2) [circlenode] {};
    \node[above=1cm of s2] (s1) [circlenode] {}; %s1
    \node[below left=1.35cm and 0cm of s2,] (s3) [redcirclenode] {}; %s3
    \node[right=1.5cm of r3] (s4) [circlenode] {};
    \node[below=1cm of s4] (s5) [circlenode] {}; %s5
    \node[right=0.5cm of s3] (s6) [redcirclenode] {}; %s6
    \node[left=1ex of s3,red] {$s$};
    \node[right=1ex of s6,red] {$\bar{s}$};
    \draw[fill=black] (s1.center) circle [radius=0.1cm];
    \draw[fill=black] (s5.center) circle [radius=0.1cm];
    \draw[fill=black] ($(s6.center)-(0.23,0.15)$) circle [radius=0.1cm];
    \draw[fill=black] ($(s6.center)-(0,-0.23)$) circle [radius=0.1cm];
    \draw[fill=black] ($(s6.center)+(0.23,-0.15)$) circle [radius=0.1cm];
    \draw[->] (r1) -- (s2);
    \draw[->] (s2) -- (r2);
    \draw[->] (r3) -- (s4);
    \draw[->] (s4) -- (r4);
    \draw[->] (s1) to [out=180,in=90] (r1);
    \draw[->] (r1) to (s3);
    \draw[->] (s6) to (r1);
    \draw[->] (s3) to (r2);
    \draw[->] (r2) to (s6);
    \draw[->] (r2) to [bend right=45] node[auto,swap] {2} (s1);
    \draw[->] (r3) to (s3);
    \draw[->] (s6) to (r3);
    \draw[->] (s3) to (r4);
    \draw[->] (r4) to (s6);
    \draw[->] (r4) to [bend left=45] node[auto] {2} (s5);
    \draw[->] (s5) to [bend left=45] (r3);
        \begin{scope}[on background layer]
            \node [fill=gray!15,rounded corners,fit=(s1) (s3) (s5) (r1) (r2)] (R2) {};
        \end{scope}
  \end{scope}

  \draw[shorten >=1mm,-to,thick,decorate,decoration={snake,amplitude=.4mm,segment length=2mm,pre=moveto,pre length=1mm,post length=2mm}] (R1) -- (R2) node [above=1mm,midway,text width=3cm,align=center] {replacement of the \textcolor{red}{capacity} by \textcolor{red}{two places}};

\end{tikzpicture}}
\end{center}
\end{document}

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.