3

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.

1
  • @HarishKumar I've tried that with tikzpicture and it starts giving me errors about ampersand. – AbstractDissonance Aug 30 '12 at 2:41
4

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

2
  • 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

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.