Difference between revisions of "Math/fractions"

From Wiki
Jump to navigation Jump to search
(Added information on binomials.)
 
 
(55 intermediate revisions by 3 users not shown)
Line 1: Line 1:
= Fractions, Binomials, genfrac, continued fractions =
+
= Fractions =
  
== Binomials ==
+
{{cmd|frac}} typeset fraction, according to the current style.
 +
Others commands enforce a certain style :
 +
* {{cmd|tfrac}} for text style
 +
* {{cmd|dfrac}} for display style
 +
* {{cmd|xfrac}} for script style.
 +
* {{cmd|sfrac}} and {{cmd|xxfrac}} for script of script style.
 +
* {{cmd|cfrac}} for continued style
 +
* {{cmd|vfrac}} for semi-horizontal style
 +
* {{cmd|hfrac}} for horizontal style
  
Binomials: Wolfgang explains (ntg-context list, 2011-12-04): Instead if \choose you can use \binom which is the recommended way in context. The \dbinom command sets the content in display style while \tbinom use textstyle.
 
  
<texcode>
+
<context source="yes">
\starttext
+
\starttabulate[|l|l|l|]
 +
\HL
 +
\NC {\bf Size or style} \NC {\bf Code}          \NC {\bf Typesetting} \NC\NR
 +
\HL
 +
\NC math                \NC \type{\frac{1}{3}}  \NC \m{\frac{1}{3}}  \NC\NR
 +
\NC display            \NC \type{\dfrac{1}{3}}  \NC \m{\dfrac{1}{3}}  \NC\NR
 +
\NC script              \NC \type{\xfrac{1}{3}}  \NC \m{\xfrac{1}{3}}  \NC\NR
 +
\NC scriptscript        \NC \type{\sfrac{1}{3}}  \NC \m{\sfrac{1}{3}}  \NC\NR
 +
\NC scriptscript        \NC \type{\xxfrac{1}{3}} \NC \m{\xxfrac{1}{3}} \NC\NR
 +
\NC semi-horizontal    \NC \type{\vfrac{1}{3}}  \NC \m{\vfrac{1}{3}}  \NC\NR
 +
\NC horizontal          \NC \type{\hfrac{1}{3}}  \NC \m{\hfrac{1}{3}}  \NC\NR
 +
\NC continued          \NC \type{\cfrac{1}{3}}  \NC \m{\cfrac{1}{3}}  \NC\NR
 +
\NC text                \NC \type{\tfrac{1}{3}}  \NC \m{\tfrac{1}{3}}  \NC\NR
 +
\stoptabulate
 +
 
 +
</context>
 +
 
 +
== Different behaviors for inline and display mode ==
 +
 
 +
<context source="yes">
 +
\startbuffer[frac]
 +
\frac{1}{2} + \dfrac{1}{2} - \sfrac{1}{2} - \xfrac{1}{2}  - \xxfrac{1}{2} + \vfrac{1}{2} + \hfrac{1}{2}  + \tfrac{1}{2}
 +
\stopbuffer
 +
 
 +
\framed[frame=off,align=normal]{%  <=== just to improve wiki rendering
 +
Each time, we display the different style:
 +
 
 +
With \tex{startformula} (display mode)
 +
\startformula
 +
\getbuffer[frac]
 +
\stopformula
 +
 
 +
With \tex{m} (inline mode): \m{\getbuffer[frac]}}
 +
</context>
 +
 
  
Using \tex{binom} in text $\binom{n}{\frac{n}{2}}$.
+
== Fractions of fractions ==
  
Using \tex{dbinom} in text $\dbinom{n}{\frac{n}{2}}$.
+
<context source="yes">
 +
\startbuffer[frac]
 +
\frac{1}{2} + \dfrac{1}{2} - \sfrac{1}{2} - \xfrac{1}{2}  - \xxfrac{1}{2} + \vfrac{1}{2} + \hfrac{1}{2} + \tfrac{1}{2}
 +
