5

I'm trying to add two latex variables in a Tikz environment in a beamer presentation, but it's not working for me. Here is my example where I get: i+j=4+17 instead of i+j=21:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}

\setbeamertemplate{footline}[frame number]{}
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}{}
\setbeamertemplate{frametitle}[default][center]

\begin{document}

\title{Some Title}
\author{greatest author out there}
\frame{\titlepage}
\begin{frame}
\frametitle{Adding two integers}
\begin{tikzpicture}
\def \i {4}
\def \j {17}
\draw (0,0) node{\tiny{i+j = \i+\j}};
\end{tikzpicture}
\end{frame}

\end{document}
8

Just use \the\numexpr\i+\j. This works regardless of whether or not you have loaded TikZ. (And use math mode and \scriptstyle instead of \tiny.)

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}

\setbeamertemplate{footline}[frame number]{}
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}{}
\setbeamertemplate{frametitle}[default][center]

\begin{document}

\title{Some Title}
\author{greatest author out there}
\frame{\titlepage}
\begin{frame}
\frametitle{Adding two integers}
\begin{tikzpicture}
\def \i {4}
\def \j {17}
\draw (0,0) node{$\scriptstyle i+j =\the\numexpr\i+\j$};
\end{tikzpicture}
\end{frame}
\end{document}
4

Try this way:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}

\setbeamertemplate{footline}[frame number]{}
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}{}
\setbeamertemplate{frametitle}[default][center]

\begin{document}

\title{Some Title}
\author{greatest author out there}
\frame{\titlepage}
\begin{frame}
\frametitle{Adding two integers}
\begin{tikzpicture}
\def \i {4}
\def \j {17}
\pgfmathtruncatemacro\ij{\i+\j}
\draw (0,0) node{\tiny i+j = \ij};
\end{tikzpicture}
\end{frame}

\end{document}

You have to tell tikz/pgf to calculate the result first ... This is what \pgfmathtruncatemacro does here.

PS: See also \pgfmathsetmacro

4

tikz does not know that you want the sum, you need to tell it with \pgfmathparse

\tiny{...} is wrong. It should be {\tiny ...}, but better use the tikz key font=\tiny

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}

\setbeamertemplate{footline}[frame number]{}
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}{}
\setbeamertemplate{frametitle}[default][center]

\begin{document}

\title{Some Title}
\author{greatest author out there}
\frame{\titlepage}
\begin{frame}
\frametitle{Adding two integers}
\begin{tikzpicture}
\def \i {4}
\def \j {17}

\draw (0,0) node[font=\tiny] {i+j =\pgfmathparse{\i+\j}\pgfmathresult };
\end{tikzpicture}
\end{frame}

\end{document}
1

Welcome to TeX.SX!

You can use \pgfmathparse and related expressions. Please see section 94 (Mathematical Expressions) of the current TikZ documentation for more details and examples.

\documentclass{beamer}
\usepackage{tikz}
\begin{document}

\begin{frame}
\frametitle{Adding two integers}
\begin{tikzpicture}
\def \i {4}
\def \j {17}
\draw (0,0) node{\tiny{i+j = \pgfmathparse{\i+\j}\pgfmathresult}};
\end{tikzpicture}
\end{frame}

\end{document}
0

I recommend using \pgfmathsetmacro; and (very flexible) node option [scale=5], [scale=.5], etc instead of \tiny.

enter image description here

\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\begin{frame}
\frametitle{Adding two integers}
\begin{tikzpicture}
\def\i{4}
\def\j{17}
\pgfmathsetmacro{\s}{int(\i+\j)}
\path (0,0) node[scale=5]{$i+j=\s$};
\end{tikzpicture}
\end{frame}
\end{document}

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.