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:

\sbox{\mybox}{Hello World!}

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.


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


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


\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.

  • 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.) – Will Robertson Sep 7 '10 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? – Konrad Rudolph Sep 7 '10 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 '10 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 '10 at 13:53

ConTeXt defines \withoutpt like this:




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

  • Is \@@other the same as \other? – Charles Stewart Sep 6 '10 at 20:27
  • I guess that both are the same. \other is a LaTeX chardef, right? – topskip Sep 6 '10 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 '10 at 22:25
  • Neither \other nor \@@other exist in standard LaTeX. – Will Robertson Sep 7 '10 at 1:12
  • @TH: paranoia, I guess. In some environments, the dot can be active. – Taco Hoekwater Sep 7 '10 at 5:46

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.

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.