{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

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 ...
47
votes
3answers
11k 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 ...
37
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.
36
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
10k 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
5k 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.
27
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 ...
26
votes
6answers
12k 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
24
votes
4answers
587 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
1k 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 ...
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
4answers
834 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 ...
22
votes
4answers
548 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 ...
21
votes
2answers
5k 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 ...
20
votes
2answers
27k 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 ...
20
votes
1answer
228 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 ...
19
votes
4answers
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 ...
19
votes
1answer
1k 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 ...
18
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). ...
18
votes
2answers
775 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
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
4answers
335 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
4k 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
658 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
483 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
3answers
609 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 ...
16
votes
4answers
658 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. ...
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
2answers
3k 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
5answers
591 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
3answers
636 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 ...
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
6answers
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 ...
13
votes
5answers
385 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
606 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
3answers
944 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 ...
13
votes
5answers
522 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
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
1answer
170 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
4k 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
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 ...
12
votes
3answers
342 views

Formatting numbers while printing

Is there a way to format number strings while printing? Suppose I have the following declaration: \newcommand{\MyNumberA}{40} \newcommand{\MyNumberB}{60} The following in the text: ...
12
votes
2answers
444 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 ...
12
votes
2answers
915 views

Where is the left-hand margin?

I'm working on the TikZ-from-TeX-SX package, specifically developing the code for putting equation numbering at places within a tikzpicture environment. To do this, I need to know where the text ...
12
votes
3answers
1k views

How can I calculate the difference of 2 counters (pageref)?

How can I calculate the difference between 2 counters (like page numbers), so that pdfLateX outputs "3" in the last line of my example? and how could I calculate the difference between the "current" ...
12
votes
1answer
837 views

How to implicitly specify column width (in a columns-like environment)?

I am using the columns environment in Beamer-documents like this: \begin{columns} \begin{column}{0.8\textwidth} %% \end{column} \begin{column}{0.2\textwidth} %% ...
11
votes
3answers
288 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: ...
11
votes
2answers
262 views

Factorial (or, even better, binomial coefficient) function

Is there a function in l3fp for calculating factorials or, even better, binomial coefficients directly?