# List of Unused \newbox, \newcount, \newdimen, \newskip, etc

Want a appropriate command to list out unused Box, Counter, Dimension, Skip from the CLS/STY file.

• Could you expand on this a bit? I'm not sure what you are after: if you do \newbox\mybox some box register number is associated with \mybox globally, so we can't know it's 'not used'. – Joseph Wright Jun 24 '16 at 6:32
• why do you want this list? there are over 32 thousand boxes (in etex based formats) so if you have 50 unused does it matter?, and how can you tell if it is unused? the normal case would be for it to be declared in the class but used in a document. – David Carlisle Jun 24 '16 at 7:04
• IMHO the easist way is to use your editor search (find) \mybox if only one occurrence the box (register) is not used or you can use a script. – touhami Jun 24 '16 at 7:08
• @KumareshPS Does "my CLS and supporting STY files" mean written by you? Or they are standard ones? – Przemysław Scherwentke Jun 24 '16 at 7:19
• an unused \newbox has no affect on anything (other than the space taken up for a command name). All it does is assign that name to be a number the numbered box register is there all the time, there are a fixed number of them. You should not be using the etex package these days latex will allocate into the extended range by default. Really you have thousands of box allocations??? what are you doing, I don't think I have seen a class that has more than a dozen or so?????? – David Carlisle Jun 24 '16 at 7:53

Since the question appears to be about the resources taken up by register allocations it should be noted that despite the names \newbox, \newcount etc do not generate new box or count registers.

There are 256 or 32768 or 65536 registers of each type available (depending on the engine). These are available all the time whether or not you allocate anything.

\count600=42


sets count register 600 to 42 and doesn't need you to allocate 600 registers with \newcount.

\newcount\mycount


or

\newbox\mybox


just declare \newcount or \newbox to be a number associated to the register (in the case of \newbox just using \chardef usually.)

So the only extra resource of having unused \newcount is like the resource of \let\myname\@empty of the space taken up to store the commandname itself, and its association to the register.

in etex and xetex there is the theoretical issue that the first 256 "classical" registers are stored more efficiently than the extended registers (which are stored as a sparse array) but on modern machines this is unlikely to make any observable difference, and isn't an issue in luatex at all, which stores the full set of 65536 registers in the same way.

If you always access the registers via non-expandable macros, those macros could be extended to log usage, but in general this is not possible, if you have a usage like

\newcount\mycount
...
\typeout{\the\mycount}


Then it is unlikely that you can log the usage of \mycount from within tex in any reliable way. It is of course easy just to search for \mycount in your source files in an editor or using sed or perl etc, so that is likely to be the most reliable method.

If your class files have lots of unused register allocations (or even lots of register allocations that are used) then there is something very strange in the code, very few classes have more than ten or so such allocations.

• It's very kind of you for the brief explanation... – Kumaresh PS Jun 25 '16 at 4:18
• The "just declare..." part seems to be a body text, not a code to me. I leave a comment here because I couldn't submit such a tiny edit. – yudai-nkt Jun 25 '16 at 16:35
• Performed this by using script... – Kumaresh PS Jul 4 '16 at 9:25

Here is an example of how to locate any unused \newbox. The MWE below defines three boxes:

\newbox{\MyBoxA}
\newbox{\MyBoxB}
\newbox{\MyBoxC}


but only box \MyBoxB is ever used, so the other two get reported as unused:

## Notes:

• \usepackage{debugboxes} needs to be before any \newbox.

• If you comment out the \usepackage{debugboxes}, then things work as normal, but when that is included, you get the report of unused boxes.

• The filecontents package was used to package the test case and create the debugboxes.sty. It is not needed in you actual use case.

## Code:

\begin{filecontents*}{debugboxes.sty}
\usepackage{pgffor}
\usepackage{etoolbox}
\usepackage{expl3}

%%% http://tex.stackexchange.com/questions/100542/how-to-extract-the-name-of-a-macro
\ExplSyntaxOn
\newcommand{\CsToStr}[1]{\cs_to_str:N #1}
\ExplSyntaxOff

%% http://tex.stackexchange.com/questions/14393/how-keep-a-running-list-of-strings-and-then-process-them-one-at-a-time
\newcommand\SaveboxList{}
\makeatletter
}
\makeatother
\let\OldNewBox\newbox
\renewcommand{\newbox}[1]{%
\OldNewBox{#1}%
}%

\let\OldUsebox\usebox%
\renewcommand{\usebox}[1]{%
\csgdef{SaveboxUsed \CsToStr{#1}}{}%
\OldUsebox{#1}%
}%

\AtEndDocument{%
\bigskip
\section{Unused saveboxes}%
\foreach \x in \SaveboxList {%
\ifcsdef{SaveboxUsed \x}{}{%
\par\noindent\x
}%
}
}%
\end{filecontents*}

\documentclass{article}

\usepackage{debugboxes}% Comment this out if don't need the debugging,

\newbox{\MyBoxA}
\newbox{\MyBoxB}
\newbox{\MyBoxC}

\begin{document}
\savebox{\MyBoxB}{some content placed in box}
\usebox{\MyBoxB}
\end{document}

• Of course, this is easily defeated if the class/package says \box\whatever instead of \usebox{\whatever}. Such an usage would cause a false positive. – egreg Jun 24 '16 at 14:03
• @egreg: Yep, hence my request in the comments as to a MWE that shows how things are used. This was just meant as an example of how things might be done. Not intended as a general solution to work in all cases. – Peter Grill Jun 25 '16 at 8:55