Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I center a table or in my case a tikzpicture horizontally ignoring the margins? My tikzpicture is slightly too big causing it not to be centered properly(sticks into the right margin slightly too much but if properly centered it will be less)

I've tried

 \makebox[\textwidth][c]{}

but I just get a bunch of errors.

share|improve this question
    
@HarishKumar I've tried that with tikzpicture and it starts giving me errors about ampersand. –  AbstractDissonance Aug 30 '12 at 2:41

1 Answer 1

up vote 4 down vote accepted

Add ampersand replacement=\& to the tikzpicture options and change every & in your tikzpicture to \& (See Section 17.5 Considerations Concerning Active Characters of the pgf manual):

\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\lipsum[2]

\noindent\makebox[\textwidth]{%
\begin{tikzpicture}[ampersand replacement=\&]
\matrix [matrix of nodes]
{
8 \& 1 \& 6 \& 8 \& 1 \& 6 \& 8 \& 1 \& 6 \& 6 \& 8 \& 1 \& 6 \& 6 \& 8 \& 1 \& 6 \& 6 \& 8 \& 1 \& 6 \& 6 \& 8 \& 1 \& 6 \& 6 \& 8 \& 1 \& 6 \& 6 \& 8 \& 1 \& 6 \& 6 \& 8 \& 1 \& 6 \\
};
\end{tikzpicture}%
}

\end{document} 

enter image description here

If you want to have a caption, you can use a minipage and the \captionof command from the caption or capt-of packages (you cannoy use a float inside a \makebox):

\documentclass{article}
\usepackage{caption}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{lipsum}

\begin{document}

\lipsum[2]

\noindent\makebox[\textwidth]{%
\begin{minipage}{2\textwidth}
\centering
\begin{tikzpicture}[ampersand replacement=\&]
\matrix [matrix of nodes]
{
8 \& 1 \& 6 \& 8 \& 1 \& 6 \& 8 \& 1 \& 6 \& 6 \& 8 \& 1 \& 6 \& 6 \& 8 \& 1 \& 6 \& 6 \& 8 \& 1 \& 6 \& 6 \& 8 \& 1 \& 6 \& 6 \& 8 \& 1 \& 6 \& 6 \& 8 \& 1 \& 6 \& 6 \& 8 \& 1 \& 6 \\
};
\end{tikzpicture}
\captionof{figure}{Test caption}
\end{minipage}%
}

\end{document} 

enter image description here

share|improve this answer
    
Unfortunately in my code I still get errors ;/ Stuff like Latex Error: Not in outer par mode and undefined control sequence ...\@currbox.... The problem seems to be entirely due to wrapping the tikzpicture with a figure. Removing it works but then I lose my caption. –  AbstractDissonance Aug 30 '12 at 10:25
    
@AbstractDissonance you cannot use floats inside a \makebox; for a solution, please see my updated answer. –  Gonzalo Medina Aug 30 '12 at 12:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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