2

The following command

\newcommand{\PDef}{\mathcal{P}_{\operatorname{Def}}}

can be defined, alternatively, by iterating several new commands:

\newcommand{\CP}{\mathcal{P}}
\newcommand{\PDef}{\CP_{\operatorname{Def}}}

or

\newcommand{\CP}{\mathcal{P}}
\DeclareMathOperator{\Def}{Def} % command provided by amsmath
\newcommand{\PDef}{\CP_{\Def}}

QUESTION: What is the best course of action for defining new commands requiring several intermediate commands (new or existent), in terms of memory usage?

1
  • 1
    Unrelated, but for this kind of subscript I would use \mathrm, not \operatorname. The latter is slower.
    – campa
    Commented Jun 14 at 12:28

2 Answers 2

5

At the time LaTeX2e was developed the number of command names was critical, and essentially the limit in PC implementations of the time determined how big LaTex2e could be and why packages like amsmath, graphicx, array,... were kept as packages and not part of the format, there simply was not space.

Looking at the end of a log file of a recent document I made

 24639 multiletter control sequences out of 65536+600000

so despite the format having preloaded expl3 and the document having loaded several packages there are still hundreds of thousands of slots left in the csname hash table.

For comparson the TeXBook shows the same log entry of a log of TeX while producing that book

 1172 multiletter control sequences out of 2500

So my latex document used almost exactly ten times the maximum number of command names that were available in that early tex.

So basically now structure your code for maintenance don't worry about the memory consumption unless you are defining thousands of commands (as might happen if defining some new Unicode support for example).

If using pdftex or xetex (not luatex) the actual memory consumption does not depend on the macros defined, the relevant arrays are fixed size depending on the texmf.cnf configuration and the memory for the definitions is allocated whether or not you use it.

3
  • 3
    Wen I try the same with OpTeX then i got "5404 multiletter control sequences out of 65536+600000". OpTeX implements colors, graphics, hyperlinks, bibtex, geometry and more in the format but LaTeX doesn't. OpTeX declares most of control sequences twice but LaTeX doesn't. Yet OpTeX allocates 4.5 times fewer control sequences than LaTeX.
    – wipet
    Commented Jun 14 at 19:03
  • @wipet and has only a quarter of the functionality, so that seems about right. a large chunk of those csnames are expl3, I know you don't like expl3 but well you don't have to use it but it provides an awful lot of features and as shown here doesn't really take an appreciable amount of space in a modern format. latex declares all tex primitives twice (\def and \tex_def:D for example) Commented Jun 14 at 19:19
  • 1
    If you say that LaTeX without packages has only quarter functionality than OpTeX without packages then you might be right (but IMHO the fraction is much smaller]. OpTeX needs no more programming tools than classical TeX and Lua language provide. For example, we don't need to implement regular expressions at macro level because they are available in Lua.
    – wipet
    Commented Jun 15 at 5:03
3

The impact in terms of memory is negligible. What might change is execution time.

If I run LaTeX on

\documentclass{article}
\usepackage{amsmath}
\usepackage{l3benchmark}

\newcommand{\Def}{\mathrm{Def}}
\newcommand{\CP}{\mathcal{P}}

\newcommand{\PDefA}{\mathcal{P}_{\mathrm{Def}}}
\newcommand{\PDefB}{\CP_{\mathrm{Def}}}
\newcommand{\PDefC}{\CP_{\Def}}

\begin{document}

\ExplSyntaxOn
\benchmark:n { \sbox0{$\PDefA$} }
\benchmark:n { \sbox0{$\PDefB$} }
\benchmark:n { \sbox0{$\PDefC$} }
\ExplSyntaxOff

\end{document}

the output is

3.81e-6 seconds (25.2 ops)
3.83e-6 seconds (25.4 ops)
3.84e-6 seconds (25.5 ops)

The difference is in the range of tens of nanoseconds.

Note. I changed \operatorname into \mathrm because the former is the wrong choice for the job. See What's the difference between \mathrm and \operatorname?

Even if \PDefC is a tad slower than the other two, it should be preferred, because you may have to use \CP in other situations and having a single place where it is defined reduces the risks of inconsistency.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .