TikZ 图片未在图形 fbox 中居中

TikZ 图片未在图形 fbox 中居中

我试图将 tikzpicture 正确置于子图/图形的中央,但它并没有正确居中。当我将 tikzpicture 放在 \fbox 中时,框架框正确居中,并且大小也根据 tikzpicture 而定,但 tikzpicture 不在框架框内。

下面是一些示例代码,用于存在相同问题的图形和子图形。在第二个代码中,我从 (0,0) 开始绘制第一条线,这似乎基本解决了居中问题(虽然我不知道为什么),但并不能完全解决垂直居中问题。到目前为止,我在这里找到的所有关于居中的类似问题都没有以这种方式表现。

\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{subcaption}
\usepackage{graphicx}
\begin{document}

\begin{figure}[h]
\centering

\begin{subfigure}[b]{.8\textwidth}
\centering
\fbox{\begin{tikzpicture}
\tikz[scale=1]{%
    \draw[dashed,blue,very thick](-3,-.75) -- (-2,0);
    \draw[very thick](-3,.75) -- (-2,0);
    \draw[very thick, black] (-2,0) arc (180:90:1.5 and 1);
    \draw[very thick, black] (-.5,1) arc (90:0:1.5 and 1);
    \draw[dashed, very thick, blue] (1,0) arc (0:-90:1.5 and 1);
    \draw[very thick, blue] (-.5,-1) arc (-90:-180:1.5 and 1);
    \draw[blue,very thick](1,0) -- (2,-.75);
    \draw[-,very thick](1,0) -- (2,.75);
  }

\end{tikzpicture}}
\caption{}
\end{subfigure}
\end{figure}



\begin{figure}[h]
\centering

\fbox{\begin{tikzpicture}
\tikz[scale=1]{%
    \draw[dashed,blue,very thick](0,0) -- (1,.75);
    \draw[very thick](0,1.5) -- (1,.75);
    \draw[very thick, black] (1,.75) arc (180:90:1.5 and 1);
    \draw[very thick, black] (2.5,1.75) arc (90:0:1.5 and 1);
    \draw[dashed, very thick, blue] (4,.75) arc (0:-90:1.5 and 1);
    \draw[very thick, blue] (2.5,-.25) arc (-90:-180:1.5 and 1);
    \draw[blue,very thick](4,.75) -- (5,0);
    \draw[-,very thick](4,.75) -- (5,1.5);
  }

\end{tikzpicture}}
\end{figure}

\end{document}

答案1

\tikz{ .. }是 的简写形式\begin{tikzpicture} .. \end{tikzpicture},您不想同时使用两者。删除\tikz{和结束的}

enter image description here

\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{subcaption}
\usepackage{graphicx}
\begin{document}

\begin{figure}[h]
\centering

\begin{subfigure}[b]{.8\textwidth}
\centering
\fbox{\begin{tikzpicture}
    \draw[dashed,blue,very thick](-3,-.75) -- (-2,0);
    \draw[very thick](-3,.75) -- (-2,0);
    \draw[very thick, black] (-2,0) arc (180:90:1.5 and 1);
    \draw[very thick, black] (-.5,1) arc (90:0:1.5 and 1);
    \draw[dashed, very thick, blue] (1,0) arc (0:-90:1.5 and 1);
    \draw[very thick, blue] (-.5,-1) arc (-90:-180:1.5 and 1);
    \draw[blue,very thick](1,0) -- (2,-.75);
    \draw[-,very thick](1,0) -- (2,.75);
\end{tikzpicture}}
\caption{}
\end{subfigure}
\end{figure}



\begin{figure}[h]
\centering

\fbox{\begin{tikzpicture}
    \draw[dashed,blue,very thick](0,0) -- (1,.75);
    \draw[very thick](0,1.5) -- (1,.75);
    \draw[very thick, black] (1,.75) arc (180:90:1.5 and 1);
    \draw[very thick, black] (2.5,1.75) arc (90:0:1.5 and 1);
    \draw[dashed, very thick, blue] (4,.75) arc (0:-90:1.5 and 1);
    \draw[very thick, blue] (2.5,-.25) arc (-90:-180:1.5 and 1);
    \draw[blue,very thick](4,.75) -- (5,0);
    \draw[-,very thick](4,.75) -- (5,1.5);

\end{tikzpicture}}
\end{figure}

\end{document}

相关内容