I am trying to defer processing of some latex code.
This works,
\documentclass[a4paper,final,11pt]{article}
\usepackage{hyperref}
\begin{document}
here comes a reference
\href{http://www.example.com/doc#list}{doc}
\end{document}
but I need to move up the importing of codeToBeDeferred (containing the \href), and render/process it in the same place. So I changed it to this.
\documentclass[a4paper,final,11pt]{article}
\usepackage{hyperref}
\newcommand{\deferredCode}{
\href{http://www.example.com/doc#list}{doc}
}
\begin{document}
here comes a reference
\deferredCode
\end{document}
This works, except for the # in the \href, note the # was not a problem in the original code.
side note: I am reading the code early and deferring processing because the code is merged together by a preprocessor, the preprocessor needs to process the deferred code early, but latex needs to do it latter.
Well all this works, except when the codeToBeDeferred has a # in it. \newcommand does not like it. If I put a \ before the # it fixes it, however I do not want to do this: there are a lot of historic versions of this code, I don't want to have to remember when I write new code. The code is valid latex when not part of a \newcommand
How can I allow a # in a \newcommand?
or
How else can I defer the processing of this block of code?
The # is in a \href in the deferred code, it works outside of a \newcommand.
What I am hoping for, is a wrapper to stop the # being a problem. If the # is only a problem when in a \newcommand then this may be possible.
%somePreambleCode
\newcommand{\deferredCode}{
\wrapper{
\href{http://www.example.com/doc#list}{doc}
}
}
%someOtherCode
\deferredCode
I know that I can escape the # with a \ (changing it to \#), but am hoping for another way.
I don't want to edit the code to be deferred. There is a choice of hundreds of them, I don't want them to be constrained by me doing this deferral trick to fix another problem.
#as “text”? I think a more particular example would be great. – Manuel Jul 09 '14 at 11:30#then you need\#in anycase unrelated to\newcommand\begin{document} a # b \end{document}is an error you needa \# b– David Carlisle Jul 09 '14 at 11:32#character, however, this stands as a placeholder for the number of a specific argument. As the command\deferredcodehas no argument,LaTeXwill complain about a direct#occuring somewhere inside the command, especially, if not followed by a number from 1 up to 9. – Jul 09 '14 at 11:54\noexpandor\unexpanded. Could you also explain in short why there are#characters in the command body? – Jul 09 '14 at 11:56#1, etc.? If so, you are probably looking for http://tex.stackexchange.com/questions/42463/what-is-the-meaning-of-double-pound-symbol-1-in-an-argument – Joseph Wright Jul 09 '14 at 11:59\hrefinfo is new. Could you add a minimal compilable example which we could compile to reproduce your problem? – Manuel Jul 09 '14 at 12:02##1by a nested\defor\(re)newcommandetc. wouldn't be needed if there is no#1at all, or am I misinterpreting that? – Jul 09 '14 at 12:02#tokens to allow nesting. Thusdef\foo{\def\foo##1{##1}}needs two#for the inner definition even though the outer one has no parameters. – Joseph Wright Jul 09 '14 at 12:06\hrefis used, than you refer to an external document and in this case a\#is needed. – Jul 09 '14 at 12:08\newcommandof definitions in the deferred code. Just a\href, it was working for yours without the deferring (use of\newcommand). – ctrl-alt-delor Jul 09 '14 at 12:10\newcommandin order to define your\deferredcodecommand. And all this cases, as commented by David Carlisle, Joseph Wright, Manuel and me, the#character has a special meaning, which must be escaped by using\#– Jul 09 '14 at 12:20\hrefworks outside of a\newcommand, that there would be a way to wrap all of the deferred code in the\new commandto make it behave. – ctrl-alt-delor Jul 09 '14 at 12:24\hrefI can think that you loadhyperrefand henceurlis loaded (you should provide a complete compilable example, as always). Those packages offer capabilities to define urls, so it might be better here to define urls once out of\newcommandand then use the defined urls inside it, avoiding problems with#. – Manuel Jul 09 '14 at 12:40\noexpand\unexpanded, but can not workout if they are what I need. – ctrl-alt-delor Jul 09 '14 at 13:04\unexpandedwon't help here as you still end up with a token of category code 6 (macro parameter). – Joseph Wright Jul 09 '14 at 13:08\noexpandetc. before I knew about the\hrefissue. I should have removed that comment or writing an update, that it would be of no use. – Jul 09 '14 at 13:46