12

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}

enter image description here

1 Answer 1

10

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@...)

1
  • 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. Commented Apr 19, 2013 at 15:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .