this is a rather nit-picky question but I wanted to ask anyway. Is is possible to center the tikz figure horizontally with using the coordinate indicated by the red circle?
The catch is, I have found an answer already. Martin Scharrer provided a "good" compact snippet here. But considering my level of expertise and that I just copy and pasted some code I would like to make sure: is this a good practice still? Maybe some things inside TikZ changed in the last ~2 years and it now can be done easier?
MWE
\documentclass[
11pt
]{scrartcl}
\usepackage{
tikz,
relsize,
tgheros
}
\usetikzlibrary{
calc,trees,shadows,positioning,arrows,chains,shapes.geometric,
decorations.pathreplacing,decorations.pathmorphing,shapes,
matrix,shapes.symbols,patterns,intersections,fit
}
\tikzset{
>=latex
}
\tikzset{xcenter around/.style 2 args={execute at end picture={%
\useasboundingbox let \p0 = (current bounding box.south west), \p1 = (current bounding box.north east),
\p2 = (#1), \p3 = (#2)
in
({min(\x2 + \x3 - \x1,\x0)},\y0) rectangle ({max(\x3 + \x2 - \x0,\x1)},\y1);
}}}
\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small,xcenter around = {0,0}{9,6}]
%
\draw[style=help lines,step=0.5cm] (0,0) grid (9.1,6.1);
%
\draw[->,thick] (-0.1,0) -- (9.5,0) node[anchor=west]{Employees}; %X-Achse
\draw[->,thick] (0,-0.1) -- (0,6.5) node[anchor=south]{Sales}; %Y-Achse
%
\foreach \x in {0,1,...,9} \draw [thick](\x cm,-2pt) -- (\x cm,2pt);
\foreach \y in {0,1,...,6} \draw [thick](-2pt,\y) -- (2pt,\y);
%
\foreach \x in {0,1,...,9} \draw (\x cm, 0 cm) node[anchor=north]{\x} coordinate (x axis);
\foreach \y in {0,1,...,6} \draw (0 cm, \y cm) node[anchor=east]{\y} coordinate (y axis);
%
\node (orig) at (0,0) {};
\draw
(2,1) coordinate (A)
(3,3) coordinate (B)
(6.5,4.5) coordinate (C)
(5,2) coordinate (D)
(9,5) coordinate (E)
;
%
\foreach \pt/\labpos in {B/above left,C/below,D/below left}{
\filldraw (\pt) circle (2pt) node[\labpos=3pt,fill=white]{\pt};
};
\foreach \pt/\labpos in {A/below,E/below}{
\filldraw[gray] (\pt) circle (2pt) node[\labpos=3pt,fill=white]{\pt};
};
%
\draw[draw=red,fill=red] (4.5,3) circle (4pt);
\end{tikzpicture}
\end{center}
\end{document}
\draw[->,thick] (-0.1,0) -- (9.5,0) node[anchor=west]{Employees};
with\draw[->,thick] (-0.1,0) -- (9.5,0) node[below=4mm,midway]{Employees};
avoids your problem.overlay
to theEmployees
node so that it doesn’t affect the bounding box (similar to how thepgfinterruptboundingbox
environment would do). Instead ofxcenter around
you can also add theuse as bounding box
option the the path with thegrid
(making the bounding box slightly larger thanxcenter around={0,0}{9,6}
.use as bounding box
to thegrid
path would be additional to thexcenter around
option?xcenter around
, although my solution ofuse as bounding box
isn’t that different than usingxcenter around
only that you don't need to provide the points twice.