I meet the following issue when trying to use \scantokens in section titles, together with the hyperref package (whithout this package, all is fine).
\protect\input... can appear in a section title. But the same construction with \scantokens instead fails ("Second"): it appears that hyperref does not care about protection when creating a pdf string. Using \unexpanded{\protect\scantokens{...}} prevent expansion ("Fifth"), but if we leave out \protect, it crashes again ("Third"). Using two \unexpanded also works ("Fourth").
\documentclass{article}
\usepackage{hyperref}
\begin{document}
\tableofcontents
\section{\protect\input{data}}
% \section{\protect\scantokens{Second}}
% \section{\unexpanded{\scantokens{Third}}}
\section{\unexpanded{\unexpanded{\scantokens{Fourth}}}}
\section{\unexpanded{\protect\scantokens{Fifth}}}
\end{document}
(The file data only contains First (or whatever).) The example above compiles, but removing one of the % will make it fail.
Why do we need two levels of unexpansion/protection? Is there a neater way to do this?
Context: I have written a package (cprotect.dtx) that allows verbatim in the argument of most commands, by writing the argument to a file and \inputting it. Now, it would be nicer to use \scantokens rather than writing dozens of auxiliary files.
\jobname.tocand\jobname.outfiles? (Note that the latter is used for PDF Outline stuff, so if you aren't trying to make a PDF it probably won't exist...) – SamB Jan 24 '11 at 21:18\pdfstringdefexpands its argument, but does not set\protectto\noexpand\protect\noexpandas most other LaTeX cases of expanded definitions. To see this, we can run TeX on the minimal example ---\documentclass{article} \usepackage{hyperref} \begin{document} \def\AAA{\show\BBB} \pdfstringdef{\foo}{\protect \AAA} \end{document}--- Note that the error is\BBBnot defined, meaning that\showwas expanded and not executed. – Bruno Le Floch Jan 25 '11 at 11:10