Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to reproduce the design represented in the following picture with Beamer an Pgfplots.

enter image description here

Namely, I want the size of plotting area to be always the same size and have the same position whatever the axis label and graduations are and the text bellow it is.

Here is my current attempt:

\documentclass{beamer}

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{table/row sep=\\}
\begin{document}

\newlength{\imglarg}
\newlength{\imghaut}
\setlength{\imglarg}{0.9\textwidth}
\setlength{\imghaut}{0.6\textheight}

% ====
\begin{frame}{Frame 1}
  \begin{figure}
    \begin{tikzpicture}[trim axis left, trim axis right]
      \pgfplotstableread{
        1 4.3\\
        2 4.2\\
        3 3.1\\
        4 2.5\\
      }\tablea
      \begin{axis}[%
        /pgfplots/table/header=false,
        width=\imglarg,
        height=\imghaut,
        axis on top]
        \addplot table[x index=0, y index=1]{\tablea};
      \end{axis}
    \end{tikzpicture}
  \end{figure}
  \vskip0pt plus 1filll%
  \begin{itemize}
  \item You must defeat Sheng Long to stand a chance
  \end{itemize}
\end{frame}

% ====
\begin{frame}{Frame 2}
  \begin{figure}
    \begin{tikzpicture}[trim axis left, trim axis right]
      \pgfplotstableread{%
        1000000 3500000000\\
        2000000 3800000000\\
        3000000 4000000000\\
        4000000 3000000000\\
      }\tableb
      \begin{axis}[%
        /pgfplots/table/header=false,
        width=\imglarg,
        height=\imghaut,
        axis on top]
        \addplot table[x index=0, y index=1]{\tableb};
      \end{axis}
    \end{tikzpicture}
  \end{figure}
  \vskip0pt plus 1filll%
  \begin{itemize}
  \item Xyzzy !
  \item Nothing happens.
  \end{itemize}
\end{frame}

\end{document}

It only works if there are no different random graduations taking some space (for instance if both graph are plotted using \tableb). I ended here after substantial trial and error so i am far from sure that every piece of code there is useful.

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

The problem you encounter is due to the bounding box.

The bounding box is made so large that it encompasses all content within each tikzpicture.

The workaround is to set the same bounding box for each of the pictures. This will make them locate the same place in the presentation (as long as the code is consistent).

Ok, so it can be solved by resetting the bounding box of the tikzpicture and fixing it in each of the frames.

This can be done as follows:

\pgfresetboundingbox % clear the bounding box
\path (current axis.south west) rectangle (current axis.north east);

However, this will align along the axis lines. And any axis descriptions are outside the bounding box. If you have a very tightly packed slide you need to include those in the bounding box. To do this across two plots which does not have the same description you have to set the enlarged explicitly.
This might not always be the preferred way. A solution is to use the calc library and shift the position:

\pgfresetboundingbox
\path ($(current axis.south west)-(.5cm,.5cm)$) rectangle 
    ($(current axis.north east)+(.5cm,.5cm)$);

The above will take the axis lines and make the bounding box 0.5cm larger in each of the 4 directions.

A complete example is:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{small,height=5.25cm}
\begin{document}
\pagestyle{empty}
\setlength{\fboxsep}{0pt}%

\def\MyAlign{%
    \pgfresetboundingbox
    \path (current axis.south west) rectangle (current axis.north east);
}
\def\MyAlignCm#1{%
    \pgfresetboundingbox
    \path ($(current axis.south west)-(#1cm,#1cm)$) rectangle 
    ($(current axis.north east)+(#1cm,#1cm)$);
}

Following two frames are aligned:

\fbox{%
\begin{minipage}{.6\linewidth}
  \begin{tikzpicture}%
    \begin{axis}[
      xticklabel=\relax,
      yticklabel=\relax,
      domain=-2:2
      ]
      \addplot {x^2};
      \addplot {x^3};
      \addplot {x^4};
    \end{axis}
    \MyAlign
  \end{tikzpicture}%
\end{minipage}
}%

\vspace{.5cm}

\fbox{%
\begin{minipage}{.6\linewidth}
  \begin{tikzpicture}%
    \begin{axis}[
      title=A title,
      xlabel={$x$},
      ylabel={$y$},
      legend style={at={(0.5,0.97)},
          anchor=north,legend columns=-1},
      domain=-2:2
      ]
      \addplot {x^2};
      \addplot {x^3};
      \addplot {x^4};
      \legend{$x^2$,$x^3$,$x^4$}
    \end{axis}
    \MyAlign
  \end{tikzpicture}%
\end{minipage}
}%

\vspace{.5cm}
Following two frames are aligned:

\fbox{%
\begin{minipage}{.6\linewidth}
  \begin{tikzpicture}%
    \begin{axis}[
      xticklabel=\relax,
      yticklabel=\relax,
      domain=-2:2
      ]
      \addplot {x^2};
      \addplot {x^3};
      \addplot {x^4};
    \end{axis}
    \MyAlignCm{.5}
  \end{tikzpicture}%
\end{minipage}
}%

\vspace{.5cm}

\fbox{%
\begin{minipage}{.6\linewidth}
  \begin{tikzpicture}%
    \begin{axis}[
      title=A title,
      xlabel={$x$},
      ylabel={$y$},
      legend style={at={(0.5,0.97)},
          anchor=north,legend columns=-1},
      domain=-2:2
      ]
      \addplot {x^2};
      \addplot {x^3};
      \addplot {x^4};
      \legend{$x^2$,$x^3$,$x^4$}
    \end{axis}
    \MyAlignCm{.5}
  \end{tikzpicture}%
\end{minipage}
}%

\end{document}

This will yield the following output:

Notice the alignment

I will leave it as an exercise to use \phantom to aquire the same result without resetting the bounding box.

share|improve this answer
    
Changing the bounding box is a problem when using the externalization library : the axis labels and ticks non longer appears. The MyAlignCm version you proposed allows to sidestep this problem but one has to measure' the space needed by the labels and adapt the command in consequence making it less powerful. I do not know the \phantom` command but it might solve this issue? –  Alfred M. Jun 27 '12 at 15:16
    
If you use externalize, everything becomes more complicated. The easiest thing is to use \MyAlignCm until it fits. Phantom could prove useful though. Look the command up and see what it does. Then the hint is to make complete identical plots with every label, title, xtick etc. enclosed in a phantom. –  zeroth Jun 27 '12 at 15:25
    
This might be cumbersome as well. Redefining \MyAlign with four arguments makes it quite tractable. Thanks for you help! –  Alfred M. Jun 27 '12 at 15:34
1  
If you want to modify the bounding box and use the external lib, you should resort to \begin{tikzpicture}[trim axis left,trim axis right,baseline]. This will introduce suitable negative hspaces without really truncating the BB. The baseline option causes vertical alignment at the baseline of surrounding text - perhaps that helps as well. –  Christian Feuersänger Jun 27 '12 at 19:13
    
I was not able to reproduce the result obtained by altering bounding box with the method you suggested. Maybe some other paramameters I am using messed it up but as my presentation is soon due I cannot investigate right now. –  Alfred M. Jul 3 '12 at 17:54
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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