{calculations} is about calculations in (La)TeX documents. A popular package is calc although may other also exist (fp, pgf, and LaTeX3 packages).

learn more… | top users | synonyms

54
votes
3answers
14k views

Get width of a given text as length

Is there a TeX command that returns the width of a given text as length value, so that I can use the result directly as a length argument of another command? I mean, I would like to have a command ...
52
votes
11answers
2k views

How can I define a mathematical function as a LaTeX macro?

I am attempting to create a usable function within LaTeX such that its inputs are transformed mathematically. However, I cannot find any documentation that can perform such mathematical function ...
38
votes
3answers
3k views

Can LaTeX perform calculation like Excel formula table?

I wonder if this feature is available in TeX system? For example, given a formula within a table, it can apply the formula and display data after the calculation.
37
votes
5answers
2k views

Perform matrix operations (addition, product, transpose, etc.) in LaTeX?

I know the calc package can perform infix-notation arithmetic in LaTeX... but I want more! I'd like to perform (not necessarily infix-notation) linear-algebra operations such as scalar ...
34
votes
5answers
11k views

How can I automatically calculate sums in a LaTeX table?

Sometimes I would like LaTeX to automatically calculate sums (or other simple arithmetic) for me, eg. in a table. I would like to ensure that when I update the table, I don't accidentally forget to ...
33
votes
7answers
6k views

How do I calculate n modulo 3 in LaTeX?

I don't want to display the modulo symbol, I want to programmatically calculate n modulo 3 and display the result.
30
votes
6answers
15k views

How can I sum two values and store the result in other variable?

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
29
votes
6answers
3k views

How to use \pageref{foo} as a number?

What I want to do is to show a "progress bar" in the header to show how far you are in the document, by making a rule with width pagenumber divided by total pages times the paper width. This is what ...
25
votes
2answers
2k views

Date calculations

I'm building a LaTeX document that helps me to formulate quotations for my customers. Since I decided to offer monthly recurring payments I'd like to have payment dates calculated automatically ...
24
votes
4answers
619 views

duodecimal page number

I would like to have page numbers in my document written in base 12. Here's an MWE \documentclass[12pt]{article} \usepackage[paperheight=1.8in, textheight=0.8in]{geometry} ...
24
votes
2answers
33k views

How to fit a large figure to page [duplicate]

Possible Duplicate: Best figure size adjustment when dealing with different image sizes Given: a large figure, whose height:width ratio is unknown. Todo: Include this fig in LaTeX, such ...
23
votes
4answers
1k views

How to add, subtract, multiply, and divide in plain TeX?

I find many cases where I need to calculate many numbers, using adding, subtracting, multiplying, and dividing, but it would save much time if TeX could simply compute these for me. This is a sample ...
23
votes
4answers
572 views

Day of the year

I'm using the day of the year a lot in my documents and I wonder if I can have LaTex calculating it for me? Example: 2013178 where 2013 is (surprise) the current year (which I know could be achieved ...
23
votes
1answer
2k views

References for \dimexpr \numexpr

I'm not really sure whether this is the right place to ask this question, but here I go.... Over the holiday I've been taking time to read through a lot of the Q&A here. I've been running across ...
22
votes
4answers
1k views

Get derivative of a function

I want to write a macro, which takes a mathematical function as argument and returns a plot using pgfplots of the function and of its derivative. I guess that this isn't possible using just LaTeX; ...
22
votes
5answers
4k views

Floating point calculations in LaTeX?

I am looking for a method to do minimal floating point / integer calculations in LaTeX, not for the purpose of package writing, but for the production of actual text. Here is an example to ...
22
votes
2answers
6k views

Problems with TikZ calculations

When can I use functions and do calculations in TikZ? How do I make this work: \draw (0,0) arc(0:90:sqrt(15)); %not ok why is this working: \draw (0,0) arc(0:asin(1):5); %ok with ...
21
votes
2answers
397 views

Sum of finite series using LaTeX 3

Is it possible to calculate the value of a finite series, say, using LaTeX 3?
20
votes
3answers
2k views

