#### How to perform calculations in plain TeX

Hi,

how can I perform basic arithmetic calculations in plain TeX? For instance,
say I want some command "\fixedplusvarskip" that's supposed to set up a vskip
of at least some fixed value F, but ideally some size F+V, I would like to be
able to make a command as:

\newcommand{\fixedplusvarskip}[2]{\vskip{#1+#2}pt plus0pt minus#2pt}

but how do I get the calculation to work?

Similarly, say I want a skip that's only half of some indicated font size for
some text:

\newcommand{\textplushalfskip}[2]{\fontsize{#1pt}{{#1*1.2}pt}#2\vskip{#1/2}pt}

How do I get these multiplication/division evaluated?

Thanks,

- Mike "Pomax" Kamermans
nihongoresources.com

 0
PmI (9)
12/15/2009 10:37:40 AM
comp.text.tex 37968 articles. 1 followers.

11 Replies
868 Views

Similar Articles

[PageSpeed] 28
 "PmI" <pmi@int13h.com> writes:
>how can I perform basic arithmetic calculations in plain TeX? For instance,
>say I want some command "\fixedplusvarskip" that's supposed to set up a vskip
>of at least some fixed value F, but ideally some size F+V, I would like to be
>able to make a command as:
>
>\newcommand{\fixedplusvarskip}[2]{\vskip{#1+#2}pt plus0pt minus#2pt}
>
>but how do I get the calculation to work?
>
>Similarly, say I want a skip that's only half of some indicated font size for
>some text:
>
>
>\newcommand{\textplushalfskip}[2]{\fontsize{#1pt}{{#1*1.2}pt}#2\vskip{#1/2}pt}
>
>How do I get these multiplication/division evaluated?

either write out the expression in terms of \advance, \divide,
\multiply using scratch registers, or look up etex \dimexpr --
requires a rather formalised sort of expression, but it's pretty
powerful.

there's also a \numexpr.
--
Robin Fairbairns, Cambridge

 0
rf10
12/15/2009 11:28:49 AM
PmI <pmi@int13h.com> wrote:

> Hi,
>
> how can I perform basic arithmetic calculations in plain TeX? For instance,
> say I want some command "\fixedplusvarskip" that's supposed to set up a vskip
> of at least some fixed value F, but ideally some size F+V, I would like to be
> able to make a command as:
>
> \newcommand{\fixedplusvarskip}[2]{\vskip{#1+#2}pt plus0pt minus#2pt}
>
> but how do I get the calculation to work?

Is it plain TeX or LaTeX? Assuming a fairly recent LaTeX,

\newcommand{\fixedplusvarskip}[2]{%
\vskip\numexpr(#1+#2)pt plus 0pt minus #2pt}

I'd rather put the unit in the argument (perhaps you may want

\newcommand{\fixedplusvarskip}[2]{%
\vskip\dimexpr(#1+#2) plus 0pt minus #2}

and \fixedplusvarskip{3ex}{1cm} would work without doing the math.

> Similarly, say I want a skip that's only half of some indicated font size for
> some text:
>
>
> \newcommand{\textplushalfskip}[2]{\fontsize{#1pt}{{#1*1.2}pt}#2\vskip{#1/2}pt}
>
> How do I get these multiplication/division evaluated?

\newcommand{\textplushalfskip}[2]{%
\fontsize{#1}{\dimexpr(#1*12/10)}\selectfont #2\vskip\dimexpr(#1/2)}

I'd rather use an environment for this one:

\newenvironment{plushalfskip}[1]
{\par\def\keeparg{#1}%
\fontsize{#1}{\dimexpr(#1*12/10)}\selectfont}
{\vskip\dimexpr(\keeparg/2)}

Ciao
Enrico

 0
Enrico
12/15/2009 11:37:58 AM
 Enrico Gregorio <gregorio@math.unipd.it> writes:
>PmI <pmi@int13h.com> wrote:
>> how can I perform basic arithmetic calculations in plain TeX? For instance,
>> say I want some command "\fixedplusvarskip" that's supposed to set up a vskip
>> of at least some fixed value F, but ideally some size F+V, I would like to be
>> able to make a command as:
>>
>> \newcommand{\fixedplusvarskip}[2]{\vskip{#1+#2}pt plus0pt minus#2pt}
>>
>> but how do I get the calculation to work?
>
>Is it plain TeX or LaTeX? Assuming a fairly recent LaTeX,
>
>\newcommand{\fixedplusvarskip}[2]{%
>  \vskip\numexpr(#1+#2)pt plus 0pt minus #2pt}
>
>I'd rather put the unit in the argument (perhaps you may want
>
>\newcommand{\fixedplusvarskip}[2]{%
>  \vskip\dimexpr(#1+#2) plus 0pt minus #2}
>
>and \fixedplusvarskip{3ex}{1cm} would work without doing the math.
>
>
>> Similarly, say I want a skip that's only half of some indicated font size for
>> some text:
>>
>>
>> \newcommand{\textplushalfskip}[2]{\fontsize{#1pt}{{#1*1.2}pt}#2\vskip{#1/2}pt}
>>
>> How do I get these multiplication/division evaluated?
>
>\newcommand{\textplushalfskip}[2]{%
>  \fontsize{#1}{\dimexpr(#1*12/10)}\selectfont #2\vskip\dimexpr(#1/2)}
>
>I'd rather use an environment for this one:
>
>\newenvironment{plushalfskip}[1]
>  {\par\def\keeparg{#1}%
>   \fontsize{#1}{\dimexpr(#1*12/10)}\selectfont}
>  {\vskip\dimexpr(\keeparg/2)}

last argument should be {\par\vskip\dimexpr(\keeparg/2)}
(as otherwise the baselineskip of the text of the enclosing paragraph
will be applied to the special text.
--
Robin Fairbairns, Cambridge

 0
rf10
12/15/2009 12:02:23 PM
PmI a �crit :
> Hi,
>
> how can I perform basic arithmetic calculations in plain TeX? For instance,
> say I want some command "\fixedplusvarskip" that's supposed to set up a vskip
> of at least some fixed value F, but ideally some size F+V, I would like to be
> able to make a command as:
>
> \newcommand{\fixedplusvarskip}[2]{\vskip{#1+#2}pt plus0pt minus#2pt}
>
> but how do I get the calculation to work?
>
> Similarly, say I want a skip that's only half of some indicated font size for
> some text:
>
>
> \newcommand{\textplushalfskip}[2]{\fontsize{#1pt}{{#1*1.2}pt}#2\vskip{#1/2}pt}
>
> How do I get these multiplication/division evaluated?
>
> Thanks,
>
> - Mike "Pomax" Kamermans
> nihongoresources.com
For your purpose, I sugest you use the setspace package.
I am wrong ?

 0
GL
12/15/2009 1:44:12 PM
Robin Fairbairns <rf10@cl.cam.ac.uk> wrote:

> >I'd rather use an environment for this one:
> >
> >\newenvironment{plushalfskip}[1]
> >  {\par\def\keeparg{#1}%
> >   \fontsize{#1}{\dimexpr(#1*12/10)}\selectfont}
> >  {\vskip\dimexpr(\keeparg/2)}
>
> last argument should be {\par\vskip\dimexpr(\keeparg/2)}
> (as otherwise the baselineskip of the text of the enclosing paragraph
> will be applied to the special text.

A \vskip found in horizontal mode issues a \par automatically, and the
current paragraph has been started in the environment. :) There's a \par
in the opening code.

Not that I would really define and use environments such as this one. :)

Ciao
Enrico

 0
Enrico
12/15/2009 1:51:21 PM
Enrico Gregorio <gregorio@math.unipd.it> wrote:

> PmI <pmi@int13h.com> wrote:
>
>   \vskip\numexpr(#1+#2)pt plus 0pt minus #2pt}
>
> I'd rather put the unit in the argument (perhaps you may want

Disadvantage: \numexpr doesn't work with decimal numbers.

In this case:
\vskip#1\relax
\vskip#2 minus #2\relax

Or have I missed something?

Yours sincerely
Heiko <oberdiek@uni-freiburg.de>

 0
Heiko
12/15/2009 2:15:30 PM
<<Disadvantage: \numexpr doesn't work with decimal numbers.
In this case:
\vskip#1\relax
\vskip#2 minus #2\relax
Or have I missed something?>>

My only concern is that I may have given too specific examples - the fontsize
command, for instance, also relies on some arithmetics being performed
(following the standard {<dim> }{1,2<dim>}), and there are other instances
where one might want to do simple arithmetics.

While the two step vskip solution works, for instance, it's a solution of
only the example code, rather than a general solution to doing arithmetics in
TeX (that is not to say I don't appreciate the vskip solution, it just
doesn't let me generalise the principle to arbitrary numbers).

- Mike

 0
PmI
12/15/2009 2:33:53 PM
<<For your purpose, I sugest you use the setspace package.
I am wrong ?>>

The examples were purely that - the question remains how to do basic
arithmetic in TeX, not how to set specific spaces or how to set specific font
sizes. Can this be done? (without integer rounding messing up the result,
although if integer math is the only option, then integer math it has to be).

- Mike

 0
PmI
12/15/2009 2:35:03 PM
On 15 Dez., 15:35, "PmI" <p...@int13h.com> wrote:
> <<For your purpose, I sugest you use the setspace package.
> I am wrong ?>>
>
> The examples were purely that - the question remains how to do basic
> arithmetic in TeX, not how to set specific spaces or how to set specific font
> sizes. Can this be done?

In that case I suggest the fp package. As far as I know it can be used
with plain TeX too.

Alexander

 0
Alexander
12/15/2009 3:03:45 PM
Heiko Oberdiek <oberdiek@uni-freiburg.de> writes:

> Enrico Gregorio <gregorio@math.unipd.it> wrote:
>
>> PmI <pmi@int13h.com> wrote:
>>
>>   \vskip\numexpr(#1+#2)pt plus 0pt minus #2pt}
>>
>> I'd rather put the unit in the argument (perhaps you may want
>
> Disadvantage: \numexpr doesn't work with decimal numbers.
>
> In this case:
>   \vskip#1\relax
>   \vskip#2 minus #2\relax
>
> Or have I missed something?

Different effects on \lastskip?  May sound academical, but has

--
David Kastrup
UKTUG FAQ: <URL:http://www.tex.ac.uk/cgi-bin/texfaq2html>

 0
David
12/15/2009 3:27:24 PM
David Kastrup <dak@gnu.org> wrote:

> Heiko Oberdiek <oberdiek@uni-freiburg.de> writes:
>
> > Enrico Gregorio <gregorio@math.unipd.it> wrote:
> >
> >> PmI <pmi@int13h.com> wrote:
> >>
> >>   \vskip\numexpr(#1+#2)pt plus 0pt minus #2pt}
> >>
> >> I'd rather put the unit in the argument (perhaps you may want
> >> ex instead of pt):
> >
> > Disadvantage: \numexpr doesn't work with decimal numbers.
> >
> > In this case:
> >   \vskip#1\relax
> >   \vskip#2 minus #2\relax
> >
> > Or have I missed something?
>
> Different effects on \lastskip?

Yes, thanks, I knew I had forgotten something, ...

Yours sincerely
Heiko <oberdiek@uni-freiburg.de>

 0
Heiko
12/15/2009 4:41:12 PM
Similar artilces about - How to perform calculations in plain TeX:

Specifying "target field" by calculation (in "Set Field")
In order to make a Find script as generic as possible, I want to be able to pass it (as parameters) a layout name, a field name, and a field value. So the script will go to the layout (using P1), set the field (using P2 and P3), and do the find. Trouble is, I can't see how to use P2 in the Set Field script step - it seems only to allow me to specify an actual field name (rather than a field name calculated from the parameter). Any suggestions? (FM9, PC). Carla. "Carla" <carla_sloan@yahoo.co.uk> schreef in bericht news:5eb843e6-9286-4051-a506-f348eebb6cba@e4g2000hsg....

Calculator style sound generator?
I'm looking for something portable that will enable me to program it to generate tones; a waveform generator if you like with an output jack. Even better, a programmable calculator with an output jack. Does anything like this which is cheap exist? Thankyou! On Thu, 5 Feb 2004 22:40:40 -0000, "Funky" <hello@hello.com> wrote: >I'm looking for something portable that will enable me to program it to >generate tones; a waveform generator if you like with an output jack. Even >better, a programmable calculator with an output jack. Does anything like >this whic...

Strange performance
My OpenGL program runs with almost identical speed no mater what resolution I set (the funniest thing is that it runs slighty faster (+1 FPS) on 1600x1200 than in 640x480). It was tested on GeForce2 and Riva (I don't remember which version). The application uses a variety of things that could cause this (lighting, collision detection, sorting, alpha blending and lots of non opengl stuff). I use depth buffer, no extensions, no draw/read pixel. Where could be the problem? -- Archibald Sounds like you've got your VSYNC enabled in your video driver, which means that no matter h...

How to calculate the every grid area?
If I have a set of irregular grid lines like x = 0 0.1047 0.2095 0.3142 0.4189 0.1272 0.2040 0.2787 0.3517 0.4237 0.2354 0.2871 0.3380 0.3883 0.4381 0.3302 0.3635 0.3965 0.4293 0.4620 0.4156 0.4355 0.4554 0.4753 0.4950 0.4943 0.5050 0.5157 0.5263 0.5370 0.5683 0.5731 0.5779 0.5827 0.5875 0.6396 0.6412 0.6428 0.6443 0.6459 0.7095 0.7101 0.7106 0.7112 0.7118 y = 0 0 0 0 0 0.1272 0.1197 0.1123 ...

Oracle Performance Management and Tuning
As a user of Oracle and Linux technology, we are pleased to announce the availability of empower! v2.1 for Oracle. This short note introduces 6 key features that make empower! different from the Oracle desktop tuning tools you're used to. The purpose of empower! is to make all the Oracle tuning facilities and related operations available to you from a single Windows based desktop application. In the past, you might have required a database server login to run essential utilities like STATSPACK and Tkprof - with empower! you no longer need that server login because you can run e...

CTAN update: UK TeX FAQ
i have just installed a new version of the uk tex faq. this version is not a "finished product" (a bug was discovered in the html files that tex distributions use, so *something* had to be done in short order -- so i have released a development snapshot). i believe that it's self consistent, however. Users may view the package's catalogue entry at http://www.tex.ac.uk/tex-archive/help/Catalogue/uk-tex-faq.html or browse the package's directory at http://www.tex.ac.uk/tex-archive/help/uk-tex-faq/ or, of course, they can browse the Web version at http://www.tex.ac....

Matching Video Card to CPU performance
I have an AthlonXP 1800+ CPU on a Leadtek 7350KDA m/board - 4x AGP, 266 (DDR) FSB and 512MB DDR SDRAM. I want to replace the current GeForce4 MX440 video card with something faster for gaming. But at what point am I getting too much video card for the CPU/motherboard combination? Would a Radeon 9600XT be wasted on such a combo? Is there a more approporiate 4xAGP card? Any sites that provide references for CPU to Video Card performance matching? Thanks arawata "Andrew" <arawata@ozemail.com.au> wrote in message news:5640b017.0402101836.7957bf96@posting.google.com... > I hav...

Performance profiling
Hi there, I've written a time library to profile functions using JavaScript without being too intrusive. It works in all the major browsers, IE6+, Firefox, Opera and Safari. You can hook any function and anonymous function using: time.func(myFunction); myFunction(var1, var2, var3); or document.getElementById('link').onclick = time.func(function() { // do something return value; }); Examples of profiling can be seen here: http://remysharp.com/2007/04/20/performance-profiling-javascript/ Hopefully this is of some help - I found it really useful when trying to profile wh...

copy constructors hurting performance
I have a String class (I know I am re-inventing the wheel, yes I have heard of boost, and of QString). My copy constructor does a deep (strcpy) of the char *_buffer member. I have a member function func(const String &param). When an actual String is passed as the param this is nice and efficient. I have a constructor which takes a const char* as an argument. And performs a deep copy of the const char *buffer. The issue occurrs when I make the following call: func("this is the param"); The String(const char *buf) constructor is called, making a deep copy of the data. B...

to calculate bitsize of a byte
I am reading "Joel on Software" these days, and am in stuck with the question of "how to calculate bitsize of a byte" which is listed as one of the basic interview questions in Joel's book. Anyone could give some ideas?I am expecting your reply. David. david ullua wrote: > I am reading "Joel on Software" these days, and am in stuck with the > question of "how to calculate bitsize of a byte" which is listed as one > of the basic interview questions in Joel's book. Anyone could give some > ideas?I am expecting your reply. The terms &...

AWK command for calculating average
I want to calculate average (response times) from a log file. This is what I got so far: $gawk '/timer\.asp/ {gsub(/\&/,"\n",$5);print \$5}' ex050117.log |tail -n 7|sort T[SS01]=3D140,ServiceS01%2C1retries T[SS01]=3D140,ServiceS01%2C1retries T[SS11]=3D2203,ServiceS11%2C20retries T[clientside]=3D125,Client%20onload T[clientside]=3D62,Client%20onload T[webserver]=3D2250,Webserver%20m.servicekald T[webserver]=3D578,Webserver%20m.servicekald Now, each group is before the '=3D' and the response time right after. Any ideas on how I calculate and group the times? Thank...

ATI FireGL V3100 Video card performance issues
I am looking for some assistance with increasing the performance and/or provide GPU priority to ustation, using an ATI FireGL V3100 Video card. The Video card specs are as follows; ATI FireGL V3100 400MHZ 128MB Bios version: 113-A33414-101 Driver version: 8.293.0.0 The PC is an HP workstation XW4200, P4 HT 3.2GHZ, with 1GB RAM, running Windows XP Pro SP2. I notice that when rotating an drawing, with the display mode set to Constant (and graphics Accel turned on), the image rotation is choppy. This is not an overly large drawing. Any recommendations? The RAM usage is abo...

Improve query performance on large table
Hi Folks, I have a very large table containing 170 million rows of coordinats: CREATE TABLE "DB2ADMIN"."COORDINATE" ( "FID" INTEGER NOT NULL , "AID" INTEGER NOT NULL , "X" REAL NOT NULL , "Y" REAL NOT NULL , "Z" REAL NOT NULL ) IN "USERSPACE1" ; ALTER TABLE "DB2ADMIN"."COORDINATE" APPEND ON; CREATE INDEX "DB2ADMIN"."IDX_COORDINATE1" ON "DB2ADMIN&qu...

Calculate dBFS?
Hi guys, I'm a bit confused by something (well a lot of things really..) Assuming I want to make a compressor with a threshold of say -10dBFS. The user sets the threshold value to be -15. I get my input sample and convert it into the Logarithmic domain using 20*log10(abs(sample)) How can I now compare the two? Do you have to create a sample at maximum value, convert it to dB then take away the value of the actual input sample to find the difference and get the -dBFS value..?? I'm looking into a simple compressor and I understand that if I get a dBFS value of the input (say -10dBF...

Scilab * TeX
Hi, all, a friend of mine wants to insert simple TeX commands in legends and texts of scilab plots. For example, a^2, \sqrt{b}, and such. If it is not possible (certainly this is the case), I leave here the suggestion to add this feature to Scilab. If anybody has any hints about how one would insert such mathematical formulas into a scilab plot, please let us know. best, Ricardo. -- Ricardo Fabbri, Cybernetic Vision Research Group, USP, Brazil. TEL: +55 016 273.9882 CEL: +55 016 9121.9046 ICQ: 208974212 "With pain and suddenness is how Real Offsprings are born....

performance
Hi, I have a 4647KB LabView Application running on a laptop (498MHz, 384MB of RAM, 768 MB of Virtual Memory). This application would run and then crashes in the middle and give me a blue screen, and the computer would restart itself. But smaller application about one fourth of the size (1000KB or so) works fine. Does this mean I need more RAM or something? I pull up the task manager and performance is about 30% or less, unless it needs to open a spread sheet, then performance goes to 100%, but then it goes back down to like 5%-30%. Thanks I don't think the application size is a big is...

Solar Winds Orion Network Performance Monitor
Currently, I'm running the 7.2.0 - April, 2004 release of Orion Network Performance Monitor. For the most part, I'm very excited about the application. It's easy to maintain and does a great job of gathering utilization data. However, I am seeing some issues with false ICMP packet loss, as it is displayed on the website, to many devices on my network. It seems to be an application issue. Anyone seen this before? This is a multipart message in MIME format. --=_alternative 007B26EB88256EB4_= Content-Type: text/plain; charset="US-ASCII" That's not FALSE informati...

Hi, I have setup a HADR between two servers in different locations. When I compare the application response time in standard environment and in HADR environment, I find the latter is more than twice slower than the former. I have tried to tune some parameters like DB2_HADR_BUF_SIZE, log buffer size, dbheap, but not much improvement. Can anyone give me some suggestions of how to improve HADR performance? We use sync mode of HADR. Thanks. "ebusiness" <wendy_xie@hotmail.com> wrote in message news:1185461168.310732.176250@j4g2000prf.googlegroups.com... > H...

SVE performance
Now we are using about 100 share varible and still all in office LAN, but takes 3 or 5 mins to initial the share varible link, any setting can improve it? &nbsp; The 'initial' means the PSP LED turning from red to green. They have already been deployed from server side. Have you tried using the Shared Variable Manager like I mentioned in my last post?&nbsp; This would be a good debugging step to take LabVIEW out of the equation. -Justin D Yes, in the share variable manager, it will take 5 mins to see the quality turn from 'connecting, disconnect' to 'good' af...

GSMC
Hi, I've updated GTK Smith Chart Calculator (gsmc) now to version v1.1 that can be downloaded from www.qsl.net/ik5nax/gsmc-1.0.tar.gz Major update are ps/eps printout, autotune, save/load project To contact me ik5nax@amsat.org. Lapo Pieri ########################################################################## # Send submissions for comp.os.linux.announce to: cola@stump.algebra.com # # PLEASE remember a short description of the software and the LOCATION. # # This group is archived at http://stump.algebra.com/~cola/ # #########################...

US-CO: EVERGREEN-Oracle DBA (RAC, Performance, UNIX)
************************************************************** JobCircle.com is the largest regional job board in the Mid-Atlantic region, with tens of thousands of job offerings in PA, NJ, DE, MD, NY, and Washington D.C. To learn more, visit http://www.jobcircle.com?source=ng ************************************************************** Job Title: Oracle DBA (RAC, Performance, UNIX) Job Location: CO: EVERGREEN Pay Rate: Open Job Length: 4-6 months (contract) Start Date: 2011-01-07 Company Name: Genesis10 Contact: Terry Huegel Phone: email o...

Getting Files By Mail: Server Performance #40
FTPMail, Agora, etc. statistics for Tue 12 Aug 2003, posted Wed,= 13 Aug 2003 05:00:29 GMT Less than 10 minutes ftpmail@ftp.sunet.se getweb@healthnet.org page@grabpage.org webgate@vancouver-webpages.com text@pagegetter.com web@pagegetter.com www4mail@ftp.uni-stuttgart.de 10 minutes-1 hour ftpmail@dna.affrc.go.jp agora@dna.affrc.go.jp www4mail@wm.ictp.trieste.it 1-4 hours None 4-10 hours binky@junoaccmail.org More than 10 hours None Response within 4 hours in at least 5 out of 7 recent tests agora@dna.affrc.go.jp ftpmail@dna.affrc.go.jp ftpmail@ftp.sunet.se page@grabpage.org text...

10 reasons to love Ubuntu, Performance, performance performance
Top 10 reason why I love Ubuntu ,----[ Quote ] | 1. *Performance, performance performance* I'm running it on a 512MB | RAM/2.7Ghz Celeron processor, and my Windows XP was unbearably | slow. Not only does Ubuntu function smoothly, my internet is faster | and I enjoy much higher download speeds (sometimes even up to 5x | faster) | | 2. *The Update Manager* I'm big with having the latest | builds/releases/updates on programs, and I can get all the updates I | need (or don't need) through one convenient little program. Makes | life a lot easier. | | 3. *GNOME* GNOME looks great, espe...