1

I have some trouble to understand how I ought to handle some values I calculated with fp to use them as length. It is obvious that I have to add an unit to the number to use it, but trying to do so, I get one of the following errors:

Missing number, treated as zero

or

Illegal unit of measure (pt inserted)

Here is, what I am trying to do:

\makeatletter
\newcommand\stripPT[1]{\strip@pt#1}
\makeatother
\newcommand\addPT[1]{{#1}pt}
\newcommand{\setImageToGrid}[2]{%
  \FPmul\xyz{12}{20}%
  \FPdiv\xyz{\xyz{}}{\stripPT{\onelineskip}}%
  \FPclip\xyz{\xyz}%
  \begin{figure}[ht]
    \noindent\includegraphics[width=\textwidth, trim={0 \addPT{\xyz} 0 \addPT{\xyz}},%
     clip, height=\xyz]{#1}
      \legend{#2}
  \end{figure}
}

(The calculation does not make sense, it's only for testing.) The only workaround I found so fare is to do something like this \xyz\onelineskip, of course I would have to divide the \xyz by \onelineskip before to get the correct value. But there must be a cleaner way. I also tried to save the value to a custom length before using it, but then I get the same problem there. As there are no types in LaTeX I am stuck.

Here is a working example:

\documentclass[a4paper,10pt,twoside]{memoir}
\usepackage{graphicx}
\usepackage{fp}
\usepackage{lipsum}


\makeatletter
\newcommand\stripPT[1]{\strip@pt#1}
\makeatother
\newcommand\addPT[1]{{#1}pt}

% Create and save the box.
\newsavebox{\Image}
\newlength{\imageh}

\newcommand{\setImageToGrid}[2]{
  \FPmul\xyz{12}{20}
  \FPdiv\xyz{\xyz{}}{\stripPT{\onelineskip}}
  \FPclip\xyz{\xyz}
  \begin{figure}[ht]
    \noindent\includegraphics[width=\textwidth, trim={0 \addPT{\xyz} 0 \addPT{\xyz}},%
     clip, height=\xyz]{#1}
      \legend{#2}
  \end{figure}
}


\begin{document}
\lipsum[2-6]
\newpage
\setImageToGrid{some.png}{Something}
\lipsum[2-4]

\end{document}

UPDATE: I added some % to the example as suggested in some comments.

  • 2
    You have to remove the braces from your \addPT command: \newcommand\addPT[1]{#1 pt}. You also have to \addPT in the height argument: clip, height=\addPT{\xyz}]{#1}. – Phelype Oleinik Sep 9 '18 at 15:20
  • 1
    unrelated but you need several % at ends of lines to avoid generating unwanted white space. – David Carlisle Sep 9 '18 at 15:22
  • @PhelypeOleinik Thanks, that's it. But why? I thought I have to avoid a white space. And how would I notate this, without declaring an extra command? (The different treatment of the height was only for testing). – user5950 Sep 9 '18 at 15:33
  • @DavidCarlisle Thanks for pointing this out. I'm using % in the original context. – user5950 Sep 9 '18 at 15:34
  • 1
    The whitespace is automatically ignored after a control sequence. – TeXnician Sep 9 '18 at 15:34
1

This is an alternative approach using expl3 (the code is not that idiomatic, but it does the job and you do not have to care about spaces). It evaluates only once (as you do not pass any parameters).

\documentclass[a4paper,10pt,twoside]{memoir}
\usepackage{graphicx}
\usepackage{expl3}
\usepackage{lipsum}

\ExplSyntaxOn
\makeatletter
\edef\mycalcres{ \dim_eval:n { \fp_eval:n { floor((12*20)/\strip@pt\onelineskip) } pt } }
\makeatother
\ExplSyntaxOff

\newcommand{\setImageToGrid}[2]{
  \begin{figure}[ht]
    \includegraphics[width=\textwidth, trim={0 \mycalcres{} 0 \mycalcres},%
     clip, height=\mycalcres]{#1}
      \legend{#2}
  \end{figure}
}


\begin{document}
\lipsum[2-6]
\newpage
\setImageToGrid{example-image-duck.pdf}{Something}
\lipsum[2-4]

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.