5

I am trying to place a tikz picture to the right of an \item in a beamer frame and so far the closest I have managed to come up with is this:

\documentclass{beamer}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{wrapfig}
\usetikzlibrary{shapes.arrows,positioning}
\pgfmathdeclarefunction{gauss}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{document}
    \frame{
        \frametitle{Lorem ipsum}
        \begin{block}{Lorem ipsum}
        \begin{itemize}
            \item Lorem ipsum dolor sit amet, consectetur adipiscing elit.  
            \item \begin{minipage}[t]{1\linewidth}\raggedright
        \begin{wrapfigure}{r}{0.5\textwidth}
        \resizebox{!}{0.25\textheight}{
        \begin{tikzpicture}
            \begin{axis}[every axis plot post/.append style={
            mark=none,domain=-2:3,samples=50,smooth}, 
            axis x line*=bottom, 
            axis y line*=left, 
            enlargelimits=upper] 
            \addplot[color=red!75!black] {gauss(1,0.5)};
            \addplot[color=red!75!black] {gauss(1,0.75)};
            \addplot[color=red!75!black] {gauss(1,1)};
            \addplot[color=red!75!black] {gauss(1,2)};
            \end{axis}
        \end{tikzpicture}
        }
        \end{wrapfigure}
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis venenatis
        id lorem a placerat. Integer nec tincidunt risus.       
        \end{minipage}
        \end{itemize}
        \end{block}
    }
\end{document}

enter image description here

However, as can be seen alignments are way off. The text begins underneath the item marker and the tikz figure is really strangely put with regards to vertical alignment. How can this be made to look more ``correct''?


EDIT: I have updated the example to make it clear that I want the first item to span the entire width and the second one to be placed to the left of the figure.

2
+50

