44

I'm trying to get these two tikzpictures to be shown side by side. This is what I have:

Not the most minimal example, but should be sufficient:

\documentclass[12pt,a4]{article}
\usepackage{tikz}

\begin{document}

\begin{center}
\begin{minipage}{.2\textwidth}

\begin{tikzpicture}[scale=0.33] %[x={10.0pt},y={10.0pt}]

\draw[line width=2pt] (0,0) -- (0,10);
\draw[line width=2pt] (0,0) -- (10,0);
\draw[line width=2pt] (0,10) -- (10,10);
\draw[line width=2pt] (0,10) -- (10,10);
\draw[line width=2pt] (10,0) -- (10,10);

\draw[line width=2pt] (0,0) -- (3,5);

\draw[line width=2pt] (10,0) -- (13,5);

\draw[line width=2pt] (0,10) -- (3,15);

\draw[line width=2pt] (10,10) -- (13,15);

\draw[line width=2pt] (3,5) -- (13,5);

\draw[line width=2pt] (3,5) -- (3,15);

\draw[line width=2pt] (13,5) -- (13,15);

\draw[line width=2pt] (3,15) -- (13,15);

\draw[line width=1pt] (5,0) -- (5,10);

\draw[line width=1pt] (5,10) -- (8,15);

\draw[line width=1pt] (5,0) -- (8,5);

\draw[line width=1pt] (8,5) -- (8,15);

\draw[black, fill=blue,fill opacity=0.5] (5,0) -- (5,10) -- (8,15) -- (8,5) -- cycle;

\end{tikzpicture} % pic 1

\end{minipage}

\begin{minipage}{.2\textwidth}

\begin{tikzpicture}[scale=0.33] %[x={10.0pt},y={10.0pt}]

\draw[line width=2pt] (0,0) -- (0,10);
\draw[line width=2pt] (0,0) -- (10,0);
\draw[line width=2pt] (0,10) -- (10,10);
\draw[line width=2pt] (0,10) -- (10,10);
\draw[line width=2pt] (10,0) -- (10,10);

\draw[line width=2pt] (0,0) -- (3,5);

\draw[line width=2pt] (10,0) -- (13,5);

\draw[line width=2pt] (0,10) -- (3,15);

\draw[line width=2pt] (10,10) -- (13,15);

\draw[line width=2pt] (3,5) -- (13,5);

\draw[line width=2pt] (3,5) -- (3,15);

\draw[line width=2pt] (13,5) -- (13,15);

\draw[line width=2pt] (3,15) -- (13,15);

\draw[line  width=1pt] (0,10) -- (10,0);

\draw[line  width=1pt] (0,10) -- (3,5);

\draw[line  width=1pt] (3,5) -- (10,0);

\draw[black, fill=blue,fill opacity=0.5] (0,10) -- (10,0) -- (3,5) -- cycle;

\draw[line  width=1pt] (3,15) -- (10,10);

\draw[line  width=1pt] (3,15) -- (13,5);

\draw[line  width=1pt] (10,10) -- (13,5);

\draw[black, fill=blue,fill opacity=0.5] (3,15) -- (10,10) -- (13,5) -- cycle;

\end{tikzpicture}

\end{minipage}

\end{center}

\end{document}

I've tried using minipage and subfigure/subcaption packages with no luck.

Edit: I'd like to have a caption below each tikzpicture (but I don't need to refer to it, so no external package is needed per se), hence why minipage is used, although now the spacing isn't consistent (too much space on the right):

enter image description here

Argo
  • 1,724

4 Answers4

48

Why the minipages?

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}

\begin{document}

