# Passing length in form of a string instead of measure [duplicate]

I'd like to pass a measure of length in form of a string, i.e.

\setlength{string}


or

\setlength{'string'}


\setlength{2.5cm}


I seem to remember seeing something like that somewhere in LaTeX, but can't find it. Appreciate your input!

• Are you looking for \settowidth{\somelength}{string}? Jan 27, 2016 at 22:23
• Possible duplicate: Get width of a given text as length
– Werner
Jan 27, 2016 at 22:25
• 1 minute. I love StackExchange. @egreg Wonderful, thanks a lot! I'd mark it as the answer, but I can't do that with a comment. Werner - it's not the exact same issue, though I see your point.
– Zubo
Jan 27, 2016 at 22:28
• @Zubo: Questions are phrased differently, but they use the same principles. The other is actually a little more comprehensive than this request, making this one a subset of the linked post (in my opinion). I'm voting to close as a duplicate.
– Werner
Jan 27, 2016 at 23:41

The LaTeX kernel provides

\settowidth
\settoheight
\settodepth


and you probably want the first one:

\newlength{\somelength}

\settowidth{\somelength}{some text}


Note that it's better to set lengths to a text dependent width after \begin{document}. So, if you need \somelength for a macro definition in the preamble, you should do

\newcommand{\foo}{...depending on \somelenght...}

\AtBeginDocument{\settowidth{\somelength}{some text}}

• Excellent, thanks for the detailed explanation!
– Zubo
Jan 27, 2016 at 22:58