3

It seems to be lots of problems like this in the archive but none seems to match mine.

I have a TikZ figure that depends on \draw. I know that keys off an assumed Cartesian plane and maybe that is the problem with centering -- if so I might be screwed.

Anyway, here is the code:

\documentclass{amsbook}
\usepackage{tikz}

\begin{document}

\def\SetA{(0,0) circle (1.5cm)}
\def\SetB{(1,2) circle (1.5cm)}
\def\SetC{(2,0) circle (1.5cm)}

\begin{center}
\begin{figure}
\begin{tikzpicture}
        \draw (-3,-2) rectangle (5,4);
        \draw (4.5,3.5) node {$\mathcal{U}$};
        \draw (-2,2) node {$A \cap B$};
        \draw (4,2) node {$A \cap C$};
    \draw \SetA node[below] {$A$};
    \draw \SetB node [above] {$B$};
    \draw \SetC node [below] {$C$};

    \begin{scope}
      \clip \SetA;  
      \fill[red] \SetB;  
    \end{scope}

    \begin{scope}
      \clip \SetA;
      \clip \SetB;
      \fill[green] \SetC;
    \end{scope}
\end{tikzpicture}
\caption{Examples of Intersection}
\end{figure}
\end{center}

\end{document}

And here is the output:

enter image description here

2
  • Since I haven't figured out how to add a graphic I'll describe it: the TikZ figure is a little to the left and the caption appears to be centered. Of course. I'd like them both to be centered. (Maybe some kind person could explain what I should do After I click image to add same.)
    – TonyK
    Dec 14, 2021 at 6:50
  • For adding the image, see here: tex.meta.stackexchange.com/questions/2136/…
    – CarLaTeX
    Dec 14, 2021 at 7:24

1 Answer 1

5

Don't use figure environment within a center evironment, just use figure with \centering.

See When should we use \begin{center} instead of \centering?.

I also added caption package for a better caption alignment.

\documentclass{amsbook}
\usepackage{caption}
\usepackage{tikz}
\begin{document}
\def\SetA{(0,0) circle (1.5cm)}
\def\SetB{(1,2) circle (1.5cm)}
\def\SetC{(2,0) circle (1.5cm)}
\begin{figure}\centering
\begin{tikzpicture}
        \draw (-3,-2) rectangle (5,4);
        \draw (4.5,3.5) node {$\mathcal{U}$};
        \draw (-2,2) node {$A \cap B$};
        \draw (4,2) node {$A \cap C$};
    \draw \SetA node[below] {$A$};
    \draw \SetB node [above] {$B$};
    \draw \SetC node [below] {$C$};
    \begin{scope}
      \clip \SetA;  
      \fill[red] \SetB;  
    \end{scope}
    \begin{scope}
      \clip \SetA;
      \clip \SetB;
      \fill[green] \SetC;
    \end{scope}
\end{tikzpicture}
\caption{Examples of Intersection}
\end{figure}
\end{document}

enter image description here

Alternatively, you can use center without figure, in this case add the caption with \captionof:

\documentclass{amsbook}
\usepackage{caption}
\usepackage{tikz}
\begin{document}
\def\SetA{(0,0) circle (1.5cm)}
\def\SetB{(1,2) circle (1.5cm)}
\def\SetC{(2,0) circle (1.5cm)}
\begin{center}
\begin{tikzpicture}
        \draw (-3,-2) rectangle (5,4);
        \draw (4.5,3.5) node {$\mathcal{U}$};
        \draw (-2,2) node {$A \cap B$};
        \draw (4,2) node {$A \cap C$};
    \draw \SetA node[below] {$A$};
    \draw \SetB node [above] {$B$};
    \draw \SetC node [below] {$C$};
    \begin{scope}
      \clip \SetA;  
      \fill[red] \SetB;  
    \end{scope}
    \begin{scope}
      \clip \SetA;
      \clip \SetB;
      \fill[green] \SetC;
    \end{scope}
\end{tikzpicture}
\captionof{figure}{Examples of Intersection}
\end{center}
\end{document}

The difference between the two is that figure floats (that is, LaTeX put it where it's better), whereas center does not (it is placed where it is).

2
  • Two answers for the price of one. Awesome! Thank you CarlaTeX. So much I don't know. And just in case anyone thinks I created this TikZ figure -- nope. Piecing things together from code stolen from here.
    – TonyK
    Dec 14, 2021 at 7:14
  • @TonyK As an old Italian COBOL programmer I told you only Spoletini wrote, all the others copy-pasted (Spoletini wrote a COBOL manual in Italian) :D Thank you for accepting my answer.
    – CarLaTeX
    Dec 14, 2021 at 7:19

You must log in to answer this question.

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