Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
I think you could clarify a little bit the question. What's exactly what you are trying to achieve? Using # as “text”? I think a more particular example would be great. –  Manuel Jul 9 at 11:30
1  
If you want to typeset a # then you need \# in anycase unrelated to \newcommand \begin{document} a # b \end{document} is an error you need a \# b –  David Carlisle Jul 9 at 11:32
    
I guess that the OP wants a later execution/expansion of code with some # character, however, this stands as a placeholder for the number of a specific argument. As the command \deferredcode has no argument, LaTeX will complain about a direct # occuring somewhere inside the command, especially, if not followed by a number from 1 up to 9. –  Christian Hupfer Jul 9 at 11:54
    
@richard: I am unsure whether you rather mean something like \noexpand or \unexpanded. Could you also explain in short why there are # characters in the command body? –  Christian Hupfer Jul 9 at 11:56
2  
Does your deferred code perhaps itself include definitions which thus use #1, etc.? If so, you are probably looking for tex.stackexchange.com/questions/42463/… –  Joseph Wright Jul 9 at 11:59

2 Answers 2

Once TeX has tokenized input, which occurs for example when you save the input in a macro, then it's not generally straight-forward to alter the nature of the tokens. In particular, this restricts our ability to use verbatim or verbatim-like input in the arguments and definitions of macros. The \href command needs to use a verbatim-like approach to allow for example # inside hyperlinks, so that causes the problem.

In the question, you've noted that using \# is not a usable solution. This the most obvious approach is to alter the category code of # for the duration of the block you want to defer:

\documentclass[a4paper,final,11pt]{article}
\usepackage{hyperref}
\catcode`\#=12 %
\newcommand*\deferredCode{%
    \href{http://www.example.com/doc#list}{doc}%
}
\catcode`\#=6 %
\begin{document}
here comes a reference
\deferredCode
\end{document}

(Category code 12 is 'other' and so is harmless). Of course this prevents the deferred block containing any macro definitions requiring parameters, or at least that you pick some other character for that purpose.

share|improve this answer

When using the reference to external labels, the # character is used, according to the hypertext specification, but this means, that has to be escaped with\# within another command such as \newcommand.

\documentclass[12pt]{scrbook}

\usepackage{hyperref}


\newcommand{\deferredcode}{%
  \href{anotherfile.pdf\#mylabel}{Some Text}
}%

\begin{document}

In chapter \deferredcode we saw that
\end{document}

Where anotherfile.tex contains

\documentclass[12pt]{scrbook}

\usepackage{hyperref}

\begin{document}
\chapter{first}\label{mylabel}%
\end{document}

I omitted a screenshot, since it does not reveal much.

share|improve this answer
    
Perhaps note why this is required inside a definition but not outside (verbatim-like reading of arguments is possible only if they've not been tokenized). –  Joseph Wright Jul 9 at 12:28
    
@JosephWright: I will add you comment, with reference to you, of course, but is it possible to prevent the tokenizing? –  Christian Hupfer Jul 9 at 12:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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