How to perform calculations in plain TeX

  • Permalink
  • submit to reddit
  • 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
Reply 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
ex instead of pt):

\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
Reply 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
>ex instead of pt):
>
>\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
Reply 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
Reply 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
Reply 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
> 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?

Yours sincerely
  Heiko <oberdiek@uni-freiburg.de>
0
Reply 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
Reply 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
Reply 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
Reply 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
>> 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?  May sound academical, but has
repercussions with macros like \addvspace.

-- 
David Kastrup
UKTUG FAQ: <URL:http://www.tex.ac.uk/cgi-bin/texfaq2html>
0
Reply 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
Reply Heiko 12/15/2009 4:41:12 PM
comp.text.tex 36441 articles. 68 followers. Post

11 Replies
707 Views

Similar Articles

[PageSpeed] 26


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

Table.TeX in Plain TeX
Dear all Normally we use the "Table.TeX" for aligning and formatting Tables in Plain TeX. Unfortunately I don't have the "Table.TeX". Can any one supply this or give any ideas of where it can be found. Thanks and Regards Saravanan,M. India Saravanan_Typesetter@yahoo.co.in (Saravanan,M.) writes: >Normally we use the "Table.TeX" for aligning and formatting Tables in >Plain TeX. Unfortunately I don't have the "Table.TeX". i suspect you may be thinking of tables.tex (plural tables). that is available on ctan, together with some documenta...

Old version of plain.tex and hyphen.tex
Hello All, I am trying to recreate an old version of our software that assumes an old version of Knuth's plain.tex (together with the version of hyphen.tex that accompanied it). I have searched the Internet for plain.tex Version 3.1415926 (together with hyphen.tex) with no success. Does anyone have a copy of these two files that they could email back to me? With great appreciation in advance! Brian O'Toole On Jul 2, 5:01 pm, "Brian O'Toole" <boto...@mathworks.com> wrote: > Hello All, > > I am trying to recreate an old version of our software tha...

plain TeX
Hello, \def\magnification{\afterassignment\m@g\count@} \def\m@g{\mag\count@ \hsize6.5truein\vsize8.9truein\dimen\footins8truein} But \afterassignment\m@g \mag works well too... ...

