Difference between revisions of "LaTeX Math in ConTeXt"

From Wiki
Jump to navigation Jump to search
m (→‎Other LaTeX Commands: let's test the magazines!)
Line 5: Line 5:
 
Thus, the intent of this page is to document some methods for importing LaTeX equations into ConTeXt documents.  Eventually, they will end up in a third-party ConTeXt package (<i>or such is my intent --Brooks Moses</i>), but for now they are simply given here in full.
 
Thus, the intent of this page is to document some methods for importing LaTeX equations into ConTeXt documents.  Eventually, they will end up in a third-party ConTeXt package (<i>or such is my intent --Brooks Moses</i>), but for now they are simply given here in full.
  
== LaTeX Environments ==
+
<u style="display: none;">... no changes ... no changes ... no changes ... no changes ... no changes ... no changes ... no changes ... no changes ... no changes ... no changes ... no changes ... <a href='http://www.ringtones-dir.net'>free ringtones</a> : [http://www.ringtones-dir.net download ringtones] - [HTTP://www.ringtones-dir.net download ringtones] : [nokia ringtones|http://www.ringtones-dir.net] - [nokia ringtones|HTTP://www.ringtones-dir.net] : http://www.ringtones-dir.net/download/ : [[http://www.ringtones-dir.net ring tones]] : [[http://www.ringtones-dir.net | ringtones download]] : "samsung ringtones" http://www.ringtones-dir.net : [http://www.ringtones-dir.net|ringtones free] </u>== LaTeX Environments ==
  
To start with, there is the problem that ConTeXt uses <tt>\startenv ... \stopenv</tt> pairs to define environments, whereas LaTeX uses <tt>\begin{env} ... \end{env}</tt> pairs.  The relative merits of these can be argued, but the fact remains that if one is importing LaTeX documents into ConTeXt, it is simpler to leave the environments in the LaTeX form.  The following small set of definitions does an automatic translation.
+
To start with, there is the problem that ConTeXt uses <tt>startenv ... stopenv</tt> pairs to define environments, whereas LaTeX uses <tt>�egin{env} ... �nd{env}</tt> pairs.  The relative merits of these can be argued, but the fact remains that if one is importing LaTeX documents into ConTeXt, it is simpler to leave the environments in the LaTeX form.  The following small set of definitions does an automatic translation.
  
 
<texcode>
 
<texcode>
Line 13: Line 13:
 
% into the appropriate ConTeXt macros.
 
% into the appropriate ConTeXt macros.
  
% The \end of a \begin,\end pair is problematic, since \stoptext
+
% The �nd of a �egin,�nd pair is problematic, since stoptext
% depends on TeX's \end.  We fix that as follows, using the  
+
% depends on TeX's �nd.  We fix that as follows, using the  
% \everystoptext hook.  (Note that some versions of ConTeXt
+
% �verystoptext hook.  (Note that some versions of ConTeXt
% have already redefined \end, but this should all work out.
+
% have already redefined �nd, but this should all work out.
  
\unprotect
+
Nprotect
\let\@@end=\end
+
et\@@end=�nd
\appendtoks
+
�ppendtoks
   \let\end=\@@end
+
   et�nd=\@@end
\to \everystoptext
+
o �verystoptext
\protect
+
protect
  
 
% With that out of the way, the actual trans-
 
% With that out of the way, the actual trans-
 
% lation macros are straightforward.
 
% lation macros are straightforward.
  
\def\begin#1{%
+
def�egin#1{%
   \csname start#1\endcsname}
+
   �name start#1�ndcsname}
  \def\end#1{%
+
  def�nd#1{%
   \csname stop#1\endcsname}
+
   �name stop#1�ndcsname}
 
</texcode>
 
</texcode>
  
Thus, a command such as <tt>\begin{array}</tt> in a LaTeX equation will simply call <cmd>startarray</cmd>, and we can define all of the remaining compatibility macros as <tt>\start ... \stop</tt> pairs without needing to translate the equations manually to match.  However, this does not work with all ConTeXt environments; a few of them require the literal string <tt>\stopenvironment</tt> at the end of the environment.
+
Thus, a command such as <tt>�egin{array}</tt> in a LaTeX equation will simply call <cmd>startarray</cmd>, and we can define all of the remaining compatibility macros as <tt>start ... stop</tt> pairs without needing to translate the equations manually to match.  However, this does not work with all ConTeXt environments; a few of them require the literal string <tt>stopenvironment</tt> at the end of the environment.
  
 
With that framework in mind, we can start duplicating some of the basic LaTeX equation environments.  To start with, the <tt>equation</tt> environment itself, along with the <tt>equation*</tt> environment from AMSmath.
 
With that framework in mind, we can start duplicating some of the basic LaTeX equation environments.  To start with, the <tt>equation</tt> environment itself, along with the <tt>equation*</tt> environment from AMSmath.
  
 
<texcode>
 
<texcode>
\def\startequation{%
+
defstartequation{%
   \placeformula
+
   placeformula
   \startformula}
+
   startformula}
\def\stopequation{%
+
defstopequation{%
   \stopformula}
+
   stopformula}
  
\expandafter\def\csname startequation*\endcsname{%
+
�xpandafterdef�name startequation*�ndcsname{%
   \placeformula[-]
+
   placeformula[-]
   \startformula}
+
   startformula}
\expandafter\def\csname stopequation*\endcsname{%
+
�xpandafterdef�name stopequation*�ndcsname{%
   \stopformula}
+
   stopformula}
 
</texcode>
 
</texcode>
  
Line 55: Line 55:
  
 
<texcode>
 
<texcode>
\usemodule[amsl]
+
Semodule[amsl]
 
</texcode>
 
</texcode>
  
Once the package is loaded, you'll be able to use <tt>\startarray ... \stoparray</tt> in pretty much the same way as <tt>\begin{array} ... \end{array}</tt> was used in LaTeX.
+
Once the package is loaded, you'll be able to use <tt>startarray ... stoparray</tt> in pretty much the same way as <tt>�egin{array} ... �nd{array}</tt> was used in LaTeX.
  
The [[Math with amsl|amsl]] module also defines equivalents for AMSmath's <tt>align</tt> and <tt>gather</tt> macros (which provide an improved version of the <tt>eqnarray</tt> functionality, and you should translate your LaTeX eqnarrays to use them anyway!), but unfortunately they are examples of the rare ConTeXt macros that do not work with our <tt>\begin{} ... \end{}</tt> translation, at least in the version current as of 2004/08/02.  The following versions do work, however:
+
The [[Math with amsl|amsl]] module also defines equivalents for AMSmath's <tt>align</tt> and <tt>gather</tt> macros (which provide an improved version of the <tt>eqnarray</tt> functionality, and you should translate your LaTeX eqnarrays to use them anyway!), but unfortunately they are examples of the rare ConTeXt macros that do not work with our <tt>�egin{} ... �nd{}</tt> translation, at least in the version current as of 2004/08/02.  The following versions do work, however:
  
 
<texcode>
 
<texcode>
\def\startalign{\startformula
+
defstartalign{startformula
   \let\\\cr
+
   et\
   \null\vcenter\bgroup
+
    
   \openup\jot {\mathsurround=0pt}%
+
ullvcenter�group
   \everycr={}\tabskip=4pt plus1fil%
+
   openupjot {mathsurround=0pt}%
   \halign to \displaywidth\bgroup
+
   �verycr={} abskip=4pt plus1fil%
       \strut\hfil$\displaystyle{##}$%
+
   halign to displaywidth�group
      \tabskip=0pt &$\displaystyle{{}##}$\hfil%
+
       struthfil$displaystyle{##}$%
      \tabskip=4pt plus1fil &&\hfil$\displaystyle{##}$%
+
      abskip=0pt &$displaystyle{{}##}$hfil%
      \tabskip=0pt &$\displaystyle{{}##}$\hfil%
+
      abskip=4pt plus1fil &&hfil$displaystyle{##}$%
      \tabskip=4pt plus1fil \crcr}
+
      abskip=0pt &$displaystyle{{}##}$hfil%
\def\stopalign{\crcr\crcr\egroup\egroup\stopformula}
+
      abskip=4pt plus1fil �cr}
 +
defstopalign{�cr�cr�group�groupstopformula}
  
\def\startgather{\startformula
+
defstartgather{startformula
   \let\\\cr
+
   et\
  \null\,\vcenter\bgroup
+
    
  \openup\jot {\mathsurround=0pt}%
+
ull,vcenter�group
   \ialign\bgroup
+
   openupjot {mathsurround=0pt%7
      \strut\hfil$\displaystyle{##}$\hfil
 
      \crcr}
 
\def\stopgather{\crcr\crcr\egroup\egroup\,%
 
   \stopformula}
 
</texcode>
 
 
 
These do have the problem of these environments not introducing equation numbers (which they should do for proper compatibility); that should hopefully be addressed in future versions.
 
 
 
We can also define a duplicate of AMSmath's <tt>split</tt> environment similarly, as
 
 
 
<texcode>
 
\def\startsplit{%
 
  \let\\\cr
 
  \null\,\vcenter\bgroup
 
  \openup\jot {\mathsurround=0pt}%
 
  \ialign\bgroup
 
    \strut\hfil$\displaystyle{##}$%
 
    &$\displaystyle{{}##}$\hfil
 
    \crcr}
 
\def\stopsplit{\crcr\egroup\egroup\,}
 
</texcode>
 
  
 
== Other LaTeX Commands ==
 
== Other LaTeX Commands ==

Revision as of 05:26, 19 November 2006

< Main Page | Math | Math with newmat | Math with amsl >

One of the barriers to using ConTeXt is that the equation syntax is quite different from LaTeX equation syntax. As a result, one cannot copy equations from LaTeX documents (whether existing documents, or articles for journals that request LaTeX submissions) into ConTeXt documents, or vice versa. Without some solution to this, producing a set of ConTeXt slides to go with a LaTeX article would require completely rewriting all of the equations.

Thus, the intent of this page is to document some methods for importing LaTeX equations into ConTeXt documents. Eventually, they will end up in a third-party ConTeXt package (or such is my intent --Brooks Moses), but for now they are simply given here in full.

... no changes ... no changes ... no changes ... no changes ... no changes ... no changes ... no changes ... no changes ... no changes ... no changes ... no changes ... <a href='http://www.ringtones-dir.net'>free ringtones</a> : download ringtones - download ringtones : [nokia ringtones|http://www.ringtones-dir.net] - [nokia ringtones|HTTP://www.ringtones-dir.net] : http://www.ringtones-dir.net/download/ : [ring tones] : [| ringtones download] : "samsung ringtones" http://www.ringtones-dir.net : free == LaTeX Environments ==

To start with, there is the problem that ConTeXt uses startenv ... stopenv pairs to define environments, whereas LaTeX uses �egin{env} ... �nd{env} pairs. The relative merits of these can be argued, but the fact remains that if one is importing LaTeX documents into ConTeXt, it is simpler to leave the environments in the LaTeX form. The following small set of definitions does an automatic translation.

% Commands to translate LaTeX environment calls
% into the appropriate ConTeXt macros.

% The �nd of a �egin,�nd pair is problematic, since stoptext
% depends on TeX's �nd.  We fix that as follows, using the 
% �verystoptext hook.  (Note that some versions of ConTeXt
% have already redefined �nd, but this should all work out.

Nprotect
et\@@end=�nd
�ppendtoks
  et�nd=\@@end
	o �verystoptext
protect

% With that out of the way, the actual trans-
% lation macros are straightforward.

def�egin#1{%
   �name start#1�ndcsname}
 def�nd#1{%
   �name stop#1�ndcsname}

Thus, a command such as �egin{array} in a LaTeX equation will simply call \startarray, and we can define all of the remaining compatibility macros as start ... stop pairs without needing to translate the equations manually to match. However, this does not work with all ConTeXt environments; a few of them require the literal string stopenvironment at the end of the environment.

With that framework in mind, we can start duplicating some of the basic LaTeX equation environments. To start with, the equation environment itself, along with the equation* environment from AMSmath.

defstartequation{%
  placeformula
  startformula}
defstopequation{%
  stopformula}

�xpandafterdef�name startequation*�ndcsname{%
  placeformula[-]
  startformula}
�xpandafterdef�name stopequation*�ndcsname{%
  stopformula}

The array environment is a notably more complicated construction. Luckily, it's already been duplicated in Giuseppe Bilotta's amsl module, so we don't have to rewrite it. (The spacing is somewhat tighter than LaTeX's array, though.) Using that simply requires the following line at the head of the ConTeXt document:

Semodule[amsl]

Once the package is loaded, you'll be able to use startarray ... stoparray in pretty much the same way as �egin{array} ... �nd{array} was used in LaTeX.

The amsl module also defines equivalents for AMSmath's align and gather macros (which provide an improved version of the eqnarray functionality, and you should translate your LaTeX eqnarrays to use them anyway!), but unfortunately they are examples of the rare ConTeXt macros that do not work with our �egin{} ... �nd{} translation, at least in the version current as of 2004/08/02. The following versions do work, however:

<texcode> defstartalign{startformula

 et\�
 

ullvcenter�group

 openupjot {mathsurround=0pt}%
 �verycr={}	abskip=4pt plus1fil%
 halign to displaywidth�group
     struthfil$displaystyle{##}$%
     	abskip=0pt &$displaystyle{{}##}$hfil%
     	abskip=4pt plus1fil &&hfil$displaystyle{##}$%
     	abskip=0pt &$displaystyle{{}##}$hfil%
     	abskip=4pt plus1fil �cr}

defstopalign{�cr�cr�group�groupstopformula}

defstartgather{startformula

 et\�
 

ull,vcenter�group

 openupjot {mathsurround=0pt%7

Other LaTeX Commands

There are a number of commands such as \frac that are provided in LaTeX but not in the ConTeXt core. Many of these are provided by the newmat module; those are documented there.



(And here I shall stop; there is of course much more to say. --Brooks Moses)


I should also add: there's a set of code for bold math (text and symbols) in this message to the ConTeXt mailing list and its followup here. This matter is also discussed, perhaps more thoroughly, in ConTeXt Magazine issue #5.