A tikzpicture can be aligned relative to the baseline using baseline. It can also be scaled using scale. One advantage of the latter is that fonts will not be scaled so there will not be a mismatch between font sizes in different figures or figures and texts. (Of course, this isn't always an advantage!)

I would put the text in a simple minipage and then align and scale the tikzpicture using its native keys. For example:

\documentclass{beamer}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfmathdeclarefunction{gauss}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{document}
  \begin{frame}{Lorem ipsum}
    \begin{block}{Lorem ipsum}
      \begin{itemize}
        \item Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        \item \begin{minipage}[t]{.5\linewidth}\raggedright
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis venenatis
          id lorem a placerat. Integer nec tincidunt risus.
        \end{minipage}%
        \begin{tikzpicture}[scale=.5, baseline=(c)]
          \begin{axis}[every axis plot post/.append style={
              mark=none,domain=-2:3,samples=50,smooth},
            axis x line*=bottom,
            axis y line*=left,
            enlargelimits=upper]
            \addplot[color=red!75!black] {gauss(1,0.5)};
            \addplot[color=red!75!black] {gauss(1,0.75)};
            \addplot[color=red!75!black] {gauss(1,1)};
            \addplot[color=red!75!black] {gauss(1,2)};
          \end{axis}
          \path [fill=red] (current bounding box.north) ++(0,-\baselineskip) coordinate (c);
        \end{tikzpicture}
      \end{itemize}
    \end{block}
  \end{frame}
\end{document}

baseline can either take a dimension (e.g. 1ex) or a coordinate (e.g. c or current bounding box.north or whatever). See the TikZ manual for details. (Page 125 in the v3 manual.)

minipage and tikzpicture

2

Here's a solution without wrapfigure and resizebox. I also put the text within it's own minipage. Might be an acceptable answer as long as you don't want to wrap alot of text around the figure.

Since you want the text to the left of the figure, I put that first.

\documentclass{beamer}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{float}
\usepackage{wrapfig}
\usetikzlibrary{shapes.arrows,positioning}
\pgfmathdeclarefunction{gauss}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{document}
    \frame{
        \frametitle{Lorem ipsum}
        \begin{block}{Lorem ipsum}
        \begin{itemize}
            \item Lorem 
            \item {\begin{minipage}[t]{.45\textwidth}Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis venenatis
        id lorem a placerat. Integer nec tincidunt risus.\end{minipage}\begin{minipage}[h]{.45\textwidth}
        \begin{flushright}
        \begin{tikzpicture}
            \begin{axis}[every axis plot post/.append style={
            mark=none,domain=-2:3,samples=50,smooth}, 
            axis x line*=bottom, 
            axis y line*=left, 
            width=\textwidth,
            enlargelimits=upper] 
            \addplot[color=red!75!black] {gauss(1,0.5)};
            \addplot[color=red!75!black] {gauss(1,0.75)};
            \addplot[color=red!75!black] {gauss(1,1)};
            \addplot[color=red!75!black] {gauss(1,2)};
            \end{axis}
        \end{tikzpicture}
        \end{flushright}
        \end{minipage}
        }
        \end{itemize}
        \end{block}
    }
\end{document} 

Result: Result


Edit: Second suggestion.

Second suggestion

\documentclass{beamer}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{float}
\usepackage{wrapfig}
\usetikzlibrary{shapes.arrows,positioning}
\pgfmathdeclarefunction{gauss}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{document}
    \frame{
        \frametitle{Lorem ipsum}
        \begin{block}{Lorem ipsum}
        \begin{minipage}[b]{.49\textwidth}\begin{itemize}
            \item Lorem 
            \item Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis venenatis
        id lorem a placerat. Integer nec tincidunt risus.
        \end{itemize}\end{minipage}\begin{minipage}[b]{.45\textwidth}
        \begin{flushright}
        \begin{tikzpicture}[baseline]
            \begin{axis}[every axis plot post/.append style={
            mark=none,domain=-2:3,samples=50,smooth}, 
            axis x line*=bottom, 
            axis y line*=left, 
            width=\textwidth,
            enlargelimits=upper] 
            \addplot[color=red!75!black] {gauss(1,0.5)};
            \addplot[color=red!75!black] {gauss(1,0.75)};
            \addplot[color=red!75!black] {gauss(1,1)};
            \addplot[color=red!75!black] {gauss(1,2)};
            \end{axis}
        \end{tikzpicture}
        \end{flushright}
        \end{minipage}
        \end{block}
    }
\end{document}
  • Ah, I see. Change the first minipage position to [t] and the second minipage position to [h]. It's not a perfect alignment though. Would it be okay to have the whole itemize list in one minipage and the figure in another? I'll edit and add that suggestion as well. – Grimler May 29 '15 at 12:10
  • I have updated the example to make it clear that I need the first item to span the entire width – jonalv May 29 '15 at 13:10
1

Following code shows an example with adjustbox.

Second item line is divided in two fragments, the first one is an adjustbox box mimicking a minipage and the second one another box adjusted at top (valign=t) with previous one.

\documentclass{beamer}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{adjustbox}
\usetikzlibrary{shapes.arrows,positioning}
\pgfmathdeclarefunction{gauss}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{document}

    \frame{
        \frametitle{Lorem ipsum}
        \begin{block}{Lorem ipsum}
        \begin{itemize}
            \item Lorem ipsum dolor sit amet, consectetur adipiscing elit.
       \item \adjustbox{minipage=[t]{.45\textwidth}}{  
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis venenatis
        id lorem a placerat. Integer nec tincidunt risus.       
        }
        \hfill
        \adjustbox{valign=t, max width=.45\textwidth}
        {
        \begin{tikzpicture}
            \begin{axis}[every axis plot post/.append style={
            mark=none,domain=-2:3,samples=50,smooth}, 
            axis x line*=bottom, 
            axis y line*=left, 
            enlargelimits=upper] 
            \addplot[color=red!75!black] {gauss(1,0.5)};
            \addplot[color=red!75!black] {gauss(1,0.75)};
            \addplot[color=red!75!black] {gauss(1,1)};
            \addplot[color=red!75!black] {gauss(1,2)};
            \end{axis}
        \end{tikzpicture}}
        \item Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        \end{itemize}
        \end{block}
    }
\end{document}

enter image description here

1

Here I dispense with wrapfigure. Instead, I put the second item text (and only text) into a top-aligned minipage of width .45\linewidth. Then, I use the \belowbaseline[-\ht\strutbox]{...} construct of the stackengine package to place the tikz figure into. This will make it so that it properly aligns with the top of the prior minipage. I also added an \hfil between the items to provide some horizontal separation.

\documentclass{beamer}
\usepackage{tikz}
\usepackage{pgfplots,stackengine}
\usetikzlibrary{shapes.arrows,positioning}
\pgfmathdeclarefunction{gauss}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{document}
    \frame{
        \frametitle{Lorem ipsum}
        \begin{block}{Lorem ipsum}
        \begin{itemize}
            \item Lorem ipsum dolor sit amet, consectetur adipiscing elit.  
            \item \begin{minipage}[t]{.45\linewidth}\raggedright
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis venenatis
        id lorem a placerat. Integer nec tincidunt risus.       
        \end{minipage}\hfil
        \belowbaseline[-\ht\strutbox]{\resizebox{!}{0.25\textheight}{
        \begin{tikzpicture}
            \begin{axis}[every axis plot post/.append style={
            mark=none,domain=-2:3,samples=50,smooth}, 
            axis x line*=bottom, 
            axis y line*=left, 
            enlargelimits=upper] 
            \addplot[color=red!75!black] {gauss(1,0.5)};
            \addplot[color=red!75!black] {gauss(1,0.75)};
            \addplot[color=red!75!black] {gauss(1,1)};
            \addplot[color=red!75!black] {gauss(1,2)};
            \end{axis}
        \end{tikzpicture}
        }}
        \end{itemize}
        \end{block}
    }
\end{document}

enter image description here

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.