Is there a “smarter” way to know the size of \psbarcode?

The size of 2D barcode depends on the first argument of \psbarcode. I usually do trial and error to find the size of \pspicture such that I get a tight output.

\documentclass{article}

\usepackage{pst-barcode}
\usepackage[active,tightpage]{preview}
\PreviewBorder=1bp
\PreviewEnvironment{pspicture}

\def\s{1.765}% I find this value by trial and error.

\begin{document}

\begin{pspicture}(\s,\s)
\psbarcode{http://example.org}{eclevel=L}{qrcode}
\end{pspicture}

\end{document}


Is there a "smarter" way to know the size of \psbarcode?

Edit 1: Please make sure you don't remove the preview package and compile it with either latex-dvips-ps2pdf or xelatex.

Edit 2:

\documentclass{article}

\usepackage{pst-barcode}
\usepackage[active,tightpage]{preview}
\PreviewBorder=1bp
\PreviewEnvironment{pspicture}

\usepackage{multido}

\begin{document}

\multido{\n=1.50+0.05}{15}{%
\begin{pspicture}(\n,\n)
\psframe[linecolor=blue,fillstyle=solid,fillcolor=yellow](\n,\n)
\psbarcode{http://example.org}{eclevel=L}{qrcode}
\rput[tr](\n,\n){{\color{red}\n}}
\end{pspicture}}

\end{document}


-
Changing \s does not have any effect on the output. Please give an example where it has some. Also, how do you create the pdf file? –  canaaerus Jul 10 '12 at 10:52
I use exactly your source code. Or is there some more meaning to "using preview"? Ok your edit made it clear. compiling with xelatex did not work. But after applying dvips and ps2pdf the page got smaller. –  canaaerus Jul 10 '12 at 11:07

\documentclass{minimal}
\usepackage{auto-pst-pdf,pst-barcode}
\begin{document}

\fbox{\begin{pspicture}(2in,2in)
\psbarcode{http://example.org}{eclevel=L width=2 height=2}{qrcode}
\end{pspicture}}

\end{document}


Internally the width/height are used in inch, but you can set this one also on TeX side

-

If you want to have a specific size Hebert's answer is certainly correct. Otherwise you have to calculate, which version of the QR Code has to be used. That depends on the error correction level and the number of bytes you want to store. Assuming you are using level n, the QR Code has a side length of 4n+17 blocks. Each block has a side length of 0.07 which gives you for \s the formula 1.2+0.28n.

According to my tests you will get the following levels with eclevel=L:

0-17 chars – Level 1

18-32 chars – Level 2

33-53 chars – Level 3

54-78 chars – Level 4

This is useful for example if you have QR Codes of different sizes but want them to look homogeneous.

-