COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### How to perform calculations in plain TeX

• Email
• Follow

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
Reply PmI (9) 12/15/2009 10:37:40 AM

See related articles to this posting

 "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

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

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

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

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

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

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

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

11 Replies
564 Views

Similar Articles

12/9/2013 9:20:24 AM
page loaded in 32796 ms. (0)

Similar Artilces:

COMPENSATION PLAN
http://www.travelwithdht.com Your efforts are rewarded with a generous compensation plan which is more generous than any existing plan. Providing many ways to easily earn money both immediate and residual income, includes revolving matrix, leadership commissions, Global bonus, president bonus. You are also offered additional incentive prizes. Such as: exotic luxurious vacations, luxury cars, villas, or cash award. This compensation plan enables you to be paid not only for your own effort, but also the efforts of many others, effectively multiplying your income potential and creating financia

Timing calculations
Hi All, I m working on DM642 DSP from TI . I would like to know as to how can i calculate the "time taken by a particular task/function". The CCS ver2.21 has the profiler in that,but,it takes hell lot of time(when using profiler,the CCS goes into Simulator MODE). Is there any other way around?. I have tried using DSP/BIOS object STS ,as specified in DSP/BIOS debugging tutorial.this particular object uses CLKgettime() function. Thanks MA Imran "MIA" <imran_akthar@hotmail.com> wrote in message news:b36ae06a.0407211928.ec1cbf@posting.google.com... > Hi All, >

Re: Plain truth is that unix/linux is NOT secure! #3
Rudolf, But remember the DECnet worm, too. eberhard ----- Original Message ----- From: "Rudolf Wingert" <win@fom.fgan.de> To: <Info-VAX@Mvb.Saic.Com> Sent: Wednesday, March 01, 2006 9:33 AM Subject: Re: Plain truth is that unix/linux is NOT secure! > Hello, > > the buffer overflow problem does still exists within all the TCP/IP > waste (also in OpenVMS). But other then in any *X and WindowsX flavor, > you would not get any additional rights under OpenVMS. Also remember, > the first virus was on BSD!! an UNIX flavor. The most problems (virus, > Trojans ...) which I do see under Windows are also a problem under *X. > But there are not enough hackers which favorite *X. > > Best regards R. Wingert >

[9fans] plan9ports rio patch
--mYCpIKhGyMATD0i+ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline greetings list, i've attached a patch for the current source for rio from plan9ports. it appends a window id to menu items which are duplicated in the menu, making it easier to locate a certain client. somebody else might find it useful. -- glaive@vaned.net 4405 5BA1 62D1 9DA1 7774 B4A3 2CB2 3872 24ED 2441 --mYCpIKhGyMATD0i+ Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="rio-xid.diff" --- fns.h.orig Sat Sep 4 15:38:59 2004 +++ fns.h Sat Sep 4 15:39:24 2004 @@ -71,6 +71,7 @@ void unhide(); void unhidec(); void renamec(); +char *gethideid(); /* client.c */ void setactive(); --- menu.c.orig Sat Sep 4 15:32:09 2004 +++ menu.c Sat Sep 4 16:39:41 2004 @@ -34,6 +34,8 @@ version, }; +static char extid[14]; + void button(XButtonEvent *e) { @@ -282,4 +284,32 @@ b3items[B3FIXED+i] = name; return; } +} + +char * +gethideid(int n) +{ + Client *c; + int i; + + if (n >= numhidden) { + fprintf(stderr, "rio: gethideid: n %d numhidden %d\n", n, numhidden); + return 0; + } + c = hiddenc[n]; + if (!hidden

How to write efficient calculations?
FM5.5 WINXP I have been working on a calculation that would subtract the type of street at the end of an address field [Location of Offense] and leave the number and the street. As in: "1234 n anywhere dr" or "1234 n anywhere dr #37" to "1234 n anywhere". Anything after the type of street would be subtracted out. This would go for "ave", "dr", "ln", "st", etc. Below is a sample of what I came up with and would you know it works! However, I am always looking for a more compact or efficient method of doing things. Can anyone

Capture Text Plain in Cisco AS5300
How can I capture a text plain with CISCO AS5300? On 16 Nov 2005 11:30:56 -0800, juanitomoyano@gmail.com wrote: ~ How can I capture a text plain with CISCO AS5300? I have no clue what you are talking about. Can you describe your intended application in more detail? Where is this text coming from? Who is outputting/transmitting it? Who needs to capture it? What do you intend to do with it once it's captured? The first thing you need to do is to forget about doing Cisco system administration with a Windows PC. Set up a Linux server and transfer your files with tftp. #copy startup-config tftp: -RFH "Ramon F Herrera" <ramon@conexus.net> wrote: >The first thing you need to do is to forget about doing Cisco system >administration with a Windows PC. Set up a Linux server and transfer >your files with tftp. > >#copy startup-config tftp: > Why? I setup 100's of Cisco switches using windows and tftp..

Re: Calculations using dates #4
If the quantity 60 is the supply for 30 days, shouldn't it be 2 per day, not .5 per day? -----Original Message----- From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Sdlentertd Sent: Friday, January 22, 2010 9:19 AM To: SAS-L@LISTSERV.UGA.EDU Subject: Calculations using dates Have this dataset DeathDT DatePurch DAYSUPPLY QTYPurch PriceUnit 12/02/2009 11/15/2009 30 60 1.00 I am trying to find a way to figure out how much money is lost due to death: if a person purchased 60units at $1.00, but died after 16 days of taking that medication, we have 14 days left (need to calculated the days left) and need to figure out how much that 14 days is worth Need to do this in sas: if death is after datePurchased then do; Death date minus DatePurch = x; QTY / DAYSUPPLY =qtyPerDay; count how many days left after death until DatePurch+Daysupply (to figure out when the supply expires) = y; y (days left after death) * qtyPerDay = z; (QTY left after death) then z* PriceUnit to figure out the final$ left on medicine after a person died. So in my example: 14 days after death, 0.5 medicine per day = 7qty * $1.00 =$7.00 loss Thank you

Proc Report calculations across ROWS
Hi, Is it possible to use proc report to compute calculations across two rows. It might be very simple , however right now I am just moving in cirles. Appreciate any pointers in this direction. I have created a small sample of my data to make it simpler to understand. Here is the issue. I need to compute a new row called Other_Media that is the difference of (ALL - Media1 TOT) as shown in the output below. Summary of Profits Marketing Campaign 'X' Source AO1 AO2 AO3 Direct Mail $100$150 $500 Newsletter$200 $200$100 Phone $200$350 $50 Web$100 $100$150 ------------------------------------------------------------------------------ Media1 TOT $600$800 $800 Other_Media$2,000 $3,000$2,000 ALL $2,600$3,800 $2,800 data camp; length Source$11.; input Area_Office $Source$ Profits; cards; AO1 ALL 2600 AO2 ALL 3800

any objects for handling (program) state transitions
Hi, before going on to re-invent the wheel, I was wondering if there is a well written and tested object to manipulate state transitions. Let me explain. I have an object whose actions and transitions to other states are constrained by the current state the application is at a particular moment. So, I need an object that lists the allowed states, and conditions under which one can go from one state to another. Finally it will provide tests for each particular state (is it in that state or not). The more I think about it, the more the wheel looks complex, and the more I hope someone alread

MathTime Pro 2 Fonts and plain TeX
Anyone have any experience they would care to share with using MathTime Pro 2 Fonts with plain TeX? Specifically, is it (relatively) straightforward to configure (say) TeXLive 2007 and plain to use these fonts? (They are on sale from PCTeX for the next few days, in case anyone here is interested.) Thanks. Jim