# Tagged Questions

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

16k 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 ...
6k 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 illustrate....
11k 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 \usetikzlibrary{...
7k 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: \...
6k 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 ...
5k 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 ...
25k 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 \...
2k views

### Computing value using pgfmath for use in coordinates?

Why can't I use the outcome of using pgfmath in a TikZ coordinate? Here is a (minimal) example that does not work: \documentclass[11pt]{article} \usepackage{tikz} \begin{document} \begin{tikzpicture}...
2k views

### \widthof within tikzpicture

In this minimal example \documentclass{scrartcl} \usepackage{calc} \usepackage{tikz} \newlength{\TestLength} \begin{document} \setlength{\TestLength}{\widthof{\tikz \node {bla};}} \...
1k views

### TikZ: Make node height span several others

How can I make the Z node span the total height of the ABCD column? My current code is: \documentclass{article} \usepackage{tikz} \usetikzlibrary{calc,positioning} \begin{document} \begin{...
4k 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 ...
8k 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.
14k 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 ...
2k 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 / \...
3k 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 ...
3k 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 ...
5k 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 ...
3k views

### How to find the ratio of a length command (e.g., \textwidth) to a reference value (e.g., 6cm) ?

How to find the ratio of a length command (e.g., \textwidth) to a reference value (e.g., 6cm) ? Actually I want to use the ratio for the \scalebox argument, e.g., \scalebox{<ratio>}. \...
31k 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
5k views

### How to produce a list of prime numbers in LaTeX

I would like to write a LaTeX script that produces all the prime numbers between the numbers n and m, where n < m. How can I do this? I feel it should not be that hard, but I cannot seem to program ...
3k 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 ...
406 views

### How to fit ellipse into another object - using intersections and calc library

I would like to ask, how to place ellipse into any object, so that the length of the main arm is defined by line1 (point A, B) in my code: calculate the coordinates of the ellipse midpoint rotation ...
2k views

### Automatic calculation of error in pgfplots

When taking in data through a file (such as table[x index=0, y index=1, y error index=2]{plots/mydata.table};) is it possible to automatically calculate the error bars (instead of putting them in ...
3k views

### Coordinates A, B: compute |B-A| and angle between +x and (B-A)

This question has two parts: why is the angle computed always 0 (should be 45) what is a straightforward way to compute distance between coordinates (there is How can I compute the distance between ...
1k views

### Compute the x distance between two nodes

Is there a way to define a macro which would evaluate to the x part of the distance between two nodes? I know there are the point registers \p and the correspoding \x commands, but I find them rather ...
2k views

### Shrinking text to the width of a node within a tikzpicture

I would like to be able to scale text to the width of a TikZ node. Using \widthof within tikzpicture as a starting point, I've arrived at somewhat of a solution, except it has incorrect spacing, and ...
5k views

### Multiply the value by decimal number in latex?

I used the following construct quite often: \includegraphics[width=\textwidth/2]{pic.png} But when I try to adjust the size a small bit, like this: \includegraphics[width=\textwidth*0.45]{pic.png} ...
5k 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.
71k 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 that: ...
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 ...
1k 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. ...
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 ...
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}{ \parbox[t]{\textwidth}{...
2k 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" ...
680 views

### How to ensure document total page numbers is a multiple of four?

I'm producing a document (book class) that will ultimately be produced using offset lithography. It's therefore useful to ensure the total number of pages is a multiple of four by adding as many blank ...
142 views

### Handling numbers and calculations [duplicate]

I have to deal with a lot of numbers in my latex documents. So far, I have been using a rudimentary \newcommand{\VarOne}{1000} to define \VarOne and then use it as it is using \VarOne{} in the ...
2k 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; ...
721 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 ...
2k 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 ...
546 views

### How to define a macro that uses image width and height to define a variable for later use

Ok, the title might be a bit cryptic and my question is a bit lengthy, so please bear with me. What I want to do is: I have two images with different aspect ratios that I want to place next to each ...
11k views

### Create a table with calculations of binomial and poisson distribution

Well, I was looking for some tables on the internet about the binomial and poisson distribution (not the cumulative ones) in LaTeX format. But I've only found the code of the cumulative distributions, ...
665 views

### Sum of finite series using LaTeX 3

Is it possible to calculate the value of a finite series, say, using LaTeX 3?
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".
2k views

### Measuring height of fixed-width text box

Suppose some text is put inside a fixed width box. The text will then continue in vertical direction. How can you measure the total height of that text? Original text: text text text text text ...
3k 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). ...
2k 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 ...
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
661 views

### Factorial (or, even better, binomial coefficient) function

Is there a function in l3fp for calculating factorials or, even better, binomial coefficients directly?
1k 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 ...
494 views

### How to pass in a ratio in fraction form (as opposed to decimal form) to table column definition?

My scenario is to pass in a ratio (in fractional form as opposed to decimal form) to each table column definition. The following code cannot be compiled. \documentclass{article} \usepackage{pgf} \...