Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 15 down vote accepted

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.

share|improve this answer
    
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
add comment

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.

share|improve this answer
    
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
show 2 more comments

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.