6

I have used the following code:

\begin{figure}[h]  
\begin{tikzpicture}   
      \draw[->] (-0.5,0) -- (4,0) node[right] {$x$};  
      \draw[->] (0,-0.5) -- (0,4) node[above] {$y$};  
      \draw (0,0.5) -- (4,2);  
      \draw [dashed] (0,1.5) -- (4,4);  
\end{tikzpicture}  
\caption{No Interaction} \label{fig:M1}  

\begin{tikzpicture}  
      \draw[->] (-0.5,0) -- (4,0) node[right] {$x$};  
      \draw[->] (0,-0.5) -- (0,4) node[above] {$y$};  
      \draw (0,0.5) -- (4,2.5);  
      \draw [dashed] (0,1.5) -- (4,3.5);  
\end{tikzpicture}. 
\caption{Interaction} \label{fig:M2}  
\end{figure}  

to insert two figures, however it places them one on top of the other and I would like them next to each other but labelled independently, is this possible?

5
  • 1
    Well, if you want to have two TikZ pictures side by side you should not use two captions and no empty lines in between the tikzpicture environments. Either use something like subcaption package or just one caption for both images.
    – user31729
    May 3 '17 at 16:07
  • Sorry I don't understand what you mean? May 3 '17 at 16:09
  • Two captions force necessarily a line break, so the pictures are above each other, not side by side
    – user31729
    May 3 '17 at 16:11
  • I have removed the captions and they are still above each other May 3 '17 at 16:12
  • Because you most likely still have an empty line between
    – user31729
    May 3 '17 at 16:13
11

A tikzpicture acts basically like a letter, so if you want to place two letters side by side you should not use any command that inserts vertical space or leave an empty line. In the first example I removed the first caption and combined it with the second one, removing also the empty line in between.

The second solution shows a possible way with subfigure from subcaption package: Put each tikzpicture into a subfigure environment and provide a \caption for each.

\documentclass{article}

\usepackage{tikz}

\usepackage{subcaption}


\begin{document}

\begin{figure}[h]  
\centering 

\begin{tikzpicture}   
      \draw[->] (-0.5,0) -- (4,0) node[right] {$x$};  
      \draw[->] (0,-0.5) -- (0,4) node[above] {$y$};  
      \draw (0,0.5) -- (4,2);  
      \draw [dashed] (0,1.5) -- (4,4);  
\end{tikzpicture}% NO EMPTY LINE HERE!!!! 
\begin{tikzpicture}  
      \draw[->] (-0.5,0) -- (4,0) node[right] {$x$};  
      \draw[->] (0,-0.5) -- (0,4) node[above] {$y$};  
      \draw (0,0.5) -- (4,2.5);  
      \draw [dashed] (0,1.5) -- (4,3.5);  
\end{tikzpicture} 
\caption{Left: No Interaction. Right: Interaction} \label{fig:M}  
\end{figure}  

\begin{figure}[h]  
\centering 
  \begin{subfigure}[b]{0.4\linewidth}
    \begin{tikzpicture}   
      \draw[->] (-0.5,0) -- (4,0) node[right] {$x$};  
      \draw[->] (0,-0.5) -- (0,4) node[above] {$y$};  
      \draw (0,0.5) -- (4,2);  
      \draw [dashed] (0,1.5) -- (4,4);  
    \end{tikzpicture}%
    \caption{No interaction} \label{fig:M1}  
  \end{subfigure}
\begin{subfigure}[b]{0.4\linewidth}
  \begin{tikzpicture}  
      \draw[->] (-0.5,0) -- (4,0) node[right] {$x$};  
      \draw[->] (0,-0.5) -- (0,4) node[above] {$y$};  
      \draw (0,0.5) -- (4,2.5);  
      \draw [dashed] (0,1.5) -- (4,3.5);  
\end{tikzpicture}
\caption{Interaction} \label{fig:M2}  
\end{subfigure}
\caption{Interaction figures}
\end{figure}  
\end{document}

enter image description here

0

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