Still having difficulty with use of `\expandafter`
I'm a bit confused by your code. But there are a couple of things I see that might help you.
Writing
\expandfater\getlength{#1}
is effectively the same as writing
\getlength{#1}
without any expansion.
If it's #1
that you want expanded first, that's not going to happen as you wrote it. Instead, it's the {
TeX is going to try to expand. To reach #1
you need to write something like
\expandafter\getlength\expandafter{#1}
But this most likely won't do what you want either. If #1
is a string of tokens, for example if you try
\def\a{ABC}
\def\b{\c}
\def\c{XYZ}
\def\mythe#1{\expandafter\getlength\expandafter{#1}}
Then
\mythe{\a\b\c}
expands to
\expandafter\getlength\expandafter{\a\b\c}
which then expands to
\getlength{ABC\b\c}
The \b\c
of #1
is not accessible through the use of \expandafter
as you've written it.
Now, if you know that #1
should only be one token, that all is OK at this point.
If you want to get the integer and fractional parts of a dimension, then the following will accomplish that without calling any special packages.
\documentclass{article}
\makeatletter
\def\getparts#1{%%
\edef\my@stripped@length{\strip@pt#1}%%
\expandafter\ae@int@frac\my@stripped@length..\@nil
}
\def\ae@int@frac#1.#2.#3\@nil{%%
\def\aeinteger{#1}%%
\def\aefraction{#2}%%
}
\newlength{\aetemp}
\setlength{\aetemp}{1.234cm}
\def\showparts{%%
\begin{tabular}{ll}\hline
Length & \the\aetemp\\
Integer & \aeinteger \\
Fraction & \aefraction\\\hline
\end{tabular}}
\makeatother
\pagestyle{empty}
\begin{document}
\setlength{\aetemp}{1cm}
\getparts{\aetemp}
\showparts
\setlength{\aetemp}{215pt}
\getparts{\aetemp}
\showparts
\end{document}
The problem is that \getlength
requires several expansion steps to end its job delivering a sequence of digits (with a decimal dot in the middle). I'll show them in successive lines
\getlength{\thislength}
\strip@pt\thislength
\expandafter\rem@pt\the\thislength
\[email protected]
123\ifnum 456>\z@ .456\fi
123.456
A total of five expansion steps that require 31 \expandafter
tokens to be performed if you want that \@printplainbefore
to see what it expects, not just one. But the presence of \ifnum
makes the macro unusable in practice.
Since you don't want to remove a zero fractional part, you can use a \romannumeral
trick:
\documentclass{article}
\makeatletter
\def\printplainbefore#1{\expandafter\@printplainbefore\romannumeral-`Q#1..\@nil}
\def\@printplainbefore#1.#2.#3\@nil{#1}
\def\printplainafter#1{\expandafter\@printplainafter\romannumeral-`Q#1..\@nil}
\def\@printplainafter#1.#2.#3\@nil{#2}
\begingroup\catcode`P=12 \catcode`T=12
\lowercase{\endgroup\def\simplerem@pt#1PT{#1}}
\def\simplestrip@pt{\expandafter\simplerem@pt\the}
\newcommand*{\getlength}[1]{\simplestrip@pt#1}
\makeatother
\newlength{\thislength}
\setlength{\thislength}{123.456pt}
\begin{document}
Number: 123.456
\quad Split: \printplainbefore{123.456} -- \printplainafter{123.456}
\def\temp{123.456}
Macro: \texttt{\meaning\temp}
\quad Split: \printplainbefore{\temp} -- \printplainafter{\temp}
The: \the\thislength
\quad Split: \printplainbefore{\the\thislength} -- \printplainafter{\the\thislength}
Getlength: \getlength{\thislength}
\quad Split: \printplainbefore{\getlength{\thislength}} -- \printplainafter{\getlength{\thislength}}
\end{document}