2

By reading the contents of the filebook.cls (the standard book class for LaTeX), I can see there are many uses of\def and\newcommand. What I don't seem to understand is why one is used in some cases and why the other in other cases. I couldn't find a pattern.

I understand the differences between\def and\newcommand, and I am aware that I should prefer the latter in everyday document writing. However, my question goes on the context of class and package development. For example, I could define a\title command for my new book class, that also accepts a subtitle as optional argument, by writing

\def\doc@title{}\def\doc@subtitle{}\def\title{\kernel@ifnextchar[{\@@title}{\@title}%\def\@title#1{    \@@title[]{#1}%}\def\@@title[#1]#2{%    \gdef\doc@subtitle{#1}    \gdef\doc@title{#2}}

However, I could also say

\newcommand\doc@title{}\newcommand\doc@subtitle{}\newcommand{\title}[2][]{    \gdef\doc@subtitle{#1}    \gdef\doc@title{#2}}

Is there any reason to prefer one way over the other in the previous pieces of code?

Similarly, thebook.cls file has this line:

\DeclareOption{10pt}{\renewcommand\@ptsize{0}}

Why is it not written as

\DeclareOption{10pt}{\def\@ptsize{0}}

In class/package development, what are the criteria to use\newcommand in some situation and\def in other?

askedDec 6, 2022 at 4:54
Diego Sejas's user avatar
3
  • 2
    Related question, but not quite the answer I was looking for:tex.stackexchange.com/questions/150137/…CommentedDec 6, 2022 at 4:55
  • 2
    what answer are you looking for that is not a duplicate of that?CommentedDec 6, 2022 at 12:34
  • "Sadly the answers in the linked questions is the truth, regardless whether you like it or not." ???CommentedDec 6, 2022 at 13:47

1 Answer1

2

Use expl3, and all the\XX_new:N command families and their nuances enter the picture too. :)

expl3

Expl3 ismuch easier to code in.

Same (philosophical) answer at base as the linked question, though.

MWE

% arara: lualatex\documentclass{article}\ExplSyntaxOn\tl_new:N \g_ds_doctitle_tl\tl_new:N \g_ds_docsubtitle_tl\tex_def:D \mymeta #1 {    \textsf{\textbf{ #1 }}}\tex_def:D \commandbuild #1#2#3 {        \c_backslash_str        #1        \c_underscore_str        #2        \c_colon_str        #3}\tex_def:D \texdef {        \begin{center}        The ~ difference ~ between ~ X ~ and ~ Y        \end{center}        \begin{center}        \mymeta { \commandbuild {tex}{def}{D} } ~ and ~ all ~ the ~        \mymeta { \commandbuild {XX}{new}{N} } ~ commands        \end{center}        }\NewDocumentCommand { \mytitle } { o +m } {    \tl_gset:Nx \g_ds_doctitle_tl { #2 }    \IfValueT { #1 }    {            \tl_gset:Nx \g_ds_docsubtitle_tl { #1 }            \regex_replace_all:nnN                    { (subtitle)+ }                    { \c{underline} \cB\{ \0 \cE\} }                    \g_ds_docsubtitle_tl            \tl_gput_left:Nn \g_ds_docsubtitle_tl             {                     \vspace { 2\tex_baselineskip:D }                    \begin{center}            }            \tl_gput_right:Nn \g_ds_docsubtitle_tl             {                     \end{center}            }                                \tl_gput_right:Nn \g_ds_doctitle_tl             {                     \large \upshape             }            \tl_gput_right:NV \g_ds_doctitle_tl \g_ds_docsubtitle_tl    }        \tl_gput_left:Nn \g_ds_doctitle_tl { \Large }    \tl_gput_right:Nn \g_ds_doctitle_tl { \texdef }            }\NewDocumentCommand { \printtitle } { } {        \tl_if_empty:NF \g_ds_doctitle_tl        {        \group_begin:        \center        \itshape        \tl_use:N \g_ds_doctitle_tl        \group_end:        }}\ExplSyntaxOff\mytitle[My subtitle A]{MultiparagraphMy Title A}%============  document \begin{document}\printtitle\end{document}
answeredDec 6, 2022 at 12:43
Cicada's user avatar
2
  • 2
    Computer scientists keep telling me that new languages are easier to use. I guess it's true if you think more like a computer sicentist an less like a computer. I find assembly language easier to use.CommentedDec 6, 2022 at 16:41
  • 2
    Expl3 is much easier to code in, but only in the sense that web pages with pop-up ads are much easier to read.CommentedMay 5, 2023 at 18:22

You mustlog in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.