Extract first & last characters of macro argument?

In LaTeX, how do I extract/isolate/determine the first and last characters of a macro argument? Specifically, in the case I'm dealing with, the argument happens to be a base-10 integer (call it N). ...
20
votes
1answer
254 views

Using \fontdimen as an array to store data

Reading through pi.tex, I discovered the trick of using \fontdimen as an array of dimensions (which is equivalent to storing integers between ±(2^31-1)sp). Namely, \font\x=cmr10 at 1sp % or whatever ...
18
votes
2answers
881 views

Automated age calculation

How can I calculate my age at compile-time in LaTeX? Something like: I'm \myage{day}{month}{year} years old. would be most awesome.
17
votes
6answers
2k views

Checking if a number is a multiple of 4

Using etoolbox, ifthen, or another form of conditional checking, how can I check if a value is a multiple of 4? E.g. if the number is 4, 8, 12, 16, etc., then it returns "true".
17
votes
4answers
774 views

Calculate prices

I want to calculate prices in a LaTex file. This means I want to add two numbers with exactly 2 decimal places and I want to multiply these values by an integer. An other thing is the output format. ...
17
votes
3answers
753 views

square root of number in counter

I would like to get the square root of a number which I have stored into a counter. I have been using the calc package for summation and multiplication operations, but I cannot find anything like ...
17
votes
3answers
2k views

Doing maths with distance values in LaTeX source code

