# A macro that expands to the length of its argument

If I want to anonymise a paper I've written for review, I don't just want to delete my name from the author field. I want to replace my name with a rule as tall as a capital letter and as long as my name would be. This way, it looks like my name is being censored out of history. (I don't really do this...) Here's an MWE:

\documentclass[12pt]{article}
\usepackage{lipsum}
\newlength\namelength
\title{A paper with no author}
\author{\rule{\namelength}{12pt}}
\begin{document}
\maketitle
\lipsum
\end{document}


Now here's the question. How would I define a \lengthof macro that looks at its argument and then expands to that length? Since you can't put a \settowidth inside a \rule (or it didn't work when I tried it), this isn't as trivial as I thought...

Is there a way to do this? Or a good reason why there isn't a good way to do this?

-
Strictly speaking, the macro won't expand to the length; at least I don't think this can be done in an expandable way. But this is merely a TeXnical point on the meaning of the word "expand". –  Hendrik Vogt Mar 14 '11 at 15:42
So basically you want a \phantom which is filled. –  Martin Scharrer Mar 14 '11 at 16:08
The following is probably overkill, but: If you really want to be anonymous, then you don't want to replace your name with anything that tells the reader how much space your name would occupy if it were printed. To avoid giving hints about your name, you'd want something independent of the length (or height, or depth). That is, just leave a blank, or the word "Hidden", or whatever. –  Phil Hirschhorn Mar 14 '11 at 17:47

First let me answer the question in the title: You can't expand a macro to the width/length of its argument because you need assignments for this.

The reason why \widthof works in the argument of \rule in the other answers (which are all quiet good), is that it uses the calc version of \setlength which parses its argument and doesn't require expandability.

So, I assume you probably don't mind that and only want to get the width of some text. All the mentioned macro simply put the argument into a TeX box and use \ht, \dp or \wd on it to get the height, depth and width, respectively. If you need more than one of these it would be more efficient to box the content by yourself (see Herbert updated answer).

Here my solution of modifying the normal \phantom macro to produce a rule instead of the usual empty box. The advantage here is that it re-uses the existing \phantom code to also work correctly in math-mode.

\documentclass{article}

\makeatletter
\def\rulephantom#1{%
\begingroup
\def\finph@nt{\vrule\@width\wd\z@\@height\ht\z@\@depth\dp\z@}%
\phantom{#1}%
\endgroup
}
\makeatother

\parindent=0pt
\begin{document}

Text

\rulephantom{Text} Text

\rulephantom{Text\strut} Text

$\rulephantom{Text} Text$

\end{document}

-
\documentclass[12pt]{article}
\usepackage{calc}
\usepackage{lipsum}
\newcommand*{\anonauthor}[1]{\rule{\widthof{#1}}{\totalheightof{#1}}}
\title{A paper with no author}
\begin{document}
\maketitle
\lipsum
\end{document}


EDIT: Herbert was faster. However, \totalheigthof will make the height of the rule scale with the basic font size (if that's important to you).

EDIT 2: An (inefficient?) macro variant with correct vertical alignment:

\newcommand*{\anonauthor}[1]{\rule[\depthof{#1}*{-1}]{\widthof{#1}}{\totalheightof{#1}}}

-
You could also use \ht\strutbox for the height to get a font size dependent height. This avoids re-evaluating the argument twice. Also \rule[-\dp\strutbox]{\widthof{#1}}{\dimexpr\ht\strutbox+\dp\strutbox\relax} would also take the depth into account. (The \dimexpr and \relax are actually optional when calc is loaded) –  Martin Scharrer Mar 14 '11 at 15:52
@Martin: I'm not experienced enough to write efficient code. ;-) As for the depth, "totalheight" is the sum of height and depth. –  lockstep Mar 14 '11 at 16:03
I know, but you make the rule as high as the totalheight of the original text but with zero depth! In other words the vertical alignment of the resulting rule is wrong in your case. –  Martin Scharrer Mar 14 '11 at 16:05
@Martin: I see ... the optional argument of \rule adjusts the vertical positioning. –  lockstep Mar 14 '11 at 16:07

It may be overkill in your situation, but there's also the censor package which produces these bars with the \censor command:

\documentclass[12pt]{article}
\usepackage{lipsum}
\usepackage{censor}
\title{A paper with no author}
\begin{document}
\maketitle
\lipsum
\end{document}

-
Damn! I was going to write that package. But it already exists... –  Seamus Mar 14 '11 at 17:01
The censor package has been upgraded to allow for redacting figure and table contents, as well (i.e., things storable in a LaTeX box). –  Steven B. Segletes Feb 19 '13 at 16:46
\documentclass[12pt]{article}
\usepackage{calc}
\newsavebox\TBox
\def\phantomrule#1{\sbox\TBox{#1}\rule[-\dp\TBox]{\wd\TBox}{\ht\TBox+\dp\TBox}}

\title{A paper with no author}