# Tag Info

57

Use the calc package: \parbox{\widthof{my text}}{...}

49

There is the package spreadtab which provides spreadsheet like features. These examples are taken from the documentation: \documentclass{article} \usepackage{spreadtab} \begin{document} \begin{spreadtab}{{tabular}{rr|r}} 22 & 54 & a1+b1 \\ 43 & 65 & a2+b2 \\ 49 & 37 & a3+b3 \\ \hline a1+a2+a3 ...

42

I like to answer the question in a more general way, so that it is useful to a wider group of people. There are the following macros which allow to store the width, height (the material above the baseline) and depth (the material below the baseline) of a given content. \settowidth{\somelength}{<content>} \settodepth{\somelength}{<content>} ...

39

LaTeX is a typesetting system, and trying to use it for anything other than that will probably lead you to frustration at some point or another. Unless your table is really very simple, I think going for a spreadsheet and then exporting that to LaTeX is definitely the best way to go. Now, having said that, for a simple table you can use, as Thorsten ...

38

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 ...

38

Here is some code to manipulate matrices of any size. Currently, it can perform additions, subtractions, and multiplication (as well as fetching individual entries, and transposing a matrix, for instance). Entries are floating points that l3fp supports (16 digits of precision). % Programming-level functions: \fpm_new:N, \fpm_set:Nn, \fpm_gset:Nn, % ...

37

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), ...

34

