COMPGROUPS.NET | Browse | Post | Groups | Users | Stream | About | |

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

Similar Articles

[PageSpeed] 45

• Email
• Follow

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

Type1 in plain TeX
Hello all Can I use Type1 fonts in plain Tex? Sergio Kvantos <kvantos@plotinka.ru> wrote: > Can I use Type1 fonts in plain Tex? Yes. TeX does not matter about font formats, because it uses font metrics (TFM files) only. It it the task of your driver to embed Type 1 fonts, if they are available and the driver is configured to use them. Yours sincerely Heiko <oberdiek@uni-freiburg.de> On Sun, 27 Feb 2005 14:02:30 +0200, Sergio Kvantos <kvantos@plotinka.ru> wrote: >Hello all > >Can I use Type1 fonts in plain Tex? The answer is yes, of course. For what I un...

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

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