Issue
I have reviewed various related questions including
- How do I get MathJax to enable the mhchem extension in ipython notebook
- IPython (Jupyter) MathJaX preamble
but I cannot get it to work...
My test case is simply
from IPython.display import display, HTML, Math, Latex
display(Math(r'\uppi'))
expecting upright $\pi$ but currently getting just '\uppi' back raw.
I have tried the following using cell magic
%%javascript
<script type="text/Javascript">
MathJax.Hub.Config({
loader: {load: ['[tex]/upgreek']},
tex: {packages: {'[+]': ['upgreek']}}
});
</script>
which generates Javascript Error: Unexpected token '<'
%%javascript
MathJax.Hub.Config({
loader: {load: ['[tex]/upgreek']},
tex: {extensions: ['require.js'], packages: {'[+]': ['upgreek']}}
});
which throws no error but doesn't seem to have any effect
%%javascript
window.MathJax = {
loader: {load: ['[tex]/upgreek']},
tex: {packages: {'[+]': ['upgreek']}}
};
which also does not seem to have the desired effect
Question: I don't want to modify configuration/javascript files outside the notebook. How can I enable MathJax upgreek dynamically in a jupyter notebook?
(Currently on jupyterlab 3.2.2, python 3.8.10 under Windows 10)
Solution
This does not address the OP's fundamental issue, which relates to the control of the installation of MathJax extensions in jupyter, but it does address the objective: to alter the rendered appearance of greek symbols (but can be applied to other font-related needs as well)
Updated The following will create TeX macros for all upper and lower case greek characters, as also available in Upgreek if it could be installed.
from IPython.display import display, Math
# Using codes from https://unicode-table.com/en/sets/greek-symbols/, create dictionaries of unicodes keyed by character names
grLcUpChars = [rf'\unicode{{{code}}}' for code in range(0x3B1, 0x3CA) if code != 0x3C2] # omit 'final sigma'
grUcUpChars = [rf'\unicode{{{code}}}' for code in range(0x391, 0x3AA) if code != 0x3A2] # omit omitted code for 'final sigma' in uppercase
grCharNames = ['alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta', 'eta', 'theta', 'iota', 'kappa', 'lambda',
'mu', 'nu', 'xi', 'omicron', 'pi', 'rho', 'sigma', 'tau', 'upsilon', 'phi', 'chi', 'psi', 'omega']
zipIt = zip(grCharNames, grLcUpChars)
grLcUpDict = dict(zipIt)
zipIt = zip(grCharNames, grUcUpChars)
grUcUpDict = dict(zipIt)
# Generate the MathJax code to create 'macro' definitions for upright greek characters
# See e.g. https://docs.mathjax.org/en/latest/input/tex/macros.html
# Paste output text from $ to $ inclusive into a markdown cell and execute it
print('$')
for grCharName in grCharNames:
print(fr' \def\Up{grCharName}{{{{{grLcUpDict[grCharName]}}}}}')
for grCharName in grCharNames:
print(fr' \def\Up{grCharName.title()}{{{{{grUcUpDict[grCharName]}}}}}')
print('$')
which produces the TeX macros for a Markdown cell:
$
\def\Upalpha{{\unicode{945}}}
\def\Upbeta{{\unicode{946}}}
\def\Upgamma{{\unicode{947}}}
\def\Updelta{{\unicode{948}}}
\def\Upepsilon{{\unicode{949}}}
\def\Upzeta{{\unicode{950}}}
\def\Upeta{{\unicode{951}}}
\def\Uptheta{{\unicode{952}}}
\def\Upiota{{\unicode{953}}}
\def\Upkappa{{\unicode{954}}}
\def\Uplambda{{\unicode{955}}}
\def\Upmu{{\unicode{956}}}
\def\Upnu{{\unicode{957}}}
\def\Upxi{{\unicode{958}}}
\def\Upomicron{{\unicode{959}}}
\def\Uppi{{\unicode{960}}}
\def\Uprho{{\unicode{961}}}
\def\Upsigma{{\unicode{963}}}
\def\Uptau{{\unicode{964}}}
\def\Upupsilon{{\unicode{965}}}
\def\Upphi{{\unicode{966}}}
\def\Upchi{{\unicode{967}}}
\def\Uppsi{{\unicode{968}}}
\def\Upomega{{\unicode{969}}}
\def\UpAlpha{{\unicode{913}}}
\def\UpBeta{{\unicode{914}}}
\def\UpGamma{{\unicode{915}}}
\def\UpDelta{{\unicode{916}}}
\def\UpEpsilon{{\unicode{917}}}
\def\UpZeta{{\unicode{918}}}
\def\UpEta{{\unicode{919}}}
\def\UpTheta{{\unicode{920}}}
\def\UpIota{{\unicode{921}}}
\def\UpKappa{{\unicode{922}}}
\def\UpLambda{{\unicode{923}}}
\def\UpMu{{\unicode{924}}}
\def\UpNu{{\unicode{925}}}
\def\UpXi{{\unicode{926}}}
\def\UpOmicron{{\unicode{927}}}
\def\UpPi{{\unicode{928}}}
\def\UpRho{{\unicode{929}}}
\def\UpSigma{{\unicode{931}}}
\def\UpTau{{\unicode{932}}}
\def\UpUpsilon{{\unicode{933}}}
\def\UpPhi{{\unicode{934}}}
\def\UpChi{{\unicode{935}}}
\def\UpPsi{{\unicode{936}}}
\def\UpOmega{{\unicode{937}}}
$
For clarity & test, in another cell, this will list the defined macro names and display the upper and lower case alphabets
grLcAlphabetMathJax = [fr'\Up{grCharName}' for grCharName in grCharNames]
grUcAlphabetMathJax = [fr'\Up{grCharName.title()}' for grCharName in grCharNames]
display(grLcAlphabetMathJax)
display(grUcAlphabetMathJax)
display(Math(''.join(grLcAlphabetMathJax)))
display(Math(''.join(grUcAlphabetMathJax)))
Thereby effectively reproducing Upgreek; skipping the lists its output is:
αβγδεζηθικλμνξοπρστυφχψω ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ
Superseded Original Workaround: referring to this excellent MathJax/TeX documentation we find the \unicode instruction for MathJax, and via this site the codes for an upright Pi symbol (as example).
This then allows the following in a notebook cell, after from IPython.display import display, HTML, Math, Latex
display(Math(r'\unicode[Segoe UI]{x03C0}'))
which generates π
whereas
display(Math(r'\unicode[familySTIXMathJax_Normal-italic]{x03C0}'))
generates an italicised 𝜋
(that attempting to switch to familySTIXMathJax_Normal does not seem to affect!?)
and ordinary display(Math(r'\pi'))
generates 𝜋
Answered By - Julian Moore
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.