4

I use morsbug's code to generate an isbn barcode. It works fine, at least as long as I provide the isbn as a string. If I pass the isbn number using a variable defined by \newcommand{\isbn}{123...} the compilation fails.

The following MWE should demonstrate the goal:

\documentclass{article}

\def\barcode#1#2#3#4#5#6#7{\begingroup%
\dimen0=0.1em
\def\stack##1##2{\oalign{##1\cr\hidewidth##2\hidewidth}}%
\def\0##1{\kern##1\dimen0}%
\def\1##1{\vrule height10ex width##1\dimen0}%
\def\L##1{\ifcase##1\bc3211##1\or\bc2221##1\or\bc2122##1\or\bc1411##1%
    \or\bc1132##1\or\bc1231##1\or\bc1114##1\or\bc1312##1\or\bc1213##1%
    \or\bc3112##1\fi}%
\def\R##1{\bgroup\let\next\1\let\1\0\let\0\next\L##1\egroup}%
\def\G##1{\bgroup\let\bc\bcg\L##1\egroup}% reverse
\def\bc##1##2##3##4##5{\stack{\0##1\1##2\0##3\1##4}##5}%
\def\bcg##1##2##3##4##5{\stack{\0##4\1##3\0##2\1##1}##5}%
\def\bcR##1##2##3##4##5##6{\R##1\R##2\R##3\R##4\R##5\R##6\11\01\11\09%
    \endgroup}%
\stack{\09}#1\11\01\11\L#2%
\ifcase#1\L#3\L#4\L#5\L#6\L#7\or\L#3\G#4\L#5\G#6\G#7%
\or\L#3\G#4\G#5\L#6\G#7\or\L#3\G#4\G#5\G#6\L#7%
\or\G#3\L#4\L#5\G#6\G#7\or\G#3\G#4\L#5\L#6\G#7%
\or\G#3\G#4\G#5\L#6\L#7\or\G#3\L#4\G#5\L#6\G#7%
\or\G#3\L#4\G#5\G#6\L#7\or\G#3\G#4\L#5\G#6\L#7%
\fi\01\11\01\11\01\bcR}

\begin{document}
\barcode 7046260816138 %<-- compilation succeeds

\newcommand{\isbn}{7046260816138}
%\barcode {\isbn} %<-- compilation fails
\end{document}
lAtExFaN
  • 1,131

2 Answers2

3

As you have defined your command to absorb 7 tokens you cannot go with only one. But if you know that your variable contains 7 characters you could use \expandafter. The important line in the following code is \expandafter\barcode \isbn.

\documentclass{article}

\def\barcode#1#2#3#4#5#6#7{\begingroup%
\dimen0=0.1em
\def\stack##1##2{\oalign{##1\cr\hidewidth##2\hidewidth}}%
\def\0##1{\kern##1\dimen0}%
\def\1##1{\vrule height10ex width##1\dimen0}%
\def\L##1{\ifcase##1\bc3211##1\or\bc2221##1\or\bc2122##1\or\bc1411##1%
    \or\bc1132##1\or\bc1231##1\or\bc1114##1\or\bc1312##1\or\bc1213##1%
    \or\bc3112##1\fi}%
\def\R##1{\bgroup\let\next\1\let\1\0\let\0\next\L##1\egroup}%
\def\G##1{\bgroup\let\bc\bcg\L##1\egroup}% reverse
\def\bc##1##2##3##4##5{\stack{\0##1\1##2\0##3\1##4}##5}%
\def\bcg##1##2##3##4##5{\stack{\0##4\1##3\0##2\1##1}##5}%
\def\bcR##1##2##3##4##5##6{\R##1\R##2\R##3\R##4\R##5\R##6\11\01\11\09%
    \endgroup}%
\stack{\09}#1\11\01\11\L#2%
\ifcase#1\L#3\L#4\L#5\L#6\L#7\or\L#3\G#4\L#5\G#6\G#7%
\or\L#3\G#4\G#5\L#6\G#7\or\L#3\G#4\G#5\G#6\L#7%
\or\G#3\L#4\L#5\G#6\G#7\or\G#3\G#4\L#5\L#6\G#7%
\or\G#3\G#4\G#5\L#6\L#7\or\G#3\L#4\G#5\L#6\G#7%
\or\G#3\L#4\G#5\G#6\L#7\or\G#3\G#4\L#5\G#6\L#7%
\fi\01\11\01\11\01\bcR}

\begin{document}
\barcode 7046260816138 %<-- compilation succeeds

\newcommand{\isbn}{7046260816138}
\expandafter\barcode \isbn
\end{document}
TeXnician
  • 33,589
3

Since the final string to be passed to \barcode has to be numbers only, it makes sense to make the macro compliant with LaTeX syntax and do full expansion.

\documentclass{article}

\makeatletter
\newcommand{\barcode}[1]{%
  \begingroup\edef\x{\endgroup\noexpand\@barcode#1}\x
}

\def\@barcode#1#2#3#4#5#6#7{\begingroup%
\dimen0=0.1em
\def\stack##1##2{\oalign{##1\cr\hidewidth##2\hidewidth}}%
\def\0##1{\kern##1\dimen0}%
\def\1##1{\vrule height10ex width##1\dimen0}%
\def\L##1{\ifcase##1\bc3211##1\or\bc2221##1\or\bc2122##1\or\bc1411##1%
    \or\bc1132##1\or\bc1231##1\or\bc1114##1\or\bc1312##1\or\bc1213##1%
    \or\bc3112##1\fi}%
\def\R##1{\bgroup\let\next\1\let\1\0\let\0\next\L##1\egroup}%
\def\G##1{\bgroup\let\bc\bcg\L##1\egroup}% reverse
\def\bc##1##2##3##4##5{\stack{\0##1\1##2\0##3\1##4}##5}%
\def\bcg##1##2##3##4##5{\stack{\0##4\1##3\0##2\1##1}##5}%
\def\bcR##1##2##3##4##5##6{\R##1\R##2\R##3\R##4\R##5\R##6\11\01\11\09%
    \endgroup}%
\stack{\09}#1\11\01\11\L#2%
\ifcase#1\L#3\L#4\L#5\L#6\L#7\or\L#3\G#4\L#5\G#6\G#7%
\or\L#3\G#4\G#5\L#6\G#7\or\L#3\G#4\G#5\G#6\L#7%
\or\G#3\L#4\L#5\G#6\G#7\or\G#3\G#4\L#5\L#6\G#7%
\or\G#3\G#4\G#5\L#6\L#7\or\G#3\L#4\G#5\L#6\G#7%
\or\G#3\L#4\G#5\G#6\L#7\or\G#3\G#4\L#5\G#6\L#7%
\fi\01\11\01\11\01\bcR}
\makeatother

\begin{document}

\barcode{7046260816138}

\newcommand{\isbn}{7046260816138}
\barcode{\isbn}

\end{document}

Note the change in syntax for the first example: the original one, with the number just following \barcode is error prone in a LaTeX context.

enter image description here

egreg
  • 1,121,712
  • also great - even greater/cleaner I think ;-) Thanks a lot egreg. In the meanwhile I added a small lua function infront of #1 to remove possible dashes from the input. – lAtExFaN May 15 '17 at 09:24