-
Notifications
You must be signed in to change notification settings - Fork 231
How to create new LaTeX (box) environment
From https://groups.google.com/d/msg/mathjax-users/qsm48FdrniI/_OaBl2VzTrkJ
How to define new environments in MathJax
You can do that using the \newenvironment
command.
Fred
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.
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>
Thanks its working fine still I have one doubt, how to define new environment \begin{boxed}...\end{boxed}
within equation.
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
Thanks a lot, its working perfect.