3

I'm trying to properly center a tikzpicture within a subfigure/figure, however it has not been centering properly. When I enclose the tikzpicture in \fbox, the frame box is properly centered and is also sized according to the tikzpicture, but the tikzpicture does not fall within the frame box.

Below is some sample code for both a figure and subfigure with the same problem. In the second, I draw the first line starting from (0,0), which seems to mostly fix the centering (although I don't know why), but not entirely for the vertical centering. None of the similar questions regarding centering I've found here so far behave in this way.

\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}
2
  • 1
    Welcome to TeX.SE. Shouldn't you draw the frame with TikZ yourself instead of using \fbox here?
    – user31729
    Commented Oct 28, 2017 at 19:06
  • @ChristianHupfer The frame was primarily to troubleshoot the centering based on another centering issue I came across.
    – Cider
    Commented Oct 28, 2017 at 19:24

1 Answer 1

2

\tikz{ .. } is a short form for \begin{tikzpicture} .. \end{tikzpicture}, you don't want to use both. Remove \tikz{ and the closing }.

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}
1
  • Thanks, that fixed it. I figured it had to be something simple.
    – Cider
    Commented Oct 28, 2017 at 19:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .