7

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,...}?

7

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}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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