Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

As an extension of my earlier question Automatically generate new commands, I want to generate commands for Greek random variables as well, that is:

\usepackage{bm}
\newcommand{\myrv}[1]{\bm{#1}}
\newcommand{\rvepsilon}{\myrv{\epsilon}}
\newcommand{\rvnu}{\myrv{\nu}}

Unfortunately, the tricks in the answers to the earlier question don't work in this case. For example, when I tried

\documentclass{article}

\usepackage{bm}

% Notation for Greek random variables
\newcommand{\myrv}[1]{\bm{#1}}

\makeatletter
\newcommand*\defrvar[1]{
  \expandafter\newcommand\csname rv#1\endcsname[1][]{\myrv{#1}}}

\newcommand*\defrvars[1]{
  \@for\@i:=#1\do{\expandafter\defrvar\expandafter{\@i}}}
\makeatother

\defrvars{epsilon,nu}

\begin{document}
$\rvepsilon = 1$
\end{document}

The output is "epsilon = 1" instead of the desired output "ε = 1".

Is there a way to define the commands automatically by looping over a list, say {alpha,beta,gamma,...}?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

You're making a couple of mistakes:

\documentclass{article}

\usepackage{bm}

% Notation for Greek random variables
\newcommand{\myrv}[1]{\bm{#1}}

\makeatletter
\newcommand*\defrvar[1]{%
  \expandafter\newcommand\csname rv#1\endcsname{\myrv{\csname #1\endcsname}}}

\newcommand*\defrvars[1]{%
  \@for\@i:=#1\do{\expandafter\defrvar\expandafter{\@i}}%
}
\makeatother

\defrvars{epsilon,nu}

\begin{document}
$\rvepsilon = 1$
\end{document}
  1. The \rvepsilon command shouldn't have an optional argument
  2. You missed \csname...\endcsname, so \defrvar{epsilon} defined \rvepsilon to give \myrv{epsilon}; now it gives \myrv{\csname epsilon\endcsname}

The unavoidable expl3 version:

\documentclass{article}

\usepackage{bm}
\usepackage{xparse}

% Notation for Greek random variables
\NewDocumentCommand{\myrv}{m}{\bm{#1}}

\ExplSyntaxOn
\NewDocumentCommand{\defrvars}{ m }
 {
  \clist_map_inline:nn { #1 }
   {
    \cs_new:cpx { rv##1 } { \myrv { \exp_not:c { ##1 } } }
   }
 }
\ExplSyntaxOff

\defrvars{epsilon,nu}

\begin{document}
$\rvepsilon = 1$
\end{document}
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.