\stopbuffer
  
Using \tex{tbinom} in text $\tbinom{n}{\frac{n}{2}}$.
+
\framed[frame=off,align=normal]{%  <=== just to improve wiki rendering
 +
Now fraction of fractions:
  
 +
With \tex{startformula} (display mode)
 
\startformula
 
\startformula
\binom{n}{\frac{n}{2}}
+
  \frac
 +
    {\getbuffer[frac]}
 +
    {\getbuffer[frac]}
 
\stopformula
 
\stopformula
  
 +
 +
With \tex{m}  (inline mode) and \tex{frac}:
 +
\m{\frac
 +
  {\getbuffer[frac]}
 +
  {\getbuffer[frac]}}
 +
 +
With \tex{m}  (inline mode) and \tex{cfrac} (continued):
 +
\m{\cfrac
 +
  {\getbuffer[frac]}
 +
  {\getbuffer[frac]}}}
 +
</context>
 +
 +
== Formatting with {{cmd|definemathfraction}} ==
 +
 +
Just an example from [https://www.mail-archive.com/ntg-context@ntg.nl/msg69987.html mailing list]
 +
 +
<context source="yes">
 +
\startformula    \frac {a}  { \frac{b}{c} }  \stopformula
 +
 +
\definemathfraction
 +
  [wfrac]
 +
  [margin=.25em]
 +
 +
\startformula    \wfrac {a} { \frac{b}{c} }  \stopformula
 +
</context>
 +
 +
Just an second example from [https://www.mail-archive.com/ntg-context@ntg.nl/msg68782.html mailing list]
 +
 +
<context source="yes">
 +
\definemathfraction [myfrac] [mathstyle=script]
 +
\definemathfraction [myfracx][mathstyle=script,alternative=outer]
 +
 +
\framed[frame=off,align=normal]{%  <=== just to improve wiki rendering
 +
${a^{2^{2^2}}\over a}$
 +
$\frac{a^{2^{2^2}}}{a}$
 +
$\frac{a^{2}}{b}$
 +
$\myfrac{a^{2}}{b}$
 +
$\myfracx{a^{2}}{b}$}
 +
</context>
 +
 +
= Binomial coefficients =
 +
 +
To write binomial coefficients you use any of {{cmd|binom}}, {{cmd|tbinom}} or {{cmd|dbinom}} (the latter two if you want to force text style or display style respectively). These commands are defined as fractions in ConTeXt, and can be set up with
 +
{{cmd|setupmathfractions}}.
 +
 +
 +
Below we show some of the often used fonts, together with the default setting and the one just suggested. The default look of the binomial coefficients look good in several fonts, but for example not perfect in Latin Modern. You may want to play with {{cmd|definemathfraction}}.
 +
 +
 +
<context source="yes">
 +
\setuphead[chapter][align=middle]
 +
\setuphead[section][style=\bfa]
 +
\setuphead[subsection][style=\it]
 +
 +
\setuplayout[
 +
topspace=1in,
 +
bottomspace=1in,
 +
header=0pt,
 +
footer=0pt,
 +
backspace=1in,
 +
margin=0pt,
 +
width=fit,
 +
height=fit,
 +
]
 +
 +
\startbuffer
 +
\type{\binom{n}{k}}: $\binom{n}{k}$\par
 +
\type{\tbinom{n}{k}}: $\tbinom{n}{k}$\par
 +
\type{\dbinom{n}{k}}: $\dbinom{n}{k}$\par
 +
\stopbuffer
 +
 +
\starttext
 +
 +
\title{Binomial coefficients}
 +
 +
\startcolumns[n=2]
 +
 +
\subject{Default settings}
 +
 +
\subsubject{Latin Modern}
 +
\getbuffer
 +
 +
\switchtobodyfont[pagella]
 +
\subsubject{Pagella}
 +
\getbuffer
 +
 +
\switchtobodyfont[stixtwo]
 +
\subsubject{STIX Two}
 +
\getbuffer
 +
 +
%\switchtobodyfont[lucidaot]
 +
%\subsubject{Lucida}
 +
%\getbuffer
 +
 +
\column[yes]
 +
 +
\definemathfraction[binomial]  [binom]    [fences=parenthesis,rule=no]
 +
\definemathfraction[tbinomial] [binomial] [mathstyle=text]
 +
\definemathfraction[dbinomial] [binomial] [mathstyle=display]
 +
 +
\startbuffer
 +
\type{\binomial{n}{k}}: $\binomial{n}{k}$\par
 +
\type{\tbinomial{n}{k}}: $\tbinomial{n}{k}$\par
 +
\type{\dbinomial{n}{k}}: $\dbinomial{n}{k}$\par
 +
\stopbuffer
 +
 +
\switchtobodyfont[modern]
 +
\subject{\tex{definemathfraction}}
 +
\subsubject{Latin Modern}
 +
\getbuffer
 +
 +
\switchtobodyfont[pagella]
 +
\subsubject{Pagella}
 +
\getbuffer
 +
 +
\switchtobodyfont[stixtwo]
 +
\subsubject{STIX Two}
 +
\getbuffer
 +
 +
%\switchtobodyfont[lucidaot]
 +
%\subsubject{Lucida}
 +
%\getbuffer
 +
 +
\stopcolumns
 
\stoptext
 
\stoptext
</texcode>
+
 
 +
</context>
 +
 
 +
The result of the code above, with the standalone from 2017-09-05 is given below.
 +
 
 +
[[File:binomialcoefficients.png|700px]]

Latest revision as of 20:38, 15 May 2024

Fractions

\frac typeset fraction, according to the current style. Others commands enforce a certain style :


\starttabulate[|l|l|l|]
\HL
\NC {\bf Size or style} \NC {\bf Code}           \NC {\bf Typesetting} \NC\NR
\HL
\NC math                \NC \type{\frac{1}{3}}   \NC \m{\frac{1}{3}}   \NC\NR
\NC display             \NC \type{\dfrac{1}{3}}  \NC \m{\dfrac{1}{3}}  \NC\NR
\NC script              \NC \type{\xfrac{1}{3}}  \NC \m{\xfrac{1}{3}}  \NC\NR
\NC scriptscript        \NC \type{\sfrac{1}{3}}  \NC \m{\sfrac{1}{3}}  \NC\NR
\NC scriptscript        \NC \type{\xxfrac{1}{3}} \NC \m{\xxfrac{1}{3}} \NC\NR
\NC semi-horizontal     \NC \type{\vfrac{1}{3}}  \NC \m{\vfrac{1}{3}}  \NC\NR
\NC horizontal          \NC \type{\hfrac{1}{3}}  \NC \m{\hfrac{1}{3}}  \NC\NR
\NC continued           \NC \type{\cfrac{1}{3}}  \NC \m{\cfrac{1}{3}}  \NC\NR
\NC text                \NC \type{\tfrac{1}{3}}  \NC \m{\tfrac{1}{3}}  \NC\NR
\stoptabulate

Different behaviors for inline and display mode

\startbuffer[frac]
\frac{1}{2} + \dfrac{1}{2} - \sfrac{1}{2} - \xfrac{1}{2}  - \xxfrac{1}{2} + \vfrac{1}{2} + \hfrac{1}{2}  + \tfrac{1}{2}
\stopbuffer

\framed[frame=off,align=normal]{%   <=== just to improve wiki rendering
Each time, we display the different style:

With \tex{startformula} (display mode)
\startformula
\getbuffer[frac]
\stopformula

With \tex{m} (inline mode): \m{\getbuffer[frac]}}


Fractions of fractions

\startbuffer[frac]
\frac{1}{2} + \dfrac{1}{2} - \sfrac{1}{2} - \xfrac{1}{2}  - \xxfrac{1}{2} + \vfrac{1}{2} + \hfrac{1}{2}  + \tfrac{1}{2}
\stopbuffer

\framed[frame=off,align=normal]{%   <=== just to improve wiki rendering
Now fraction of fractions:

With \tex{startformula} (display mode)
\startformula
  \frac
    {\getbuffer[frac]}
    {\getbuffer[frac]}
\stopformula


With \tex{m}  (inline mode) and \tex{frac}:
\m{\frac
  {\getbuffer[frac]}
  {\getbuffer[frac]}}

With \tex{m}  (inline mode) and \tex{cfrac} (continued):
\m{\cfrac
  {\getbuffer[frac]}
  {\getbuffer[frac]}}}

Formatting with \definemathfraction

Just an example from mailing list

\startformula    \frac {a}  { \frac{b}{c} }   \stopformula

\definemathfraction
  [wfrac]
  [margin=.25em]

\startformula    \wfrac {a} { \frac{b}{c} }   \stopformula

Just an second example from mailing list

\definemathfraction [myfrac] [mathstyle=script]
\definemathfraction [myfracx][mathstyle=script,alternative=outer]

\framed[frame=off,align=normal]{%   <=== just to improve wiki rendering
${a^{2^{2^2}}\over a}$
$\frac{a^{2^{2^2}}}{a}$
$\frac{a^{2}}{b}$
$\myfrac{a^{2}}{b}$
$\myfracx{a^{2}}{b}$}

Binomial coefficients

To write binomial coefficients you use any of \binom, \tbinom or \dbinom (the latter two if you want to force text style or display style respectively). These commands are defined as fractions in ConTeXt, and can be set up with \setupmathfractions.


Below we show some of the often used fonts, together with the default setting and the one just suggested. The default look of the binomial coefficients look good in several fonts, but for example not perfect in Latin Modern. You may want to play with \definemathfraction.


\setuphead[chapter][align=middle]
\setuphead[section][style=\bfa]
\setuphead[subsection][style=\it]

\setuplayout[
topspace=1in,
bottomspace=1in,
header=0pt,
footer=0pt,
backspace=1in,
margin=0pt,
width=fit,
height=fit,
]

\startbuffer
\type{\binom{n}{k}}: $\binom{n}{k}$\par
\type{\tbinom{n}{k}}: $\tbinom{n}{k}$\par
\type{\dbinom{n}{k}}: $\dbinom{n}{k}$\par
\stopbuffer

\starttext

\title{Binomial coefficients}

\startcolumns[n=2]

\subject{Default settings}

\subsubject{Latin Modern}
\getbuffer

\switchtobodyfont[pagella]
\subsubject{Pagella}
\getbuffer

\switchtobodyfont[stixtwo]
\subsubject{STIX Two}
\getbuffer

%\switchtobodyfont[lucidaot]
%\subsubject{Lucida}
%\getbuffer

\column[yes]

\definemathfraction[binomial]  [binom]    [fences=parenthesis,rule=no]
\definemathfraction[tbinomial] [binomial] [mathstyle=text]
\definemathfraction[dbinomial] [binomial] [mathstyle=display]

\startbuffer
\type{\binomial{n}{k}}: $\binomial{n}{k}$\par
\type{\tbinomial{n}{k}}: $\tbinomial{n}{k}$\par
\type{\dbinomial{n}{k}}: $\dbinomial{n}{k}$\par
\stopbuffer

\switchtobodyfont[modern]
\subject{\tex{definemathfraction}}
\subsubject{Latin Modern}
\getbuffer

\switchtobodyfont[pagella]
\subsubject{Pagella}
\getbuffer

\switchtobodyfont[stixtwo]
\subsubject{STIX Two}
\getbuffer

%\switchtobodyfont[lucidaot]
%\subsubject{Lucida}
%\getbuffer

\stopcolumns
\stoptext

The result of the code above, with the standalone from 2017-09-05 is given below.

binomialcoefficients.png