我有下图。在我看来,标记为 G 的路径弄乱了文档中图形的中心位置,可能是因为 TikZ 使用了一些不可见的控制点来绘制此弧。(我让 TikZ 显示图片的边界框以使问题更加明显。)我怎样才能使实际图片居中?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[node distance = 2cm, auto]
\begin{scope}[local bounding box=a]
\node (Kinf) {$K_\infty$};
\node[above of=Kinf, right of=Kinf] (Linf) {$L_\infty$};
\node[below of=Kinf, right of=Kinf, node distance=4cm] (K0) {$K_{0}$};
\node[above of=K0, right of=K0] (L0) {$L_{0}$};
\draw[-] (Kinf) to node [] {$Z_\infty $} (Linf);
\draw[-] (Kinf) to node [rotate=-45, midway, above] {$\ldots$} (K0);
\draw[-] (Linf) to node [rotate=-45, midway, above] {$\ldots$} (L0);
\draw[-] (K0) to node [] {$Z_0$} (L0);
\draw (Kinf) to[out=-90, in=180, edge node={node [near start, left] {$\Gamma$}}] (K0);
\draw[-] (Linf) to[out=0, in=0, looseness=2, edge node={node [near start] {$G$}}] (K0);
\end {scope}
\node [fit=(a),inner sep=0pt,draw] {};
\end{tikzpicture}
\end{figure}
\end{document}
答案1
使用该bbox
包(参见 TikZ 和 PGF 手册,v 3.1.5b,46 贝塞尔曲线的边界框,第 581 页):
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{bbox,
fit}
\begin{document}
\begin{tikzpicture}[
bezier bounding box=true, % <---
node distance = 2cm, auto]
\begin{scope}[local bounding box=a]
\node (Kinf) {$K_\infty$};
\node[above of=Kinf, right of=Kinf] (Linf) {$L_\infty$};
\node[below of=Kinf, right of=Kinf, node distance=4cm] (K0) {$K_{0}$};
\node[above of=K0, right of=K0] (L0) {$L_{0}$};
\draw[-] (Kinf) to node [] {$Z_\infty $} (Linf);
\draw[-] (Kinf) to node [rotate=-45, midway, above] {$\ldots$} (K0);
\draw[-] (Linf) to node [rotate=-45, midway, above] {$\ldots$} (L0);
\draw[-] (K0) to node [] {$Z_0$} (L0);
\draw (Kinf) to[out=-90, in=180, edge node={node [near start, left] {$\Gamma$}}] (K0);
\draw[-] (Linf) to[out=0, in=0, looseness=2, edge node={node [near start] {$G$}}] (K0);
\end {scope}
\node [fit=(a),inner sep=0pt,draw] {};
\end{tikzpicture}
\end{document}
附录和编辑:
设计略有不同。节点的定位使用positioning
包和语法,边标签使用quotes
库。bbox
无需库也能工作代码稍微缩短了一点:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{fit,
positioning,
quotes}
\begin{document}
\begin{tikzpicture}[
node distance = 12mm and 11mm, % <---
]
\begin{scope}[local bounding box=a] % in real document is not needed
\node (Kinf) {$K_\infty$};
\node[above right=of Kinf] (Linf) {$L_\infty$};
\node[below right=24mm and 11 mm of Kinf] (K0) {$K_{0}$};
\node[above right=of K0] (L0) {$L_{0}$};
\draw (Kinf) to ["$Z_\infty $"] (Linf)
(Kinf) to [sloped, "$\ldots$"] (K0)
(Linf) to [sloped, "$\ldots$"] (L0)
(K0) to ["$Z_0$"] (L0)
(Kinf) to[out=240, in=180, "$\Gamma$" '] (K0)
(Linf) to[out=345, in=0,
looseness=1.5, "$G$"] (K0);
\end{scope}
\node[fit=(a),inner sep=0pt,draw=teal] {}; % in real document is not needed
\end{tikzpicture}
\end{document}
*警告: 第一个解决方案不适用于新版本的 TikZ,即 3.1.6 版,因为如github.com/pgf-tikz/pgf/releases/tag/3.1.6:
PGF 3.1.5 中引入的 bbox 库被删除,因为作者决定对重新分发施加限制,这违反了自由软件定义的自由 2 和 3,使其实际上不自由,这与 PGF/TikZ 分发时遵循的 GPLv2 + LPPLv1.3c 条款相矛盾。我进一步建议,如果有包含以前版本遗留的 bbox 代码的文件,请将其删除,以避免潜在的法律问题。
注意:附录中的第二个答案不使用bbox
库,因此它也适用于新版本的 TikZ/PGF 包。请参阅答案下方的@Torbjørn T. 评论(非常感谢您的信息)。
答案2
bbox 库允许您使用选项计算包含图形的最小边界框bezier bounding box=true
。
我没有使用fit
库来构造这个图形,current bounding box
而是使用了尺寸。由于这个框在右侧太窄(它与框架相切),所以我把它做得稍微大一些。
\documentclass{article}
\usepackage{tikz}
%\usetikzlibrary {fit}
\usetikzlibrary{bbox}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[node distance = 2cm, auto,bezier bounding box=true]
\begin{scope}%[local bounding box=a]
\node (Kinf) {$K_\infty$};
\node[above of=Kinf, right of=Kinf] (Linf) {$L_\infty$};
\node[below of=Kinf, right of=Kinf, node distance=4cm] (K0) {$K_{0}$};
\node[above of=K0, right of=K0] (L0) {$L_{0}$};
\draw[-] (Kinf) to node [] {$Z_\infty $} (Linf);
\draw[-] (Kinf) to node [rotate=-45, midway, above] {$\ldots$} (K0);
\draw[-] (Linf) to node [rotate=-45, midway, above] {$\ldots$} (L0);
\draw[-] (K0) to node [] {$Z_0$} (L0);
\draw (Kinf) to[out=-90, in=180, edge node={node [near start, left] {$\Gamma$}}] (K0);
\draw[-] (Linf) to[out=0, in=0, looseness=2, edge node={node [near start] {$G$}}] (K0);
\end {scope}
% \node [fit=(a),inner sep=0pt,draw] {};
\draw (current bounding box.south west) rectangle
([xshift=5pt]current bounding box.north east);
\end{tikzpicture}
\end{figure}
\end{document}