Re: [mathjax-users] New Environment

133 views
Skip to first unread message
Message has been deleted

Frédéric WANG

unread,
Aug 28, 2012, 1:26:41 AM8/28/12
to mathja...@googlegroups.com
On 28/08/2012 06:53, Karthikeyan santosh wrote:
> How to define new environments in MathJax
You can do that using the \newenvironment command.

--
Frédéric Wang
maths-informatique-jeux.com/blog/frederic

Karthikeyan santosh

unread,
Aug 28, 2012, 3:20:06 AM8/28/12
to mathja...@googlegroups.com
I'm using like this "\newenvironment{myEnvironmentName}
  [ <optional # of arguments, from 1 to 9> ]
  { <replacement text for each occurrence of \begin{myEnvironmentName}> } 
  { <replacement text for each occurrence of \end{myEnvironmentName}> }" its works fine but i have some trouble to declare in mathjax where to define.

Davide P. Cervone

unread,
Aug 28, 2012, 6:39:37 AM8/28/12
to mathja...@googlegroups.com
There are several ways you can do it.  

First, you could put the \newcommand call within math delimiters at the top of the page (or anywhere before it is used), as in

\(\newcommand{ABC}{before}{after}\)

You can put that inside <div style="display:none">...</div> to prevent it from inserting any space into your document if you need to.

This requires the definition to be part of the page, but you can also arrange for it to be included as part of the configuration if you prefer.  
Alternatively, you can use the Environment() command of the TeX input jax.  This requires that you load the newcommand.js extension and wait for that to load before using the Enviroment() command (since it is part of that package).  This is illustrated in the example below.  Here, you pass the environment name, the before-text, the after-text and the number of arguments.  Note that since these are javascript strings, you must double the backslashes in the before and after strings.

Hope that does the trick for you.

Davide

_____________________________

<!DOCTYPE html>
<html>
<head>
<title>Add new environments in-line and in configuration</title>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    TeX: {extensions: ["newcommand.js"]}
  });
  MathJax.Hub.Register.StartupHook("TeX newcommand Ready", function () {
    var TEX = MathJax.InputJax.TeX;   // makes it easier if you want to do several definitions
    TEX.Environment("XYZ","XYZ_{\\rm before}(#1)","XYZ_{\\rm after}",1);
  });
</script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
</head>
<body>

<div style="display:none">
\(\newenvironment{ABC}{ABX_{\rm before}}{ABC_{\rm after}}\)
</div>

\begin{ABC}-inbetween-\end{ABC}

\begin{XYZ}{argument}-inbetween-\end{XYZ}

</body>
</html>
_____________________________

Karthikeyan santosh

unread,
Aug 29, 2012, 12:33:33 AM8/29/12
to mathja...@googlegroups.com
Thanks its working fine still i have one doubt, how to define new environment \begin{boxed}...\end{boxed} within equation.

Davide P. Cervone

unread,
Aug 29, 2012, 3:33:56 PM8/29/12
to mathja...@googlegroups.com
Well, you don't actually say what you want the boxed environment to do, but I assume you want it to put a box around its contents.  That actually is a bit harder than I thought it was going to be when I first read your message, as it requires doing a javascript-based definition rather than the TeX-based one.  That is because the TeX code doesn't have access to the contents of what the environment encloses, while the javascript does.

The following is one approach to this:

<!DOCTYPE html>
<html>
<head>
<title>Add a new environment defined in javascript</title>
<script type="text/x-mathjax-config">
MathJax.Hub.Register.StartupHook("TeX Jax Ready", function () {
  var TEX = MathJax.InputJax.TeX,
      MML = MathJax.ElementJax.mml;
  //
  //  Define a new environment that calls the myBoxed method defined below.
  //
  TEX.Definitions.environment.boxed = [null,"myBoxed"];
  //
  //  Add myBoxed to the TeX parser
  //
  TEX.Parse.Augment({
    //
    //  This is called when \end{boxed} is processed, and the contents of the
    //  environment is passed as an array of MML elements in row.
    //  We insert them into an menclose element with notation="box" and
    //  return an array containing that one MML element.
    //
    myBoxed: function (begin,row) {
      return [MML.menclose.apply(MML.menclose,row).With({notation:"box"})];
    }
  });
});
</script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
</head>
<body>
$$\sqrt{\,\begin{boxed}x+1\end{boxed}\,}$$
</body>
</html>

Hope that helps.

Davide

Karthikeyan santosh

unread,
Aug 30, 2012, 12:53:01 AM8/30/12
to mathja...@googlegroups.com
Thanks a lot, its working perfect.


On Tuesday, 28 August 2012 10:54:55 UTC+5:30, fred wrote:
Reply all
Reply to author
Forward
0 new messages