I am aware that it is not generally possible to modify the contents of a box (as per https://tex.stackexchange.com/a/49903/17427), though I believe it is sometimes possible to "unbox" material and hack away at it (but not familiar with that).
However, my interest at the moment lies in removing (or disabling) a \write node from a box, which would not affect how it appears etc. The motivation for this is primarily as per my "answer" here regarding the way beamer puts a shrunk copy of slides in the notes pages and the resulting multiply defined labels it causes. For the purposes of the MWE, suppose I am creating a box that contains a \label, but I want to show that box twice, once with the \label taking effect and later without, and I want to prevent the multiple labels issue without subverting other features of the labelling mechanism in general, and without collecting the raw code in the box and processing it twice.
I'm sure this is possible with LuaTeX, and I'm interested in solutions both with and without LuaTeX (hence I haven't tagged this as {luatex}).
MWE to play with:
\documentclass{article}
\tracingonline2
\showboxdepth\maxdimen
\showboxbreadth\maxdimen
\newbox\boxa
\newbox\boxb
\newcommand{\showtwo}[2]{\showtokens{^^J^^J#1, #2^^J^^J}}
\begin{document}
\section{Hello}
{
\setbox\boxa\hbox{abc\label{hi}def}
\showbox\boxa
\setbox\boxb=\copy\boxa
\box\boxa
\let\write\showtwo
% edit \boxb somehow
\showbox\boxb
\box\boxb
}
\end{document}
The approach I've tried to take at the moment is not to delete the node from the box (as I have no idea where to start), but rather to add an extra layer of indirection to the \write before it goes in the box via \latelua, and then I should theoretically be able to change the behaviour of the indirection layer without editing the box. This may not be a good idea in general (thoughts?), but it shows I've made some effort! However, according to the manual, "the result of using tex.print from inside callbacks is undefined", and I am not sure if \latelua is considered a callback. In any case, the only things I've managed to do from a \latelua without my commands being silently ignored are tex.error and tex.sprint([[\noexpand\undefinedcs]]), and certainly nothing like tex.sprint([[\unexpanded{\immediate\write\@auxout{...}}]]).
Lua indirection attempt (but note my label isn't written to the aux file before I disable writing):
\documentclass{article}
\tracingonline2
\showboxdepth\maxdimen
\showboxbreadth\maxdimen
\newbox\boxa
\newbox\boxb
\directlua{tex.enableprimitives('',tex.extraprimitives())}
\setcounter{errorcontextlines}{\maxdimen}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\label}{\protected@write}{\protected@labelwrite}{}{\showtokens{patching \label failed}}
\let\protected@labelwrite\protected@write
\patchcmd{\protected@labelwrite}{\write#1}{\latelabelwritehelper{#1}}{}{\showtokens{patching \protected@labelwrite failed}}
\newcommand*{\latelabelwritehelper}[2]{%
\latelua{tex.sprint([[\noexpand\latelabelwrite{#1}{#2}]])}}
\newcommand*{\latelabelwrite}[2]{\immediate\write#1{#2}}
\newcommand*{\disablelatelabelwrite}{\let\latelabelwrite\@gobbletwo}
\makeatother
\begin{document}
\section{Hello}
\setbox\boxa\hbox{abc\label{hi}def}
\showbox\boxa
\setbox\boxb=\copy\boxa
\box\boxa
\disablelatelabelwrite
\showbox\boxb
\box\boxb
\end{document}
(Note also that this is my first real attempt to do something in Lua!)
\lateluanode to replace itself with a real\writenode? But again, I'm stuck as to how one might go about that. – cyberSingularity Nov 11 '12 at 00:56\leadersignores write whatsits. – Stephan Lehmke Nov 11 '12 at 05:14\global\setbox\@cclv=\hbox{\leaders\copy\@cclv\hskip\wd\@cclv}to "filter out" whatsits before shipout. – Stephan Lehmke Nov 11 '12 at 10:07\setbox\boxb=\hbox{\leaders\copy\boxa\hskip\wd\boxa}would be a possible solution. – Stephan Lehmke Nov 11 '12 at 10:20beamerin the linked question. Please post your comment as an answer and I will accept it (though other answers regarding Lua will still be of interest!) – cyberSingularity Nov 11 '12 at 12:53