\begin{center}
\begin{tikzpicture}[scale=0.33] %[x={10.0pt},y={10.0pt}]
\draw[line width=2pt] (0,0) -- (0,10);
\draw[line width=2pt] (0,0) -- (10,0);
\draw[line width=2pt] (0,10) -- (10,10);
\draw[line width=2pt] (0,10) -- (10,10);
\draw[line width=2pt] (10,0) -- (10,10);
\draw[line width=2pt] (0,0) -- (3,5);
\draw[line width=2pt] (10,0) -- (13,5);
\draw[line width=2pt] (0,10) -- (3,15);
\draw[line width=2pt] (10,10) -- (13,15);
\draw[line width=2pt] (3,5) -- (13,5);
\draw[line width=2pt] (3,5) -- (3,15);
\draw[line width=2pt] (13,5) -- (13,15);
\draw[line width=2pt] (3,15) -- (13,15);
\draw[line width=1pt] (5,0) -- (5,10);
\draw[line width=1pt] (5,10) -- (8,15);
\draw[line width=1pt] (5,0) -- (8,5);
\draw[line width=1pt] (8,5) -- (8,15);
\draw[black, fill=blue,fill opacity=0.5] (5,0) -- (5,10) -- (8,15) -- (8,5) -- cycle;
\end{tikzpicture}% pic 1
\qquad % <----------------- SPACE BETWEEN PICTURES
\begin{tikzpicture}[scale=0.33] %[x={10.0pt},y={10.0pt}]
\draw[line width=2pt] (0,0) -- (0,10);
\draw[line width=2pt] (0,0) -- (10,0);
\draw[line width=2pt] (0,10) -- (10,10);
\draw[line width=2pt] (0,10) -- (10,10);
\draw[line width=2pt] (10,0) -- (10,10);
\draw[line width=2pt] (0,0) -- (3,5);
\draw[line width=2pt] (10,0) -- (13,5);
\draw[line width=2pt] (0,10) -- (3,15);
\draw[line width=2pt] (10,10) -- (13,15);
\draw[line width=2pt] (3,5) -- (13,5);
\draw[line width=2pt] (3,5) -- (3,15);
\draw[line width=2pt] (13,5) -- (13,15);
\draw[line width=2pt] (3,15) -- (13,15);
\draw[line  width=1pt] (0,10) -- (10,0);
\draw[line  width=1pt] (0,10) -- (3,5);
\draw[line  width=1pt] (3,5) -- (10,0);
\draw[black, fill=blue,fill opacity=0.5] (0,10) -- (10,0) -- (3,5) -- cycle;
\draw[line  width=1pt] (3,15) -- (10,10);
\draw[line  width=1pt] (3,15) -- (13,5);
\draw[line  width=1pt] (10,10) -- (13,5);
\draw[black, fill=blue,fill opacity=0.5] (3,15) -- (10,10) -- (13,5) -- cycle;
\end{tikzpicture}% pic 2

\end{center}

\end{document}

Instead of \qquad you can have \hspace{<length>} (just remember the form

\end{tikzpicture}% NO SPACE!
\hspace{3cm}% NO SPACE!
\begin{tikzpicture}[...]

so you precisely control the horizontal space between the images. Vertical alignment can be obtained with TikZ methods (look for baseline in the manual).

Note also that the a4 option is invalid, it should be a4paper.

enter image description here

egreg
  • 1,121,712
  • I'd like to have a caption below each tikzpicture, but not necessarily made by a package (I don't need to refer to it). – Argo Nov 20 '14 at 13:36
  • By adding \vrule{} I was able to introduce a line between the two pictures, but it was not centered. Do you know offhand how to fix this? – rschwieb Aug 20 '15 at 13:29
  • (I hacked it by adding an extra blank column to one of the diagrams) – rschwieb Aug 20 '15 at 13:35
  • @rschwieb \qquad\vrule\qquad should do – egreg Aug 20 '15 at 15:11
  • @egreg, Can you help me with another issue ? Often, when I place a Figure (tikz) in a proof of a Theorem, I see that after compiling the latex, the Figure is placed in another place (before or after the theorem) where I don't want. So how exactly place a Figure exactly in some where in the proof of a Theorem ? Thanks – learner Aug 26 '21 at 12:25
  • @M.A.SARKAR You don’t want to use figure – egreg Aug 26 '21 at 12:59
  • @egreg, I mean I want to place a Figure in a proof of a Theorem in a particular place (e.g., after a particular line). But in most cases the figure is automatically adjusted in arbitrary place rather than in between the Proof. – learner Aug 26 '21 at 13:02
  • 1
    @M.A.SARKAR You use figure if you need a caption and then there’s no problem in letting the object to float. Just use ‘center’ or even \[...\] – egreg Aug 26 '21 at 13:04
  • @egreg, Thank you very much. Now if I have two side-by-side figures and I need to give two different captions. How do we do that ? For example, in your above nice answer, you placed two figures side-by-side through command \qquad. How do we write two different captions below each figure ? Any Help please – learner Aug 26 '21 at 13:07
  • can we have (a) (b) below of each image respectively? – alper Dec 10 '22 at 14:40
  • 1
    @alper \begin{tabular}{@{}cc@{}}\begin{tikzpicture}<first>\end{tikzpicture} & \begin{tikzpicture}<second>\end{tikzpicture} \\ (a) & (b) \end{tabular} – egreg Dec 10 '22 at 14:45
  • When we use figure can we keep the figures centered like using in \begin{center}? – alper Aug 23 '23 at 08:42
  • 1
    @alper Replace \begin{center} with \begin{figure}[htp]\centering (and of course \end{center} with \end{figure}). – egreg Aug 23 '23 at 09:49
