8

Okay, I tried a lot of things and can't really find it anywhere. Is there a simple example code for how to put a picture next to a graph?

I tried to play around with hspace and vspace as well as an example of having two plots next to each other but replacing it by a figure is not possible for me at the moment.

8

Here's a solution using the little yet powerful subcaption package (which has a better interaction with hyperref than subfig):

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{pgfplots}
\pgfplotsset{width=3in}

\begin{document}
\begin{figure}
\begin{subfigure}[b]{.5\textwidth}
  \centering
  \includegraphics{picture} 
  \caption{A picture}
\end{subfigure}%
\begin{subfigure}[b]{.5\textwidth}
  \centering
  \begin{tikzpicture}[scale=0.8]
  \begin{axis}
  \addplot    {x^2 * sin(x)};
  \end{axis}
  \end{tikzpicture}
  \caption{A plot}
\end{subfigure}
\end{figure}

\end{document}

enter image description here

The relative vertical alignment can be controlled by using the optional argument of the subfigure environment.

In the above approach I assumed that the figure and plot should be treated as subfigures; if this is not the case and both should be considered as independent figures, then two side-by-side minipages can be an option; the \captionof command could be used to produce the captions and an additional figure environment could be used to make the whole arrangement float:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{pgfplots}
\pgfplotsset{width=3in}

\begin{document}

\begin{figure}
\begin{minipage}[b]{.5\textwidth}
  \centering
  \includegraphics{picture} 
  \captionof{figure}{A picture}
\end{minipage}%
\begin{minipage}[b]{.5\textwidth}
  \centering
  \begin{tikzpicture}[scale=0.8]
  \begin{axis}
  \addplot    {x^2 * sin(x)};
  \end{axis}
  \end{tikzpicture}
  \captionof{figure}{A plot}
\end{minipage}
\end{figure}

\end{document}

enter image description here

  • +1: Other advantages of subcaption are that you can very easily customize the font and enumeration style of (a), (b), etc (I know that Gonzalo knows this, thought I'd mention it for the OP) – cmhughes Nov 22 '11 at 19:17
4

This looks like a job for the subfig package. Examples can be found in its documentation.

A quick example using TikZ to draw a very simple plot:

\documentclass{article}
\usepackage{subfig}
\usepackage{tikz}
\begin{document}
\begin{figure}%
  \centering
  \subfloat[Your image]{\rule{30pt}{30pt}}\qquad
  \subfloat[Your plot]{
    \begin{tikzpicture}
      \draw[gray, very thin] (0,0) grid (3,3);
      \draw (0,0) node[below left] {0} -- ++(3,0) node [right] {x};
      \draw (0,0) -- ++(0,3) node [above] {y};
      \draw (0,0) -- (1,2) -- (3,3);
    \end{tikzpicture}
  }
  \caption{Image next to plot}
\end{figure}
\end{document}

looks like this:

subfig example

2

For this kind of thing, a simple tabular can do the trick. If you want your figures to have separate captions you can use the \captionof command from the caption package to insert them. I've used the array package to format the table cells so that the content is centred vertically. Here's an example:

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage[demo]{graphicx}
\usepackage{array}
\usepackage{caption}
\usepackage{pgfplots}
\pgfplotsset{width=3in}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\begin{document}
\begin{tabular}{M{2in}M{3in}}
\includegraphics{picture} & 
\begin{tikzpicture}[]
\begin{axis}
\addplot    {x^2 * sin(x)};
\end{axis}
\end{tikzpicture}\\
\captionof{figure}{A picture} & \captionof{figure}{A plot}
\end{tabular}
\end{document}

output of code

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.