[pdftex] calculating in LaTeX

Ross Moore ross at ics.mq.edu.au
Tue Oct 8 20:53:23 CEST 2002


[Charset iso-8859-1 unsupported, filtering to ASCII...]
> 
> 
> I want to define a new command like this:
> \newcommand{\mycommand}[2]{\begin{figure} \includegraphics[height=#2,
> width=2*#2]{#1} \end{figure}}

Try the following quick-and-dirty approach:

\newcommand{\mycommand}[2]{\begin{figure}\dimen0=#2\relax
 \includegraphics[height=\the\dimen0, width=2\dimen0]{#1}\end{figure}}

If that doesn't work (because \dimen0 might be used in the parsing of
the arguments to \includegraphics, so its value may have changed by the
vital time when it is needed as the height or width) then try this more
robust solution:

\newcommand{\mycommand}[2]{\begin{figure}%
 \dimen0=#2\relax \dimen2=2\dimen0\relax
 \edef\next{[\the\dimen0,\the\dimen2]}%
 \expandafter\includegraphics\next{#1}%
 \end{figure}}
 
 
> Every graphic should be twice as wide as high. How can I achieve this? The
> proposed solution does
> not work, because 2*#2 is not evaluated.
 
> Is there any way to evaluate expressions? Another example could be to write

You can do very efficient addition, subtraction and multiplication;
but division is harder. Yes, there is the calc package,
but that is not so efficient, despite being conceptually easy.

> sqrt(2) instead of 1.141...

Presumably you want to multiply lengths by sqrt(2).
Just define
   \def\rootofii{1.1415926}
then use:   \dimen2=\rootofii\dimen0
where \dimen0  contains a length.
Then \dimen2 will contain the multiplied length.
To see what it is, by printing to the console (.log file), use:
  \showthe\dimen2 

If you have no further use for the length contained in \dimen0,
then you could just do  \dimen0=\rootofii\dimen0 
rather than use 2 different scratch registers.


Hope this helps,

	Ross Moore


> Thanks 
> Dirk
> _______________________________________________
> pdftex mailing list
> pdftex at tug.org
> http://tug.org/mailman/listinfo/pdftex




More information about the pdftex mailing list