Tx · LaTeX & TikZ

How to Create a Vector QR Code in LaTeX

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose a package

    Use qrcode for QR codes in standard LaTeX engines, or pst-barcode for broader barcode support.

  2. 2. Generate the code

    Place the payload in the package command and set the desired output size.

  3. 3. Compile as PDF

    Use pdfLaTeX, LuaLaTeX, or XeLaTeX with qrcode to produce scalable vector output.

  4. 4. Check scanning

    Preserve a clear margin, print at a practical size, and test the final PDF with a scanner.

You can generate a QR code directly in LaTeX without making a PNG or other bitmap first. For a document compiled with pdfLaTeX, LuaLaTeX, or XeLaTeX, the qrcode package is a straightforward choice: it draws the code as scalable elements in the PDF.

If you need a different barcode format, or already use PSTricks, pst-barcode supports a range of barcode types. Its compilation requirements differ from those of a standard PDFLaTeX workflow.

Create a QR code with the qrcode package

Load qrcode in your preamble, then call \qrcode with the data to encode. The height option controls the size of the generated symbol.

\documentclass{article}
\usepackage[margin=25mm]{geometry}
\usepackage{qrcode}

\begin{document}
\begin{center}
  \qrcode[height=35mm]{https://example.com}
\end{center}
\end{document}

Save this as a .tex file and compile it with a standard PDF-producing engine, for example:

pdflatex qr-example.tex

The resulting PDF contains the QR modules as vector drawing elements. They remain sharp when you zoom in or print at a different size, unlike a low-resolution raster image. The document also remains easy to edit: change the text passed to \qrcode to encode a different URL or other payload.

Why the result is vector

A QR code is a grid of square modules. The qrcode package constructs that grid using TeX drawing operations, which PDF output can represent as vector shapes. The package is not placing a screenshot of the code into your document.

You can therefore include the code directly in the document, or compile the example as a standalone PDF and use that PDF in another project. If you export the page to PNG or JPEG afterward, that exported file is raster; the vector information is retained in the PDF.

Using PSTricks for other barcode types

For a broader set of barcode formats, including formats beyond QR, look at the pst-barcode package documentation to check whether the specific symbology you need is supported. PSTricks produces PostScript-based graphics, so its usual route is to compile with LaTeX to DVI, convert the DVI to PostScript with dvips, and then convert the PostScript to PDF with ps2pdf.

That route is different from compiling a document directly with pdfLaTeX. If you need PSTricks in a PDFLaTeX-centered workflow, a conversion wrapper such as auto-pst-pdf may be useful, but it adds setup and shell-escape requirements. For a simple QR code in a modern PDF workflow, starting with qrcode is usually less complicated.

Size, margins, and scanning

Vector output does not guarantee that a printed code will scan. A QR code needs a clear, light margin around the symbol—the standard quiet zone is at least four modules wide. Keep text, borders, and nearby graphics out of this space. The example leaves surrounding whitespace; when placing the code in a tightly controlled layout, check that the required margin remains clear.

The number of modules depends on the encoded data and selected QR version. A longer payload generally produces a denser code, so at the same overall height its individual modules will be smaller. For printed use, choose a size appropriate to the payload and test a proof at the actual print size. Also test the final PDF rather than relying only on a source preview.

Common mistakes

  • Assuming vector means any size will scan. Enlarging a vector code keeps its edges sharp, but a code printed too small can still be difficult to read.
  • Removing the quiet zone. A tight crop or a neighboring graphic can interfere with scanning even when the QR modules themselves are correct.
  • Rasterizing the PDF unnecessarily. Keep the PDF as PDF when sending it to a printer or placing it in another document; avoid converting it to a small PNG unless a raster file is specifically required.
  • Using a package that does not match the compiler. pst-barcode relies on PSTricks conventions; use its documented conversion workflow rather than expecting every PSTricks example to compile directly with pdfLaTeX.
  • Changing the payload without checking the result. After editing the encoded text, compile again and scan the new output to verify that it contains the intended data.

References

  1. The qrcode package on CTAN
  2. The pst-barcode package on CTAN

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.