46

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

0

4 Answers 4

40

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}
[...]
4
  • 6
    When using minipages this way, my diagrams overlapped and chomped captions to their last couple words. Can anything be done about that?
    – rschwieb
    Commented Aug 20, 2015 at 13:05
  • 1
    The two figures overlap if I use minipage. Do you have a suggestion about that?
    – desmond13
    Commented Aug 2, 2017 at 8:19
  • then your figures are too big. Resize it with \scalebox or \resizebox
    – user2478
    Commented Aug 2, 2017 at 8:35
  • Why .2\textwidth instead of .5\textwidth? Could that be what caused the confusion?
    – glibg10b
    Commented Jul 19, 2023 at 6:21
50

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

13
  • 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
    Commented Nov 20, 2014 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
    Commented Aug 20, 2015 at 13:29
  • 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
    Commented Aug 26, 2021 at 13:04
  • 1
    @alper \begin{tabular}{@{}cc@{}}\begin{tikzpicture}<first>\end{tikzpicture} & \begin{tikzpicture}<second>\end{tikzpicture} \\ (a) & (b) \end{tabular}
    – egreg
    Commented Dec 10, 2022 at 14:45
  • 1
    @alper Replace \begin{center} with \begin{figure}[htp]\centering (and of course \end{center} with \end{figure}).
    – egreg
    Commented Aug 23, 2023 at 9:49
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.

1
  • Can we use figure instead of \end{center} while keeping it centered?
    – alper
    Commented Aug 23, 2023 at 8:34
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}
1

You must log in to answer this question.

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