I want to define a command that behaves differently for single-character arguments than it does for longer arguments. How do I retrieve the number of characters in, e.g., #1
?
The specific application that I have in mind is to set variable names in italic if they are single letters and otherwise to set them in Roman. Something like
\newcommand \mathvar [1] {
\ifthenelse {\characters{#1} = 1}
{\mathit{#1))
{\mathrm{#1}}
}
And, yes, I really mean the number of nonblank characters, not the size of the rendered text.
Note that I want to be able to submit to arXiv.org, and they do not support, e.g., LuaTeX, XeTeX. I'm currently using PDFlatex via MIKTeX, and don't know whether e-TeX is acceptable to arXiv; their current build is rather old.
I tried using xstring; since it doesn't support #, I had to resort to a hack:
\documentclass{article}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{ifthen}
\usepackage{xstring}
\newcommand {\funcname} [1]
{
\edef\Parameter1{#1}
\StrDel[0]{\Parameter1}{ }[\stripped]
\ifthenelse {\StrLen{\stripped} > 1}
{\mathrm{#1}}
{#1}
}
\begin{document}
Test StrDel \{ foo \} \{ \} = /\StrDel { foo } { }/
\edef\Parameter1{ foo }
\StrDel {\Parameter1} { }[\stripped]
Test StrLen StrDel \{ foo \} \{ \} = \StrLen{\stripped}
Test funcname T funcname Table = $\funcname{T} \funcname{Table}$
\end{document}
and that gave me
! Argument of \@xs@StrDel has an extra }.
<inserted text>
\par
l.25 ...funcname T funcname Table = $\funcname{T}
\funcname{Table}$