Difference between revisions of "Math/Display"

From Wiki
Jump to navigation Jump to search
(add {{cmd|startformulas}})
 
(20 intermediate revisions by the same user not shown)
Line 82: Line 82:
  
 
\startsubformulas[eq:total]
 
\startsubformulas[eq:total]
\placeformula
+
\startplaceformula
 
\startformula
 
\startformula
 
\startalign
 
\startalign
Line 90: Line 90:
 
\stopformula
 
\stopformula
 
\stopsubformulas
 
\stopsubformulas
 +
\stopplaceformula
  
 
Formula \eqref[eq:total] states the Pythagora's Theorem twice,
 
Formula \eqref[eq:total] states the Pythagora's Theorem twice,
Line 95: Line 96:
  
 
You can compare without \tex{startsubformulas}:
 
You can compare without \tex{startsubformulas}:
\placeformula
+
\startplaceformula
 
\startformula
 
\startformula
 
\startalign
 
\startalign
Line 102: Line 103:
 
\stopalign
 
\stopalign
 
\stopformula
 
\stopformula
 +
\stopplaceformula
  
 
We now have references for \eqref[eq2:first] and \eqref[eq2:second].
 
We now have references for \eqref[eq2:first] and \eqref[eq2:second].
 +
</context>
 +
 +
 +
= Combination of formulae with {{cmd|startformulas}} =
 +
 +
To use subformula numbering, you have to use {{cmd|startsubformulas}}/{{cmd|stopsubformulas}} and {{cmd|startalign}}. For example
 +
 +
<context source="yes" text="This, when typeset, produces the following:">
 +
\definereferenceformat[eqref][left=(,right=)]
 +
 +
The famous result (once more) is given by
 +
\startsubformulas[eq:total]
 +
\startplaceformula
 +
\startformulas
 +
 +
\startformula
 +
\startalign
 +
\NC a^2 + b^2 \NC = c^2 \NR[eq:first]
 +
\NC a^2 + b^2 \NC = c^2 \NR[eq:second]
 +
\stopalign
 +
\stopformula
 +
 +
 +
\startformula
 +
\startalign
 +
\NC a^2 + b^2 \NC = c^2 \NR[eq:third]
 +
\NC a^2 + b^2 \NC = c^2 \NR[eq:forth]
 +
\stopalign
 +
\stopformula
 +
 +
\stopformulas
 +
\stopplaceformula
 +
\stopsubformulas
 +
 +
We now have references for \eqref[eq:forth] for example.
 +
</context>
 +
 +
= List of Formulae with {{cmd|placelist}} =
 +
 +
Since MKIV there are two types of formulas: numbered and named. Numbered formulas have the number as a reference and caption next to the formula and are listed only with the number and pagenumber in the list of formulas. Named formulas use the name as the caption instead of a number and are referenced with the name and pagenumber in the list of formulas.
 +
 +
<context source=yes>
 +
2 examples:
 +
 +
% no reference        ==> no entry in the list of formulas
 +
\startplaceformula
 +
\startformula e=mc^2 \stopformula
 +
\stopplaceformula
 +
 +
% a reference        ==> entry in the list of formulas
 +
\startplaceformula
 +
  [reference=formula:mass1]              %  <====== here
 +
\startformula e=mc^2 \stopformula
 +
\stopplaceformula
 +
 +
This is the list:
 +
 +
\placelist[formula][criterium=all,alternative=c]
 +
</context>
 +
 +
{{cmd|startplaceformula}} comes with the following key options: bookmark, list, reference, suffix, title. The title option changes the formula to a named formula (no number).
 +
 +
<context source=yes>
 +
3 examples:
 +
 +
% a reference + list  ==> entry with number + additional text in the list
 +
\startplaceformula
 +
  [reference=formula:mass2,
 +
  list={Mass-energy equivalence from list option}]
 +
\startformula e=mc^2 \stopformula
 +
\stopplaceformula
 +
 +
% a reference + title  ==> entry with title text, no number
 +
\startplaceformula
 +
  [title={Mass-energy equivalence},    %  <====== here
 +
  reference=formula:mass3]
 +
\startformula e=mc^2 \stopformula
 +
\stopplaceformula
 +
 +
% a reference + title + list ==> entry with list text, no number
 +
\startplaceformula
 +
  [title={Mass-energy equivalence},    %  <====== here
 +
  reference=formula:mass4,
 +
  list={Mass-energy equivalence from list option}]
 +
\startformula e=mc^2 \stopformula
 +
