How to use LaTeX in React to generate PDFs
Tags: devtool, react, latex, pdf
Published: 2024-02-28 03:51:19

**tl;dr** Our open-source library [react-print-pdf](https://github.com/OnedocLabs/react-print-pdf) now support LaTeX. 

Using a simple React component you can add formulas, equations, and mathematical symbols to your PDFs with ease.
If your document is composed of **99%** of text and **1%** of mathematical content, you probably don't want to use LaTeX for the whole document. You want to use **Onedoc**.

## Introduction

If you write scientific papers, financial reports, or any other document that requires mathematical formulas, you probably use LaTeX. LaTeX is a typesetting system that is widely used for scientific and technical documents. It is particularly well-suited for documents that include complex mathematical formulas and equations.

If you ever wrote such documents you probably used solution such as overleaf or sharelatex. And by using these solutions you probably quickly realized their limitations. A non exaustive list of these limitations are:
- Steep learning curve
- Limited customization (you can't use your own packages)
- Limited WYSIWYG
- Time consuming to create high quality documents
- Limited compatibility with other tools
- Limited functionality for non-mathematical content

Among others, these limitations encouraged us to create a LaTeX component. Especially, if your document is composed of 99% of text and 1% of mathematical content, you probably don't want to use LaTeX for the whole document. You want to use our solution.

So how can you use LaTeX in React to generate PDFs? We will show you how to use LaTeX in React to generate PDFs with react-print-pdf.

## First thing first, what is react-print-pdf?

If you are still here and reading this article, you probably already know what [react-print-pdf](https://github.com/OnedocLabs/react-print-pdf) is. But just in case, is collection of high-quality, unstyled components for creating beautiful PDFs using React and TypeScript. Unlike other solutions, react-print-pdf gives you complete control over your documents, allowing you to design complex layouts with features like footnotes, headers, margins, and more. Additionally, it enables you to track and analyze specific parts of your document, and build and update charts using data from your database.

You can start using it by installing it with npm:

```bash
    npm install -y @onedoc/react-print
```

## How to use LaTeX in React to generate PDFs with react-print-pdf
As usual, we tried to make it as simple as possible. You can use the `LaTeX` component to add formulas, equations, and mathematical symbols to your PDFs with ease.

1st, you need import the `LaTeX` component from `@onedoc/react-print`:

```javascript
import { LaTeX } from '@onedoc/react-print';
```

Then, you can use the `LaTeX` component to add formulas, equations, and mathematical symbols to your PDFs:

```javascript
<Latex>
    {String.raw`f(x) = \int_{-\infty}^\infty \hat f(\xi) e^{2 \pi i \xi x} d\xi  (1)`}
</Latex>
```

And that's it! You can now use LaTeX in React to generate PDFs with react-print-pdf. This is how it would look like in a complete example:

```typescript
import React from 'react';
import { Latex, compile } from '@onedoc/react-print';
import { Onedoc } from "@onedoc/client";

const Example = () => {
    return (
      <React.Fragment>  

        <p> Fourier Transform </p>

        <Latex>{String.raw`
            \relax{x} = \int_{-\infty}^\infty
            \hat\xi\,e^{2 \pi i \xi x}
            \,d\xi
        `}</Latex>

        <p> Inverse Fourier Transform </p>

        <Latex>{String.raw`
            \hat\xi = \int_{-\infty}^\infty
            x\,e^{-2 \pi i \xi x}
            \,dx
        `}</Latex>

      </React.Fragment>
    );
  };

const onedoc = new Onedoc("process.env.ONEDOC_API_KEY"); //replace with you API key

(async () => {
    const html = await compile(<Example/>);
    const { file } = await onedoc.render({
        html,
        test: false,
        save: false,
        expiresIn: 10,
        assets: [
        ],
    });

    fs.writeFileSync("./example.pdf", Buffer.from(file));
});
```

And this is how the PDF would look like:

![Image of the PDF obtained by running the code above.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4qp279dnrctrg5dnk5l.jpg)

Note that for now, we are rendering LaTeX using KaTeX which requires pulling a remote stylesheet hosted by jsdelivr. This is done to prevent the styles from beging purged. We are working on a solution to allow you to use your own KaTeX stylesheet.

## What's next?
What's next is up to you. We give you the tool you need to create high quality documents with ease. 

On our side, we published a template of a scientific paper, similar to what you would find on Overleaf or Sharelatex. You can find it [here](https://react-print.onedoclabs.com/templates/scientific-report).

We can't wait to see what are you going to create with it. Stay tuned for more updates and new features. 

Remember that react-print-pdf is open-source and we are always looking for contributors. If you have any ideas or suggestions, please let us know. We would love to hear from you.
* [react-print-pdf](https://github.com/OnedocLabs/react-print-pdf)
* [Submit your template to our gallery](https://react-print.onedoclabs.com/templates)
* [Discord](https://discord.gg/https://discord.com/invite/uRJE6e2rgr)

This article has been taken from [Onedoc's blog](https://www.onedoclabs.com/blog/react-and-latex)