12

I'd like to know how to replace parts of a string in LaTeX. Specifically I'm given a measurement (like 3pt, 10mm, etc) and I'd like to remove the units of that measurement (so 3pt-->3, 10mm-->10, etc). The reason why I'd like a command to do this is in the following piece of code:

\newsavebox{\mybox}
\sbox{\mybox}{Hello World!}
\newlength{\myboxw}
\settowidth{\myboxw}{\usebox{\mybox}}
\begin{picture}(\myboxw,\myboxw)
\end{picture}

Basically I create a savebox called mybox. I insert the words "Hello World" into mybox. I create a new length, called myboxw. I then get the width of mybox, and store this in myboxw. Then I set up a square picture environment whose dimensions correspond to myboxw. The trouble is that myboxw is returning something of the form "132.56pt", while the input to the picture environment has to be dimensionless: \begin{picture}{132.56, 132.56}.

So, I need a command which will strip the units of measurement from a string.

3 Answers 3

16

The internal LaTeX macro \strip@pt returns a length in pt without the unit. For example, if you set \myboxw to 10 mm, it will have a length of 28.45274 pt, and this code

\makeatletter
\strip@pt\myboxw
\makeatother

will output just 28.45274. So you could use this or define your own command like:

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

\strip@pt uses \the on a dimen register and strips pt, that's why you did not get 10 (mm) but the corresponding value in pt without unit. Such a conversion to pt and stripping pt seems better to me than just doing string replacement getting some value without knowing the unit.

You can see the source code of \strip@pt with comment if you look into source2e.pdf in section 26.1 Macros for the user (part of the section 26. Selecting a new font), you could open it at the command prompt by typing texdoc source2e or get it from CTAN.

4
  • This is the right command to use under the circumstances. Being able to strip mm and other units (as specified in the question) isn't necessary in this case. (Which you'd probably do with string replacement instead of the technique here.) Sep 7, 2010 at 1:15
  • From source2e: \expandafter\endgroup\x – that’s a mean, mean trick. But I wonder: why not just use \gdef inside the group instead of this indirect definition? Any technical reason? Sep 7, 2010 at 7:25
  • Thanks a lot. The above solution seems to work, so I'll mark that as the accepted answer. I have another related question though, which you may or may not know the answer to... but when I use \stripunit{\myboxw} in a picture environment, if I prefix this with an integer, it treats the length as n times that length. If I prefix it without an integer, say 0.5\stripunit{\myboxw}, latex throws an error, complaining about units (which is confusing, because picture environment doesn't need them).
    – John
    Sep 8, 2010 at 12:23
  • At least you could write \setlength{\halfw}{.5\myboxw} and \stripunit{halfw} afterwards. Further there are packages for calculation like fp.
    – Stefan Kottwitz
    Sep 8, 2010 at 13:53
5

ConTeXt defines \withoutpt like this:

{\catcode`\.=\@@other
 \catcode`\p=\@@other
 \catcode`\t=\@@other
 \gdef\WITHOUTPT#1pt{#1}}

\def\withoutpt#1%
  {\expandafter\WITHOUTPT#1}

(syst-ext.mkii)

So you can use this or a similar mechanism to strip the "pt" suffix.

7
  • Is \@@other the same as \other? Sep 6, 2010 at 20:27
  • I guess that both are the same. \other is a LaTeX chardef, right?
    – topskip
    Sep 6, 2010 at 21:14
  • What's the point of making a period have catcode 12? And doesn't that require using \withoutpt{\the\wd0} or something?
    – TH.
    Sep 6, 2010 at 22:25
  • Neither \other nor \@@other exist in standard LaTeX. Sep 7, 2010 at 1:12
  • @TH: paranoia, I guess. In some environments, the dot can be active. Sep 7, 2010 at 5:46
1

The picture package allows you to use dimens in the arguments of picture commands, so a \usepackage{picture} should be enough to solve your problem.

You must log in to answer this question.

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