\stopplaceformula
 +
 +
This is the list:
 +
 +
\placelist[formula][criterium=all,alternative=c]
 
</context>
 
</context>
  
 
= Formating with {{cmd|setupformulas}} =
 
= Formating with {{cmd|setupformulas}} =
  
== Changing format of numbers ==
+
== Format of numbers ==
 
You can use {{cmd|setupformulas}} to change the format of numbers. For example to get bold numbers inside square brackets use
 
You can use {{cmd|setupformulas}} to change the format of numbers. For example to get bold numbers inside square brackets use
  
Line 168: Line 260:
 
</context>
 
</context>
  
== Changing Formula alignment ==
+
== Formula alignment ==
  
 
Normally a formula is centered, but in case you want to align it left or right, you can set up formulas to behave
 
Normally a formula is centered, but in case you want to align it left or right, you can set up formulas to behave
Line 296: Line 388:
 
</context>
 
</context>
  
 
+
== Shaded background for part of a displayed equation ==
=List of Formulas=
 
 
 
You can have a list of the formulas contained in a document by using {{cmd|placenamedformula}} instead of {{cmd|placeformula}}. Only the formulas written with {{cmd|placenamedformula}} are not put in the list, so that you can control precisely the content of the list.
 
 
 
{{cmd|placenamedformula}} takes as first parameter the name of the formula put in the list. The other {{cmd|placeformula}} features are still available. The list can be formatted like any other list.
 
 
 
Example:
 
<texcode>
 
\subsubject{List of Formulas}
 
\placelist[formula][criterium=text,alternative=c]
 
 
\subsubject{Formulas}
 
\placenamedformula[one]{First listed Formula}
 
\startformula a = 1 \stopformula \endgraf
 
 
 
\placeformula
 
\startformula a = 2 \stopformula \endgraf
 
 
 
\placenamedformula{Second listed Formula}{b}
 
\startformula a = 3 \stopformula \endgraf
 
</texcode>
 
 
 
Gives:
 
 
 
<context>
 
\setuppapersize[A5]
 
\setuplayout[width=13cm]
 
\subsubject{List of Formulas}
 
\placelist[formula][criterium=text,alternative=c]
 
 
\subsubject{Formulas}
 
\placenamedformula[one]{First listed Formula}
 
\startformula a = 1 \stopformula \endgraf
 
 
 
\placeformula
 
\startformula a = 2 \stopformula \endgraf
 
 
 
\placenamedformula{Second listed Formula}{b}
 
\startformula a = 3 \stopformula \endgraf
 
</context>
 
 
 
== List of Formulas with MKIV/LMTX ==
 
Since MKIV there are two types of formulas: numbered and named. Numbered formulas have the number as a reference and caption next to the formula and are listed only with the number and pagenumber in the list of formulas. Named formulas use the name as the caption instead of a number and are referenced with the name and pagenumber in the list of formulas.
 
 
 
<context source=yes>
 
% equivalent to \placenamedformula[formula:mass]{Mass-energy equivalence}
 
\startplaceformula[title={Mass-energy equivalence},reference=formula:mass] \startformula e=mc^2 \stopformula\stopplaceformula
 
 
 
% equivalent to \placeformula[formula:mass] (note: placeformula is listed in the list of formulas if it has a reference)
 
\startplaceformula[reference=formula:mass] \startformula e=mc^2 \stopformula \stopplaceformula
 
 
 
% equivalent to \placeformula (with no entry in the list of formulas)
 
\startplaceformula \startformula e=mc^2 \stopformula \stopplaceformula
 
 
 
\placelist[formula][criterium=text,alternative=c]
 
</context>
 
 
 
{{cmd|startplaceformula}} comes with the following key options: bookmark, list, reference, suffix, title.
 
 
 
To name a numbered formula in the list of formulas, one can use the list option:
 
<context source=yes>
 
% named formula
 
\startplaceformula[reference=formula:mass,title={Mass-energy equivalence}] \startformula e=mc^2 \stopformula
 
\stopplaceformula
 
 
 
% numbered formula with list text, note without a reference option the formula is just a placeformula that is not listed in the list of formulas -- needs ConTeXt latest version 2021-03-21 or later
 
\startplaceformula[reference=formula:mass,list={Mass-energy equivalence}] \startformula e=mc^2 \stopformula \stopplaceformula
 
 
 
\placelist[formula][criterium=text,alternative=c]
 
</context>
 
 
 
Note: The title option changes the formula to a named formula and will use that name in the list of formulas regardless of the list option.
 
 
 