Remarks I used the powerful LaTeX3 featureset l3fp, which is automatically loaded by xparse. Implementation \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\myMathFunction}{m} { \fp_to_decimal:n {((#1) * 5) - (#1)^2} } \ExplSyntaxOff \begin{document} \myMathFunction{2} \end{document}

30

Here is a TikZ/PGF solution. I'm not sure how it compares to the l3fp approach, but it definitely offers more flexibility than a low-level TeX approach because it works in fixed-point arithmetic, not just with integers, and by using the right PGFkeys, you can easily customise how the result should be printed (trailing zeros, scientific notation, etc.). I ...

28

In good old (Plain) TeX, i.e., without LaTeX, with the proper TeX syntax: \documentclass{article} \begin{document} \newcount\pom % temporary \newcount\kw % square \newcount\first % first \def\myMathFunction#1{\pom#1 \first\pom \kw\pom \multiply\kw by\pom \multiply\first by5 \advance\first by-\kw \the\first} \myMathFunction{2} And an example of ...

26

You can do arithmetic (with +, -, *, /, but no ^ for powers) using \numexpr expressions. The \numexpr expressions are among the e-TeX extensions to the Knuth's TeX. (e-TeX extensions: on modern installations they are activated by default, except if you use the executable named tex on the command line) However you can't use truly fractional numbers ...

26

24

You need to wrap the expression into { } to hide the second pair of ( ) from the TeX parser. Without the { } a ( will be closed by the next ) even if it belongs to another (. This means arc(0:90:sqrt(15)) will be taken as arc(0:90:sqrt(15) without the second ). This causes basically two errors, one in the expression because it misses the ) and another one in ...

23

Here are four ways for calculating the square root of a number (with varying precision). However, the result cannot be stored in a counter unless it is an integer. The calculator package \documentclass{article} \usepackage{calculator} \newcounter{mycount} \setcounter{mycount}{7} \begin{document} \SQUAREROOT{\themycount}{\solution}% ...

22

You can use PGF's calendar library to convert the current day and the first day of the current year into julian dates: \documentclass{article} \usepackage{pgfkeys, pgfcalendar} \newcount\julianA \newcount\julianB \newcommand\doy{% \pgfcalendardatetojulian{\year-\month-\day}{\julianA}% \pgfcalendardatetojulian{\year-1-1}{\julianB}% ...

22

Here's a solution using LuaLaTeX. The MWE provides a LaTeX-side macro called \MyMathFunction that interfaces with a Lua-side function called mymathfunction; the latter does the actual computations. % !TEX TS-program = lualatex \documentclass{article} \usepackage{fontspec} \usepackage{luacode} % for 'luacode' environment \usepackage{luatexbase} % for ...

21

The difficult task is generating the terms of the sequence, not computing the sum, of course; I present a macro that prints all the terms or just the sum. You can define a different starting point and another difference (defaults 0 and 1). \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\arithmeticsequence}{sO{}m} { ...

21

Within reason (i.e. for not too nonlinear functions), you can numerically differentiate your functions right within PGFPlots, using the approach f'(x)=(f(x+dx)-f(x))/dx: \documentclass{article} \usepackage{pgfplots} \begin{document} \begin{tikzpicture} \begin{axis}[no markers, legend pos=south east, legend entries={Original function, Analytical ...

21

You can also use \intcalcMod from the intcalc package: \documentclass{article} \usepackage{amsmath} \usepackage{ifthen} \usepackage{intcalc} \newcounter{mycount} \newcommand\Nmodiii[1]{% \setcounter{mycount}{0}\whiledo{\value{mycount}<#1} {$\themycount\pmod 3=\intcalcMod{\value{mycount}}{3}$\\\stepcounter{mycount}} } \begin{document} \noindent A ...

21

There are several nice answers using different packages. I'd like to note that TeX uses integer arithmetics, so it is easy to program the standard formula a-(a/b)*b, where / means integer division. Plain TeX solution: \newcount\tmpcnta \def\modulo#1#2{\tmpcnta=#1 \divide\tmpcnta by #2 \multiply\tmpcnta by #2 \multiply\tmpcnta by -1 ...

21

Solving this kind of problem is the raison d'être of the refcount package. Here's one way to use it: \documentclass[oneside]{book} \usepackage{lipsum,fancyhdr,lastpage,refcount} \pagestyle{fancy} \setrefcountdefault{-1} \lhead{\rule{\dimexpr \textwidth * \thepage/\getpagerefnumber{LastPage}}{2mm}} \begin{document} \lipsum[1-60] %Insert dummy text for ...

21

This is possible with the datenumber package \documentclass{article} \usepackage{datenumber} \begin{document} \setdatetoday \addtocounter{datenumber}{30}% \setdatebynumber{\thedatenumber}% In 30 days is \datedate \setdatetoday \addtocounter{datenumber}{60}% \setdatebynumber{\thedatenumber}% In 60 days is \datedate \setdatetoday ...

20

These are primitives which are not present in Knuth's TeX but which were added as part of the e-TeX extensions. As such, they are documented in the e-TeX manual, which is most conveniently accessed using texdoc etex.

20

PGF You might want to take a look at the mathematical capabilities of pgf. I prepared a MWE: \documentclass{standalone} \usepackage{pgf} \usepgflibrary{fpu} \pgfkeys{ /pgf/fpu = true, /pgf/number format/.cd, precision=2, fixed, fixed zerofill, use comma, 1000 sep={.} } \begin{document} \pgfmathparse{2*(1234.56+9786.45)} ...

19

The calc package allows only division by integers or by reals which are announced as \real: so \includegraphics[width=\textwidth*\real{0.45}]{pic} will do, but \includegraphics[width=0.45\textwidth]{pic} works as well, doesn't require calc and is faster.

19

As wh1t3 commented, there is a through library which even has the command circle through. Here is the example in the manual: After adding the line \usetikzlibrary{through} in the preamble, \begin{tikzpicture} \draw[help lines] (0,0) grid (3,2); \node (a) at (2,1.5) {$a$}; \node [draw] at (1,1) [circle through={(a)}] {$c$}; \end{tikzpicture} You can do ...

18


18

This can be done with the calc package \documentclass{article} \usepackage{calc} \begin{document} \newlength{\myl} \settowidth{\myl}{test text} \the\myl \end{document} \the\myl will print out the value ~37pt.

18

Use the eTeX primitive \dimexpr to allow a factor like before dimension registers: \newcommand{\foo}[1]{\noindent\kern .5\dimexpr#1\relax} Note that \dimexpr swallows the \relax and also allows for some arithmetic inside. See the etex_manual for more details.

18

\ifodd is enough to do this \documentclass[]{scrartcl} \newcount\myint \def\IsFourMultiple#1{% \myint#1\relax \ifodd\myint false \else \divide\myint by2\relax \ifodd\myint false \else true \fi \fi}% \begin{document} \IsFourMultiple{4} \IsFourMultiple{32} \IsFourMultiple{15} \end{document}

Only top voted, non community-wiki answers of a minimum length are eligible