我正在尝试让用 制作的图形tikz
精确地居中在图形自己的内部 x 轴上。我不想使用\centering
,因为至少它的默认行为是,\centering
根据tikzpicture
整体的实际宽度找到自己的中心线。我正在寻找一种将其居中在选定点上的方法,完全忽略图形的实际宽度。
MWE\centering
的行为:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{figure}\centering
\begin{tikzpicture}
\draw (-3,0) -- (2,0);
\path (0,2) node [shape=circle,draw] {};
\end{tikzpicture}
\caption{This figure should be centered on x=0, marked by the circle. It isn't.}
\end{figure}
\end{document}
简而言之,我正在寻找一种方法来获取这个精确的图形(包括偏离中心的线),并将其放置在图形浮动内,使得 x=0 处的圆圈位于标题上方的中心(线偏离中心)。这样的事情可能吗?它涉及什么?
答案1
(非覆盖)的坐标tikzpicture
没有绝对意义,因为 Ti钾Z 根据您在图片中绘制的内容计算边界框。当然,您可以重新调整边界框,使原点成为边界框的水平中心。可以手动调整,例如\path (3,0);
在本例中通过添加,也可以为其编写样式,例如
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{make origin horizontal center of bounding box/.style={%
execute at end picture={%
\path let \p1=(current bounding box.west),\p2=(current bounding box.east)
in ({-max(-1*\x1,\x2)},\y1) ({max(-1*\x1,\x2)},\y1);
}}}
\begin{document}
\begin{figure}\centering
\begin{tikzpicture}
\draw (-3,0) -- (2,0);
\path (0,2) node [shape=circle,draw] {};
\end{tikzpicture}
\caption{This figure should be centered on x=0, marked by the circle. It
isn't because the bounding box of a \textbackslash\texttt{tikzpicture}
is computed on the basis of its contents.}
\end{figure}
\begin{figure}\centering
\begin{tikzpicture}[make origin horizontal center of bounding box]
\draw (-3,0) -- (2,0);
\path (0,2) node [shape=circle,draw] {};
\end{tikzpicture}
\caption{This figure should be centered on x=0, marked by the circle. It
is now with the help of \texttt{make origin horizontal center of bounding box}.}
\end{figure}
\end{document}