Generaluse definition as an argument to another command

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Anymouse
Posts: 20
Joined: Mon May 10, 2021 2:50 am

use definition as an argument to another command

Post by Anymouse »

Suppose I have something like

Code: Select all

\newcommand{\isbn}{978-1-56619-909-4}
Then later in the document (e.g. the back page) I want to do

Code: Select all

\EANisbn[ISBN=\isbn{},SC5b]
That doesn't work.

How is that kind of thing accomplished? I know with EANisbn command you can just specify the ISBN when you first \usepackage[]{} but in general, how does one pass a variable option to a macro?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

use definition as an argument to another command

Post by Ijon Tichy »

Usually it does work:

Code: Select all

\documentclass{article}
\usepackage{keyval}
\newcommand{\isbn}{978-1-56619-909-4}
\makeatletter
\define@key{EAN}{ISBN}{Current ISBN is #1. }
\define@key{EAN}{SC5b}[true]{Option SC5b is #1. }
\makeatother
\newcommand\EANisbn[1][]{%
  \setkeys{EAN}{#1}%
}
\begin{document}
\EANisbn[ISBN=\isbn{},SC5b]
\end{document}
or somehow more realistic:

Code: Select all

\documentclass{article}
\usepackage{keyval}
\newcommand{\isbn}{978-1-56619-909-4}
\makeatletter
\newcommand*{\EANISBN}{}
\newif\ifEANSCvb
\define@key{EAN}{ISBN}{\edef\EANISBN{#1}}
\define@key{EAN}{SC5b}[true]{\expandafter\let\expandafter\ifEANSCvb\csname if#1\endcsname}
\makeatother
\newcommand\EANisbn[1][]{%
  \setkeys{EAN}{#1}%
}
\begin{document}
\EANisbn[ISBN=\isbn,SC5b]
Current ISBN is \EANISBN{} and SC5b is \ifEANSCvb true\else false\fi.
\end{document}
or

Code: Select all

\documentclass{article}
\usepackage{keyval}
\newcommand{\isbn}{978-1-56619-909-4}
\makeatletter
\newcommand{\EANISBN}{}
\newif\ifEANSCvb
\define@key{EAN}{ISBN}{\expandafter\renewcommand\expandafter\EANISBN\expandafter{#1}}
\define@key{EAN}{SC5b}[true]{\expandafter\let\expandafter\ifEANSCvb\csname if#1\endcsname}
\makeatother
\newcommand\EANisbn[1][]{%
  \setkeys{EAN}{#1}%
}
\begin{document}
\EANisbn[ISBN=\isbn,SC5b]
Current ISBN is \EANISBN{} and SC5b is \ifEANSCvb true\else false\fi.
\end{document}
So please show us a Infominimal working example that makes your problem reproducible.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
Anymouse
Posts: 20
Joined: Mon May 10, 2021 2:50 am

use definition as an argument to another command

Post by Anymouse »

Thank you!
I will have time to try these probably tomorrow.
Anymouse
Posts: 20
Joined: Mon May 10, 2021 2:50 am

use definition as an argument to another command

Post by Anymouse »

Okay test case to show what should result:

Code: Select all

\documentclass{article}
\newcommand{\isbn}{978-1-56619-909-4}
\usepackage{ean13isbn}
\begin{document}
\EANisbn[ISBN=978-1-56619-909-4,SC5b]
\end{document}
Test case to show error when using definition within another:

Code: Select all

\documentclass{article}
\newcommand{\isbn}{978-1-56619-909-4}
\usepackage{ean13isbn}
\begin{document}
\EANisbn[ISBN=\isbn{},SC5b]
\end{document}
It results in "! Missing = inserted for \ifnum" error
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

use definition as an argument to another command

Post by Ijon Tichy »

The used package does not expand the value before testing whether it is a number. So you have to do the expansion yourself, e.g.

Code: Select all

\documentclass{article}
\newcommand{\isbn}{978-1-56619-909-4}
\newcommand{\ISBNoption}{}
\edef\ISBNoption{ISBN=\isbn}
\usepackage{ean13isbn}
\begin{document}
\expandafter\EANisbn\expandafter[\ISBNoption,SC5b]
\end{document}
or use another package like

Code: Select all

\documentclass{article}
%\usepackage[undo-recent-deprecations]{expl3}% Only needed with out-dated GS1 and up-to-date expl3
\newcommand{\isbn}{978-1-56619-909-4}
\usepackage{GS1}[2021/06/17]% Need at least GS1 version 23 (see note below).
\begin{document}
\GSSetup{module_width=.46mm}
\expandafter\EANBarcode\expandafter{\isbn}
\end{document}
Note: GS1 before version 23 is not compatible with current version of l3kernel. If you get an error about the usage of deprecated \hbox_unpack_clear, you have to activate loading expl3 with option undo-recent-deprecations or—better!—update to GS1 version 23 or newer
Last edited by Ijon Tichy on Fri Jun 18, 2021 9:28 am, edited 1 time in total.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
Anymouse
Posts: 20
Joined: Mon May 10, 2021 2:50 am

use definition as an argument to another command

Post by Anymouse »

Thank you! That worked
Post Reply