Hot answers tagged

89

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


70

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


62

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


54

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


53

How about \includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{myfig.png} EDIT: added keepaspectratio


48

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


48

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


41

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, % \...


39

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


36

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}


32

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


31

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


30

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


30

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


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


29

\documentclass{article} % \makeatletter \def\primes#1#2{{% \def\comma{\def\comma{, }}% \count@\@ne\@tempcntb#2\relax\@curtab#1\relax \@primes}} \def\@primes{\loop\advance\count@\@ne \expandafter\ifx\csname p-\the\count@\endcsname\relax \ifnum\@tempcntb<\count@\else \ifnum\count@<\@curtab\else\comma\the\count@\fi\fi\else\repeat \@tempcnta\count@\...


28

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.


27

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


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

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


26

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


26

Here's a LuaLaTeX-based solution. The code sets up a LaTeX macro named \rsqrt, which invokes a Lua function named Rsqrt. The latter implements the simplification algorithm you've proposed -- with the following exceptions: For n=0 or n=1, the code simply returns n (without a square root symbol), and Care is taken to omit the \sqrt{n/i²} term if it's equal ...


25

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


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


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}% $\sqrt{\themycount}=\...


24

D.E. Knuth has done this himself on page 218 of The TeXbook: \newif\ifprime \newif\ifunknown % boolean variables \newcount\n \newcount\p \newcount\d \newcount\a % integer variables \def\primes#1{2,~3% assume that #1 is at least 3 \n=#1 \advance\n by-2 % n more to go \p=5 % odd primes starting with p \loop\ifnum\n>0 \printifprime\advance\p by2 \repeat} \...


23

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


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

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

The algorithm in the question is very inefficient: except of course if the original integer is a perfect square. This answer (in chronological order): an approach with macros which mimics the simplest factorization algorithm, an expandable approach using the algorithm as in OP. update very embarrassingly, the author had not understood the OP's algorithm ...



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