f

#### 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 39029 articles. 3 followers.

11 Replies
1314 Views

Similar Articles

[PageSpeed] 29

 "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