How can I sum two values and store the result in other variable?
In regular LaTeX, the calc
package allows for easy manipulation of length arithmetic:
\documentclass{article}
\usepackage{calc}% http://ctan.org/pkg/calc
\newlength{\mylength}
\begin{document}
\setlength{\mylength}{\textwidth}%
\noindent\rule{\mylength}{20pt}
\bigskip
\setlength{\mylength}{\textwidth-1cm}%
\noindent\rule{\mylength}{20pt}
\bigskip
\setlength{\mylength}{\textwidth-80pt+5mm-1bp}%
\noindent\rule{\mylength}{20pt}
\end{document}
The above deals with lengths. For basic arithmetic using numbers, the fp
package. Here is an example using infix notation (Reverse Polish Notation/RPN is also possible via \FPupn
):
\documentclass{article}
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
\begin{document}
The following arithmetic is easy:
\begin{itemize}
\item \FPeval{\result}{clip(5+6)}%
$5+6=\result$
\item \FPeval{\result}{round(2+3/5*pi,5)}%
$2+3/5\times\pi=\result$
\end{itemize}
\end{document}
In classical Knuth TeX,
\newdimen\len
\len=\hsize
\advance\len by -1cm
\newcount\cnt
\cnt=1
\advance\cnt by 1
eTeX,
\newdimen\len
\len=\dimexpr\hsize-1cm\relax
\newcount\cnt
\cnt=\numexpr1+1\relax
LaTeX with calc
,
\usepackage{calc}
\newlength\len
\setlength{\textwidth+1cm}
\newcounter{cnt}
\setcounter{cnt}{1+1}
LaTeX2e with expl3
(LaTeX3),
\usepackage{expl3}
\ExplSyntaxOn
\dim_new:N \l_len_dim
\dim_set:Nn \l_len_dim {\textwidth + 1cm}
\int_new:N \l_cnt_int
\int_set:Nn \l_cnt_int {1+1}
\ExplSyntaxOff
Since LuaTeX is available, forget all that complicated stuff and do something like:
\directlua{
a = 0
a = a + 1
tex.print(a)
}