# spacing from calculator package

I'm trying to include images and overlay them with their width and height info in mm. I save the image to a box and then use the calculator package to calculate the mm width and height of that box. Then I use a picture environment to place the image and, on top of it, a fcolorbox with the measurements.

What happens is that I get additional spacing before the picture environment, but only when using the commands from the calculator package. Is there a bug or am I doing something wrong?

Here's my example, with a \rule as a dummy for the image; page 1 with calculated measurements, page 2 with measurements entered manually:

\documentclass{article}
\usepackage{blindtext}
%\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{picture}
\usepackage{calculator}
\newsavebox{\tmpboxa}%
\begin{document}\noindent
\savebox{\tmpboxa}{\rule{\textwidth}{50mm}}% This is where the image will be includegraphicsed
\LENGTHDIVIDE{\wd\tmpboxa}{1mm}{\breite}%
\ROUND[2]{\breite}{\Breite}%
\LENGTHDIVIDE{\ht\tmpboxa}{1mm}{\hoehe}%
\ROUND[2]{\hoehe}{\Hoehe}%
\begin{picture}(\breite mm, \hoehe mm)%
\put(0,0){\makebox(0,0)[bl]{\usebox{\tmpboxa}}}%
\put(1mm,1mm){\makebox(0,0)[bl]{\fcolorbox{black}{black!15}{\strut \Breite\,mm $\times$ \Hoehe\,mm}}}%
\end{picture}%
\\
\blindtext
\clearpage\noindent
\savebox{\tmpboxa}{%
\rule{\textwidth}{50mm}%
}%
\begin{picture}(\textwidth, 50mm)%
\put(0,0){\makebox(0,0)[bl]{\usebox{\tmpboxa}}}%
\put(1mm,1mm){\makebox(0,0)[bl]{\fcolorbox{black}{black!15}{\strut 121.25\,mm $\times$ 50\,mm}}}%
\end{picture}%
\\
\blindtext
\end{document}


If you do

    \savebox{\tmpboxa}{\rule{\textwidth}{50mm}}% This is where the image will be includegraphicsed
\setbox0\hbox{\LENGTHDIVIDE{\wd\tmpboxa}{1mm}{\breite}%
\ROUND[2]{\breite}{\Breite}%
\LENGTHDIVIDE{\ht\tmpboxa}{1mm}{\hoehe}%
\ROUND[2]{\hoehe}{\Hoehe}}\showoutput\showbox0


you'll see

> \box0=
\hbox(0.0+0.0)x6.66666
.\glue 3.33333 plus 1.66666 minus 1.11111
.\glue 3.33333 plus 1.66666 minus 1.11111


so as you suspected these commands are adding two spaces (probably missing % at ends of lines in its code). If you want to use them a simple workaround would be to move the \noindent to just before \begin{picture} so that the calculations happen in vertical mode where spaces are ignored.

The package is clearly not expecting calculations in horizontal mode, it would need dozens of % and \relax adding to protect against white space, but in this case you can just fix \LENGTHDIVIDE and the two spaces go:

\makeatletter
\def\LENGTHDIVIDE#1#2#3{%
\begingroup
\cctr@lengtha=#1\relax
\cctr@lengthb=#2\relax
\edef\cctr@tempa{\expandafter\strip@pt\cctr@lengtha}%
\edef\cctr@tempb{\expandafter\strip@pt\cctr@lengthb}%
\DIVIDE{\cctr@tempa}{\cctr@tempb}{#3}%
\@OUTPUTSOL{#3}}
\makeatother

• I'll want the whole stuff inside a macro that will kind of replace \includegraphics', and it will sometimes be called from within a wrapfigure environment. Guess I'll have to look for a different package to do the calculations with. – Blackface Oct 28 '14 at 10:31
• @Blackface updated with a fix (for this one command) It may be worth contacting the package maintainer – David Carlisle Oct 28 '14 at 10:50
• @DavidCarlisle Most of the \UPPERCASE commands in calculator have \ignorespaces at their end; the author forgot to mask several end-of-lines where those commands aren't used. Also the \@UPPERCASE` command were forgotten, so fixing the package is quite a mess. – egreg Oct 28 '14 at 11:27
• Pity. @David's patch works fine for me and for now, but for the future: Is there a less messy alternative? Preferably documented, unlike fp? BTW, I also wrote a mail to the author. – Blackface Oct 28 '14 at 14:35
• Thank you, Blackface and David. I will try to find the parts of the package where unwanted spaces appears. – Robert Fuster Oct 30 '14 at 8:09