Difference between revisions of "LaTeX Math in ConTeXt"

From Wiki
Jump to navigation Jump to search
(added some links, corrected package=module)
m (Reverted edits by 220.52.232.111 (Talk); changed back to last version by Mojca Miklavec)
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
< [[Main Page]] | [[Math]] >
+
< [[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.
 
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 (<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 ==
  
 
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>\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.
Line 11: Line 13:
 
% into the appropriate ConTeXt macros.
 
% into the appropriate ConTeXt macros.
  
% The \end of a \begin,\end pair is problematic,
+
% The \end of a \begin,\end pair is problematic, since \stoptext
% since \stoptext depends on TeX's \end.  We fix
+
% depends on TeX's \end.  We fix that as follows, using the
% that as follows, with a slightly edited copy
+
% \everystoptext hook.  (Note that some versions of ConTeXt
% of \stoptext from core-job.tex.
+
% have already redefined \end, but this should all work out.
  
\let\therealdocumentend\end
+
\unprotect
\def\stoptekst
+
\let\@@end=\end
   {\doglobal\decrement\textlevel\relax
+
\appendtoks
  \ifnum\textlevel>\zerocount \else
+
   \let\end=\@@end
    \the\everystoptext
+
\to \everystoptext
    \expandafter\therealdocumentend
+
\protect
  \fi}
 
\let\stoptext \stoptekst
 
  
 
% With that out of the way, the actual trans-
 
% With that out of the way, the actual trans-
Line 102: Line 102:
 
\def\stopsplit{\crcr\egroup\egroup\,}
 
\def\stopsplit{\crcr\egroup\egroup\,}
 
</texcode>
 
</texcode>
 +
 +
== Other LaTeX Commands ==
 +
 +
There are a number of commands such as <cmd>frac</cmd> that are provided in LaTeX but not in the ConTeXt core.  Many of these are provided by the [[Math with newmat|newmat]] module; those are documented there.
 +
  
 
----
 
----
Line 107: Line 112:
  
 
----
 
----
I should also add: there's a set of code for bold math (text and symbols) in [http://www.ntg.nl/pipermail/ntg-context/2004/005314.html this message to the ConTeXt mailing list] and [http://www.ntg.nl/pipermail/ntg-context/2004/005315.html its followup here].  This matter is also discussed, perhaps more thoroughly, in [http://www.pragma-ade.com/general/magazines/mag-0005.pdf ConTeXt Magazine issue #5].
+
I should also add: there's a set of code for bold math (text and symbols) in [http://www.ntg.nl/pipermail/ntg-context/2004/005314.html this message to the ConTeXt mailing list] and [http://www.ntg.nl/pipermail/ntg-context/2004/005315.html its followup here].  This matter is also discussed, perhaps more thoroughly, in [[magazine:0005|ConTeXt Magazine issue #5]].
 +
 
 +
[[Category:From LaTeX]]
 +
[[Category:Math]]

Revision as of 09:53, 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.

LaTeX Environments

To start with, there is the problem that ConTeXt uses \startenv ... \stopenv pairs to define environments, whereas LaTeX uses \begin{env} ... \end{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 \end of a \begin,\end pair is problematic, since \stoptext
% depends on TeX's \end.  We fix that as follows, using the 
% \everystoptext hook.  (Note that some versions of ConTeXt
% have already redefined \end, but this should all work out.

\unprotect
\let\@@end=\end
\appendtoks
  \let\end=\@@end
\to \everystoptext
\protect

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

\def\begin#1{%
   \csname start#1\endcsname}
 \def\end#1{%
   \csname stop#1\endcsname}

Thus, a command such as \begin{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.

\def\startequation{%
  \placeformula
  \startformula}
\def\stopequation{%
  \stopformula}

\expandafter\def\csname startequation*\endcsname{%
  \placeformula[-]
  \startformula}
\expandafter\def\csname stopequation*\endcsname{%
  \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:

\usemodule[amsl]

Once the package is loaded, you'll be able to use \startarray ... \stoparray in pretty much the same way as \begin{array} ... \end{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 \begin{} ... \end{} translation, at least in the version current as of 2004/08/02. The following versions do work, however:

\def\startalign{\startformula
  \let\\\cr
  \null\vcenter\bgroup
  \openup\jot {\mathsurround=0pt}%
  \everycr={}\tabskip=4pt plus1fil%
  \halign to \displaywidth\bgroup
      \strut\hfil$\displaystyle{##}$%
      \tabskip=0pt &$\displaystyle{{}##}$\hfil%
      \tabskip=4pt plus1fil &&\hfil$\displaystyle{##}$%
      \tabskip=0pt &$\displaystyle{{}##}$\hfil%
      \tabskip=4pt plus1fil \crcr}
\def\stopalign{\crcr\crcr\egroup\egroup\stopformula}

\def\startgather{\startformula
  \let\\\cr
  \null\,\vcenter\bgroup
  \openup\jot {\mathsurround=0pt}%
  \ialign\bgroup
      \strut\hfil$\displaystyle{##}$\hfil
      \crcr}
\def\stopgather{\crcr\crcr\egroup\egroup\,%
  \stopformula}

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 split environment similarly, as

\def\startsplit{%
  \let\\\cr
  \null\,\vcenter\bgroup
  \openup\jot {\mathsurround=0pt}%
  \ialign\bgroup
     \strut\hfil$\displaystyle{##}$%
     &$\displaystyle{{}##}$\hfil
     \crcr}
\def\stopsplit{\crcr\egroup\egroup\,}

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.