Text FormattingLength of String Variable

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
parnmatt
Posts: 2
Joined: Sun Oct 16, 2011 6:33 pm

Length of String Variable

Post by parnmatt »

Hi I'm trying to set a length of a variable I've defined.
Note: I'm writing this in a class file, therefore you may need to use \makeatletter and \makeatother in the appropriated places.

Code: Select all

\newcommand*{\@name}{}
\newcommand*{\name}[1]{\renewcommand*{\@name}{#1}}

\newlength{\nameLength}
\settowidth{\nameLength}{{\huge \@name}}
as you can see I'm making a variable, and a command to store the user's name in the variable.

\name{Matthew Parnell} sets the variable \@name to "Matthew Parnell".

Now what I'm trying to do, is to create a length \nameLength with the length of whatever the \@name variable holds, in 'huge' font-size.

Now my problem here is that it's setting the length to the length of the string "\@name" not the string the variable contains.

Currently I'm using \settowidth{\nameLength}{{\huge Matthew~Parnell}} to solve the problem for now; but as you can see, it's a temporary fix until a true solution is found.


Anyone have any ideas? …I can't seemed to find one elsewhere.

Thank You

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Length of String Variable

Post by cgnieder »

I can't verify that:

Code: Select all

\documentclass{article}
\begin{document}
\makeatletter
\newcommand*{\@name}{}
\newcommand*{\name}[1]{\renewcommand*{\@name}{#1}}
\newlength{\nameLength}

\name{Mustrum Ridcully}
\settowidth{\nameLength}{{\huge\@name}}
\the\nameLength

\settowidth{\nameLength}{{\huge Mustrum Ridcully}}
\the\nameLength

\name{Ponder Stibbons}
\settowidth{\nameLength}{{\huge\@name}}
\the\nameLength

\settowidth{\nameLength}{{\huge Ponder Stibbons}}
\the\nameLength
\makeatother

\end{document}
namelength.png
namelength.png (3.76 KiB) Viewed 2912 times
Regards
site moderator & package author
Post Reply