FIRST...add number in cell but cannot run when i set calculation in button calculate in action performe
import java.awt.*; import java.awt.Color; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; import javax.swing.border.*; import javax.swing.JComboBox; import java.text.DecimalFormat; import javax.swing.JTable; public class Details extends JFrame implements ActionListener { private JFrame f1, f2, f3; private JLabel lblTitle, lblName, lblIC, lblDsh1, lblDsh2, lblAddress, lblPostalCode, lblCity, lblTel, lblHome, lblHP, lblOffice, lblFax, lblEmail; private JLabel lblReference, lblDate, lblCashier, lblSalesman, lblTotal; private JTextField txtName...

stmaryrd in plain TeX?
Hi, is there a way to get stmaryrd to work in plain TeX? More specifically I'm keen to use \llbracket and alike. Cheers, Freidoun ...

symbols in plain tex
Hi, I'm extending a refcard that is written in plain tex, and I'm having trouble typesetting some symbols. Most notably, I need the backtick (`), and straight quotes, both single and double (' ", looking exactly like that). How can I do these in plain tex? Thanks, Michael Goerz On Apr 20, 1:16 pm, Michael Goerz <newsgroup812s...@8439.e4ward.com> wrote: > Most notably, I need the backtick (`), and straight quotes, both single > and double (' ", looking exactly like that). How can I do these in plain > tex? Choose a font that has the character app...

\llbracket in plain TeX?
Hello, does anyone know how to produce \llbracket in plain TeX? I couldn't get stmaryrd to work (only in LaTeX). Thanks in advance, Freidoun On Apr 2, 1:57 pm, "freidoun" <freidoun.pichd...@gmail.com> wrote: > Hello, > > does anyone know how to produce \llbracket in plain TeX? I couldn't > get stmaryrd to work (only in LaTeX). I just put together something along these lines: http://comp.uark.edu/~luecking/tex/index.html Look near the bottom of the page for stmary.tex. Put \input stmary at the top of your file and use \llbracket and \rrbracket wher...

babel with plain tex
Hi group, has anybody a working example for using babel with plain tex? - how do I initialize babel (\input...?) - how do I switch between languages? - is only the hyphenation switched or also some set of fonts ? I'd like to compile something like: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \input <some babel files for german and greek?> \selectlanguage{german} stuff german 1 \selectlanguage{greek} stuff greek \selectlanguage{german} stuff german 2 \bye %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Using bplain.exe from MikTex2.9 shows that it ha...

Learning Plain Tex
I'm using MikTex 2.3 (total) and Winedt 5.3 (Windows XP). I'd like to learn plain Tex to understand some packages and classes available for Latex and patch the to my needs (modifying then). Are the Latex Packages and Classes writen in plain Tex? Using MikTex 2.3 and Winedt 5.3 it's possible to write .tex files using plain tex and compile then? Are there, easy to find, references to learn plain tex? I'd be very gratefull for any help lgita-2002@bol.com.br (DeSes) writes: > I'm using MikTex 2.3 (total) and Winedt 5.3 (Windows XP). > > I'd like to learn p...

Performing a Substring Calculation
I hope I'm in the right group for this. I'm using Google's Picasa program to create formatted webpages of my images. Picasa acts on html and tpl files as it creates the final html pages. I have found one variable that I would like to chop. The variable is referred to as <%itemName%>. I think the operation that I would like to perform is "substring(<%itemName%>, 1, 8)." Is it okay to include an operation like this in one of the html files that gets "included?" How do I get the substring operation to be executed by the program that is assemblying ...

Perform calculations in a pdf ?
Hi, is there a way to have some fields to fill in in a pdf file and have calculations made using their contents ? Project is we have a pdf file used a reference for management. Some operations we use need user input to perform calculations in order to get a parameter back orienting management. How can I have for example on a page say two empty fields, have the user fill them in, and when done click a "Calculate" button to show teh result in a third field. Is this possible or should I turn away from pdf ( that I mostlly use because not modifiable by users) and use html ( but forma...

tikz and plain TeX
Hi, I know that not anything is possible with plain tex setups, but currently I'm quite stuck with using trees in a plain luatex document. I know that there will be some drawbacks in color management, but at least the node placement should work, which doesn't, also not with latex, so I assume its a tikz module problem. But I don't know which modules are needed after all. I think I will simply post a code snippet to show my problem: %%%%% START % maybe I need a special \def\pgfsysdriver too ? \input tikz \usetikzlibrary{arrows,positioning,calc,trees,chains,shapes.g...

Limits in Plain Tex
I need to type in Plaint tex limit (lim) and below it gamma tending to one. What are Plain tex commands for that? Alex <akhmel6@gmail.com> wrote: > I need to type in Plaint tex limit (lim) and below it gamma tending to > one. What are Plain tex commands for that? $$\lim_{\gamma\to 1} f(\gamma)$$ Ciao Enrico On May 3, 3:51=A0pm, Enrico Gregorio <Facile.da.trov...@in.rete.it> wrote: > Alex <akhm...@gmail.com> wrote: > > I need to type in Plaint tex limit (lim) and below it gamma tending to > > one. What are Plain tex commands for that? > > $$\lim...

Plain TeX in LaTeX
I was recently told by someone fairly knowledgeable about TeX and LaTeX that, in LateX, one can turn off LaTeX and get it to behave just like Plain TeX. If this is true, how is it done? This is my last posting from the email address below because the machine on which my account resides is being decommissioned. It may be some time before I post to USENET newsgroups again, but I will still be able to read replies via dejanews in the meantime. -- Ignorantly, Allan Adler <ara@zurich.csail.mit.edu> * Disclaimer: I am a guest and *not* a member of the MIT CSAIL. My actions and * comments do ...

Storage Performance Calculator
I saw some screenshots of a tool called:" IO RAID Calculator" It looks like a Excel spreadsheet. When applying a Raid Level, no of disks, IOPS per disk, Read% it results with graphs and some I/O calculations. Do You know this tool ? Is it free? Regards ...

Graphics in plain tex
I'm using TeX, Version 3.14159 [PD VMS 3.6] with epsf.sty to include images in a document. If the images I get are saved in the correct way as eps files, everything works nicely. The problem is, that these days, I get a lot of jpegs, which I have to convert first. I can copy them to my PC and use jpeg2ps to make a tex-compatible eps file and this works well,but is a little time consuming. My question, I guess is, Does anyone know of a package for a VMS operating system that will make EPS files from JPG files, where the EPS files are small and will work with TEX, or alternatively, is ther...

Historical TeX performance
Does anyone have historical figures for TeX performance? Obviously it depends a lot on what it's processing, but there are some documents which have existed for a long time pretty much unchanged (the TeX book for instance), which are probably large enough to provide interesting data. (I'm asking because I recently started using TeX again after a gap of around 10 years, and it's interesting that (pdf)LaTeX is not quite as instant as I thought it might be, and I'm curious. I'm guessing this is mostly because there's a lot of I/O of class files etc, and ...

lmodern in plain TeX
Is there a package to use lmmodern in plain TeX? There is quite a bit customization to do; namely: redefine the accents commands \', etc. redefine special symbols commands \oe setup the sfcode and llcode of the characters. Make the accented characters letters (for proper hyphenation). I could do it myself, but maybe somebody has already done it. Olive Olive wrote: > Is there a package to use lmmodern in plain TeX? See http://www.tug.org/tex-archive/help/Catalogue/entries/fontch.html http://www.tug.org/tex-archive/help/Catalogue/entries/font-selection.html http://www.tug....

Fonts in plain TeX?
Dear All, I'm fairly new to TeX, and I'm trying to typeset a plain TeX document in Palatino (rpplr, etc). I've had some success, but no ligature substitution appears to be occurring: for example "---" appears as three hyphens rather than an mdash. Unfortunately, most of the documentation I can find refers to using fonts from LaTeX documents, but I'm not using LaTeX. Can anyone point me in the right direction? Should it help, I'm using pdfTeX in the tetex distribution. Thanks in advance, Jake > I've had some success, but no ligature substitution appears ...

CRC calculation performance
Hello. Can anyone tell me how fast a TI C6202 can perform a CRC 16 calculation? How many cycles would it take it to calculate the CRC on 1KB? Thanks, Gilad. Based on a LUT or doing all the funky shifts and XOR's ? "Gilad Cohen" <gilad_coh@walla.co.il> wrote in message news:8f9a8978.0308100903.1e7a8064@posting.google.com... > Hello. > Can anyone tell me how fast a TI C6202 can perform a CRC 16 calculation? > How many cycles would it take it to calculate the CRC on 1KB? > > Thanks, > Gilad. "Gary Pace" <___@___.com> wrote in message new...

Colourize in plain TeX
I would like to colourize parts of my plain TeX document. However, it seems like it depends heavily on the output or viewing program etc. MiKTeX has .tex -> .dvi and .tex -> .pdf For .dvi (viewed/printed with YAP) I know I have to put \special{color push rgb <n> <n> <n>} in the beginning (where each <n> is a number) and \special{color pop} at the end of coloured text. However, it won't work with pdftex (and probably with many other colour-able versions of dvi-handling programs or other (than .dvi and .pdf) outputs of TeX). So how do you colourize it...

Plain TeX question
Once a particular paragraph has been set (i.e., the line breaks and the interword glue determined, I would like to find out the width of a particular phrase within a line of the paragraph. I don't have to do this on-the-fly; I can simply use the information the next time the text is processed. How is this done in plain TeX? I have looked at the output from \tracingparagraphs=1 but I don't know how to derive the actual interword space from this information. Just to be sure my question is clear, I have this artificial example: <here is the paragraph with line breaks:> This par...

plain tex and tikz
I'm using the ancient Y&Y 2.1 version of AMSTeX (I know). I think (but am not sure) that I have placed all of the pgf files on my system in the right place. However, even when I run the simplest test in Plain TeX: \input tikz.tex \tikzpicture \draw (0,0) -- (1,1); \endtikzpicture \end I get the following error message in DVIWindo: "Premature end of special". It appears under the heading WINSPECI. And everything stops right there. The log file shows now problem at all that I can see. No complaints and it is clearly reading all of the pgf files....

Plain TeX in french
Hi, First i would like to apologize if my request has already been treated here. My problem is i do not manage to use plain TeX with french hyphenation. Everytime i try to, i get the following error : This is TeX, Version 3.14159 (Web2c 7.3.3.1) (./master.tex (c:/texmf/tex/generic/babel/francais.sty (c:/texmf/tex/generic/babel/frenchb.ldf (c:/texmf/tex/generic/babel/babel.def (c:/texmf/tex/generic/babel/plain.def) (c:/texmf/tex/generic/babel/switch.def (c:/texmf/tex/generic/babel/plain.def))) No hyphenation patterns were loaded for the language `French' I will use the patterns loaded f...