Allow math blocks to use global Katex macros

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

Summary

Katex math blocks and inline math are rendered with There was an error rendering this math block when attempting to use global macros outside of their defining block, which is literally what global macros exist to do.

Demo

The following macro definition should display a slanted fraction, such as \raisebox{0.15em}{$ 1 $} / \raisebox{-0.15em}{$ 2 $}.

\gdef\slantedfrac#1#2{\raisebox{0.15em}{$ #1 $} / \raisebox{-0.15em}{$ #2 $}}
\slantedfrac 1 2

As we can see, this is a valid macro definition, and succeeds to render in the katex editor. It also works when typed out manually in gitlab markdown. However, typing it out with macros just doesn't work:

Without Any Macros With Local Macros
\raisebox{0.15em}{$ 1 $} / \raisebox{-0.15em}{$ 2 $}
\def\slantedfrac#1#2{
  \raisebox{0.15em}{$ #1 $}
  / % ^ dividend   v divisor
  \raisebox{-0.15em}{$ #2 $}
}
\slantedfrac 1 2
\raisebox{0.15em}{$ 1 $} / \raisebox{-0.15em}{$ 2 $}
\def\slantedfrac#1#2{
  \raisebox{0.15em}{$ #1 $}
  / % ^ dividend   v divisor
  \raisebox{-0.15em}{$ #2 $}
}
\slantedfrac 1 2
With Global Macros
\gdef\slantedfrac#1#2{
  \raisebox{0.15em}{$ #1 $}
  / % ^ dividend   v divisor
  \raisebox{-0.15em}{$ #2 $}
}
\slantedfrac 1 2
\gdef\slantedfrac#1#2{
  \raisebox{0.15em}{$ #1 $}
  / % ^ dividend   v divisor
  \raisebox{-0.15em}{$ #2 $}
}
\slantedfrac 1 2
Screenshot for historical accuracy

image

Steps to reproduce

  1. Create a new snippet

  2. Paste the following gitlab markdown into the snippet:

    View GFM Stub
    <table>
    <tr>  <!-- First Header Row -->
      <th>Without Any Macros</th>
      <th>With Local Macros</th>
    </tr>
    
    <tr> <!-- Display Row: No macros & Local Macros -->
    <!-- Without Any Macros -->
    <td>
    
    ```math
    \raisebox{0.15em}{$ 1 $} / \raisebox{-0.15em}{$ 2 $}
    ```
    </td>
    
    <!-- With Local Macros -->
    <td>
    
    ```math
    \def\slantedfrac#1#2{
      \raisebox{0.15em}{$ #1 $}
      / % ^ dividend   v divisor
      \raisebox{-0.15em}{$ #2 $}
    }
    \slantedfrac 1 2
    ```
    </td>
    </tr>
    
    <tr> <!-- Code Row: No macros & Local Macros -->
    <!-- Without Any Macros -->
    <td>
    
    ```latex
    \raisebox{0.15em}{$ 1 $} / \raisebox{-0.15em}{$ 2 $}
    ```
    </td>
    
    <!-- With Local Macros -->
    <td>
    
    ```latex
    \def\slantedfrac#1#2{
      \raisebox{0.15em}{$ #1 $}
      / % ^ dividend   v divisor
      \raisebox{-0.15em}{$ #2 $}
    }
    \slantedfrac 1 2
    ```
    </td>
    </tr>
    
    <tr>  <!-- Second Header Row -->
      <th colspan=2>With Global Macros</th>
    </tr>
    
    
    <tr> <!-- Display Row: Global Macros -->
    <!-- With Global Macros -->
    <td colspan=2>
    
    ```math
    \gdef\slantedfrac#1#2{
      \raisebox{0.15em}{$ #1 $}
      / % ^ dividend   v divisor
      \raisebox{-0.15em}{$ #2 $}
    }
    ```
    
    ```math
    \slantedfrac 1 2
    ```
    </td>
    </tr>
    
    <tr> <!-- Code Row: Global Macros -->
    <!-- With Global Macros -->
    <td colspan=2>
    
    ```latex
    \gdef\slantedfrac#1#2{
      \raisebox{0.15em}{$ #1 $}
      / % ^ dividend   v divisor
      \raisebox{-0.15em}{$ #2 $}
    }
    ```
    
    ```latex
    \slantedfrac 1 2
    ```
    </td>
    </tr>
    </table>
  3. Click Preview

  4. See bug

I should note that in my testing, this affects normal markdown files as well as snippet files & descriptions.

What is the current bug behavior?

Katex renders the global-macros table entry with There was an error rendering this math block

What is the expected correct behavior?

Katex should render the global-macros table entry like the katex demo does:

image

Edited by 🤖 GitLab Bot 🤖