TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a document that makes heavy use of predefined symbols (in order to later change them), i.e.:

\newcommand{\term}{t}
...

Now I need to introduce different annotated variants of the symbols:

\newcommand{\aterm}{\term^{\alpha}}
\newcommand{\bterm}{\term^{\beta}}
...

It is quite tedious to manually manage these sets (especially when adding a new symbol or removing an obsolete one).

So I naturally wonder, how one would define multiple (differently prefixed) arguments in one batch? Due to the macro capabilities of TeX, this is should certainly be possible, but is there a safe and convenient way?

i.e. is there some "meta"-newcommand that works somewhat like this:

\metanewcommand{\declaresymbols}[2]{\newcommand{\#1term}{t^{#2}} ... }

edit: Thanks to Steven's comment below, I was able to implement what I need myself:

\newcommand{\declaresymbol}[4]{
  %introduce a symbol \#3 -> #4 prefixed with #1 and annotated with #2
  \ifthenelse{\equal{#1}{}}{
    \expandafter\newcommand\csname#3\endcsname{#4}
  } {
    \expandafter\newcommand\csname#1#3\endcsname{#4‌​^{#2}}
  }
}

\newcommand{\declaresymbols}[2]{
  \declaresymbol{#1}{#2}{val}{v}
  \declaresymbol{#1}{#2}{term}{t}
}

% Standard symbol, no variant
\declaresymbols{}{} %gives \term -> t and \val -> v
% Alpha variant
\declaresymbols{a}{\alpha} %gives \aterm -> t^\alpha \aval -> v^\alpha
share|improve this question
1  
\newcommand{\declaresymbols}[2]{\expandafter\newcommand\csna‌​me#1term\endcsname{t‌​^{#2}}}. Of course, \declaresymbols{a}{\alpha} is as hard to type as \newcommand{\aterm}{\term^{\alpha}} – Steven B. Segletes 4 hours ago
    
Could you make this an answer? The \csname stuff was precisely the pointer, I needed. – choeger 4 hours ago
up vote 4 down vote accepted

What you are looking for is the plain-TeX directive \csname...\endcsname (cs for "control sequence"). Whereas the \<macro-name> syntax requires that the macro name be a predefined sequence of catcode 11 letters, what goes between the \csname...\endcsname delimiters can include symbols of other catcodes, and can include variables evaluated at the time of invocation.

The only trick, beyond this, is to use \expandafter prior to the defining \newcommand, so that the \csname gets expanded into the actual control sequence.

\documentclass{article}
\newcommand{\declaresymbols}[2]{\expandafter\newcommand\csname#1term\endcsname{t^{#2}}}
\begin{document}
\declaresymbols{a}{\alpha}
\declaresymbols{b}{\beta}

$\aterm \ne \bterm$
\end{document}

enter image description here

If you do a lot of work in the cs domain of programming, packages like etoolbox are extremely useful, providing whole families of macros for working with this tricky syntax in more natural ways.

share|improve this answer

You can do with

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand\definesymbol{omm}
 {
  \IfNoValueTF{#1}
   { \cs_new:cpn {#2} { #3 } }
   { \cs_new:cpn { #2 #1 } { \use:c { #1 } \sp { #3 } } }
 }
\ExplSyntaxOff

\definesymbol{term}{t}
\definesymbol[term]{a}{\alpha}
\definesymbol[term]{b}{\beta}

\definesymbol{X}{X}
\definesymbol[X]{a}{a}

\begin{document}

$\term+\aterm+\bterm+\X+\aX$

\end{document}

The call without the optional argument defines the main macro, with the optional argument it adds the superscripts and the prefix.

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.