4

Could someone please tell me how I can use the \widthof{} command from the calc package within \FPeval from the fp package?

\documentclass[10pt]{article}
\usepackage{calc}
\usepackage{fp}
\begin{document}
\FPeval\result{(2.5-\widthof{13})/2}
\end{document}

Even with \csname and \value, or \arabic, it still results in an error.

user111
  • 411

1 Answers1

6

\widthof of package calc is only supported inside \setlength and \addtolength. Also package fp does not deal with dimensions, but real numbers. \strip@pt extracts the number of a length/dimen register and removes the unit pt:

\documentclass[10pt]{article}
\usepackage{calc}
\usepackage{fp}
\newdimen\tempdimen
\begin{document}
\makeatletter
\setlength{\tempdimen}{\widthof{13}}
\FPeval\result{(2.5-\strip@pt\tempdimen)/2}
\makeatother
\typeout{Result: \result}
\end{document}

Output:

Result: -3.750015000000000000
Heiko Oberdiek
  • 271,626
  • 1
    Use \settowidth? – Joseph Wright May 21 '16 at 17:15
  • 1
    @JosephWright \settowidth{\tempdimen}{13} is shorter and does not depend on package calc; therefore it is the better choice. However, it does not show, how \widthof can be used with the fp package. ;-) – Heiko Oberdiek May 21 '16 at 17:20
  • How would you write a command to do this? So far I have \newcommand{\test}[1]{\makeatletter\setlength{\tempdimen}{\widthof{#1}}\FPeval\resultaP(2.5-\strip@pt\tempdimen)/2}\makeatother}, but this doesn't seem to work. – user111 May 21 '16 at 22:14
  • @user111 \strip@pt contains @, which needs the category code letter. The category codes are used, when TeX tokenizes the input bytes, that means definition time here, not execution time of the defined macro. \makeatletter and \makeatother must go outside the definition: \makeatletter\newcommand*{\test}[1]{\settowidth{\tempdimen}{#1}\FPeval\resultaP(2.5-\strip@pt\tempdimen)/2}}\maketother. – Heiko Oberdiek May 22 '16 at 06:57
  • BTW, there is a followup question with a good answer by cfr. – Heiko Oberdiek May 22 '16 at 07:05