111

In LaTeX2e,how can I sum two values and assign them to other variable?

I want to compute something like:

var=\textwidth - 1cm 

And if both were constants:

var=1+1
lockstep
  • 250,273
mjsr
  • 3,425
  • 2
    Please explain your intention. \textwidth is also constant. Or do you mean number vs. dimension? – Marco Daniel Sep 30 '11 at 17:05
  • 1
    Depending on where you want to use it, there might be different options (pure TeX, TikZ/PGF, Lua, etc.) – raphink Sep 30 '11 at 17:07
  • @Marco my intention is use the result. – mjsr Sep 30 '11 at 17:07
  • 4
    Why do you use the tag plain-tex together with latex3? – Leo Liu Sep 30 '11 at 17:08
  • There is a difference between the length 1cm and the constant 1, the latter being anything from text, to a number, to a counter. – Werner Sep 30 '11 at 17:11
  • @LeoLiu because I don't know how that can be solved. Could be a latex expression or a plain tex expression or both. – mjsr Sep 30 '11 at 17:14
  • 2
    @voodoomsr: The current version of LaTeX is LaTeX2e, not LaTeX3. LaTeX3 is in development and only a few experimental functions are available. Since Plain TeX and LaTeX are totally different TeX formats, you can't use them together. If you are using LaTeX, you don't need to use any tag for this, and don't use latex3 tag if you are not using it. – Leo Liu Sep 30 '11 at 17:27
  • 1
    @LeoLiu thanks Leo I wasn't clear that tex and latex can't be mixed, so I'm going to retag the question – mjsr Sep 30 '11 at 17:32

9 Answers9

118

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}

Lines of different lengths using calc package

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}

Regular arithmetic using fp package

Werner
  • 603,163
81

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
Leo Liu
  • 77,365
39

Since LuaTeX is available, forget all that complicated stuff and do something like:

\directlua{
a = 0
a = a + 1

tex.print(a)
}
Keks Dose
  • 30,892
  • 3
    How would one adapt this approach to deal with length variables, rather than scalars? – Mico Mar 14 '17 at 06:49
26

In LaTeX, if you just want to subtract one known length (say, 1cm) from another (say, \textwidth) to obtain a new length variable, you can do so using the \newlength, \setlength, and \addtolength instructions, as in the following example:

\newlength\mylength
\setlength\mylength\textwidth
\addtolength\mylength{-1cm} %% note the minus sign
Mico
  • 506,678
  • Good and simple solution, but I prefer to wrap the command names in curly brackets to make it clearer. – erickrf Jan 13 '13 at 13:16
  • @erickrf: Thanks for this feedback. In the cod I provided, using or omitting the curly braces around the macros are entirely equivalent. Feel free to use whichever method you feel provides more clarity (or, equivalently, less ambiguity). :-) – Mico Jan 13 '13 at 14:36
14

With a fairly recent TeX distribution

\newdimen\len
\len=\dimexpr\textwidth-1cm\relax

\newcount\cnt
\cnt=\numexpr1+1\relax

It's not quite clear what's the framework you're interested in, though.

egreg
  • 1,121,712
13

This is a bit overkill for the particular examples that you mention, but since this works for more complicated expressions I tend to use it:

\documentclass{article}
\usepackage{pgf}

\begin{document}
\pgfmathsetmacro{\var}{\textwidth - 1cm}

The value of var is \var
\end{document}

Note that with pgfmathsetmacro the result is a decimal without units. If you are only interested in lengths, then you can use a similar macro \pgfmathsetlength.


If you want to minimize what gets loaded and still use pgfmath, then see Is it possible to load pgfmath without loading the full pgf package?

Peter Grill
  • 223,288
10

I often use the tikz package for its wide range of capabilities; consider the following:

\documentclass[varwidth]{standalone}

\usepackage{tikz}
\usetikzlibrary{math}

\begin{document}

textwidth = \the\textwidth 

\tikzmath{
    \resultone = \textwidth - 1cm;
}

textwidth - 1cm = \resultone


\tikzmath{
    \resulttwo = 1 + 1;  % default type is float
    %
    integer \resultthree;  % declare a variable as an integer
    \resultthree = 1 + 1;
}

1 + 1 as float  = \resulttwo\\
1 + 1 as integer = \resultthree

\end{document}

produces the following:

textwidth = 345.0pt textwidth - 1cm = 316.54726 1 + 1 as float = 2.0 1 + 1 as integer = 2

More information can be found in the pgf/tikz manual in the chapter for the Math Library.

jeschwar
  • 233
  • +1, and welcome to TeX.SE! The value of \resultone appears to be a scalar, not a length variable. Can you modify the code so that \resultone contains a length variable? – Mico Oct 16 '18 at 16:25
7

this helped me:

\documentclass{article}
\usepackage{xfp}
\begin{document}

$12.5+13.8 = \fpeval{12.5+13.8} $

\end{document}
Vadim
  • 551
2

There is the adjcalc package which is a sub-package of adjustbox used to abstract the different calculation backends (eTeX, calc, pgfmath) to a common interface. Which backend is used can be selected by package options.

\usepackage{adjcalc} % or {adjustbox}
\newdimen\lengthmacro
\adjsetlength{\lengthmacro}{\textwidth-1cm}
Martin Scharrer
  • 262,582