# How to use custom command output as lenght?

In order to convince TeX to keep the grid I tried to program a command, that takes a font size switch as parameter and calculates the reminder of the division of the corresponding baseline skip and the \onelineskip (the baseline skip of the default size). The commands output should then be use to set some lengths.

Basically, what I want to do, is passing the output of a custom command directly to \setlength.

But I ran into some problems, as if I do something like this:

\setlength{\trueGridDiv}{\trueGrid{\secheadstyle}}


where \trueGird is my custom command putting out a length, I get:

Missing number, treated as zero


I guess this hangs together with the way TeX is expanding, and I'm wondering, if the approach of using a commands output within \setlength is possible at all, and if it is, how?

A workaround could be to set a custom length within the command and use that in \setlength:

\trueGrid{\secheadstyle} % put the result of it calculations into the length trueGridOutput
\setbeforesecskip{-2\onelineskip + \trueGridOutput}


Here is a working example:

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

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

\newlength{\trueGridH}
\newlength{\trueGridDiv}
\newlength{\test}

\newcommand{\trueGrid}[2][\onelineskip]{%
% Get baselineskip of the given font size.
\settoheight{\trueGridH}{#2 \raisebox{0pt}[\baselineskip][0pt]{}}%
%
% Set the baselineskip. Default is \onelineskip.
\setlength{\trueGridDiv}{#1}%
%
% We do not have modulo function, so we calculate the
% reminder of the devision the given (#2) hight and
% the default line hight.
\FPdiv\result{\stripPT{\trueGridH}}{\stripPT{\trueGridDiv}}%
\FPtrunc\trunced{\result{}}{0}%
\FPsub\remainder{\result{}}{\trunced{}}%
\FPmul\remainder{\remainder{}}{\stripPT{\trueGridDiv}}%
%
\remainder pt%
}

• I might be wrong, but I think it should be expandable to be used within \setlength and your macro contains assignments. But do you really need to create your own version instead of simply using one of the existing packages? – TeXnician Sep 24 '18 at 8:22
• Unlike TikZ, which runs everything through \pgfmathparse, LaTeX command do not evaluate math expression. You need \dimexpr (or \numexpr) for that. – John Kormylo Sep 24 '18 at 14:51