2

I would like to center the plot in the following code. How can I do that without changing the dimensions of the page for the parts containing text ?

I must keep the dimensions of the plot as it without scaling the plot.

\documentclass[a4,12pt]{article}
    \usepackage[utf8x]{inputenc}
    \usepackage{tikz}
    \usepackage{pgf,tikz}
    \usetikzlibrary{arrows}

\begin{document}

\begin{center}
    \definecolor{cqcqcq}{rgb}{0.75,0.75,0.75}
    \begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=2.0cm,y=2.0cm]
    \draw [color=cqcqcq,dash pattern=on 4pt off 4pt, xstep=2.0cm,ystep=2.0cm] (-3.39,-4.23) grid (5.49,4.35);
    \draw[->,color=black] (-3.39,0) -- (5.49,0);
    \foreach \x in {-3,-2,-1,1,2,3,4,5}
    \draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
    \draw[->,color=black] (0,-4.23) -- (0,4.35);
    \foreach \y in {-4,-3,-2,-1,1,2,3,4}
    \draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
    \draw[color=black] (0pt,-10pt) node[right] {\footnotesize $0$};
    \clip(-3.39,-4.23) rectangle (5.49,4.35);
    \end{tikzpicture}
\end{center}

\end{document}
cmhughes
  • 100,947
projetmbc
  • 13,315

2 Answers2

7

Although you can use one of the packages offering adjustwidth, what I normally do is to actually just use \hspace*{<dim>} and to "eye" the correction. This way you sort of optically correcting for the way the page looks. You can also calculate it if you wish, but what I normally find out when you print it, fully centered images that are wider than the \textwidth, need some optical correction.

\documentclass[a4,12pt]{article}
    \usepackage[utf8x]{inputenc}
    \usepackage{tikz}
    \usepackage{pgf,tikz}
    \usetikzlibrary{arrows}
\usepackage{lipsum}
\begin{document}
\lipsum[1]

\hspace*{-2.7cm}
    \definecolor{cqcqcq}{rgb}{0.75,0.75,0.75}
    \begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=2.0cm,y=2.0cm]
    \draw [color=cqcqcq,dash pattern=on 4pt off 4pt, xstep=2.0cm,ystep=2.0cm] (-3.39,-4.23) grid (5.49,4.35);
    \draw[->,color=black] (-3.39,0) -- (5.49,0);
    \foreach \x in {-3,-2,-1,1,2,3,4,5}
    \draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
    \draw[->,color=black] (0,-4.23) -- (0,4.35);
    \foreach \y in {-4,-3,-2,-1,1,2,3,4}
    \draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
    \draw[color=black] (0pt,-10pt) node[right] {\footnotesize $0$};
    \clip(-3.39,-4.23) rectangle (5.49,4.35);
    \end{tikzpicture}

\lipsum[1]

\end{document}
yannisl
  • 117,160
  • Thanks for this. It could be interesting to have one solution which does the calculation automatically from the iwdth of the plot. – projetmbc Jan 30 '12 at 22:04
  • @projetmbc They are not difficult to do, but its better to place the graph in a box and then measure its width (there are quite a few posts around for this). The paper width is given by \paperwidth and the equation is very simple to calculate with eTeX. If it was a picture or photograph I would push to the edge of the paper rather than center it. – yannisl Jan 30 '12 at 22:15
  • I'll see this later. – projetmbc Jan 30 '12 at 22:50
4

You can temporarily change the size of a section of the page using the adjustwidth environment from the changepage package

\begin{adjustwidth}{2cm}{}
  ....
\end{adjustwidth}

On another note, you may like to look at the pgfplots package, which does all the work your example does a lot more simply- I've translated your code in the following

\documentclass{article}

\usepackage{pgfplots}

% grid style
\pgfplotsset{grid style={dashed,gray}}

\begin{document}

\begin{center}
        \begin{tikzpicture}
            \begin{axis}[
                xmin=-3,xmax=4,ymin=-4,ymax=4,
                    axis x line=middle,
                    axis y line=middle,
                width=\textwidth,
                grid=major              
            ]
            \end{axis}
            \end{tikzpicture}
\end{center}

\end{document}

If you plan to have a lot of these types of plots, it's almost certainly setting a global style, something like

\pgfplotsset{every axis/.append style={
                    axis x line=middle,
                    axis y line=middle,
                    axis line style={<->},
                    line width=1pt}}
cmhughes
  • 100,947