This question led to a new package:

I am trying to write a command with an optional parameter, and feed the output to the \url command. My motivation is to generate links to online references using the bibleref package. That package already defined an API with optional parameters, so I don't want to change the syntax.


But a command with optional arguments is apparently too fragile to be consumed by \url. The story is in my example code.


\usepackage{hyperref}[2011/04/09 v6.82f]

% Macro that takes an argument and encloses it in angle brackets

% Macro that does not take an argument

% Check whether there is a parenthesis in the input, then call
% \parenmatch or \parenmismatch

% The \matcher command is probably fragile, so try to make it robust.
% Actually, I found that \matcher behaves identically to \robustmatcher
% in the examples below.


% This line correctly produces: MATCH<arg>

% This line correctly produces: MISMATCH!

% This block emits an error: ! Use of \parenmatch doesn't match its definition.
% If I loosen the restrictions on the \parenmatch arguments, it
% only produces a different warning: ! Missing control sequence inserted.
% The evaluation does not respect protected commands, so it either fails to
% compile or it creates incorrect output.
% A better option is below.

% This block correctly produces: MATCH<arg>
% As long as I use \protected@edef instead of \edef, it compiles successfully.

% Now I would like to use the command output as a link target.
% This was my goal all along.
% But the link doesn't have the address I would expect. It shows in the document as:
%   \protect\let\reserved@d=(\def\defMISMATCH{MISMATCH}\futurelet\@let@token\letMATCH$\delimiter"426830Aarg\delimiter"526930B$
% I tried sprinkling \protect and \expandafter commands in the definitions and
% applications, but nothing has helped.


Is there something about hyperref that is going to make this impossible? I expect that somehow I should be able to reorder the evaluations so that a plain string, already expanded, could be passed to \url safely.

  • Have you tried using the \urldef command provided by the url package? Its advantage is that it creates url strings that are robust. – Mico Nov 22 '11 at 4:42
  • I tried that and got the same result. The command \urldef\link\url{\captured} produced the same compiler error as \url{\captured} alone. That makes me believe that \urldef is meant to solve a different problem. – Chad Parry Nov 22 '11 at 4:53
  • \protect\def should definitely be \protected\def (eTeX's way of having protection, unrelated to LateX's \protect). Also, \protected@edef\captured{\robustmatcher(arg)} doesn't capture anything: \robustmatcher is just not expanded. – Bruno Le Floch Nov 22 '11 at 5:07
  • Your best bet is probably to put the \url command within the final macros: \def\urlmatcher{\@ifnextchar{(}\urlparenmatch\urlparenmismatch} \def\urlparenmatch(#1){\url{MATCH(#1)}} \def\urlparenmismatch{\url{MISMATCH!}}. – Bruno Le Floch Nov 22 '11 at 5:12
  • Thanks for the suggestion, Bruno! I was hoping to not have to change the bibleref package internals, but I prefer an invasive solution to no solution at all. – Chad Parry Nov 22 '11 at 5:16

The problem you are facing is that it is not possible to parse optional arguments in an expandable way. Thus, \url will never see the expansion of your macros. At best (i.e., if the macros are made robust), the macros are not expanded at all, at worst, they are partly expanded, and blow up with typically some ! Undefined control sequence errors.

Your best bet is thus to put the \url command within the final macros rather than putting it around your macros. This way, the command which takes an optional argument is "outside" everything else, and can be as non-expandable as you wish.

\usepackage{hyperref}[2011/04/09 v6.82f]
\urlmatcher Hello, world!

To help you debug, you can use \show, very useful TeX primitive (used as \show\urlmatcher for instance, or in your example, \show\captured). I sometimes also use \tracingall.

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.