Tag Info

70

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

56

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

51

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

48

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

44

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

44

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

39

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

35

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

28

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

27

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

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.

26

You are using latex to process a plain TeX document and this, of course, triggers the error message. You have two options: Process the document as it is using (pdf)tex. Convert your document to a latex document. Here's an illustration of the second option: \documentclass{article} \begin{document} \newcount\n \newcount\np \newcount\npp \newcount\m ...

26

Yes, you can, and pretty easily too. \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\computesum}{mmm} {% pass control to an internal function \svend_compute_sum:nnn { #1 } { #2 } { #3 } } % a variable for storing the partial sums \fp_new:N \l_svend_partial_sum_fp \cs_new_protected:Npn \svend_compute_sum:nnn #1 #2 #3 { ...

25

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.

24

Here is a flat latex2e implementation. \documentclass{article} \usepackage{amsmath} \newcount{\numerator} \newcount{\denominator} \newcount{\gcd} % compute \gcd and returns reduced \numerator and \denominator \newcommand{\reduce}[2]% #1=numerator, #2=denominator {\numerator=#1\relax \denominator=#2\relax \loop \ifnum\numerator<\denominator ...

24

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

24

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

23

An implementation in LaTeX3: \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \cs_new:Npn \fibo #1 { \fibo_recurrence:nnnn{0}{1}{0}{#1} } \cs_new:Npn \fibo_recurrence:nnnn #1 #2 #3 #4 { \int_compare:nTF { #1 = #4 } { #3 } { #3 ~ \fibo_recurrence:ffnn { \int_eval:n {#1+1} } { \int_eval:n {#2+#3} } { #2 } { #4 } } ...

23

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

23

If you are not bound to expl3 (in which case you “just” need to implement the algorithm): \documentclass{scrartcl} \usepackage{xintgcd,xintfrac} \newcommand*\reducedfrac[2] {\begingroup \edef\gcd{\xintGCD{#1}{#2}}% \frac{\xintNum{\xintDiv{#1}{\gcd}}}{\xintNum{\xintDiv{#2}{\gcd}}}% \endgroup} \begin{document} \[ \frac{278922}{74088} = ...

22

An option using Lua+LaTeX. Made small improvement. Made a Lua function to be called as a LaTeX command, with the numerator and denominator passed as arguments, instead of hardcoding the values in as before. The command is \simplify{a}{b}: \documentclass{article} \usepackage{luacode} \usepackage{amsmath} %------------------------ \begin{luacode} ...

22

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

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{amsmath} % for '\ensuremath' macro \usepackage{luacode} % for 'luacode' environment % ...

21

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

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

21

Since LuaTeX is available, forget all that complicated stuff and do something like: \directlua{ a = 0 a = a + 1 tex.print(a) }

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