3

TL;DR-

Works

\psbarcode{NODATA}{height=0.33 width=1.6 includecheck includetext}{code128}

But "parametrized", does not work as expected (produces big transparent output)

\def\h{0.33}
\def\w{1.6}
\psbarcode{NODATA}{height=\h width=\w includecheck includetext}{code128}

Reason: I'd love to allow user to specify those \h and \w as parameter on commandline following this solution.

To be more specific

Given : nodata.tex

\documentclass{standalone}
\usepackage{pst-barcode}
\usepackage{auto-pst-pdf} % pdflatex require -shell-escape option!!!
\begin{document}
\begin{pspicture}
\psbarcode{NODATA}{height=0.33 width=1.6 includecheck includetext}{code128}
\end{pspicture}
\end{document}

with

$ pdflatex -shell-escape nodata.tex && convert -density 300x300 nodata.{pdf,png} && display nodata.png

generated properly nodata.pdf and nodata.png:

enter image description here

However when I change psbarcode parameters to use \h \w, i.e. \psbarcode{NODATA}{height=\h width=\w includecheck includetext}{code128} it stops rendering, and results in big transparent output. Full working differently then expected code:

\documentclass{standalone}
\usepackage{pst-barcode}
\usepackage{auto-pst-pdf} % pdflatex require -shell-escape option!!!
\begin{document}
\begin{pspicture}
\def\h{0.33}
\def\w{1.6}
\def\d{DATA}
\psbarcode{\d}{height=\h width=\w includecheck includetext}{code128}
\end{pspicture}
\end{document}

Rendering same but smaller resolution (30x30), as it results big:

$ pdflatex -shell-escape nodata.tex && convert -density 30x30 nodata.{pdf,png} && display nodata.png

Big transparent output, for completeness:

enter image description here

  • Could please someone add more tags if some are appropriate for issues with parametrizing using \def ? – Grzegorz Wierzowiecki May 09 '16 at 22:18
  • Why not use \newcommand\barcode[2]{\psbarcode{NODATA}{height=#1 width=#2 includecheck includetext}{code128}} ? –  May 09 '16 at 23:24
  • I like your idea @Andrew . Did you consider adding also a bit about this use case of "#1" into http://tex.stackexchange.com/q/123186/7128 ? Imho it's not clearly visible there and might help some users. Why do I still want to use \def\Key{Val} way? Still, giving ability to name parameters in "key1=val1,key2=val2" manner, has still some advantages. In my case I'd like to allow multiple optional arguments for tex template. – Grzegorz Wierzowiecki May 10 '16 at 05:41

2 Answers2

4

The problem is that the key-value pairs are separated by spaces and in

\psbarcode{NODATA}{height=\h width=\w includecheck includetext}{code128}

there is no space after \h and \w, because they're ignored at tokenization time.

I tried with height={\h} or {height=\h} but neither works. A workaround is to define the macros with a parameter text:

\documentclass[border=4]{standalone}
\usepackage{pst-barcode}
\usepackage{auto-pst-pdf} % pdflatex require -shell-escape option!!!
\begin{document}
\begin{pspicture}
\def\h/{0.33}
\def\w/{1.6}
\psbarcode{NODATA}{height=\h/ width=\w/ includecheck includetext}{code128}
\end{pspicture}
\end{document}

enter image description here

Alternatively:

\documentclass[border=4]{standalone}
\usepackage{pst-barcode}
\usepackage{auto-pst-pdf} % pdflatex require -shell-escape option!!!

\newcommand{\psbarcodemacro}[3]{%
  \begingroup\edef\x{\endgroup
    \noexpand\psbarcode{#1}{#2}{#3}%
  }\x
}

\begin{document}
\begin{pspicture}
\def\h{0.33 }
\def\w{1.6 }
\psbarcodemacro{NODATA}{height=\h width=\w includecheck includetext}{code128}
\end{pspicture}
\end{document}
egreg
  • 1,121,712
  • Could you please also give a hint how to make your mathod work with barcode data/payload? I've tried the same \def\d/{Data} \psbarcodemacro{\d/} but it does not work. (Before asking I've poked around various TeX/LaTeX topics like \xdef, \expandafter, \csname ... and got lost ). Thanks for \def\h/ , for width and height is works perfectly! – Grzegorz Wierzowiecki May 10 '16 at 06:40
  • @GrzegorzWierzowiecki I've no idea what “payload” is. If you are meaning the first argument, I see no problem with both approaches (the second one might give some if special characters in the data are used). – egreg May 10 '16 at 06:48
  • Yes, by "data/payload" I've meant first argument. Now I see where things break. Still don't know solution. So if I specify \def\d/{in tex file} in tex file, then it works. But if I remove definition from .tex file and try to specify via command line, by calling: pdflatex -shell-escape '\def\d/{DATA} \input{t.tex}' , then \psbarcodemarco produces strange barcode, like containing sth else then just "DATA" : http://imgur.com/1gEDuuv . Somehow defining in commandline differs from defining in tex file - but I am afraid it's might be of scope of this question and topic for another:) – Grzegorz Wierzowiecki May 10 '16 at 07:12
  • (The same with \h/ and \w/ they behave differently when defined via command line instead of "in tex" file - still I am aware it's out of scope of this question) – Grzegorz Wierzowiecki May 10 '16 at 07:16
  • @GrzegorzWierzowiecki I get nothing strange if I do pdflatex -shell-escape '\def\NO{NODATA}\input{file}, with \psbarcode{\NO}{...}{...}. – egreg May 10 '16 at 07:23
  • Here I've made minimal not-working example, basically, copy&paste of your code and it does not work with `\psbarcode{\NO}' and '\def\NO{DATA}' supplied via command line , here is verbatim code: http://pastebin.com/0v4P3bY6 and here is effect: http://imgur.com/425mXGg . – Grzegorz Wierzowiecki May 10 '16 at 20:03
  • @GrzegorzWierzowiecki I'm afraid it can't work, unless you pass the correct command line also when latex is called from inside the compilation. – egreg May 10 '16 at 20:14
3

Let a space in the \def or use \space in \psbarcode

\documentclass[border=4,pstricks]{standalone}
\usepackage{pst-barcode}
\usepackage{auto-pst-pdf} % pdflatex require -shell-escape option!!!
\begin{document}

\begin{pspicture}(0,-0.2)(1.6in,0.5in)
    \def\h{0.33 }
    \def\w{1.6 }
\psbarcode{NODATA}{height=\h width=\w includecheck includetext}{code128}
\end{pspicture}

\begin{pspicture}(0,-0.2)(1.6in,0.5in)
    \def\h{0.33}
    \def\w{1.6}
    \psbarcode{NODATA}{height=\h\space width=\w\space includecheck includetext}{code128}
\end{pspicture}

\end{document}

enter image description here

from a command line you can run

xelatex --shell-escape "\def\d{DATA} \def\w{0.33} \def\h{1.6} \input{t.tex}"
  • Do you know how to parametrize it so user can specify \h \w \d (\d instead of "NODATA") from command line in manner: pdflatex -shell-escape '\def\d/{DATA} \def\w/{0.33} \def\h/{1.6} \input{t.tex} ? I am aware this might be out of scope of this question, but maybe, by chance you know :). – Grzegorz Wierzowiecki May 10 '16 at 07:27
  • use xelatex then the command line is possible, see edited answer –  May 10 '16 at 07:44
  • I've tried xelatex -shell-escape '\def\NO{DATA}\input{egreg_v3d.tex}' with http://pastebin.com/0v4P3bY6 and got http://imgur.com/ajfuwmP , so it passes "data" but has trouble with automatic cropping. And as finally I'd love user to be able to specify different sizes, it recalculating manual cropping does not sound like incentive. Still, thanks! I've read more about xelatex and now I see what kind of benefits it might bring, and that it's part of TeX Live, so it should be widely available. – Grzegorz Wierzowiecki May 10 '16 at 20:30
  • You have to pass values to the pspicture environment. If they are missing it uses an area of 10cm x 10cm. See the values in my example –  May 10 '16 at 20:32
  • Now I have also xelatex alternative if at any day pdflatex would turn against me :). Thank you for a lot of help! :) – Grzegorz Wierzowiecki May 10 '16 at 20:40