这是一个相当挑剔的问题,但我还是想问一下。是否可以使用红色圆圈指示的坐标将 tikz 图形水平居中?
问题是,我已经找到了答案。Martin Scharrer 提供了一个“好的”紧凑片段这里。但考虑到我的专业水平以及我只是复制粘贴了一些代码,我想确定一下:这仍然是一种好的做法吗?也许 TikZ 内部的一些东西在过去的两年里发生了变化,现在可以更容易地完成?
平均能量损失
\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}
答案1
您可以使用 fit 库来监督以红点为中心的所有方案,并指定等于页面宽度的最小宽度
\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}