Certainly! Below is a React component LatexRenderer.js that mirrors the functionality of your provided Python Flask application. It reads JSON data (which you can import or pass as a prop) and renders the content with LaTeX expressions using MathJax. The component uses better-react-mathjax for rendering LaTeX in React.
LatexRenderer.js
import React from 'react';
import { MathJax, MathJaxContext } from 'better-react-mathjax';
import './LatexRenderer.css';
// Sample JSON data
const data = [
{
"text": "A bookstore sold a total of $50$ books consisting of novels and comic books. Each novel costs $\\$20$, and each comic book costs $\\$10$. If the total sales amount was $\\$800$, how many novels did the bookstore sell?",
"type": "multiple-choice-single-answer",
"id": 90,
"correctAnswer": "c",
"quiztype": "satmath",
"explanation": "Let the number of novels sold be $x$.\n\nThen the number of comic books sold is $50 - x$.\n\nThe total sales amount is $20x + 10(50 - x) = 800$.\n\nSimplify: $20x + 500 - 10x = 800$.\n\nCombine like terms: $10x + 500 = 800$.\n\nSubtract $500$ from both sides: $10x = 300$.\n\nDivide both sides by $10$: $x = 30$.\n\nTherefore, the bookstore sold $30$ novels.",
"options": [
{
"text": "$20$",
"id": "a"
},
{
"text": "$25$",
"id": "b"
},
{
"text": "$30$",
"id": "c"
},
{
"text": "$35$",
"id": "d"
}
]
}
];
const LatexRenderer = () => {
const mathJaxConfig = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
},
svg: {
fontCache: 'global',
},
};
const renderTextWithLineBreaks = (text) => {
return text.split('\n').map((line, index) => (
<React.Fragment key={index}>
<MathJax inline>{line}</MathJax>
<br />
</React.Fragment>
));
};
return (
<MathJaxContext config={mathJaxConfig}>
<div className="latex-renderer">
{data.map((question) => (
<div key={question.id}>
<h1>Question ID: {question.id}</h1>
<div className="question">
<p>{renderTextWithLineBreaks(question.text)}</p>
</div>
<h2>Options</h2>
<ol>
{question.options.map((option) => (
<li key={option.id}>
<MathJax inline>{option.text}</MathJax>
</li>
))}
</ol>
<h2>Explanation</h2>
<div className="explanation">
<p>{renderTextWithLineBreaks(question.explanation)}</p>
</div>
<hr />
</div>
))}
</div>
</MathJaxContext>
);
};
export default LatexRenderer;
LatexRenderer.css
.latex-renderer {
font-family: Arial, sans-serif;
margin: 40px;
}
.latex-renderer h1,
.latex-renderer h2 {
color: #333;
}
.latex-renderer .question,
.latex-renderer .explanation {
margin-bottom: 20px;
}
.latex-renderer ol {
list-style-type: lower-alpha;
}
Instructions:
-
Install Dependencies:
Make sure to install the better-react-mathjax package:
npm install better-react-mathjax
-
Include the Component:
Place LatexRenderer.js and LatexRenderer.css in your React project. You may need to adjust import paths depending on your project structure.
-
Usage:
If you have a different JSON data source, you can pass it as a prop to the LatexRenderer component. Modify the component accordingly:
const LatexRenderer = ({ data }) => {
// ...rest of the code
};
Then, use it in your application:
import React from 'react';
import LatexRenderer from './LatexRenderer';
import data from './yourData.json'; // Adjust the path to your data
function App() {
return (
<div>
<LatexRenderer data={data} />
</div>
);
}
export default App;
Explanation:
-
MathJax Integration:
The component uses better-react-mathjax to render LaTeX expressions. The MathJaxContext provides configuration, and MathJax renders the expressions.
-
Handling Newlines:
The renderTextWithLineBreaks function splits the text at each newline character \n and maps over the array to insert <br /> elements between lines.
-
Styling:
The CSS styles mimic your Flask application's styling, adjusting for use within a React component.
-
Data Rendering:
The component maps over the data array, rendering each question's text, options, and explanation, similar to your Flask template logic.
Note:
-
This component assumes that the LaTeX expressions are enclosed within $...$ for inline math, matching the configuration.
-
If your data contains block-level LaTeX expressions (e.g., \[ ... \]), you may need to adjust the inline prop in the MathJax component accordingly.
-
Ensure that any user input or dynamic content is sanitized appropriately to prevent XSS attacks, especially if rendering HTML or using the dangerouslySetInnerHTML prop in React (which is not used in this example).
This LatexRenderer component should provide the same functionality as your Python Flask application within a React environment, rendering questions with LaTeX expressions from JSON data.