# Align tikzpicture to body text with node as reference

In a example like the one below I would like to adjust the figure horizontally so that the grid aligns with the surrounding text and the y-axis and the label protrudes into the margin. Note that the width of the x-axis is exactly \textwidth so it should fit comfortably. I could do this manually with a negative \hspace, but this is not very exact and also un-TeXy.

Ideally what I would like to do is to define a point within the tikzpicture environment to serve as reference point for external alignment. Thin I could experiment with aligning the y-axis with the text and whatnot. I did not find a way to this in the Tikz manual.

\documentclass[a4paper,onesided]{article}
\usepackage{tikz}
\usepackage{blindtext}

\begin{document}
\blindtext

\begin{figure}[h!]
\begin{tikzpicture}[y=0.5mm, x=0.333\textwidth]

\def\axissep{5mm}
\def\tickl{2mm}
\def\xlabsep{3mm}

% y-axis
\draw (-\axissep,0) -- (-\axissep,100);
% y-ticks
\foreach \x in {10,20,30,40,60,70,80,90} % avoids double inking on 0, 50 and 100 that becomes thicker
\draw (-\axissep,\x) -- ++ (-\tickl,0);
% y-tickslabels
\foreach \x in {0,50,100}
\draw (-\axissep,\x) -- ++ (-\tickl*2,0);
\foreach \x in {0,50,100}
\node[anchor=east] at (-\axissep-\tickl*2,\x) {\x};

% x-axis
\draw (0,-\axissep) -- ++ (3,0);
\foreach \x in {0,1,...,3}
\draw (\x,-\axissep) -- ++ (0,-\tickl);

% x-labels
\node[below] at (0,-\axissep-\tickl){1};
\node[below] at (1,-\axissep-\tickl){2};
\node[below] at (2,-\axissep-\tickl){3};
\node[below,align=center] at (3,-\axissep-\tickl){4};

% grid
\foreach \x in {0,10,...,100}
\draw[very thin, color=gray] (0,\x) -- (3,\x);

\draw (0,10) -- (1,14) --(2,45) -- (3,23) ++ (1mm,0) node[anchor=west] {\tiny Some label};

\end{tikzpicture}
\end{figure}

\blindtext

\end{document}


-

\useasboundingbox (0,-20) rectangle (3,100);


(The -20 is to make room for the ticks in the x axis)

-

You can add trim left to tikzpicture:

\documentclass[a4paper,oneside]{article}
\usepackage{tikz}
\usepackage{blindtext}

\begin{document}
\blindtext

\begin{figure}[h!]
\makebox[0pt][l]{\begin{tikzpicture}[y=0.5mm, x=0.333\textwidth,trim left]

\def\axissep{5mm}
\def\tickl{2mm}
\def\xlabsep{3mm}

% y-axis
\draw (-\axissep,0) -- (-\axissep,100);
% y-ticks
\foreach \x in {10,20,30,40,60,70,80,90} % avoids double inking on 0, 50 and 100 that becomes thicker
\draw (-\axissep,\x) -- ++ (-\tickl,0);
% y-tickslabels
\foreach \x in {0,50,100}
\draw (-\axissep,\x) -- ++ (-\tickl*2,0);
\foreach \x in {0,50,100}
\node[anchor=east] at (-\axissep-\tickl*2,\x) {\x};

% x-axis
\draw (0,-\axissep) -- ++ (3,0);
\foreach \x in {0,1,...,3}
\draw (\x,-\axissep) -- ++ (0,-\tickl);

% x-labels
\node[below] at (0,-\axissep-\tickl){1};
\node[below] at (1,-\axissep-\tickl){2};
\node[below] at (2,-\axissep-\tickl){3};
\node[below,align=center] at (3,-\axissep-\tickl){4};

% grid
\foreach \x in {0,10,...,100}
\draw[very thin, color=gray] (0,\x) -- (3,\x);

\draw (0,10) -- (1,14) --(2,45) -- (3,23) ++ (1mm,0) node[anchor=west] {\tiny Some label};

\end{tikzpicture}}
\end{figure}

\blindtext

\end{document}


I also placed the tikzpicture inside a \makebox of 0pt width to prevent the overfull bad box and changed the incorrect onesided to oneside (though that is the default for article).

-
Is trim left in the tikz package? A search in the manual gives no hit. – Andreas May 12 '14 at 13:54
@Andreas sure they are; the description is on page 177 (of the manual for version 3.0.0), and they were also present (and documented) in version 2.10 (but I don't have that manual anymore). – Gonzalo Medina May 12 '14 at 13:57
Thanks. Didn't realize I had an outdated version. EDIT: 1165 p.! – Andreas May 13 '14 at 7:39