What is the best way to globally import KaTeX in Vue.js for my document?

When dealing with static htmls, I usually include the following code in the <head> tag:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9c2c8ddccd1e9998798998798">[email protected]</a>/dist/katex.min.css" integrity="sha384-dbVIfZGuN1Yq7/1Ocstc1lUEm+AT+/rCkibIcC/OmWo5f0EA48Vf8CytHzGrSwbQ" crossorigin="anonymous">
              <script defer  src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f893998c9d80b8c8d6c9c8d6c9">[email protected]</a>/dist/katex.min.js" integrity="sha384-2BKqo+exmr9su6dir+qCw08N2ZKRucY4PrGQPPWU1A7FtlCGjmEGFqXCv5nyM5Ij" crossorigin="anonymous"></script>
              <script defer src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="345f5540514c74041a05041a05">[email protected]</a>/dist/contrib/auto-render.min.js" integrity="sha384-kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI" crossorigin="anonymous"></script>
              <script>                 
                  document.addEventListener("DOMContentLoaded", function() {
                      renderMathInElement(document.body, {
                          delimiters:[
                              {left: '\\begin{equation}', right: '\\end{equation}', display:true},
                              {left: '$$', right:'$$',display:true},
                              {left:'$', right:'$', display: false},
                              {left:'\\(', right:'\\)', display: false}
                          ]
                      });
                  });

              </script>

This code allows for math rendering throughout the document as long as $ is used correctly. I wanted to replicate this functionality in my VUE app.

I added the same code in index.html, and it worked initially. However, when I navigate away from a page containing math and then return, the math doesn't render properly, displaying only the code itself.

What have I tried?

I discovered that router-link does not render the head tag, preventing auto-rendering of math when switching routers. To address this, I included the following in the mounted hook:

renderMathInElement(document.body, {
                          delimiters:[
                              {left: '\\begin{equation}', right: '\\end{equation}', display:true},
                              {left: '$$', right:'$$',display:true},
                              {left:'$', right:'$', display: false},
                              {left:'\\(', right:'\\)', display: false}
                          ]
                      });

While this approach successfully renders the math within the view, it triggers an error message in the terminal:

