6

I want to stretch the length of every fraction bar a bit. I know I can define a new command like this:

\newcommand\newfrac[2]{\frac{\,#1\,}{\,#2\,}}

However, I want to set the longer-style fraction as default, so I can keep writing $\frac{}{}$ and get what I want.

I know that there are some commands related to this in the Appendix G in the TeXBook. However, this is hard for me to figure out how to. Need your help.

PS: I'm using 12pt, book class and mtpro2 math font with XeLaTeX.

2 Answers 2

8

Assuming you're using amsmath, where the definition

\DeclareRobustCommand{\frac}[2]{{\begingroup#1\endgroup\@@over#2}}

is found, just modify it; in your preamble add

\makeatletter
\DeclareRobustCommand{\frac}[2]{{\begingroup\,#1\,\endgroup\@@over\,#2\,}}
\makeatother

In math mode \, means \mskip\thinmuskip, so its value depends on the quad in the current math symbol font (and scales automatically with it).

6

You can use \let to copy the original command and than you can redefine it:

\let \originalfrac=\frac
\renewcommand{\frac}[2]{\originalfrac{\,#1\,}{\,#2\,}}

EDIT: better:

\let \originalfrac=\frac
\DeclareRobustCommand{\myfrac}[2]{\originalfrac{\,#1\,}{\,#2\,}} 
\let \frac=\myfrac

This differs from the first approach in 2 ways:

  • It defines a newcommand instead of renewing an existing one. Replacing the command happens with \let instead of with \renewcommand. Regarding why this is better, see the example below.
  • It makes the new command robust. As pointed out by Marco Daniel a simple \newcommand instead of \DeclareRobustCommand would cause double spacing in the table of contents if using this command in a section title.

With the first approach you will run into trouble when dealing with commands which do not do the job itself but call another command which does it for them, like for example commands with an optional parameter or with a starred version. Consider the following example:

\documentclass{article}

\newcommand{\test}[1][world]{hello #1}
    \show\test
    \expandafter\show\csname\string\test\endcsname

\let \originaltest=\test
\renewcommand{\test}[1][world]{\originaltest[\emph{#1}]}
    \show\test
    \show\originaltest
    \expandafter\show\csname\string\test\endcsname

\begin{document}
\ifx \test \originaltest
    true
\else
    false
\fi
\end{document}

Here, the definition of \test does not change because (leaving all of the protection stuff aside) it does nothing more than handling the optional parameter (which is the same in this example). The really interesting stuff, that you intended to copy, however, was saved in another command called \\test and was overridden by \renewcommand.

If you would try to expand \test now you would run into an infinite loop: \test would somehow expand to \\test which would expand to \originaltest[\emph{#1}] which would somehow expand to \\test again and so on.

6
  • By the way, will the actual length of \, differ in the different environment(e.g. in \large or in \tiny)? I'm afraid that the length of \, is too 'absolute', not flexible. But I just came up with this command in my mind, so I wonder if there are better one. :)
    – Eric
    Mar 11, 2017 at 11:28
  • 1
    Be careful with this solution. amsmath makes \fracrobust. So you can't use \let. I recommend the package letltxmacro: \usepackage{letltxmacro} \LetLtxMacro\originalfrac\frac \DeclareRobustCommand\frac[2]{\originalfrac{\,#1\,}{\,#2\,}} Mar 11, 2017 at 11:31
  • @MarcoDaniel I'm using amsmath, but I can write the command \let \originalfrac=\frac and pass the compiler. Why?
    – Eric
    Mar 11, 2017 at 11:35
  • @Eric \show\, shows that \, is defined as \tmspace +\thinmuskip {.1667em} (with amsmath loaded). Sounds like it does depend on the font size.
    – jakun
    Mar 11, 2017 at 11:41
  • @jakun: In most cases it will work. Here an example which shows the error: \documentclass{article} \usepackage{amsmath} %\let \originalfrac=\frac %renewcommand{\frac}[2]{\originalfrac{\,#1\,}{\,#2\,}} \usepackage{letltxmacro} \LetLtxMacro\originalfrac\frac \DeclareRobustCommand\frac[2]{\originalfrac{\,#1\,}{\,#2\,}} \begin{document} \tableofcontents \section{$\frac{\frac{a}{b}}{c}$} \end{document} Mar 11, 2017 at 11:53

You must log in to answer this question.

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