[Tex/LaTex] Extending the faktor package

math-modepackage-writing

I'm writing a script (topic ring theory) where I have to set lots of quotients of algebraic objects. Usually I simply write M/N. However there are some cases where this is no easy to read (M+N/N or such). So I want to set the first object a bit higher and the second a bit lower. I found the faktor package which does this. However faktor uses \diagup in a fixed size. For larger expressions a larger one seems suitable. Do you have any idea how I can change the source code of the package so that

  1. I have two commands (one with a small and one with a large slash) or better
  2. the \diagup has automatically the right size.

I also tried xfrac. First I ran into this message:

This is a LaTeX bug: check coding!
! Command name `\if_num:w' already defined! Current meaning: \ifnum.
\msg_kernel_bug:x ...oding!}\tex_errmessage:D {#1}

A comment at Debians bug tracking system suggested to exchange mathtools and xfrac. This worked on my system. However the formulas inside a gather*-environment look too small to me:
example with \sfrac

The font size here seems smaller than at inline text. Thatswhy I wanted to try faktor.

I noticed another disadvantage with xfrac. My document needs a lot more time to compile. The initial revision took ~13 sec to compile (pdflatex) and with xfrac it took ~1300 secs or nearly half an hour.

Best Answer

In essence, the faktor package defines the command \faktor in the following way (let's call it \newfaktor):

\newcommand*{\newfaktor}[2]{% \newfaktor{#1}{#2} -> #1/#2
  \raisebox{0.5\height}{\ensuremath{#1}}% Numerator
  \mkern-5mu\diagup\mkern-4mu% Slash /
  \raisebox{-0.5\height}{\ensuremath{#2}}% Denominator
}

Here is the similarity between \faktor and \newfaktor:

\faktor vs \newfaktor

In order to satisfy the first request - to modifying the height/depth to which the numerator/denominator is raised/dropped - you could define some extra optional parameters for \newfaktor. And, for the sake of being complete, the xparse package provides a convenient way of intermixing mandatory {} and optional [] arguments:

\usepackage{xparse}%
...
\DeclareDocumentCommand{\newfaktor}{m O{0.5} m O{-0.5}}{% \newfaktor{#1}[#2]{#3}[#4] -> #1/#3
  \raisebox{#2\height}{\ensuremath{#1}}% Numerator
  \mkern-5mu\diagup\mkern-4mu% Slash /
  \raisebox{#4\height}{\ensuremath{#3}}% Denominator
}

This defines the command \newfaktor{#1}[#2]{#3}[#4] where #2 and #4 have defaults of 0.5 and -0.5 respectively, and are optional. These optional arguments specify the fractional height/depth to which the numerator #1 and denominator #3 are raised/dropped. That is, specifying 0.5 for #2 raises #1 by half of its regular height; specifying -1 for #4 drops #3 by its own height. For example:

Different new faktor styles

In the above image, the first four uses of \newfaktor yield a typesetting equivalent to \faktor.

In order to satisfy the second request - \diagup scales automatically with respect to its arguments - we have to go a different route. The reason for this is that scaling \diagup happens in 2 dimensions, thereby making \diagup fatter. See, for example, the difference between:

Scaling of \diagup

So, instead, I went with stretching a \rule, built into the following macro:

\DeclareDocumentCommand{\newfaktor}{s m O{0.5} m O{-0.5}}{% \newfaktor[*]{#2}[#3]{#4}[#5] -> #2/#4
  \setbox0=\hbox{\ensuremath{#2}}% Store numerator
  \setbox1=\hbox{\ensuremath{\diagup}}% Store slash /
  \setbox2=\hbox{\ensuremath{#4}}% Store denominator
  \raisebox{#3\ht1}{\usebox0}% Numerator
  \mkern-5mu\ifthenelse{\equal{#1}{\BooleanTrue}}% Slash /
    {\diagup}% regular \faktor slash
    {\rotatebox{-44}{\rule[#5\ht2]{0.4pt}{-#5\ht2+#3\ht0+\ht0}}}% tilted rule as a slash
  \mkern-4mu%
  \raisebox{#5\ht2}{\usebox2}% Denominator
}

This creates \newfaktor just like before, but now with an optional *. The starred version \newfaktor* reverts to the older definition, using \diagup as the slanted fraction, while the unstarred \newfaktor draws a \rule and tilts it into place. Consider the differences:

Modified \newfaktor with sizable slash

Longer equations using starred/unstarred \newfaktor

Of course, if these definitions are entirely satisfaktory(!), you can change them to your liking.

Related Question