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.

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}
share|improve this question
1  
I don't know if you will like it but replacing \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. –  Ignasi Jul 29 '13 at 12:00
1  
You can simply add overlay to the Employees node so that it doesn’t affect the bounding box (similar to how the pgfinterruptboundingbox environment would do). Instead of xcenter around you can also add the use as bounding box option the the path with the grid (making the bounding box slightly larger than xcenter around={0,0}{9,6}. –  Qrrbrbirlbel Jul 30 '13 at 15:32
    
@Qrrbrbirlbel Thanks. Just to make sure: adding use as bounding box to the grid path would be additional to the xcenter around option? –  henry Jul 31 '13 at 12:22
1  
@henry No, without xcenter around, although my solution of use as bounding box isn’t that different than using xcenter around only that you don't need to provide the points twice. –  Qrrbrbirlbel Jul 31 '13 at 16:16
    
Please add you comment as an answer so I can mark it. –  henry Aug 8 '13 at 18:02
show 1 more comment

1 Answer 1

up vote 1 down vote accepted

you can use the library fit to oversee all your scheme centering on the red dot and specifying a minimum width equal to the width of the page

\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){Employees}; %X-Achse
    \draw[->,thick] (0,-0.1) -- (0,6.5) node[anchor=south](sales){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)coordinate(center) circle (4pt);

\node[fit=(center) (employees) (sales),minimum width=\linewidth,draw]at(center){};
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

share|improve this answer
    
The rectangle shape you used for the final fit includes the outer xsep in addition to minimum width. Notice the overful \hbox warning. Best thing would be to use minimum width=\textwidth-2*(\pgfkeysvalueof{/pgf/outer xsep}) (or not using draw at all of course). –  Qrrbrbirlbel Jul 30 '13 at 15:36
    
Ok, it's better –  rpapa Jul 30 '13 at 19:22
    
the draw, is only for the MWE –  rpapa Jul 30 '13 at 19:23
add comment

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.