39

Delete the empty line between the minipages. It is like the \par command. Use instead:

[...]
\end{minipage}
%  no empty line here 
\begin{minipage}{.2\textwidth}
[...]
13

Here is another version:

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\tikzset{mynode/.style={inner sep=2pt,fill,outer sep=0,circle}}

\begin{document}

\begin{center}
\begin{tikzpicture}[scale=3,line width=1pt]
 \coordinate (1) at (0,0,0);
 \coordinate (2) at (0,1,0);
 \coordinate (3) at (1,1,0);
 \coordinate (4) at (1,0,0);
 \coordinate (5) at (0,0,1);
 \coordinate (6) at (0,1,1);
 \coordinate (7) at (1,1,1);
 \coordinate (8) at (1,0,1);
 \foreach \x in {1,2,...,8}{
     \node[mynode] at (\x) {};
 }
 \draw (1) -- (2) -- (3) -- (4) -- cycle;
 \draw (5) -- (6) -- (7) -- (8) -- cycle;
 \draw (1) -- (5)  (2) -- (6) (3) -- (7) (4) -- (8);
 \draw[thin,fill=blue!50,opacity=0.5] (0.5,0,0) -- (0.5,0,1) -- (0.5,1,1) -- (0.5,1,0) --cycle;
 \node at (0.5,-0.2,1) {(\textit{h\,k\,l})};
\end{tikzpicture}
\hspace{1cm}
\begin{tikzpicture}[scale=3,line width=1pt]
 \coordinate (1) at (0,0,0);
 \coordinate (2) at (0,1,0);
 \coordinate (3) at (1,1,0);
 \coordinate (4) at (1,0,0);
 \coordinate (5) at (0,0,1);
 \coordinate (6) at (0,1,1);
 \coordinate (7) at (1,1,1);
 \coordinate (8) at (1,0,1);
 \foreach \x in {1,2,...,8}{
     \node[mynode] at (\x) {};
 }
 \draw (1) -- (2) -- (3) -- (4) -- cycle;
 \draw (5) -- (6) -- (7) -- (8) -- cycle;
 \draw (1) -- (5)  (2) -- (6) (3) -- (7) (4) -- (8);
 \draw[thin,fill=blue!50,opacity=0.5] (1) -- (8) -- (6) --cycle;
 \draw[thin,fill=blue!50,opacity=0.5] (2) -- (4) -- (7) --cycle;
 \node at (0.5,-0.2,1) {(\textit{h\,k\,l})};
\end{tikzpicture}

\end{center}

\end{document}

enter image description here

You can reduce the \coordinate definition bit by

 \foreach \x/\y/\z/\a in {0/0/0/1,0/1/0/2,1/1/0/3,1/0/0/4,0/0/1/5,0/1/1/6,1/1/1/7,1/0/1/8} {
    \coordinate (\a) at (\x,\y,\z);
    }

if you want.

0

This worked for me. But I believe that in some cases you need to add % after each node command.

\begin{tikzpicture}
\node[draw, fill=cyan!40, minimum width=2cm, minimum height=1cm]{Ti\textit{k}Z};
\hspace{3cm}
\node[draw, fill=red, minimum width=2cm, minimum height=1cm, text=white]{Ti\textit{k}Z};
\end{tikzpicture}
Leucippus
  • 1,636