7

I have four venn-diagrams on the same line but the captions are not aligned because of the setdifference symbol. Is there a way to manually override this and force the rectangles to align?

\documentclass{article}
\usepackage{tikz}
\usepackage[margin=1.1in]{geometry}
\def \setA{ (0,0) circle (1cm) node {$A$}}
\def \setB{ (1.5,0) circle (1cm) node {$B$}}
\def \myrectangle{ (-2, -1.5) rectangle (3.5, 1.5) }
\begin{document}
\begin{center}
%Union of A and B
\begin{tikzpicture}[scale=0.7]
\draw \myrectangle node[below left]{$U$};
\fill[gray] (0,0) circle (1cm);
\fill[gray] (1.5,0) circle (1cm);
\draw \setA;
\draw \setB;
\node [below=.1cm] at (.1,-1.5) {$A \cup B$};
\end{tikzpicture} \
%Intersection of A and B
\begin{tikzpicture}[scale=0.7]
\draw \myrectangle node[below left]{$U$};
\begin{scope}
\clip \setA ;
\fill[gray] \setB ;
\end{scope}
\draw \setA;
\draw \setB;
\node [below=.1cm] at (.1,-1.5) {$A \cap B$};
\end{tikzpicture} \
%Difference of A and B
\begin{tikzpicture}[scale=0.7]
\draw \myrectangle node[below left]{$U$};
\fill[gray] (0,0) circle (1cm);
\begin{scope}
\clip \setA ;
\fill[white] \setB ;
\end{scope}
\draw \setA;
\draw \setB;
\node [below=.1cm] at (.1,-1.5) {$A \setminus B$};
\end{tikzpicture} \
%Complement of A
\begin{tikzpicture}[scale=0.7]
\fill[gray] (-1.5,-1.5) rectangle (1.5,1.5);
\fill[white] (0,0) circle (1cm);
\draw (-1.5,-1.5) rectangle (1.5,1.5) node[below left]{$U$};
\draw \setA;
\node [below=.1cm] at (.1,-1.5) {$A^C$};
\end{tikzpicture}
\end{center}
\end{document}

Notice on the output how the first, second, and last ones are aligned but not the third.

Output

  • ! LaTeX Error: Missing \begin{document}. If I add `\begin{document) the output looks like this so not on one line as you show. – David Carlisle May 29 '17 at 19:11
  • 1
    Add baseline to the picture options: \begin{tikzpicture}[scale=0.7,baseline] – esdd May 29 '17 at 19:15
  • @DavidCarlisle I added the 1.1 in margins that i'm using. Now everything should appear on a single line. – Seth Killian May 29 '17 at 19:16
  • You can set baseline= for each tikzpicture. Moreover, you can anchor your caption nodes by setting the anchor=base key. You also might look in the TikZ matrix construction for such stuff. – marsupilam May 29 '17 at 19:16
  • @esdd to which one? All four or the one that's not in alignment? – Seth Killian May 29 '17 at 19:16
7

I would use

\tikzset{every picture/.append style={scale=0.7,baseline}}

inside the center environment to set the same options for all pictures. (Then scale=0.7 must be removed from the options of the each single picture.)

enter image description here

Code:

\documentclass{article}
\usepackage{tikz}
\usepackage[margin=1.1in]{geometry}
\def \setA{ (0,0) circle (1cm) node {$A$}}
\def \setB{ (1.5,0) circle (1cm) node {$B$}}
\def \myrectangle{ (-2, -1.5) rectangle (3.5, 1.5) }
\begin{document}
\begin{center}
\tikzset{every picture/.append style={scale=0.7,baseline}}
%Union of A and B
\begin{tikzpicture}
\draw \myrectangle node[below left]{$U$};
\fill[gray] (0,0) circle (1cm);
\fill[gray] (1.5,0) circle (1cm);
\draw \setA;
\draw \setB;
\node [below=.1cm] at (.1,-1.5) {$A \cup B$};
\end{tikzpicture} \
%Intersection of A and B
\begin{tikzpicture}
\draw \myrectangle node[below left]{$U$};
\begin{scope}
\clip \setA ;
\fill[gray] \setB ;
\end{scope}
\draw \setA;
\draw \setB;
\node [below=.1cm] at (.1,-1.5) {$A \cap B$};
\end{tikzpicture} \
%Difference of A and B
\begin{tikzpicture}
\draw \myrectangle node[below left]{$U$};
\fill[gray] (0,0) circle (1cm);
\begin{scope}
\clip \setA ;
\fill[white] \setB ;
\end{scope}
\draw \setA;
\draw \setB;
\node [below=.1cm] at (.1,-1.5) {$A \setminus B$};
\end{tikzpicture} \
%Complement of A
\begin{tikzpicture}
\fill[gray] (-1.5,-1.5) rectangle (1.5,1.5);
\fill[white] (0,0) circle (1cm);
\draw (-1.5,-1.5) rectangle (1.5,1.5) node[below left]{$U$};
\draw \setA;
\node [below=.1cm] at (.1,-1.5) {$A^C$};
\end{tikzpicture}
\end{center}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.