6

I am dealing with a proprietary .cls file, which has a lot of constructions that work like this: a capitalized Macro (made-up, not existing in the packages loaded) is def'ed to be the lower-case version of this macro(also made up). This macro is then later used to def or (re)newcommand other stuff, relating to packages or simply for layout. - My question is threefold:

  • Why does the syntax \def\MyCoverText#1 work at all? - I read this description of \def, and it does not seem to allow for a non-braces-encapsulated first argument, so i'd have thought it would need to be \def\MyCoverText[#1]
  • What is the upside of the \def\C#1{\def\c{#1}} construct? I tentatively replaced it by a direct def of command, and it seems to work like before.
  • Is there a name for C->c, or for the practice of saying C#1 instead of C[#1]?

.cls file:

\ProvidesClass{myclass}[My class]
\LoadClass[10pt]{article}
\def\MyCoverText#1{\def\mycovertext{#1}}
\MyCoverText{Hello World}
% i replaced the two above lines by:
% \def\mycovertext{Hello World}
% to no obvious ill effect
\newcommand{\mycover}{
   \thispagestyle{empty}
   \newpage\null\vskip 3em% 
   \begin{center}
      \mycovertext
   \end{center}%
   }

.tex file:

\documentclass{../myclass}
\begin{document}
   \mycover
\end{document}

EDIT: pertaining to my confusion about \def, spurred by David Carlisles comments on it, i found this, which offers a host of different \def syntax versions and what they do. Really usefull stuff

6
  • 2
    \def\C#1{\def\c{#1}} The usual use is something like \def\title#1{\def\@title{#1}} which allows the author to use \title{my title} and have it internally saved in \@title for use by \maketitle (your use of camelcase for the user level command doesn't really follow latex naming guidelines) Apr 30, 2020 at 9:41
  • 1
    \def\foo#1{} \nd \def\foo[#1]{} define completely different syntax, so it depends which you need. Apr 30, 2020 at 9:42
  • 4
    Eek! Somewhat off-topic, but exceedingly important: Don't redefine \c! That is the canonical command for a cedilla. (You have no idea what consternation it produces among a journal's editorial staff when suddenly a French name is corrupted by such a thoughtless substitution.) Don't (re)define one-letter control sequences, Ever. Please. Apr 30, 2020 at 14:33
  • 1
    On the naming convention see tex.stackexchange.com/questions/48195/… Apr 30, 2020 at 15:21
  • 1
    @barbarabeeton thanks for the advice, but i just used c as a shorthand in describing this. the actual macros have ReallyLongAndImprobableNames, so editorial staff can sleep soundly tonight ;-)
    – bukwyrm
    Apr 30, 2020 at 20:43

1 Answer 1

9

The most common use of this pattern is something like:

\def\Title#1{\def\title{#1}}

Now \Title{This is a title} defines \title to be "This is a title". That is, you have a command that expands to the document title, which I am sure you will agree is a useful thing. It is important to note that LaTeX is case sensitive, so \Title and \title are completely different commands, even though their names might suggest otherwise.

The \MyCoverText, \mycovertext and \mycover triple in the OP are similar. The initial \MyCoverText{Hello World} defines \mycovertext to be "Hello World" so that \mycover now expands to:

\thispagestyle{empty}
\newpage\null\vskip 3em% 
\begin{center}
  Hello World
\end{center}%

If you like, the \MyCoverText is a "setter" command and the \mycovertext" is a "getter" command - and \mycover is a getter with some additional formatting.

[In the LaTeX internals what normally happens is more like \def\title#1{\def\@title{#1} after which \@title expands to the title. This is slightly better because command names that contain the @ character are protected: if you use them without "escaping" them you will get an error.]

7
  • Sorry if I just overwrote your edit accidentally. Apr 30, 2020 at 9:45
  • @StevenB.Segletes Thanks for trying to correct my typos:)
    – user30471
    Apr 30, 2020 at 9:47
  • So the style has no deeper relevance than sparing the user a \def later on?
    – bukwyrm
    Apr 30, 2020 at 10:03
  • .. In that i then only have to write \C{something} instead of \def\c{something} - in the non-protected version at least?
    – bukwyrm
    Apr 30, 2020 at 10:09
  • 1
    Yes. The difference is that if you are writing a package then you want to provide commands for the users that do certain things. If your code requires that \c be a certain type of value (some text, a number, an address, ...) then you can use \C to provide an easy interface to allow the user to set this value (and also have a default value). Although package authors could tell their users "you have to use \def\c{....} to set XXX" a much better and more user friendly approach is to provide a command \C that defines \c, especially as some users are "scared" of using \def.
    – user30471
    Apr 30, 2020 at 23:32

You must log in to answer this question.

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