3

Instead of writting very long lines to create complex equations it would be great to use variables to substitute smaller chunks...

A simple example:

\[
\frac{ \sqrt{ \mu(i)^{ \frac{3}{2}} (i^{2} -1) } }
{ \sqrt[3]{\rho(i) - 2} + \sqrt[3]{\rho(i) - 1} }
\]

It would be done something like this:

A = \sqrt{ \mu(i)^{ \frac{3}{2}} (i^{2} -1) }
B = \sqrt[3]{\rho(i) - 2} 
C = \sqrt[3]{\rho(i) - 1} 
\[
\frac{ A } { B + C }
\]

And the result would be the same but the code easier to read with more complex examples.

How can I define easily these substitutions or parameters? or using any specialized package?

PD: there are other questions at Tex.StackExchange asking how to split an equation in several lines, but this is not the same.

1 Answer 1

4

You sure can, because TeX is a macro expansion language:

\[
\newcommand\cA{\sqrt{ \mu(i)^{ \frac{3}{2}} (i^{2} -1) }}
\newcommand\cB{\sqrt[3]{\rho(i) - 2}}
\newcommand\cC{\sqrt[3]{\rho(i) - 1}}
%
\frac{ \cA } { \cB + \cC }
\]

I prefer using a prefix for the “chunks” in order not to clobber already defined macros (you'd be warned anyway, because of \newcommand).

Since \[...\] forms a group, those macros will become undefined at the end of the display.

A different option, with math active characters (but more risky):

\documentclass{article}
\usepackage{amsmath}

\newcommand{\chunk}[2]{%
  % #1 is a letter, #2 the replacement text
  \begingroup\lccode`~=`#1\lowercase{\endgroup\def~}{#2}%
  \mathcode`#1="8000
}

\newenvironment{abbrev}{}{}

\begin{document}

\[
\chunk{A}{\sqrt{ \mu(i)^{ \frac{3}{2}} (i^{2} -1) }}
\chunk{B}{\sqrt[3]{\rho(i) - 2}}
\chunk{C}{\sqrt[3]{\rho(i) - 1}}
%
\frac{ A } { B + C }
\]

\begin{abbrev}% defining \chunk in align wouldn't work
\chunk{A}{a+b}
\chunk{B}{c+d}
\begin{align}
x &= A\\
y &= B
\end{align}
\end{abbrev}

\end{document}

enter image description here

3
  • Thanks. I didn't thought "newcommand" was used here because I don't want to define a "new command". I'll try. Any other option?
    – skan
    Commented Sep 25, 2015 at 11:03
  • @skan Do you like the addition?
    – egreg
    Commented Sep 25, 2015 at 11:13
  • Yes, thanks, I won't even wait for other replies. I prefer the first answer, the second one makes it much more complex unnecessarily. When I said a different approach I meant different commands or maybe an specialized package.
    – skan
    Commented Sep 25, 2015 at 11:39

You must log in to answer this question.

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