3

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

Kumaresh PS
  • 1,267
  • 2
    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 06:32
  • I have lots of \newbox declared in my CLS file, most of those are not assigned. And its very difficult to find the unused (\newbox alone found more than 50, hardly 10-20 \newbox used)... – Kumaresh PS Jun 24 '16 at 06:40
  • Expected Command like \listfiles (which provides the list of files used), if any... – Kumaresh PS Jun 24 '16 at 06:41
  • 5
    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 07:04
  • 1
    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 07:08
  • @David: my CLS and supporting STY files having thousands of declaration for \newbox, \newdimen, \newcount. I used etex package 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
  • @touhami: If no option found then I need to use a script. – Kumaresh PS Jun 24 '16 at 07:17
  • 1
    @KumareshPS Does "my CLS and supporting STY files" mean written by you? Or they are standard ones? – Przemysław Scherwentke Jun 24 '16 at 07:19
  • @PrzemysławScherwentke: Written by many developers. – Kumaresh PS Jun 24 '16 at 07:21
  • 1
    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 07:53
  • Please consider with other commands \newcount, \newskip, \newdimen too... 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
  • 1
    You can easily get such a list using grep or any editor, but why does your class allocate registers that it doesn't use? why did anyone write \newcount\foo into the class if they neither use \foo nor 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
  • 1
    a make a loop with 5000 \newbox, \newcount, \newskip, \newdimen it compile normally. – touhami Jun 24 '16 at 08:15
  • 1
    @KumareshPS: It would be helpful if you composed a fully compilable MWE including \documentclass and 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 \savebox to 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

2 Answers2

9

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.

David Carlisle
  • 757,742
4

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:

enter image description here

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 \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}

Peter Grill
  • 223,288
  • 2
    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 08:55