4

Note: I've separated this question from what was becoming a very tangled discussion. I know egreg already has a solution, so I'll just wait until he posts it here.

Question: The solution has to take a decimal number in any form, that is, a dimen/length variable, a plain \def, a number string, whatever and use only basic LaTeX to separate out the integer part and only the digits of the fractional part without preceding "." or "0." leaving the user of said solution to then format these results any way they like.

The related question using pgfmath is here. The pgfmath routines chop off the pt for you. In that case you only need a macro that either reads \pgfmathresult or similar.

Answer code output

My MWE presents a limited solution that I came up with using help from E.Ellett and egreg before they knew just how general I wanted this. It provides two routines that read a length variable, that is, one declared with \newlength{\thelength}, but as pointed out by egreg, it still has only limited expanding ability.

MWE Code

\documentclass[border=5mm]{standalone}

\makeatletter

\def\my@get@length#1{\edef\pure@length{\strip@pt#1}}

\def\printplainbefore#1{\my@get@length#1\expandafter\@printint\pure@length..\@nil}
\def\@printint#1.#2.#3\@nil{#1}
\def\printplainafter#1{\my@get@length#1\expandafter\@printfrac\pure@length..\@nil}
\def\@printfrac#1.#2.#3\@nil{#2}

\makeatother

\newlength{\thislength}
\setlength{\thislength}{123.456pt}
\tracingmacros=1

\begin{document}
The: \the\thislength\quad
Split: \printplainbefore{\thislength} -- \printplainafter{\thislength}
\end{document}
1
  • @TorbjørnT. I was coming back to do what you just did. I was in the middle of a three post editing juggle to clarify this situation. You don't let the moss gather do you? ;-) Cheers Nov 5, 2013 at 1:16

1 Answer 1

3

Here's an extended version of the macros proposed in Separating a decimal number into whole and fractional parts with only the digits using pgfmath that accepts as argument to \printplainbefore and \printplainafter also a length or skip parameter or a macro that expands to a length (see the last call with \test). I used \dimexpr, so e-TeX is needed.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\makeatletter
\begingroup\catcode`P=12 \catcode`T=12 \lowercase{\endgroup

\def\printplainbefore#1{%
  \expandafter\geoff@strip@pt\expandafter\@printplainbefore\the\dimexpr#1PT\relax\@nil
}
\def\printplainafter#1{%
  \expandafter\geoff@strip@pt\expandafter\@printplainafter\the\dimexpr#1PT\relax\@nil
}
\def\@printplainbefore#1.#2.#3PT\@nil{#1}
\def\@printplainafter#1.#2.#3PT\@nil{#2}

\def\geoff@strip@pt#1#2PT#3\@nil{#1#2..PT\@nil}

} % balance the open brace after \lowercase
\makeatother

\begin{document}

\printplainbefore{1.41421} -- \printplainafter{1.41421}

\pgfmathparse{sqrt(2)}
\printplainbefore{\pgfmathresult} -- \printplainafter{\pgfmathresult}

\printplainbefore{1} -- \printplainafter{1}

\printplainbefore{.1} -- \printplainafter{.1}

\printplainbefore{0.1} -- \printplainafter{0.1}

\printplainbefore{\baselineskip} -- \printplainafter{\baselineskip}

\printplainbefore{\medskipamount} -- \printplainafter{\medskipamount}

\printplainbefore{12.34pt} -- \printplainafter{12.34pt}

\def\test{12.34pt}

\printplainbefore{\test} -- \printplainafter{\test}
\end{document}

enter image description here

NOTE

With this generalized macro it's impossible to distinguish the case where the integer or fractional part is empty. So \printplainbefore{.1} and \printplainbefore{0.1} will give the same result 0.

7
  • I've read a page here about e-TeX and I'm still confused about the difference between it and etex.sty. Is that stuff now just a part of LaTeX2e? You're saying e-TeX is needed but the code works for me without any explicit usepackage call. Nov 5, 2013 at 22:21
  • e-TeX is the engine that's been in use since some years. LaTeX doesn't use the provided extensions, but several packages do.
    – egreg
    Nov 5, 2013 at 22:23
  • Does saying e-TeX is the engine mean I can use it's functionality without any usepackage calls? I still have a lot to learn about how LaTeX uses TeX. I have Knuth's TeXbook and I'm making some progress with it - it's basically all about understanding how TeX converts raw source into tokens and then how it expands them - yes?? But I still have to learn what TeX you can use and what you can't within LaTeX. EG - I learnt the hard way that you can't use registers directly. Lamport's LaTeX:ADPS2ed is on it's way in the post, will that be a good start? Nov 5, 2013 at 23:42
  • Yes, you can use the new primitives like \unexpanded or \dimexpr in any LaTeX document. However, in order to access the extended register pool you have to \usepackage{etex}.
    – egreg
    Nov 5, 2013 at 23:43
  • Any thoughts about the rest of my comment? Nov 6, 2013 at 0:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .