TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

share|improve this question
Please explain your intention. \textwidth is also constant. Or do you mean number vs. dimension? – Marco Daniel Sep 30 '11 at 17:05
Depending on where you want to use it, there might be different options (pure TeX, TikZ/PGF, Lua, etc.) – ℝaphink Sep 30 '11 at 17:07
Why do you use the tag plain-tex together with latex3? – Leo Liu Sep 30 '11 at 17:08
@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
@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
up vote 63 down vote accepted

In regular LaTeX, the calc package allows for easy manipulation of length arithmetic:

\usepackage{calc}% http://ctan.org/pkg/calc



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):

\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
The following arithmetic is easy:
  \item \FPeval{\result}{clip(5+6)}%
  \item \FPeval{\result}{round(2+3/5*pi,5)}%

Regular arithmetic using fp package

share|improve this answer
I think it is simpler to use \FPeval{result}{round(2+3/5*pi,5)} – Leo Liu Sep 30 '11 at 17:33
@LeoLiu: Thanks; sure is easier to read/follow. – Werner Sep 30 '11 at 18:59

In classical Knuth TeX,

\advance\len by -1cm

\advance\cnt by 1




LaTeX with calc,




LaTeX2e with expl3 (LaTeX3),

\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}
share|improve this answer
I'm using LaTeX with calc, and want to use in second argument of \setcounter other counter value, but got an error. Is there a way to calculate value based on other counter? – Artem Pelenitsyn Jul 24 '14 at 19:55
@ArtemPelenitsyn: Use \value{countername}. – Leo Liu Jul 25 '14 at 2:38
thank you! It works! – Artem Pelenitsyn Jul 25 '14 at 5:02

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

a = 0
a = a + 1

share|improve this answer

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:

\addtolength\mylength{-1cm} %% note the minus sign
share|improve this answer
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

With a fairly recent TeX distribution



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

share|improve this answer

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:


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

The value of var is \var

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?

share|improve this answer

Your Answer


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.