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 am trying to put some TikZ pictures into a Beamer slide to enhance readability. The position of the TikZ pictures on the slide will depend on how much text comes before. Since there will be many TikZ pictures, and each will have to be adjusted by the same amount if I change the text at the top of the slide, I tried to make a macro to avoid having to adjust the position of each TikZ picture manually each time I change the text..

Consider the following code:

\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\pgfmathsetmacro{\yoff}{5}    
\newcommand{\print}[1]{\pgfmathparse{#1}\pgfmathresult}
\begin{frame}
  \frametitle{Testing}
  \begin{itemize}
  \item Test picture
  \begin{picture}(0, 0)
     \put(0,\print{8+\yoff}){
         \tikz \draw[red,thick] (0,0) ellipse (5pt and 5pt);
      }
  \end{picture}
  \end{itemize}
\end{frame}
\end{document}

Here I get the following error:

! Missing number, treated as zero.

  \begingroup 

l.17 \end{frame}

However, putting the macro within the \frametitle of the slide works as expected (without errors):

\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\pgfmathsetmacro{\yoff}{5}
\newcommand{\print}[1]{\pgfmathparse{#1}\pgfmathresult}
\begin{frame}
  \frametitle{Testing \print{8+\yoff}}
  \begin{itemize}
  \item Test picture
    \begin{picture}(0, 0)
      \put(0,\yoff){
          \tikz \draw[red,thick] (0,0) ellipse (5pt and 5pt);
       }
     \end{picture}
  \end{itemize}
\end{frame}
\end{document}
share|improve this question

1 Answer 1

up vote 8 down vote accepted

\put needs something that expands to a number:

\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\def\yoff{5}    
\newcommand{\print}[1]{\pgfmathparse{#1}\pgfmathresult}
\begin{frame}
  \frametitle{Testing}
  \begin{itemize}
  \item Test picture
  \begin{picture}(0, 0)
     \put(0,\numexpr8+\yoff\relax){
         \tikz \draw[red,thick] (0,0) ellipse (5pt and 5pt);
      }
  \end{picture}
  \end{itemize}
\end{frame}

\end{document}

If you want to use decimal arithmetic with non integral values you need to add pt use dimen arithmetic and then remove the pt

 \put(0,\csname strip@pt\endcsname\dimexpr8pt+\yoff pt\relax){

would work for example.

share|improve this answer
    
Thank you! If I replace \def\yoff{5} with \pgfmathsetmacro{\yoff}{5} I get the error message: ! Illegal unit of measure (pt inserted) –  Håkon Hægland Jan 15 '13 at 10:25
1  
yes that's why I changed it:-) \pgfmathsetmacro{\yoff}{5} sets yoff to 5.0 not 5 and that isn't a number according to primitive TeX number syntax (which has to be an integer literal) –  David Carlisle Jan 15 '13 at 10:30
    
Ok, I see:) But \put accepts decimal numbers like \put(1.3,2.5).. What if I define \def\yoff{5.5}.. How can I deal with this situation? –  Håkon Hægland Jan 15 '13 at 10:48
1  
updated answer for that case –  David Carlisle Jan 15 '13 at 10:56
1  
yes that \print command does not just work via expansion it defines internal macros as part of its definition so it can not be used inline in \put. It is exactly the same as you can go \def\foo{3pt} \dimen@=\foo but you can not go \dimen@=\def\foo{3pt}\foo you can not have the definition inline in teh length setting, it has yo be first, or not there at al \dimen@=3pt –  David Carlisle Jan 15 '13 at 18:15

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.