= Shaded background for part of a displayed equation =
 
 
(see also [[Framed]])
 
(see also [[Framed]])
  
To highlight part of a formula, you can give it a gray background using {{cmd|mframed}}: the following is the code you can use in mkii (see below what one has to do in mkiv):
+
One may define a specific framed command, for example <tt>coloredmath<tt>, with {{cmd|definemathframed}}.
  
 
<context source="yes">
 
<context source="yes">
\setuppapersize[A5]
+
\setuppapersize[A7,landscape]
\setupcolors[state=start]
+
\definemathframed[coloredmath]
\def\graymath{\mframed[frame=off,
 
    background=color,
 
    backgroundcolor=gray,
 
    backgroundoffset=3pt]}
 
 
 
\startformula
 
  \ln (1+x) =\, \graymath{x - {x^2\over2}} \,+ {x^3\over3}-\cdots.
 
\stopformula
 
</context>
 
 
 
In mkiv the code is slightly different: one may define {{cmd|graymath}} directly using {{cmd|definemathframed}}
 
 
 
<texcode>
 
\setuppapersize[A5]
 
\definemathframed[graymath]
 
 
[
 
[
 
frame=off,
 
frame=off,
 
location=mathematics,
 
location=mathematics,
 
background=color,
 
background=color,
backgroundcolor=lightgray,
+
backgroundcolor=lightcyan,
backgroundoffset=2pt
+
backgroundoffset=1pt
 
]
 
]
 
\starttext
 
\starttext
 
Since for $|x| < 1$ we have
 
Since for $|x| < 1$ we have
 
\startformula
 
\startformula
\log(1+x) = \graymath{x- \displaystyle{x^2\over2}} + {x^3 \over 3} + \cdots
+
\log(1+x) = \coloredmath{x- \displaystyle{x^2\over2}} + {x^3 \over 3} + \cdots
 
\stopformula
 
\stopformula
 
we may write $\log(1+x) = x + O(x^2)$.
 
we may write $\log(1+x) = x + O(x^2)$.
 
\stoptext
 
\stoptext
</texcode>
 
 
The result is shown below (possibly the framed part of the formula is not aligned correctly with the remainder of the formula because the mkiv engine on Context Garden is not up to date…).
 
 
<context mode=mkiv>
 
\setuppapersize[A5]
 
\definemathframed[graymath]
 
[
 
frame=off,
 
location=mathematics,
 
background=color,
 
backgroundcolor=lightgray,
 
backgroundoffset=2pt
 
]
 
 
Since for $|x| < 1$ we have
 
\startformula
 
\log(1+x) = \graymath{x- \displaystyle{x^2\over2}} + {x^3 \over 3} + \cdots
 
\stopformula
 
we may write $\log(1+x) = x + O(x^2)$.
 
 
</context>
 
</context>
  
 
[[Category:Math]]
 
[[Category:Math]]

Latest revision as of 13:14, 18 May 2024

First \startformula

Display math is enclosed in a \startformula / \stopformula pair. Thus

The famous result (once more) is given by
\startformula
c^2 = a^2 + b^2
\stopformula

This, when typeset, produces the following:

Numbering Formulae \startformula

ConTeXt provides an easy way to number the display maths equations. Simply, insert the \startformula in the \startplaceformula environment, and you will get numbered equations.

The famous result (once more) is given by
\startplaceformula
\startformula 
c^2 = a^2 + b^2 
\stopformula
\stopplaceformula

This, when typeset, produces the following:

The \startplaceformula command is optional, and produces the equation number; leaving it off produces an unnumbered equation.

Referencing formulae \in and \at

