1

I want to create a macro that expands to some parameters to provide to another macro. Here is a very simple example:

\documentclass[]{article}
\begin{document}
  \newcommand{\parameters} { {2} {3} }
  \[
     \frac\parameters % \frac{2}{3}
   \] 

 \end{document}
  • 3
    You cannot. This is a macro expansion language, macro does not expand them self. In this case \expandafter\frac\parameters help. There are methods in LaTeX3 that can expand parameters automatically before use. – daleif Oct 16 '18 at 10:41
  • But you can use \newcommand{\twothirds}{\hbox{$\frac{2}{3}$}} – John Kormylo Oct 16 '18 at 10:48
1

You could use

\expandafter\frac\parameters

The problem is that \frac wants two arguments and looks for them without interpreting tokens (and discarding white space during such search).

In your case the first argument would be \parameters and the second one \], which obviously leads to the very wrong

\frac{\parameters}{\]}

that is then reduced to the more primitive

{\parameters\over\]}

Chaos ensues.

Let's implement a new command, namely

\newcommand{\FRAC}{\expandafter\frac}

so that \FRAC{2}{3} and \FRAC\parameters would produce the same result.

\documentclass{article}
\usepackage{amsmath}

\newcommand{\FRAC}{\expandafter\frac}

\newcommand{\parameters}{{2}{3}}

\begin{document}
\[
\FRAC{2}{3}=\FRAC\parameters
\]
\end{document}

enter image description here

This works because

\expandafter\frac\parameters

is translated first into \frac{2}{3}. On the other hand,

\expandafter\frac{2}{3}

will try to expand {, which isn't expandable.

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.