Mako Math Helper

<<everything-else>>

Introduction

This is an extension of the math_helper.tmpl mako template that comes with nikola. The original version checks for the has_math: True entry in the post meta-data and then loads either Kate or MathJax based on what's in the conf.py file. My extension adds support for pseudocode.js.

Everything Else

This is a dump for stuff until I finish this literate-code re-write.

### Note: at present, MathJax and KaTeX do not respect the USE_CDN configuration option
<%def name="math_scripts()">
  %if use_katex:
        <script src="https://cdn.jsdelivr.net/npm/katex@0.10.2/dist/katex.min.js" integrity="sha384-9Nhn55MVVN0/4OFx7EE5kpFBPsEMZxKTCnA+4fqDmg12eCTqGi6+BB2LjY8brQxJ" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/katex@0.10.2/dist/contrib/auto-render.min.js" integrity="sha384-kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI" crossorigin="anonymous"></script>
        % if katex_auto_render:
            <script>
                renderMathInElement(document.body,
                    {
                        ${katex_auto_render}
                    }
                );
            </script>
        % else:
            <script>
                renderMathInElement(document.body,
                    {
                        delimiters: [
                            {left: "$$", right: "$$", display: true},
                            {left: "\\[", right: "\\]", display: true},
                            {left: "\\begin{equation*}", right: "\\end{equation*}", display: true},
                            {left: "\\(", right: "\\)", display: false}
                        ]
                    }
                );
            </script>
        % endif # katex_auto_render
    %else:
### Note: given the size of MathJax; nikola will retrieve MathJax from a CDN regardless of use_cdn configuration
#### the default config has been updated with stuff that pseudocode.js needs

            <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML" integrity="sha384-3lJUsx1TJHt7BA4udB5KPnDrlkO8T6J6v/op7ui0BbCjvZ9WqV4Xm6DTP6kQ/iBH" crossorigin="anonymous"></script>
            % if mathjax_config:
            ${mathjax_config}
            % else:
            <script type="text/x-mathjax-config">
             MathJax.Hub.Config({
               tex2jax: {
                 inlineMath: [['$','$'], ['\\(','\\)']],
                 displayMath: [['$$','$$'], ['\\[','\\]']],
                 processEscapes: true,
                 processEnvironments: true,
               },
               displayAlign: 'center',
               "HTML-CSS": {
                 styles: {'.MathJax_Display': {"margin": 0}}
               }
             });
            </script>
            % endif # mathjax_config
            %endif # use_katex
        </%def> # end of math_scripts

        <%def name="pseudocode()">
            <script src="https://cdn.jsdelivr.net/npm/pseudocode@latest/build/pseudocode.min.js"></script>
            </%def>

            <%def name="code_styles()">
            <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pseudocode@latest/build/pseudocode.min.css">
            </%def>

            <%def name="math_styles()">
            % if use_katex:
            <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.10.2/dist/katex.min.css" integrity="sha384-yFRtMMDnQtDRO8rLpMIKrtPCD5jdktao2TV19YiZYWMDkUR5GQZR/NOVTdquEx1j" crossorigin="anonymous">
            % endif
            </%def>


            # This first section is used when an individual post is loaded,
            # not for the front page where you have multiple posts loaded.

            <%def name="math_scripts_ifpost(post)">
            % if post.meta("has_pseudocode"):
            ${math_scripts()}
            ${pseudocode()}

            % elif post.has_math:
            ${math_scripts()}
            % endif
            </%def> # math_scripts_ifpost

            <%def name="math_styles_ifpost(post)">
            % if post.has_math:
            ${math_styles()}
            % elif post.meta("has_pseudocode"):
            ${math_styles()}
            ${code_styles()}
            %endif
            </%def> # math_styles_ifpost

            # The next section is for the main posts page with multiple posts
            # displayed.
            # Since the pseudocode blocks are supersets of the math blocks, you
            # need to give them priority over the mathblocks when setting up the
            # conditonal or they won't be used.

            <%def name="math_scripts_ifposts(posts)">
            % if any(post.meta("has_pseudocode") for post in posts):
            ${math_scripts()}
            ${pseudocode()}

            % elif any(post.has_math for post in posts):
            ${math_scripts()}
            % endif
            </%def> # math_scripts_ifposts


        <%def name="math_styles_ifposts(posts)">
            % if any(post.meta("has_pseudocode") for post in posts):
            ${math_styles()}
            ${code_styles()}
            % elif any(post.has_math for post in posts):
            ${math_styles()}
            % endif
        </%def> # math_styles_ifposts