7

I'm trying to use \psbarcode in a manner that it will take a premade variable to render the barcode. MWE that fails is below,

\documentclass{minimal}
\usepackage{auto-pst-pdf}
\usepackage{pst-barcode}

\newcommand*{\tozip}[1]{0123456789}

\begin{document}
   \begin{pspicture}\psbarcode{\tozip}{}{postnet}\end{pspicture}
\end{document}

If I replace \tozip with actual numbers it works... Anyone have an idea that will work with my \newcommand?

I guess my example was a bit too minimal... I'm actually using scrlttr2 and KOMA variables. This MWE above works once the edits are made (thx!) but the actual example does not. Any ideas about this related example?

\documentclass[letterpaper]{scrlttr2}
\usepackage[english]{babel}
\usepackage{auto-pst-pdf}
\usepackage{pst-barcode}

\newkomavar[to_zip]{tozip}
\setkomavar{tozip}[ToZip]{02115}

\begin{document}
\begin{letter}{%
   \begin{pspicture}
    \expandafter\psbarcode\expandafter{\usekomavar{tozip}}{}{postnet}
   \end{pspicture}
}

\opening{Dear Users,}
Than you in advance for helping fix this problem!
\end{letter}
\end{document}
  • Honestly, I reused that bit of code from something else. It compiled so I wasn't looking for it. :] – Joshua Sep 13 '12 at 18:50
5

\usekomavar is not expandable, so we write an own command:

\documentclass[letterpaper]{scrlttr2}
\usepackage[english]{babel}
\usepackage{auto-pst-pdf}
\usepackage{pst-barcode}    
\newkomavar[to_zip]{tozip}
\setkomavar{tozip}[ToZip]{02115}
\makeatletter  \def\KOMAvar#1{\@nameuse{scr@#1@var}}  \makeatother

\begin{document}

\begin{letter}{%
   \pspicture(10,10)
   \psbarcode{\KOMAvar{tozip}}{}{postnet}
   \endpspicture}

\opening{Dear Users,}
Than you in advance for helping fix this problem!
\end{letter}

\end{document}

enter image description here

2

You can't use \usekomavar in an expansion context as needed by \expandafter.

For this task (but you can't use none of the advanced features of \usekomavar), you can do like this:

\documentclass[letterpaper]{scrlttr2}
\usepackage[english]{babel}
\usepackage{auto-pst-pdf}
\usepackage{pst-barcode}

\newcommand\psbarcodekomavar[1]{%
  \begingroup\edef\x{\endgroup\noexpand\psbarcode{\csname scr@#1@var\endcsname}}\x
}

\newkomavar[to_zip]{tozip}
\setkomavar{tozip}[ToZip]{02115}


\begin{document}

\begin{letter}{%
 \begin{pspicture}
   \psbarcodekomavar{tozip}{}{postnet}
 \end{pspicture}
}

\opening{Dear Users,}
Than you in advance for helping fix this problem!
\end{letter}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.