error: 'renderMathInElement' is not defined (no-undef) at src/views/Home.vue:57:2:
  55 |   },
  56 |  mounted(){
> 57 |  renderMathInElement(document.body, {
     |  ^
  58 |                           delimiters:[
  59 |                               {left: '\\begin{equation}', right: '\\end{equation}', display
:true},
  60 |                               {left: '$$', right:'$$',display:true},


1 error found.

How can I resolve this issue?

Answer №1

One way to incorporate math rendering in your component is by utilizing the script

window.renderMathInElement(window.document)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to pass scope between the same controller multiple times

There is a unique scenario in which I have a controller appearing in 2 different locations on a page. This arrangement is necessary for specific reasons. To illustrate, the simplified HTML structure is as follows: <aside ng-if="aside.on" ng-controller ...

Can you please guide me on how I can implement a top AJAX menu like the one on StackOverflow

Have you seen the menu that appears when you first visit a website, with an 'x' on the right to close it? I believe this is done through AJAX - what specific terms should I look up to implement this feature? ...

Plane is constantly cloaked in darkness

I'm having trouble adding a texture to my plane that repeats both horizontally and vertically. Every time I try to apply the texture, it shows up as black. I've attempted to add some lights to the scene, but the issue persists without any errors. ...

Sending string variable from Perl CGI script to HTML frontend

Having just begun experimenting with AJAX, I'm encountering difficulties in passing variables back to my HTML script. In this setup, I'm using a combination of a XAMPP webserver, JavaScript, and jQuery for the HTML script, along with a Perl cgi ...

interaction & portal

I am currently managing a website that includes an iframe. Both the main site and the embedded site within the iframe are verifying the $_SESSION["login"]. Therefore, if the session expires, the user will be automatically logged out. The issue I'm fa ...

Challenges with JavaScript setInterval

Hello everyone! I'm currently working on running a loop with setInterval, but I want each element in the array to use a random interval instead of the entire array doing so. Below is the code I have been using: setInterval(function() { for ( ...

What is the best way to retrieve the overall error status from the Material UI DataGrid?

I am currently utilizing the Material UI DataGrid element to display information from an EXCEL file. Each Excel document consists of numerous column titles with specific types assigned to them. As an example: const columns = [ { "field&quo ...

What is the purpose of the execute_script() function in Selenium?

browser.execute_script("window.open('about:blank', 'tab2');") browser.switch_to.window("tab2") browser.get('http://bing.com') While exploring ways to open a new tab using Selenium in Python, I found the ab ...

Steps for generating a hierarchical menu utilizing data from a CSV file

I am looking to use a CSV file to structure the menu on my webpage. Could someone assist me in creating a nested menu using JavaScript with the provided CSV data? The columns consist of: Level, Menu name, URL 0;"Service"; 1;"Service 1";"http://some-url- ...

Make sure to push the object into two separate arrays without creating any reference

Is there a way to push an object into two different arrays without sharing the reference? Below is the object in question: minigroup = { "Id": response[0].Id, "MemberId": response[0].MemberId, "NotMemberId": response[0].NotMemberId } I&apo ...

Using setAttribute will convert the attribute name to lowercase

When creating elements, I use the following code: var l = document.createElement("label");. I then assign attributes with l.setAttribute("formControlName","e");. However, a problem arises where the setAttribute method converts formControlName to lowercase ...

Tips for effectively showcasing errors received from the server during validation?

I have been working on implementing a contact form on my website, but I am encountering some issues with how the error and success messages are being displayed. Since I am relatively new to PHP, I suspect that there might be errors in my code. I attempted ...

Tips for changing between two texts within a button when clicked

Seeking a solution for toggling between two different texts inside a button when making an ajax call, with only one text displayed at a time. I'm facing difficulty in targeting the spans within the button specifically. Using 'this' as conte ...

Navigating intricate data structures within React

As a newcomer to React, I could use some assistance with a project I am working on. The application is similar to Slack, where users can create new teams and channels. However, I am encountering difficulties in managing the state of the data input throug ...

A guide to streamlining the process of passing multiple variables to a function using node/javascript

Looking to streamline my method by passing an object instead of multiple variables: export class MyClass{ myMethod(a, b, c) { // do crazy stuff here return a * b * c; } } Proposing a simplified method signature: export class MyClass{ myMet ...

Develop a COM API, or equivalent, tailored for a Node.js application

I have developed a JavaScript application that is designed to run on node.js. Is there a way to create a custom COM API or something similar for a Node.js application? For instance, in languages like C, C++, C#, etc., you can develop an application and t ...

Steps for clearing the chosen input data

I am currently developing an Angular 6 application and I am working on implementing a multiple select feature using just an input box without relying on any third-party plugins, jQuery, datalist, or select boxes. The solution needs to be purely input box b ...

The Electron application is experiencing difficulties locating the module at /resources/app/index.js

Just started my journey with electron and successfully created my first electron application. It runs perfectly fine with npm start, but I encounter issues when trying to execute it with npm run. (I am using Ubuntu Linux). The command line interface displa ...

Incorporate the coordinates of Google Maps markers into your form submission

After a user clicks a position on the map, the following javascript function retrieves a javascript variable named marker containing coordinates. var marker; function placeMarker(location) { if ( marker ) { marker.setPosition(location); } else { ...

Obtain a string with commas separating the elements of an array

In my JavaScript code, I am working with the following JSON data. { "url":"http://example.com/main.aspx", "report_template_id":"1", "interval_secs":"86400", "analysis_type":"lite", "email":"<a href="/cdn-cgi/l/email-protection" class="__ ...