1

In LaTeX there is a the qrcode package. Is there any way to create a qrcode in ConTeXt?

Something like this:

\qr[1in]{Your text to be QR encoded here}

1 Answer 1

3

A first attempt. We can generate a MetaPost graphic if we want, but this seems faster and easier to do. With some more commands, we can even write a small module, in case anyone else needs it. You only need qrencode.lua from here (the other files are prescindible).

EDIT: My first solution only worked with ConTeXt LMTX. The following one should work both with MkIV and LMTX. At this point it's better to moving to the latter, though.

\startluacode

local table      = table 
local concat     = table.concat

local interfaces = interfaces
local implement  = interfaces.implement

local qr = require"qrencode"

local function helper(n)
    if n > 0  then return 0      end
    if n < 0  then return 2^24-1 end
    if n == 0 then return 2^12   end --Should it happen?
end

local function qrcode_to_ctx(size, text)
    local n, data, settings
    local ok, content = qr.qrcode(text)
    local result = ''

    if not ok then
        context.type(content) 
    else
        data     = {}
        n        = #content
        settings = {
            x      = n, 
            y      = n,
            width  = size,
            height = size
        }    
        for i = 1, n do
            data[i] = {}
            for j = 1, n do
                data[i][j] = ('%06X'):format(helper(content[i][j]))
            end
            result = result .. concat(data[i])
        end
        context.bitmapimage(settings,result)
    end
end

implement{
    name      = 'qrcode',
    arguments = {'string', 'string'},
    actions   = qrcode_to_ctx
}

\stopluacode
\unprotect
\unexpanded\def\qrcode{\dosingleempty\qrcode_direct}
\def\qrcode_direct[#1]#2%
    {\quitvmode\doifsomethingelse{#1}%
     {\clf_qrcode{#1}{#2}}
     {\clf_qrcode{1cm}{#2}}} %Change 1cm for another default
\protect
\starttext
\startTEXpage
\qrcode[5cm]{https://tex.stackexchange.com/questions/598634/qr-code-in-context}
\stopTEXpage
\stoptext

enter image description here

5
  • Jairo, perhaps you could include it as a package to context distro for "use and enjoyment" of other people. Commented May 27, 2021 at 15:22
  • @somenxavier Hans has uploaded a module which depends on an external program, but it's faster (m-zint). I'll share Hans' method ASAP, but in the meantime I could provide this alternative as a separate module :)
    – user226564
    Commented May 27, 2021 at 15:25
  • Jairo, I recommend you upload this as module, because as you said m-zint depends on external program. Your implementation is clean Commented May 27, 2021 at 18:21
  • I get /usr/share/texmf-dist/tex/context/base/mkiv/l-sandbox.lua:180: module 'qrencode' not found: Commented Jun 2, 2021 at 8:24
  • 1
    @somenxavier You need to download qrencode from the link I provided. There are two ways: (1) put it in /usr/share/texmf-modules (it should exist) and run in order mtxrun --generate, context --make from the command line or (2) put in the same folder as your main file
    – user226564
    Commented Jun 2, 2021 at 8:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .