Provide Multiple Implementations for Macros

37 views
Skip to first unread message

tj.sch...@gmail.com

unread,
Jun 26, 2018, 8:24:13 AM6/26/18
to MathJax Users
Hi

We are doing a digital edition project on early-modern mathematics and we use MathJax to render the formulae that are set in LaTeX.
Sometimes we want to display a phenomenon - mostly related to the original notation - in different ways, depending on the user's interest (original notation, modern version).

For this reason, we use custom macros in the source LaTeX that we can implement differently. The global configuration object for MathJax works fine for single page applications (the implementation of a macro is defined globally for each page).

Is there a way to flexibly change the implementation of a macro on the fly? Could the implementation of a macro be defined per formula block?


Thanks for any hints! 


tj.sch...@gmail.com

unread,
Jun 26, 2018, 9:06:16 AM6/26/18
to MathJax Users
Another way to do this is preprocessing the LaTeX before rendering it with MathJax. During preprocessing macros are replaced by a specific implementation.

I tried to do a LaTeX to LaTeX conversion with pandoc and it works!

\documentclass[12pt]{article}


# layer specific implementations of custom macros

# substitution
\renewcommand{\subst}[2]{\enclose{horizontalstrike}{#1}#2}


# mathematical text inserted above baseline
\renewcommand{\upper}[1]{^{\Large{#1}}}}



# struck text
\renewcommand{\str}[1]{\enclose{horizontalstrike}{#1}}


# mathematical text inserted below baseline
\renewcommand{\below}[1]{_{\Large{#1}}}


\begin{document}


$math
with macros$


\end{document}

Then just to a LaTeX to LaTeX conversion:  pandoc -f latex -t latex test.tex -o o.tex

And it produces the same result as if you had written the implementation directly in the document.

Davide Cervone

unread,
Jul 2, 2018, 10:09:08 AM7/2/18
to mathja...@googlegroups.com
You can use \def, \newcommand, \renewcommand, etc. within math mode at any point in your document.  So you can use something like:

$$
  \renewcommand{\upper}[1]{^{\Large{#1}}}
  x\upper{y}
$$

and later

$$
  \renewcommand{\upper}[1]{^{\Huge{#1}}}
  x\upper{y}
$$

to get two different meanings for \upper.  You can make a math item that is just definitions if you want, and they will remain in effect until the end of the page (or until redefined later):

<div style="visibility: hidden; height: 0px">
$$
  \renewcommand{\upper}[1]{^{\Large{#1}}}
  \renewcommand{\lower}[1]{_{\Large{#1}}}
$$
</div>

If you want to change definitions temporarily, then load the begingroup extension, and use \begingoup before the re-definitions, and \endgroup when you want to restore the original definitions.

<div style="visibility: hidden; height: 0px">
$$
  \require{begingroup}
  \newcommand{\upper}[1]{^{\Large{#1}}}
  \newcommand{\lower}[1]{_{\Large{#1}}}
$$
</div>

... (\upper and \lower use \Large here) ...

<div style="visibility: hidden; height: 0px">
$$
  \begingroup
  \renewcommand{\upper}[1]{^{\Huge{#1}}}
  \renewcommand{\lower}[1]{_{\Huge{#1}}}
$$
</div>

... (\upper and \lower use \Huge here) ...

<div style="visibility: hidden; height: 0px">
$$
  \endgroup
$$
</div>

... (\upper and \lower use \Large again) ...

I hope that make is possible for you to do what you want.

Davide


--
You received this message because you are subscribed to the Google Groups "MathJax Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mathjax-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

tj.sch...@gmail.com

unread,
Jul 2, 2018, 4:31:16 PM7/2/18
to MathJax Users
Hi Davide

Thanks a lot for your explanations! 

I did not know about this, I always used the global mathjax configuration object to define macros so far. I have just checked the docs again (http://docs.mathjax.org/en/latest/tex.html#defining-tex-macros) and now I see that there is also the way you mentioned.

Thanks again!
Reply all
Reply to author
Forward
0 new messages