Want a appropriate command to list out unused Box, Counter, Dimension, Skip from the CLS/STY file.
- 1,267
2 Answers
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.
- 757,742
-
-
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
-
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
filecontentspackage was used to package the test case and create thedebugboxes.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
\newcommand{\AddToSaveboxList}[1]{%
\g@addto@macro\SaveboxList{#1,}%
}
\makeatother
\let\OldNewBox\newbox
\renewcommand{\newbox}[1]{%
\AddToSaveboxList{\CsToStr{#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}
- 223,288
-
2Of course, this is easily defeated if the class/package says
\box\whateverinstead 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 08:55

\newbox\myboxsome box register number is associated with\myboxglobally, so we can't know it's 'not used'. – Joseph Wright Jun 24 '16 at 06:32\newboxdeclared in my CLS file, most of those are not assigned. And its very difficult to find the unused (\newboxalone found more than 50, hardly 10-20\newboxused)... – Kumaresh PS Jun 24 '16 at 06:40\listfiles(which provides the list of files used), if any... – Kumaresh PS Jun 24 '16 at 06:41\myboxif only one occurrence the box (register) is not used or you can use a script. – touhami Jun 24 '16 at 07:08\newbox, \newdimen, \newcount. I usedetexpackage to extend the allocation. I presume that too much of unused boxes, may lack of speed during compilation, correct me if I wrong. – Kumaresh PS Jun 24 '16 at 07:14\newcount,\newskip,\newdimentoo... Everywhere find these commands and most of them are untouched anywhere in class... Good to know these commands wont make any trouble in the performance... – Kumaresh PS Jun 24 '16 at 08:05\newcount\foointo the class if they neither use\foonor document that a user of the class can use it? If you really have thousands of allocations, something seems to be very wrong with the code. – David Carlisle Jun 24 '16 at 08:09\newbox, \newcount, \newskip, \newdimenit compile normally. – touhami Jun 24 '16 at 08:15\documentclassand the appropriate packages that sets up the problem. I am not sure about the general case, but if there is a clear way that you are using them, then it simply a matter of redefining\newbox(in DEBUG mode) to add to a list, and redefine\saveboxto add to another list of boxes that get used, and then at the end output this list. – Peter Grill Jun 24 '16 at 08:36