# TikZ plot leaves empty space when using a predefined color (reopen)

This is a reopening of a question previously asked here that was closed with an incomplete answer.

The problem is that using \definecolor in tikz picture leaves an empty space that causes misalignment. To reproduce the problem, see the following MWE.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{subfig}

\begin{document}

\begin{figure}[ht]
\centering
\subfloat[]{
\definecolor{mycolor1}{rgb}{0,0,0}
\definecolor{mycolor2}{rgb}{0,0,0}
\definecolor{mycolor3}{rgb}{0,0,0}
\begin{tikzpicture}
\draw (0,0)--(0,1cm);
\draw (0.2,0)--(0.2,1cm);
\end{tikzpicture}
}
\label{fig:SensitivityContourBfield}
\end{figure}

\end{document}


As it is the bars are not centered. If one comments out the \definecolor lines, the picture is centered. The question is then, how to make \definecolor to not to leave spaces, so that the drawing is properly centered. Or simply, how to get the picture centered.

In the original (closed)post, the solution suggested was to move the definitions to the preamble. When using tools that automatically generate tikz figures, like for example matlab2tikz, moving manually the definitions for each figure is not practical.

Additionaly, these tools might define their own set of colors for each particular figure with names like "mycolor1". Ten different colors will be called then "mycolor1" to "mycolor10". Different figures might have different colors assigned to the same color name. Thus, the colors need to be redefined within the body of the document every time a new figure is going to be used, and cannot be simply placed in the preamble, basically because there would be conflicting definitions for the color names.

Regards

-
Just add a % after all \definecolor; see What is the use of percent signs (%) at the end of lines?. –  Claudio Fiandrino Feb 12 '13 at 16:28
You can also define your new colors into the tikzpicture... –  Paul Gaborit Feb 12 '13 at 16:31

You only need to add some % sign at the end of few lines to remove spurious spaces

\documentclass{article}
\usepackage{pgfplots}
\usepackage{subfig}

\begin{document}

\begin{figure}[ht]
\centering%
\subfloat[]{%
\definecolor{mycolor1}{rgb}{0,0,0}%
\definecolor{mycolor2}{rgb}{0,0,0}%
\definecolor{mycolor3}{rgb}{0,0,0}%
\begin{tikzpicture}
\draw (0,0)--(0,1cm);
\draw (0.2,0)--(0.2,1cm);
\end{tikzpicture}%
}
\label{fig:SensitivityContourBfield}
\end{figure}

\end{document}

-