# Best Practice for creating user-defined command names

I want to let the users of a package define the name of command into which some unique content will be stuffed, so that they can then recall that content using the command name that they prefer. Is it a better practice to make them provide me a string, from which I create the command and stuff the content (as in \tokify in my MWE) or is it preferable to let them tell me the actual (as yet undefined) command name and merely have me stuff the content (as in \populate in my MWE)?

The MWE is simple enough that you might say "just let them create the \def", but for the actual application, it will save them more keystrokes to let me create the content of the command.

\documentclass{article}
\usepackage{stringstrings}
\makeatletter
\newcommand\tokify[1]{%
\expandafter\def\csname#1\endcsname{Content: #1}%
}
\newcommand\populate[1]{%
\edef\sv@name{\expandafter\@gobble\detokenize{#1}}%
\noblanks[q]{\sv@name}%
\expandafter\def\csname\thestring\endcsname{Content: \thestring}%
}
\makeatother
\begin{document}

\tokify{xyz}
\xyz

\populate{\abc}
\abc

\end{document}


I would use whichever you think more natural but use the same form for declaration and use.

So LaTeX counters use a name form

\newcounter{foo}
\arabic{foo}
\setcounter{foo}{1}


but LaTeX lengths use a command form:

\newlength\foo
\setlength\foo{3pt}


There isn't really any logical reason why these are different but at least they are individually consistent.

Similarly of course commands which use the command form

\newcommand\foo{..}
\foo


and environments which use a name

\newenviroment{foo}{..}{..}
\begin{foo}


So I would not have

\tokify{xyz}
\xyz


but

\tokify{xyz}
\usething{xyz}


is OK.

But in that case I would not use \csname#1\endcsname use \csname my-#1\endcsname or some such to avoid accidentally clashing with stuff (cf counters which use c@...)

• I infer an emphatic "use the same form for declaration and use." I'm offline for a few days but I'll check back Monday to see if other considerations arise, and make an upvote next week. Cheers. – Steven B. Segletes Apr 19 '13 at 15:54