Graphics and media/Collections of tips and tricks/Create barcodes

From Wiki
Jump to navigation Jump to search

< Graphics and media

How to create barcodes from within ConTeXt (MkIV/LMTX)?

Native commands

QR codes

ConTeXt LMTX can generate QR codes natively.

  • \qrcode[text={https://contextgarden.net},color=orange,width=2cm]
    

(ConTeXt version on the wiki is too old for this.)

Color is optional.

The Lua function behind that looks like: figures.qrcode("https://pragma-ade.com","blue",tex.sp("2cm"))

The images get generated as bytemaps and become somewhat blurry, but they’re scannable without problems. If you prefer crisp codes, use Zint.

Code 128 and EAN/GTIN

ConTeXt LMTX can also generate the most common barcodes, Code 128 and EAN/GTIN 13 (e.g. ISBN), with a MetaPost function:

\useMPlibrary[barcodes]

\definefont[EANfont][OCRB at 10bp]

\barcode[code=ean13,
  text={978396543604},
  width=37.29mm,
  height=22.85mm]
  • Valid values for code are code128 (default), ean13 and gtin13, while the latter two only differ in a > added after the number in gtin13. (That protects the right “quiet zone” as common with EAN 8.)
  • You must only give 12 digits, since the checksum in the 13th digit is autogenerated.
  • The defined font EANfont ist used for the number below the barcode.

EAN 13 can have an extension of 2 or 5 digits, it is used for the periodical issue number with ISSNs and the book price with ISBN. You can attach the numbers after a dash:

\barcode[code=ean13,text={978396543604-03995}]

The \barcode macro calls the MetaPost function mfun_barcode: mfun_barcode("code128", "0123456789", 3cm, 2cm) or mp.barcode in Lua.

Zint module

  • Download zint from https://www.zint.org.uk (Windows, Linux) or install it with Homebrew or MacPorts (MacOS).
  • MkIV: The zint executable must be in your PATH.
  • LMTX: LuaMetaTeX uses the library. On Windows, that’s libzint.dll, on Linux and MacOS libzint.so (on MacOS the actual file name is libzint.dylib, but LuaMetaTeX expects .so).
    • Find that file, it should be in /usr/local/lib (Linux, Homebrew) or /opt/local/lib (MacPorts).
    • Create the path $TEXROOT/tex/texmf-$ARCH/bin/lib/luametatex/zint.
    • Copy or symlink the library into it (on MacOS remember naming it libzint.so).
    • Run mtxrun --generate to update the file database.
\usemodule[zint]
\barcode[alternative=isbnx, text=9783865419026, width=4cm]
\barcode[alternative=qrcode, text={https://wiki.contextgarden.net}, width=3cm]

Zint can produce about 40 different code types (“alternatives“). Try the name (but lowercase with spaces) or look into the source

Deprecated solutions

Barcodes module

The old MkII module uses PStricks and is deprecated. (Source)

barcode.lua

In 2012, Patrick published a small LuaTeX library on Stack Exchange. It can only create EAN codes, but it has no dependencies and doesn’t even use MetaPost.

\loadluafile[barcode]
\define[1]\luabarcode{\directlua{generate_barcode_lua("#1")}}

\luabarcode{9783865419026}