Equations can be referred to by simply adding a label to \startplaceformula and using command for referencing :

  • \ref (note that it expects two arguments, therefore you need the brackets twice),
  • \in (a kind of \ref[number][TheRef],
  • \at (a kind of \ref[page][TheRef].
\setupinteraction[state=start]
The famous result (once more) is given by

\startplaceformula[eq:myref]                  %   <===== here
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula

And now we can refer to its number by several ways,
\startitemize
\item with \tex{in} : formula \in[eq:myref] or \in{formula}{}[eq:myref] 
\item with \tex{at} : formula \in[eq:myref] \at{page}[eq:myref]
\item with \tex{ref} : formula \ref[number] [eq:myref] page \ref[number] [eq:myref]
\stopitemize

This, when typeset, produces the following:

By default, only the formula number appears as a reference. This can be changed by using \definereferenceformat. For example, to create a command \eqref which shows the formula number in brackets, use

\setupinteraction[state=start]

\definereferenceformat[eqref][left=(,right=)]

The famous result (once more) is given by
\startplaceformula[eq:myref]
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula

And now we can refer to its number by several ways,
\startitemize
\item with \tex{in} : formula \in[eq:myref] or \in{formula}{}[eq:myref],
\item with \tex{at} : formula \in[eq:myref] \at{page}[eq:myref],
\item with \tex{ref} : formula \ref[number] [eq:myref] page \ref[number] [eq:myref],
\item with \tex{eqref} : formula \eqref[eq:myref].
\stopitemize

This, when typeset, produces the following:


Sub-Formulae with \startsubformulas and \startalign

To use subformula numbering, you have to use \startsubformulas/\stopsubformulas and \startalign. For example

The famous result (once more) is given by

Examples:
\definereferenceformat[eqref][left=(,right=)]

\startsubformulas[eq:total]
\startplaceformula
\startformula
\startalign
\NC a^2 + b^2 \NC = c^2 \NR[eq:first]
\NC a^2 + b^2 \NC = c^2 \NR[eq:second]
\stopalign
\stopformula
\stopsubformulas
\stopplaceformula

Formula \eqref[eq:total] states the Pythagora's Theorem twice,
once in \eqref[eq:first] and again in \eqref[eq:second].

You can compare without \tex{startsubformulas}:
\startplaceformula
\startformula
\startalign
\NC a^2 + b^2 \NC = c^2 \NR[eq2:first]
\NC a^2 + b^2 \NC = c^2 \NR[eq2:second]
\stopalign
\stopformula
\stopplaceformula

We now have references for \eqref[eq2:first] and \eqref[eq2:second].

This, when typeset, produces the following:


Combination of formulae with \startformulas

To use subformula numbering, you have to use \startsubformulas/\stopsubformulas and \startalign. For example

\definereferenceformat[eqref][left=(,right=)]

The famous result (once more) is given by
\startsubformulas[eq:total]
\startplaceformula
\startformulas

\startformula
\startalign
\NC a^2 + b^2 \NC = c^2 \NR[eq:first]
\NC a^2 + b^2 \NC = c^2 \NR[eq:second]
\stopalign
\stopformula


\startformula
\startalign
\NC a^2 + b^2 \NC = c^2 \NR[eq:third]
\NC a^2 + b^2 \NC = c^2 \NR[eq:forth]
\stopalign
\stopformula

\stopformulas
\stopplaceformula
\stopsubformulas

We now have references for \eqref[eq:forth] for example.

This, when typeset, produces the following:

List of Formulae with \placelist

Since MKIV there are two types of formulas: numbered and named. Numbered formulas have the number as a reference and caption next to the formula and are listed only with the number and pagenumber in the list of formulas. Named formulas use the name as the caption instead of a number and are referenced with the name and pagenumber in the list of formulas.

2 examples:

% no reference        ==> no entry in the list of formulas
\startplaceformula
\startformula e=mc^2 \stopformula 
\stopplaceformula

% a reference         ==> entry in the list of formulas
\startplaceformula
  [reference=formula:mass1]              %   <====== here
\startformula e=mc^2 \stopformula 
\stopplaceformula

This is the list:

\placelist[formula][criterium=all,alternative=c]

\startplaceformula comes with the following key options: bookmark, list, reference, suffix, title. The title option changes the formula to a named formula (no number).

3 examples:

% a reference + list  ==> entry with number + additional text in the list
\startplaceformula
  [reference=formula:mass2,
   list={Mass-energy equivalence from list option}]
\startformula e=mc^2 \stopformula
\stopplaceformula

% a reference + title  ==> entry with title text, no number
\startplaceformula
  [title={Mass-energy equivalence},     %   <====== here
   reference=formula:mass3]
\startformula e=mc^2 \stopformula
\stopplaceformula

% a reference + title + list ==> entry with list text, no number
\startplaceformula
  [title={Mass-energy equivalence},     %   <====== here
   reference=formula:mass4,
   list={Mass-energy equivalence from list option}]
\startformula e=mc^2 \stopformula
\stopplaceformula

This is the list:

\placelist[formula][criterium=all,alternative=c]

Formating with \setupformulas

Format of numbers

You can use \setupformulas to change the format of numbers. For example to get bold numbers inside square brackets use

The famous result (once more) is given by
\setupformulas
  [left={[},
   right={]},
   numberstyle=bold]

\startplaceformula
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula

This, when typeset, produces the following:

To get equations also numbered by section, use the command \setupnumbering \setupnumbering[way=bysection] in the preamble of your document. This means that the section number preceeds the formula number. Add also \setupnumber \setupnumber[formula][way=bysection] to reset with each new section.

The famous result (once more) is given by
\setupformulas
  [left={[},
   right={]},
   numberstyle=bold]

\setupnumbering        [way=bysection] % to add the section number to the formula number
\setupnumber [formula] [way=bysection] % to reset with each new section

\startsection[title=First title]
\startplaceformula
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula
\stopsection

\startsection[title=Second title]
\startplaceformula
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula
\stopsection

This, when typeset, produces the following:

To get alphabets instead of numbers, use again \setupformulas

The famous result (once more) is given by
\setupformulas[numberconversion=Character]

\startplaceformula
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula

\setupformulas[numberconversion=character]

\startplaceformula
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula

This, when typeset, produces the following:

Formula alignment

Normally a formula is centered, but in case you want to align it left or right, you can set up formulas to behave that way. Normally a formula will adapt its left indentation to the environment:

 
This is a bit of text for purpose of example.
\startitemize
\item This is some other example text.
      \startplaceformula \startformula c^2 = a^2 + b^2 \stopformula \stopplaceformula
\item This is yet other example text.
\stopitemize
This is a bit more text for other purpose of example.


In the next examples we explicitly align formulas with \setupformulas, to the left (raggedleft), center (middle, and right (raggedright):

\setuppapersize[A5]
\setuplayout[textwidth=8cm]
\setupformulas[align=left]
\startformula c^2 = a^2 + b^2 \stopformula
\setupformulas[align=middle]
\startformula c^2 = a^2 + b^2 \stopformula
\setupformulas[align=right]
\startformula c^2 = a^2 + b^2 \stopformula

With formula numbers the code is:

\setuppapersize[A7,landscape]
\setuplayout[textwidth=8cm]
\starttext
\setupformulas[align=flushleft]
\startplaceformula
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula 

\setupformulas[align=middle]
\startplaceformula 
\startformula c^2 = a^2 + b^2 \stopformula 
\stopplaceformula 

\setupformulas[align=flushright]
\startplaceformula 
\startformula c^2 = a^2 + b^2 \stopformula 
\stopplaceformula

\stoptext

When tracing is turned on (\showboxes) you can visualize the bounding box of the formula,

The famous result (once more) is given by
\showboxes
\startplaceformula 
\startformula
c^2 = a^2 + b^2
\stopformula
\stopplaceformula

This, when typeset, produces the following:

As you can see, the dimensions are the natural ones, but if needed you can force a normalized line:

The famous result (once more) is given by
\showboxes
\setupformulas[align=middle,strut=yes]
\startplaceformula 
\startformula
c^2 = a^2 + b^2
\stopformula
\stopplaceformula

This time we get a more spacy result. [Ed. Note: For this example equation, there appears to be no visible change.]

You can influence the placement of the whole box with the parameters leftmargin and rightmargin.

The famous result (once more) is given by
\showboxes
\setupformulas[align=right,leftmargin=1em]
\startplaceformula 
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula 

\setupformulas[align=right,leftmargin=3em]
\startplaceformula 
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula 

\setupformulas[align=left,rightmargin=1em]
\startplaceformula 
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula 

You can also inherit the margin from the environment.

Some example text, again, to show where the right and left margins of the text block are.
\showboxes

\setupformulas[align=right]
\startplaceformula 
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula 

\setupformulas[align=right,margin=standard]
\startplaceformula 
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula 

The distance between the formula and the number is only applied when the formula is left or right aligned.

Some example text, again, to show where the right and left margins of the text block are.
\showboxes
\setupformulas[align=left,distance=2em]
\startplaceformula 
\startformula c^2 = a^2 + b^2 \stopformula
\stopplaceformula

Shaded background for part of a displayed equation

(see also Framed)

One may define a specific framed command, for example coloredmath, with \definemathframed.

\setuppapersize[A7,landscape]
\definemathframed[coloredmath]
	[
	frame=off,
	location=mathematics,
	background=color,
	backgroundcolor=lightcyan,
	backgroundoffset=1pt
	]
\starttext
Since for $|x| < 1$ we have
\startformula
\log(1+x) = \coloredmath{x- \displaystyle{x^2\over2}} + {x^3 \over 3} + \cdots
\stopformula
we may write $\log(1+x) = x + O(x^2)$.
\stoptext