I have a sty file with the command \newcommand{\foo}[1]{\noindent\kern#1} but I'd actually like to have something like \newcommand{\foo}[1]{\noindent\kern{#1 *0.5}} meaning that I'd like to have ...
17
votes
6answers
846 views

Rounding a number to its hundred

Does anybody know how I could have LaTeX round a number like 2,386 so that I finally get only written 2,300? I tried with siunitx and its option [round-mode=places,round-precision=-2] but it didn't ...
17
votes
4answers
404 views

How can I convert units in LaTeX environment?

Example: \usepackage{siunitx} \newcommand{\temperature}[1]{\SI{#1}{\celsius}} \temperature{100} output: 100°C I would like something like: \newcommand{\temperature}[1]{\SI{#1+273}{\kelvin} ...
17
votes
2answers
5k views

Is plotting exponential graphs a known source of bugs in TikZ?

I want to plot $y = 2^x$ over a small domain. Unfortunately, even though tikz (v 2.1) seems to be able to calculate 2^{negative numbers} it is not plotting them correctly. My minimal example is: ...
17
votes
2answers
686 views

Handling different time zones

I'm looking for a way to handle date/times with different time zones. For example in svn-multi and filemod a date and time with a timezone is returned. I like to be able to compare times in different ...
17
votes
1answer
519 views

Setting TikZ curve control points and preserve area of closed curve

I would like to reproduce the shown periodic table, with more correct areas. http://www.meta-synthesis.com/webbook/35_pt/relative_abundance.jpg I would appreciate any ideas to how this can be ...
16
votes
7answers
1k views

Calculating right angle triangle side inside LaTeX

I'd like compute the width of a screen from it's diagonal and its aspect ratio inside a LaTeX document. The formula is easy to get from the Pythagorean theorem, but needs square roots. I currently ...
16
votes
3answers
1k views

PGF math engine imprecise?

The command \pgfmathparse{asin(1/\x)} yields for each \x in the range of 125 to 142 the same value, whereas \pgfmathparse{1/\x} yields the much more precise values for each \x. Is the ArcSin function ...
16
votes
3answers
779 views

Using command to compute sum?

I must admit this sounds like an easy question. I'm creating a grading model for a homework assignment, and I'm adding the maximum amount of points to each question. Is there a way to have LaTeX ...
16
votes
2answers
4k views

How can I compute the distance between two coordinates in TikZ?

Given two points (defined, for instance using nodes), I want to compute the distance between them. Is there some build in functionality in tikz to do this? If not, how can it be done using the ...
15
votes
6answers
2k views

Sum of (example: 0+1+2+3 = 6 , 0+1+2+3+4+5+6+7 = 28 and so on)

Can anyone help please? I need to know how to make the sum of value S(n) = 0+1+2+3+4...+(n-1)+n (example: 0+1+2+3 = 6 , 0+1+2+3+4+5+6+7 = 28 and so on) on Latex
15
votes
5answers
625 views

command for floor

I need something that corresponds to floor() to use it in a macro (I don't mean the notation, I really mean the function that rounds downwards). Something like this: \floor{34.75} produces 34 ...
15
votes
3answers
2k views

Why doesn't \settoheight of \parbox work?

Consider the following code: \documentclass[a4paper]{letter} \usepackage{calc} \usepackage{printlen} \newsavebox{\foobox} \newlength{\fooboxheight} \savebox{\foobox}{ ...
15
votes
2answers
1k views

Golden ratio typography in TeX

Chris Pearson makes a number of claims in his article Secret Symphony: The Ultimate Guide to Readable Web Typography, which I find fascinating, and I would like to know: are the formulas he proposes ...
14
votes
3answers
1k views

How to set a counter to double the value of another counter?

The title says it. Lets have to counters: \newcounter{myCount} \newcounter{anotherCount} I set myCount to some value for example 10 \setcounter{myCount}{10} How do I set counter anotherCount to ...
14
votes
1answer
2k views

Determine height and depth of letters relative to the font size

I would like to scale graphical elements (like images, tikz and tikz-timing diagrams) relative to the font size, so that they have the same height as an normal uppercase letter (i.e. X or M; I noticed ...
13
votes
5answers
458 views

Transform big number into scientific format

I want to convert my big integer number into a shorter form; for example \convert{123456789} = 1.23 x 10^9 Is there any package which allows me to do so?
13
votes
4answers
864 views

Dividing dimensions to get a count

Is there a way to divide two dimens and get a count? For example, if I have a box that doesn't fit on a single page, and I want to know the number of pages I'd need for the box (i.e., \heightofbox / ...
13
votes
2answers
747 views

How to compute the difference between two time points (e.g., 11:30 am and 01:20 pm => 110 min)?

Is there a simple way to compute the difference between two times (same day), so 11:30 am and 01:20 pm should lead to 110 min (or 1h 50min), for example? I saw the datenumber package but it only seems ...
13
votes
5answers
464 views

Calculations in TikZ for iterative procedure

In many \foreach iterative procedures in TikZ I would like to relate the generic node called n\x -- where x is a natural number -- to another node the name of which is obtained by applying basic ...
13
votes
2answers
484 views

Is there a better way to do intersections and calculations with relative coordinates in TikZ?

I've been doing many TikZ diagrams lately and every so often I encounter a situation where I am forced to use some temporary coordinate in a \path. In the following short example, the problem is ...
13
votes
5answers
592 views

Autocalculate result in math-mode

Are there any available programs/software/etc. that allow you to autocalculate and then autocomplete TeX equations? It would be great if for example, I were doing really long messy equations like 120 ...
13
votes
1answer
186 views

Using calc-like (calc Package) Commands within LaTeX3/expl3

In my pursue to switch as much as possible from LaTeX2e code to LaTeX3/expl3, I got stuck with the usage of some of the calc package facilities. I've read of the l3calc package, but it seems it is ...
12
votes
3answers
6k views

How to do simple calculation in Latex?

Consider the following macro: \newcommand{\sxfigure}[4]{ \includegraphics[width=#1*#3 cm, height=#2*#3 cm]{#4} } #1 and #2 is some value to determine the width and height of the figure, #3 is to ...
12
votes
3answers
339 views

Arithmetic in arguments to the geometry package

I'd like to use the geometry package to auto-crop the output of my LaTeX (so that it looks bigger on-screen as I'm editing). I have found a way to remove the margins completely and brutally: ...
12
votes
4answers
4k views

Why is this let expression not working in TikZ (calculating a midpoint)?

I have a rectangle in TikZ and the top left and bottom right corners are called (topleft) and (bottomright) respectively. I wanted to have a label midway down the left hand side of the rectangle, so I ...